%
hh sequences to single characters, and
* convert plus to space.
- * @param s A string that may contain
+ * @param string A string that may contain
* +
(plus) and
* %
hh sequences.
* @return The unescaped string.
*/
- public static String unescape(String s) {
- int len = s.length();
- StringBuffer b = new StringBuffer();
- for (int i = 0; i < len; ++i) {
- char c = s.charAt(i);
+ public static String unescape(String string) {
+ int length = string.length();
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < length; ++i) {
+ char c = string.charAt(i);
if (c == '+') {
c = ' ';
- } else if (c == '%' && i + 2 < len) {
- int d = JSONTokener.dehexchar(s.charAt(i + 1));
- int e = JSONTokener.dehexchar(s.charAt(i + 2));
+ } else if (c == '%' && i + 2 < length) {
+ int d = JSONTokener.dehexchar(string.charAt(i + 1));
+ int e = JSONTokener.dehexchar(string.charAt(i + 2));
if (d >= 0 && e >= 0) {
c = (char)(d * 16 + e);
i += 2;
}
}
- b.append(c);
+ sb.append(c);
}
- return b.toString();
+ return sb.toString();
}
}
diff --git a/CookieList.java b/CookieList.java
index 8f651f5..7f4fe07 100755
--- a/CookieList.java
+++ b/CookieList.java
@@ -29,7 +29,7 @@ import java.util.Iterator;
/**
* Convert a web browser cookie list string to a JSONObject and back.
* @author JSON.org
- * @version 2008-09-18
+ * @version 2010-12-24
*/
public class CookieList {
@@ -47,15 +47,15 @@ public class CookieList {
* @throws JSONException
*/
public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject o = new JSONObject();
+ JSONObject jo = new JSONObject();
JSONTokener x = new JSONTokener(string);
while (x.more()) {
String name = Cookie.unescape(x.nextTo('='));
x.next('=');
- o.put(name, Cookie.unescape(x.nextTo(';')));
+ jo.put(name, Cookie.unescape(x.nextTo(';')));
x.next();
}
- return o;
+ return jo;
}
@@ -64,24 +64,24 @@ public class CookieList {
* of name/value pairs. The names are separated from the values by '='.
* The pairs are separated by ';'. The characters '%', '+', '=', and ';'
* in the names and values are replaced by "%hh".
- * @param o A JSONObject
+ * @param jo A JSONObject
* @return A cookie list string
* @throws JSONException
*/
- public static String toString(JSONObject o) throws JSONException {
+ public static String toString(JSONObject jo) throws JSONException {
boolean b = false;
- Iterator keys = o.keys();
- String s;
+ Iterator keys = jo.keys();
+ String string;
StringBuffer sb = new StringBuffer();
while (keys.hasNext()) {
- s = keys.next().toString();
- if (!o.isNull(s)) {
+ string = keys.next().toString();
+ if (!jo.isNull(string)) {
if (b) {
sb.append(';');
}
- sb.append(Cookie.escape(s));
+ sb.append(Cookie.escape(string));
sb.append("=");
- sb.append(Cookie.escape(o.getString(s)));
+ sb.append(Cookie.escape(jo.getString(string)));
b = true;
}
}
diff --git a/HTTP.java b/HTTP.java
index 6624708..0ce7a21 100755
--- a/HTTP.java
+++ b/HTTP.java
@@ -29,7 +29,7 @@ import java.util.Iterator;
/**
* Convert an HTTP header to a JSONObject and back.
* @author JSON.org
- * @version 2008-09-18
+ * @version 2010-12-24
*/
public class HTTP {
@@ -69,27 +69,27 @@ public class HTTP {
* @throws JSONException
*/
public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject o = new JSONObject();
+ JSONObject jo = new JSONObject();
HTTPTokener x = new HTTPTokener(string);
- String t;
+ String token;
- t = x.nextToken();
- if (t.toUpperCase().startsWith("HTTP")) {
+ token = x.nextToken();
+ if (token.toUpperCase().startsWith("HTTP")) {
// Response
- o.put("HTTP-Version", t);
- o.put("Status-Code", x.nextToken());
- o.put("Reason-Phrase", x.nextTo('\0'));
+ jo.put("HTTP-Version", token);
+ jo.put("Status-Code", x.nextToken());
+ jo.put("Reason-Phrase", x.nextTo('\0'));
x.next();
} else {
// Request
- o.put("Method", t);
- o.put("Request-URI", x.nextToken());
- o.put("HTTP-Version", x.nextToken());
+ jo.put("Method", token);
+ jo.put("Request-URI", x.nextToken());
+ jo.put("HTTP-Version", x.nextToken());
}
// Fields
@@ -97,10 +97,10 @@ public class HTTP {
while (x.more()) {
String name = x.nextTo(':');
x.next(':');
- o.put(name, x.nextTo('\0'));
+ jo.put(name, x.nextTo('\0'));
x.next();
}
- return o;
+ return jo;
}
@@ -119,41 +119,41 @@ public class HTTP {
* }
* Any other members of the JSONObject will be output as HTTP fields.
* The result will end with two CRLF pairs.
- * @param o A JSONObject
+ * @param jo A JSONObject
* @return An HTTP header string.
* @throws JSONException if the object does not contain enough
* information.
*/
- public static String toString(JSONObject o) throws JSONException {
- Iterator keys = o.keys();
- String s;
+ public static String toString(JSONObject jo) throws JSONException {
+ Iterator keys = jo.keys();
+ String string;
StringBuffer sb = new StringBuffer();
- if (o.has("Status-Code") && o.has("Reason-Phrase")) {
- sb.append(o.getString("HTTP-Version"));
+ if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
+ sb.append(jo.getString("HTTP-Version"));
sb.append(' ');
- sb.append(o.getString("Status-Code"));
+ sb.append(jo.getString("Status-Code"));
sb.append(' ');
- sb.append(o.getString("Reason-Phrase"));
- } else if (o.has("Method") && o.has("Request-URI")) {
- sb.append(o.getString("Method"));
+ sb.append(jo.getString("Reason-Phrase"));
+ } else if (jo.has("Method") && jo.has("Request-URI")) {
+ sb.append(jo.getString("Method"));
sb.append(' ');
sb.append('"');
- sb.append(o.getString("Request-URI"));
+ sb.append(jo.getString("Request-URI"));
sb.append('"');
sb.append(' ');
- sb.append(o.getString("HTTP-Version"));
+ sb.append(jo.getString("HTTP-Version"));
} else {
throw new JSONException("Not enough material for an HTTP header.");
}
sb.append(CRLF);
while (keys.hasNext()) {
- s = keys.next().toString();
- if (!s.equals("HTTP-Version") && !s.equals("Status-Code") &&
- !s.equals("Reason-Phrase") && !s.equals("Method") &&
- !s.equals("Request-URI") && !o.isNull(s)) {
- sb.append(s);
+ string = keys.next().toString();
+ if (!string.equals("HTTP-Version") && !string.equals("Status-Code") &&
+ !string.equals("Reason-Phrase") && !string.equals("Method") &&
+ !string.equals("Request-URI") && !jo.isNull(string)) {
+ sb.append(string);
sb.append(": ");
- sb.append(o.getString(s));
+ sb.append(jo.getString(string));
sb.append(CRLF);
}
}
diff --git a/HTTPTokener.java b/HTTPTokener.java
index 410a77c..f62b3d5 100755
--- a/HTTPTokener.java
+++ b/HTTPTokener.java
@@ -28,16 +28,16 @@ SOFTWARE.
* The HTTPTokener extends the JSONTokener to provide additional methods
* for the parsing of HTTP headers.
* @author JSON.org
- * @version 2008-09-18
+ * @version 2010-12-24
*/
public class HTTPTokener extends JSONTokener {
/**
* Construct an HTTPTokener from a string.
- * @param s A source string.
+ * @param string A source string.
*/
- public HTTPTokener(String s) {
- super(s);
+ public HTTPTokener(String string) {
+ super(string);
}
diff --git a/JSONArray.java b/JSONArray.java
index 091161a..5d8841f 100755
--- a/JSONArray.java
+++ b/JSONArray.java
@@ -78,7 +78,7 @@ import java.util.Map;
*
* @author JSON.org
- * @version 2009-04-14
+ * @version 2010-12-24
*/
public class JSONArray {
@@ -103,45 +103,33 @@ public class JSONArray {
*/
public JSONArray(JSONTokener x) throws JSONException {
this();
- char c = x.nextClean();
- char q;
- if (c == '[') {
- q = ']';
- } else if (c == '(') {
- q = ')';
- } else {
+ if (x.nextClean() != '[') {
throw x.syntaxError("A JSONArray text must start with '['");
}
- if (x.nextClean() == ']') {
- return;
- }
- x.back();
- for (;;) {
- if (x.nextClean() == ',') {
- x.back();
- this.myArrayList.add(null);
- } else {
- x.back();
- this.myArrayList.add(x.nextValue());
- }
- c = x.nextClean();
- switch (c) {
- case ';':
- case ',':
- if (x.nextClean() == ']') {
- return;
- }
- x.back();
- break;
- case ']':
- case ')':
- if (q != c) {
- throw x.syntaxError("Expected a '" + new Character(q) + "'");
- }
- return;
- default:
- throw x.syntaxError("Expected a ',' or ']'");
- }
+ if (x.nextClean() != ']') {
+ x.back();
+ for (;;) {
+ if (x.nextClean() == ',') {
+ x.back();
+ this.myArrayList.add(JSONObject.NULL);
+ } else {
+ x.back();
+ this.myArrayList.add(x.nextValue());
+ }
+ switch (x.nextClean()) {
+ case ';':
+ case ',':
+ if (x.nextClean() == ']') {
+ return;
+ }
+ x.back();
+ break;
+ case ']':
+ return;
+ default:
+ throw x.syntaxError("Expected a ',' or ']'");
+ }
+ }
}
}
@@ -167,8 +155,7 @@ public class JSONArray {
if (collection != null) {
Iterator iter = collection.iterator();
while (iter.hasNext()) {
- Object o = iter.next();
- this.myArrayList.add(JSONObject.wrap(o));
+ this.myArrayList.add(JSONObject.wrap(iter.next()));
}
}
}
@@ -200,11 +187,11 @@ public class JSONArray {
* @throws JSONException If there is no value for the index.
*/
public Object get(int index) throws JSONException {
- Object o = opt(index);
- if (o == null) {
+ Object object = opt(index);
+ if (object == null) {
throw new JSONException("JSONArray[" + index + "] not found.");
}
- return o;
+ return object;
}
@@ -215,20 +202,20 @@ public class JSONArray {
* @param index The index must be between 0 and length() - 1.
* @return The truth.
* @throws JSONException If there is no value for the index or if the
- * value is not convertable to boolean.
+ * value is not convertible to boolean.
*/
public boolean getBoolean(int index) throws JSONException {
- Object o = get(index);
- if (o.equals(Boolean.FALSE) ||
- (o instanceof String &&
- ((String)o).equalsIgnoreCase("false"))) {
+ Object object = get(index);
+ if (object.equals(Boolean.FALSE) ||
+ (object instanceof String &&
+ ((String)object).equalsIgnoreCase("false"))) {
return false;
- } else if (o.equals(Boolean.TRUE) ||
- (o instanceof String &&
- ((String)o).equalsIgnoreCase("true"))) {
+ } else if (object.equals(Boolean.TRUE) ||
+ (object instanceof String &&
+ ((String)object).equalsIgnoreCase("true"))) {
return true;
}
- throw new JSONException("JSONArray[" + index + "] is not a Boolean.");
+ throw new JSONException("JSONArray[" + index + "] is not a boolean.");
}
@@ -241,11 +228,11 @@ public class JSONArray {
* be converted to a number.
*/
public double getDouble(int index) throws JSONException {
- Object o = get(index);
+ Object object = get(index);
try {
- return o instanceof Number ?
- ((Number)o).doubleValue() :
- Double.valueOf((String)o).doubleValue();
+ return object instanceof Number ?
+ ((Number)object).doubleValue() :
+ Double.parseDouble((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
@@ -258,14 +245,18 @@ public class JSONArray {
*
* @param index The index must be between 0 and length() - 1.
* @return The value.
- * @throws JSONException If the key is not found or if the value cannot
- * be converted to a number.
- * if the value cannot be converted to a number.
+ * @throws JSONException If the key is not found or if the value is not a number.
*/
public int getInt(int index) throws JSONException {
- Object o = get(index);
- return o instanceof Number ?
- ((Number)o).intValue() : (int)getDouble(index);
+ Object object = get(index);
+ try {
+ return object instanceof Number ?
+ ((Number)object).intValue() :
+ Integer.parseInt((String)object);
+ } catch (Exception e) {
+ throw new JSONException("JSONArray[" + index +
+ "] is not a number.");
+ }
}
@@ -277,9 +268,9 @@ public class JSONArray {
* value is not a JSONArray
*/
public JSONArray getJSONArray(int index) throws JSONException {
- Object o = get(index);
- if (o instanceof JSONArray) {
- return (JSONArray)o;
+ Object object = get(index);
+ if (object instanceof JSONArray) {
+ return (JSONArray)object;
}
throw new JSONException("JSONArray[" + index +
"] is not a JSONArray.");
@@ -294,9 +285,9 @@ public class JSONArray {
* value is not a JSONObject
*/
public JSONObject getJSONObject(int index) throws JSONException {
- Object o = get(index);
- if (o instanceof JSONObject) {
- return (JSONObject)o;
+ Object object = get(index);
+ if (object instanceof JSONObject) {
+ return (JSONObject)object;
}
throw new JSONException("JSONArray[" + index +
"] is not a JSONObject.");
@@ -312,9 +303,15 @@ public class JSONArray {
* be converted to a number.
*/
public long getLong(int index) throws JSONException {
- Object o = get(index);
- return o instanceof Number ?
- ((Number)o).longValue() : (long)getDouble(index);
+ Object object = get(index);
+ try {
+ return object instanceof Number ?
+ ((Number)object).longValue() :
+ Long.parseLong((String)object);
+ } catch (Exception e) {
+ throw new JSONException("JSONArray[" + index +
+ "] is not a number.");
+ }
}
@@ -553,8 +550,8 @@ public class JSONArray {
* @return A String value.
*/
public String optString(int index, String defaultValue) {
- Object o = opt(index);
- return o != null ? o.toString() : defaultValue;
+ Object object = opt(index);
+ return object != null ? object.toString() : defaultValue;
}
diff --git a/JSONException.java b/JSONException.java
index 45e3b8d..b498d4b 100755
--- a/JSONException.java
+++ b/JSONException.java
@@ -3,12 +3,9 @@ package org.json;
/**
* The JSONException is thrown by the JSON.org classes when things are amiss.
* @author JSON.org
- * @version 2008-09-18
+ * @version 2010-12-24
*/
public class JSONException extends Exception {
- /**
- *
- */
private static final long serialVersionUID = 0;
private Throwable cause;
@@ -20,9 +17,9 @@ public class JSONException extends Exception {
super(message);
}
- public JSONException(Throwable t) {
- super(t.getMessage());
- this.cause = t;
+ public JSONException(Throwable cause) {
+ super(cause.getMessage());
+ this.cause = cause;
}
public Throwable getCause() {
diff --git a/JSONML.java b/JSONML.java
index 440e4ac..95c284f 100755
--- a/JSONML.java
+++ b/JSONML.java
@@ -154,7 +154,7 @@ public class JSONML {
break;
}
-// attribute = value
+// attribute = value
attribute = (String)token;
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
@@ -259,7 +259,6 @@ public class JSONML {
return (JSONArray)parse(x, true, null);
}
-
/**
* Convert a well-formed (but not necessarily valid) XML string into a
@@ -277,6 +276,8 @@ public class JSONML {
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
return (JSONObject)parse(x, false, null);
}
+
+
/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject using the JsonML transform. Each XML tag is represented as
@@ -302,15 +303,15 @@ public class JSONML {
* @throws JSONException
*/
public static String toString(JSONArray ja) throws JSONException {
- Object e;
int i;
JSONObject jo;
- String k;
+ String key;
Iterator keys;
int length;
+ Object object;
StringBuffer sb = new StringBuffer();
String tagName;
- String v;
+ String value;
// Emit get()
and opt()
methods return an
* object, which you can cast or query for type. There are also typed
* get
and opt
methods that do type checking and type
- * coercion for you.
+ * coercion for you. The opt methods differ from the get methods in that they
+ * do not throw. Instead, they return a specified value, such as null.
*
- * The put
methods adds values to an object. For example,
- * myString = new JSONObject().put("JSON", "Hello, World!").toString();+ * The
put
methods add or replace values in an object. For example,
+ * myString = new JSONObject().put("JSON", "Hello, World!").toString();* produces the string
{"JSON": "Hello, World"}
.
*
* The texts produced by the toString
methods strictly conform to
@@ -86,7 +87,7 @@ import java.util.TreeSet;
*
0x-
(hex) prefix.
* This can sometimes be easier than using a JSONObject to build a string.
* @author JSON.org
- * @version 2010-03-11
+ * @version 2010-12-24
*/
public class JSONWriter {
private static final int maxdepth = 20;
@@ -103,12 +103,12 @@ public class JSONWriter {
/**
* Append a value.
- * @param s A string value.
+ * @param string A string value.
* @return this
* @throws JSONException If the value is out of sequence.
*/
- private JSONWriter append(String s) throws JSONException {
- if (s == null) {
+ private JSONWriter append(String string) throws JSONException {
+ if (string == null) {
throw new JSONException("Null pointer");
}
if (this.mode == 'o' || this.mode == 'a') {
@@ -116,7 +116,7 @@ public class JSONWriter {
if (this.comma && this.mode == 'a') {
this.writer.write(',');
}
- this.writer.write(s);
+ this.writer.write(string);
} catch (IOException e) {
throw new JSONException(e);
}
@@ -150,17 +150,17 @@ public class JSONWriter {
/**
* End something.
- * @param m Mode
+ * @param mode Mode
* @param c Closing character
* @return this
* @throws JSONException If unbalanced.
*/
- private JSONWriter end(char m, char c) throws JSONException {
- if (this.mode != m) {
- throw new JSONException(m == 'a' ? "Misplaced endArray." :
+ private JSONWriter end(char mode, char c) throws JSONException {
+ if (this.mode != mode) {
+ throw new JSONException(mode == 'a' ? "Misplaced endArray." :
"Misplaced endObject.");
}
- this.pop(m);
+ this.pop(mode);
try {
this.writer.write(c);
} catch (IOException e) {
@@ -193,22 +193,22 @@ public class JSONWriter {
/**
* Append a key. The key will be associated with the next value. In an
* object, every value must be preceded by a key.
- * @param s A key string.
+ * @param string A key string.
* @return this
* @throws JSONException If the key is out of place. For example, keys
* do not belong in arrays or if the key is null.
*/
- public JSONWriter key(String s) throws JSONException {
- if (s == null) {
+ public JSONWriter key(String string) throws JSONException {
+ if (string == null) {
throw new JSONException("Null key.");
}
if (this.mode == 'k') {
try {
- stack[top - 1].putOnce(s, Boolean.TRUE);
+ stack[top - 1].putOnce(string, Boolean.TRUE);
if (this.comma) {
this.writer.write(',');
}
- this.writer.write(JSONObject.quote(s));
+ this.writer.write(JSONObject.quote(string));
this.writer.write(':');
this.comma = false;
this.mode = 'o';
@@ -259,7 +259,8 @@ public class JSONWriter {
throw new JSONException("Nesting error.");
}
this.top -= 1;
- this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
+ this.mode = this.top == 0 ?
+ 'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
}
/**
@@ -311,13 +312,12 @@ public class JSONWriter {
/**
* Append an object value.
- * @param o The object to append. It can be null, or a Boolean, Number,
- * String, JSONObject, or JSONArray, or an object with a toJSONString()
- * method.
+ * @param object The object to append. It can be null, or a Boolean, Number,
+ * String, JSONObject, or JSONArray, or an object that implements JSONString.
* @return this
* @throws JSONException If the value is out of sequence.
*/
- public JSONWriter value(Object o) throws JSONException {
- return this.append(JSONObject.valueToString(o));
+ public JSONWriter value(Object object) throws JSONException {
+ return this.append(JSONObject.valueToString(object));
}
}
diff --git a/XML.java b/XML.java
index 6d9f78e..59d20d0 100755
--- a/XML.java
+++ b/XML.java
@@ -31,7 +31,7 @@ import java.util.Iterator;
* This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text.
* @author JSON.org
- * @version 2010-12-23
+ * @version 2010-12-24
*/
public class XML {
@@ -75,7 +75,7 @@ public class XML {
*/
public static String escape(String string) {
StringBuffer sb = new StringBuffer();
- for (int i = 0, len = string.length(); i < len; i++) {
+ for (int i = 0, length = string.length(); i < length; i++) {
char c = string.charAt(i);
switch (c) {
case '&':
@@ -128,10 +128,10 @@ public class XML {
String name) throws JSONException {
char c;
int i;
- String n;
- JSONObject o = null;
- String s;
- Object t;
+ JSONObject jsonobject = null;
+ String string;
+ String tagName;
+ Object token;
// Test for and skip past these forms:
//
@@ -143,11 +143,11 @@ public class XML {
// <=
// <<
- t = x.nextToken();
+ token = x.nextToken();
// 0) {
- context.accumulate("content", s);
+ string = x.nextCDATA();
+ if (string.length() > 0) {
+ context.accumulate("content", string);
}
return false;
}
@@ -170,108 +170,111 @@ public class XML {
}
i = 1;
do {
- t = x.nextMeta();
- if (t == null) {
+ token = x.nextMeta();
+ if (token == null) {
throw x.syntaxError("Missing '>' after ' 0);
return false;
- } else if (t == QUEST) {
+ } else if (token == QUEST) {
//
x.skipPast("?>");
return false;
- } else if (t == SLASH) {
+ } else if (token == SLASH) {
// Close tag
- t = x.nextToken();
+ token = x.nextToken();
if (name == null) {
- throw x.syntaxError("Mismatched close tag" + t);
+ throw x.syntaxError("Mismatched close tag " + token);
}
- if (!t.equals(name)) {
- throw x.syntaxError("Mismatched " + name + " and " + t);
+ if (!token.equals(name)) {
+ throw x.syntaxError("Mismatched " + name + " and " + token);
}
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped close tag");
}
return true;
- } else if (t instanceof Character) {
+ } else if (token instanceof Character) {
throw x.syntaxError("Misshaped tag");
// Open tag <
} else {
- n = (String)t;
- t = null;
- o = new JSONObject();
+ tagName = (String)token;
+ token = null;
+ jsonobject = new JSONObject();
for (;;) {
- if (t == null) {
- t = x.nextToken();
+ if (token == null) {
+ token = x.nextToken();
}
// attribute = value
- if (t instanceof String) {
- s = (String)t;
- t = x.nextToken();
- if (t == EQ) {
- t = x.nextToken();
- if (!(t instanceof String)) {
+ if (token instanceof String) {
+ string = (String)token;
+ token = x.nextToken();
+ if (token == EQ) {
+ token = x.nextToken();
+ if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
}
- o.accumulate(s, XML.stringToValue((String)t));
- t = null;
+ jsonobject.accumulate(string,
+ XML.stringToValue((String)token));
+ token = null;
} else {
- o.accumulate(s, "");
+ jsonobject.accumulate(string, "");
}
// Empty tag <.../>
- } else if (t == SLASH) {
+ } else if (token == SLASH) {
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped tag");
}
- if (o.length() > 0) {
- context.accumulate(n, o);
+ if (jsonobject.length() > 0) {
+ context.accumulate(tagName, jsonobject);
} else {
- context.accumulate(n, "");
+ context.accumulate(tagName, "");
}
return false;
// Content, between <...> and
- } else if (t == GT) {
+ } else if (token == GT) {
for (;;) {
- t = x.nextContent();
- if (t == null) {
- if (n != null) {
- throw x.syntaxError("Unclosed tag " + n);
+ token = x.nextContent();
+ if (token == null) {
+ if (tagName != null) {
+ throw x.syntaxError("Unclosed tag " + tagName);
}
return false;
- } else if (t instanceof String) {
- s = (String)t;
- if (s.length() > 0) {
- o.accumulate("content", XML.stringToValue(s));
+ } else if (token instanceof String) {
+ string = (String)token;
+ if (string.length() > 0) {
+ jsonobject.accumulate("content",
+ XML.stringToValue(string));
}
// Nested element
- } else if (t == LT) {
- if (parse(x, o, n)) {
- if (o.length() == 0) {
- context.accumulate(n, "");
- } else if (o.length() == 1 &&
- o.opt("content") != null) {
- context.accumulate(n, o.opt("content"));
+ } else if (token == LT) {
+ if (parse(x, jsonobject, tagName)) {
+ if (jsonobject.length() == 0) {
+ context.accumulate(tagName, "");
+ } else if (jsonobject.length() == 1 &&
+ jsonobject.opt("content") != null) {
+ context.accumulate(tagName,
+ jsonobject.opt("content"));
} else {
- context.accumulate(n, o);
+ context.accumulate(tagName, jsonobject);
}
return false;
}
@@ -354,142 +357,141 @@ public class XML {
* @throws JSONException
*/
public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject o = new JSONObject();
+ JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(string);
while (x.more() && x.skipPast("<")) {
- parse(x, o, null);
+ parse(x, jo, null);
}
- return o;
+ return jo;
}
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
- * @param o A JSONObject.
+ * @param object A JSONObject.
* @return A string.
* @throws JSONException
*/
- public static String toString(Object o) throws JSONException {
- return toString(o, null);
+ public static String toString(Object object) throws JSONException {
+ return toString(object, null);
}
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
- * @param o A JSONObject.
+ * @param object A JSONObject.
* @param tagName The optional name of the enclosing tag.
* @return A string.
* @throws JSONException
*/
- public static String toString(Object o, String tagName)
+ public static String toString(Object object, String tagName)
throws JSONException {
- StringBuffer b = new StringBuffer();
+ StringBuffer sb = new StringBuffer();
int i;
JSONArray ja;
JSONObject jo;
- String k;
+ String key;
Iterator keys;
- int len;
- String s;
- Object v;
- if (o instanceof JSONObject) {
+ int length;
+ String string;
+ Object value;
+ if (object instanceof JSONObject) {
// Emit & ' > < "
.
- * @param a An ampersand character.
+ * @param ampersand An ampersand character.
* @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity.
*/
- public Object nextEntity(char a) throws JSONException {
+ public Object nextEntity(char ampersand) throws JSONException {
StringBuffer sb = new StringBuffer();
for (;;) {
char c = next();
@@ -136,9 +136,9 @@ public class XMLTokener extends JSONTokener {
throw syntaxError("Missing ';' in XML entity: &" + sb);
}
}
- String s = sb.toString();
- Object e = entity.get(s);
- return e != null ? e : a + s + ";";
+ String string = sb.toString();
+ Object object = entity.get(string);
+ return object != null ? object : ampersand + string + ";";
}
@@ -304,15 +304,15 @@ public class XMLTokener extends JSONTokener {
int i;
int j;
int offset = 0;
- int n = to.length();
- char[] circle = new char[n];
+ int length = to.length();
+ char[] circle = new char[length];
/*
* First fill the circle buffer with as many characters as are in the
* to string. If we reach an early end, bail.
*/
- for (i = 0; i < n; i += 1) {
+ for (i = 0; i < length; i += 1) {
c = next();
if (c == 0) {
return false;
@@ -328,14 +328,14 @@ public class XMLTokener extends JSONTokener {
/*
* Compare the circle buffer with the to string.
*/
- for (i = 0; i < n; i += 1) {
+ for (i = 0; i < length; i += 1) {
if (circle[j] != to.charAt(i)) {
b = false;
break;
}
j += 1;
- if (j >= n) {
- j -= n;
+ if (j >= length) {
+ j -= length;
}
}
/*
@@ -357,8 +357,8 @@ public class XMLTokener extends JSONTokener {
*/
circle[offset] = c;
offset += 1;
- if (offset >= n) {
- offset -= n;
+ if (offset >= length) {
+ offset -= length;
}
}
}