mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-06 07:46:08 +00:00
Cleanup
This commit is contained in:
@@ -78,7 +78,8 @@ public class HTTP {
|
||||
* It does no further checking or conversion. It does not parse dates. It
|
||||
* does not do '%' transforms on URLs.
|
||||
*
|
||||
* @param string An HTTP header string.
|
||||
* @param string
|
||||
* An HTTP header string.
|
||||
* @return A JSONObject containing the elements and attributes of the XML
|
||||
* string.
|
||||
*/
|
||||
@@ -88,7 +89,7 @@ public class HTTP {
|
||||
String token;
|
||||
|
||||
token = x.nextToken();
|
||||
if (token.toUpperCase().startsWith("HTTP")) {
|
||||
if(token.toUpperCase().startsWith("HTTP")) {
|
||||
|
||||
// Response
|
||||
|
||||
@@ -108,7 +109,7 @@ public class HTTP {
|
||||
|
||||
// Fields
|
||||
|
||||
while (x.more()) {
|
||||
while(x.more()) {
|
||||
String name = x.nextTo(':');
|
||||
x.next(':');
|
||||
jo.put(name, x.nextTo('\0'));
|
||||
@@ -141,21 +142,23 @@ public class HTTP {
|
||||
* Any other members of the JSONObject will be output as HTTP fields. The
|
||||
* result will end with two CRLF pairs.
|
||||
*
|
||||
* @param jo A JSONObject
|
||||
* @param jo
|
||||
* A JSONObject
|
||||
* @return An HTTP header string.
|
||||
* @throws JSONException if the object does not contain enough information.
|
||||
* @throws JSONException
|
||||
* if the object does not contain enough information.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
Iterator<String> keys = jo.keys();
|
||||
String string;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
if(jo.has("Status-Code") && jo.has("Reason-Phrase")) {
|
||||
sb.append(jo.getString("HTTP-Version"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Status-Code"));
|
||||
sb.append(' ');
|
||||
sb.append(jo.getString("Reason-Phrase"));
|
||||
} else if (jo.has("Method") && jo.has("Request-URI")) {
|
||||
} else if(jo.has("Method") && jo.has("Request-URI")) {
|
||||
sb.append(jo.getString("Method"));
|
||||
sb.append(' ');
|
||||
sb.append('"');
|
||||
@@ -167,9 +170,9 @@ public class HTTP {
|
||||
throw new JSONException("Not enough material for an HTTP header.");
|
||||
}
|
||||
sb.append(CRLF);
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
string = keys.next();
|
||||
if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && !"Reason-Phrase".equals(string) && !"Method".equals(string) && !"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||
if(!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && !"Reason-Phrase".equals(string) && !"Method".equals(string) && !"Request-URI".equals(string) && !jo.isNull(string)) {
|
||||
sb.append(string);
|
||||
sb.append(": ");
|
||||
sb.append(jo.getString(string));
|
||||
|
||||
@@ -31,7 +31,8 @@ public class HTTPTokener extends JSONTokener {
|
||||
/**
|
||||
* Construct an HTTPTokener from a string.
|
||||
*
|
||||
* @param string A source string.
|
||||
* @param string
|
||||
* A source string.
|
||||
*/
|
||||
public HTTPTokener(String string) {
|
||||
super(string);
|
||||
@@ -48,22 +49,22 @@ public class HTTPTokener extends JSONTokener {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
if (c == '"' || c == '\'') {
|
||||
} while(Character.isWhitespace(c));
|
||||
if(c == '"' || c == '\'') {
|
||||
q = c;
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = next();
|
||||
if (c < ' ') {
|
||||
if(c < ' ') {
|
||||
throw syntaxError("Unterminated string.");
|
||||
}
|
||||
if (c == q) {
|
||||
if(c == q) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
for (; ; ) {
|
||||
if (c == 0 || Character.isWhitespace(c)) {
|
||||
for(; ; ) {
|
||||
if(c == 0 || Character.isWhitespace(c)) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
|
||||
@@ -94,27 +94,29 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Construct a JSONArray from a JSONTokener.
|
||||
*
|
||||
* @param x A JSONTokener
|
||||
* @throws JSONException If there is a syntax error.
|
||||
* @param x
|
||||
* A JSONTokener
|
||||
* @throws JSONException
|
||||
* If there is a syntax error.
|
||||
*/
|
||||
public JSONArray(JSONTokener x) throws JSONException {
|
||||
this();
|
||||
if (x.nextClean() != '[') {
|
||||
if(x.nextClean() != '[') {
|
||||
throw x.syntaxError("A JSONArray text must start with '['");
|
||||
}
|
||||
if (x.nextClean() != ']') {
|
||||
if(x.nextClean() != ']') {
|
||||
x.back();
|
||||
for (; ; ) {
|
||||
if (x.nextClean() == ',') {
|
||||
for(; ; ) {
|
||||
if(x.nextClean() == ',') {
|
||||
x.back();
|
||||
this.myArrayList.add(JSONObject.NULL);
|
||||
} else {
|
||||
x.back();
|
||||
this.myArrayList.add(x.nextValue());
|
||||
}
|
||||
switch (x.nextClean()) {
|
||||
switch(x.nextClean()) {
|
||||
case ',':
|
||||
if (x.nextClean() == ']') {
|
||||
if(x.nextClean() == ']') {
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
@@ -131,10 +133,12 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Construct a JSONArray from a source JSON text.
|
||||
*
|
||||
* @param source A string that begins with <code>[</code> <small>(left
|
||||
* bracket)</small> and ends with <code>]</code>
|
||||
* <small>(right bracket)</small>.
|
||||
* @throws JSONException If there is a syntax error.
|
||||
* @param source
|
||||
* A string that begins with <code>[</code> <small>(left
|
||||
* bracket)</small> and ends with <code>]</code>
|
||||
* <small>(right bracket)</small>.
|
||||
* @throws JSONException
|
||||
* If there is a syntax error.
|
||||
*/
|
||||
public JSONArray(String source) throws JSONException {
|
||||
this(new JSONTokener(source));
|
||||
@@ -143,13 +147,14 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Construct a JSONArray from a Collection.
|
||||
*
|
||||
* @param collection A Collection.
|
||||
* @param collection
|
||||
* A Collection.
|
||||
*/
|
||||
public JSONArray(Collection<Object> collection) {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
if (collection != null) {
|
||||
if(collection != null) {
|
||||
Iterator<Object> iter = collection.iterator();
|
||||
while (iter.hasNext()) {
|
||||
while(iter.hasNext()) {
|
||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
||||
}
|
||||
}
|
||||
@@ -158,13 +163,14 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Construct a JSONArray from an array
|
||||
*
|
||||
* @throws JSONException If not an array.
|
||||
* @throws JSONException
|
||||
* If not an array.
|
||||
*/
|
||||
public JSONArray(Object array) throws JSONException {
|
||||
this();
|
||||
if (array.getClass().isArray()) {
|
||||
if(array.getClass().isArray()) {
|
||||
int length = Array.getLength(array);
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
for(int i = 0; i < length; i += 1) {
|
||||
this.put(JSONObject.wrap(Array.get(array, i)));
|
||||
}
|
||||
} else {
|
||||
@@ -180,13 +186,15 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the object value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return An object value.
|
||||
* @throws JSONException If there is no value for the index.
|
||||
* @throws JSONException
|
||||
* If there is no value for the index.
|
||||
*/
|
||||
public Object get(int index) throws JSONException {
|
||||
Object object = this.opt(index);
|
||||
if (object == null) {
|
||||
if(object == null) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
return object;
|
||||
@@ -196,16 +204,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Get the boolean value associated with an index. The string values "true"
|
||||
* and "false" are converted to boolean.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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
|
||||
* convertible to boolean.
|
||||
* @throws JSONException
|
||||
* If there is no value for the index or if the value is not
|
||||
* convertible to boolean.
|
||||
*/
|
||||
public boolean getBoolean(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object.equals(Boolean.FALSE) || (object instanceof String && ((String) object).equalsIgnoreCase("false"))) {
|
||||
if(object.equals(Boolean.FALSE) || (object instanceof String && ((String) object).equalsIgnoreCase("false"))) {
|
||||
return false;
|
||||
} else if (object.equals(Boolean.TRUE) || (object instanceof String && ((String) object).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.");
|
||||
@@ -214,16 +224,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the double value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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.
|
||||
* @throws JSONException
|
||||
* If the key is not found or if the value cannot be converted
|
||||
* to a number.
|
||||
*/
|
||||
public double getDouble(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).doubleValue() : Double.parseDouble((String) object);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
@@ -232,15 +244,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the enum value associated with an index.
|
||||
*
|
||||
* @param clazz The type of enum to retrieve.
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param clazz
|
||||
* The type of enum to retrieve.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The enum value at the index location
|
||||
* @throws JSONException if the key is not found or if the value cannot be converted
|
||||
* to an enum.
|
||||
* @throws JSONException
|
||||
* if the key is not found or if the value cannot be converted
|
||||
* to an enum.
|
||||
*/
|
||||
public <E extends Enum<E>> E getEnum(Class<E> clazz, int index) throws JSONException {
|
||||
E val = optEnum(clazz, index);
|
||||
if (val == null) {
|
||||
if(val == null) {
|
||||
// JSONException should really take a throwable argument.
|
||||
// If it did, I would re-implement this with the Enum.valueOf
|
||||
// method and place any thrown exception in the JSONException
|
||||
@@ -252,16 +267,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the BigDecimal value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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 BigDecimal.
|
||||
* @throws JSONException
|
||||
* If the key is not found or if the value cannot be converted
|
||||
* to a BigDecimal.
|
||||
*/
|
||||
public BigDecimal getBigDecimal(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return new BigDecimal(object.toString());
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException("JSONArray[" + index + "] could not convert to BigDecimal.");
|
||||
}
|
||||
@@ -270,16 +287,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the BigInteger value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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 BigInteger.
|
||||
* @throws JSONException
|
||||
* If the key is not found or if the value cannot be converted
|
||||
* to a BigInteger.
|
||||
*/
|
||||
public BigInteger getBigInteger(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return new BigInteger(object.toString());
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException("JSONArray[" + index + "] could not convert to BigInteger.");
|
||||
}
|
||||
@@ -288,15 +307,17 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the int value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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 is not 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 object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).intValue() : Integer.parseInt((String) object);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
@@ -305,14 +326,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the JSONArray associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return A JSONArray value.
|
||||
* @throws JSONException If there is no value for the index. or if the value is not a
|
||||
* JSONArray
|
||||
* @throws JSONException
|
||||
* If there is no value for the index. or if the value is not a
|
||||
* JSONArray
|
||||
*/
|
||||
public JSONArray getJSONArray(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONArray) {
|
||||
if(object instanceof JSONArray) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
|
||||
@@ -321,14 +344,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the JSONObject associated with an index.
|
||||
*
|
||||
* @param index subscript
|
||||
* @param index
|
||||
* subscript
|
||||
* @return A JSONObject value.
|
||||
* @throws JSONException If there is no value for the index or if the value is not a
|
||||
* JSONObject
|
||||
* @throws JSONException
|
||||
* If there is no value for the index or if the value is not a
|
||||
* JSONObject
|
||||
*/
|
||||
public JSONObject getJSONObject(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof JSONObject) {
|
||||
if(object instanceof JSONObject) {
|
||||
return (JSONObject) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
|
||||
@@ -337,16 +362,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the long value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @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.
|
||||
* @throws JSONException
|
||||
* If the key is not found or if the value cannot be converted
|
||||
* to a number.
|
||||
*/
|
||||
public long getLong(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
try {
|
||||
return object instanceof Number ? ((Number) object).longValue() : Long.parseLong((String) object);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
||||
}
|
||||
@@ -355,13 +382,15 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the string associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return A string value.
|
||||
* @throws JSONException If there is no string value for the index.
|
||||
* @throws JSONException
|
||||
* If there is no string value for the index.
|
||||
*/
|
||||
public String getString(int index) throws JSONException {
|
||||
Object object = this.get(index);
|
||||
if (object instanceof String) {
|
||||
if(object instanceof String) {
|
||||
return (String) object;
|
||||
}
|
||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
||||
@@ -370,7 +399,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Determine if the value is null.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return true if the value at the index is null, or if there is no value.
|
||||
*/
|
||||
public boolean isNull(int index) {
|
||||
@@ -382,16 +412,18 @@ public class JSONArray implements Iterable<Object> {
|
||||
* <code>separator</code> string is inserted between each element. Warning:
|
||||
* This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param separator A string that will be inserted between the elements.
|
||||
* @param separator
|
||||
* A string that will be inserted between the elements.
|
||||
* @return a string.
|
||||
* @throws JSONException If the array contains an invalid number.
|
||||
* @throws JSONException
|
||||
* If the array contains an invalid number.
|
||||
*/
|
||||
public String join(String separator) throws JSONException {
|
||||
int len = this.length();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < len; i += 1) {
|
||||
if (i > 0) {
|
||||
for(int i = 0; i < len; i += 1) {
|
||||
if(i > 0) {
|
||||
sb.append(separator);
|
||||
}
|
||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
||||
@@ -411,7 +443,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the optional object value associated with an index.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return An object value, or null if there is no object at that index.
|
||||
*/
|
||||
public Object opt(int index) {
|
||||
@@ -423,7 +456,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* if there is no value at that index, or if the value is not Boolean.TRUE
|
||||
* or the String "true".
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(int index) {
|
||||
@@ -435,14 +469,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* defaultValue if there is no value at that index or if it is not a Boolean
|
||||
* or the String "true" or "false" (case insensitive).
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue A boolean default.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* A boolean default.
|
||||
* @return The truth.
|
||||
*/
|
||||
public boolean optBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -453,7 +489,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* if there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The value.
|
||||
*/
|
||||
public double optDouble(int index) {
|
||||
@@ -465,14 +502,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* is returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index subscript
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* subscript
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public double optDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -483,7 +522,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The value.
|
||||
*/
|
||||
public int optInt(int index) {
|
||||
@@ -495,14 +535,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public int optInt(int index, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -511,8 +553,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the enum value associated with a key.
|
||||
*
|
||||
* @param clazz The type of enum to retrieve.
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param clazz
|
||||
* The type of enum to retrieve.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The enum value at the index location or null if not found
|
||||
*/
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index) {
|
||||
@@ -522,29 +566,32 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the enum value associated with a key.
|
||||
*
|
||||
* @param clazz The type of enum to retrieve.
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default in case the value is not found
|
||||
* @param clazz
|
||||
* The type of enum to retrieve.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default in case the value is not found
|
||||
* @return The enum value at the index location or defaultValue if the value
|
||||
* is not found or cannot be assigned to clazz
|
||||
*/
|
||||
public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue) {
|
||||
try {
|
||||
Object val = this.opt(index);
|
||||
if (JSONObject.NULL.equals(val)) {
|
||||
if(JSONObject.NULL.equals(val)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (clazz.isAssignableFrom(val.getClass())) {
|
||||
if(clazz.isAssignableFrom(val.getClass())) {
|
||||
// we just checked it!
|
||||
@SuppressWarnings("unchecked")
|
||||
E myE = (E) val;
|
||||
return myE;
|
||||
}
|
||||
return Enum.valueOf(clazz, val.toString());
|
||||
} catch (IllegalArgumentException e) {
|
||||
} catch(IllegalArgumentException e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
} catch (NullPointerException e) {
|
||||
} catch(NullPointerException e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -555,14 +602,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
* value is not a number and cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||
try {
|
||||
return this.getBigInteger(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -573,14 +622,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
* value is not a number and cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
|
||||
try {
|
||||
return this.getBigDecimal(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -589,7 +640,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Get the optional JSONArray associated with an index.
|
||||
*
|
||||
* @param index subscript
|
||||
* @param index
|
||||
* subscript
|
||||
* @return A JSONArray value, or null if the index has no value, or if the
|
||||
* value is not a JSONArray.
|
||||
*/
|
||||
@@ -603,7 +655,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* the key is not found, or null if the index has no value, or if the value
|
||||
* is not a JSONObject.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return A JSONObject value.
|
||||
*/
|
||||
public JSONObject optJSONObject(int index) {
|
||||
@@ -616,7 +669,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* there is no value for the index, or if the value is not a number and
|
||||
* cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return The value.
|
||||
*/
|
||||
public long optLong(int index) {
|
||||
@@ -628,14 +682,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* returned if there is no value for the index, or if the value is not a
|
||||
* number and cannot be converted to a number.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public long optLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -646,7 +702,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* empty string if there is no value at that index. If the value is not a
|
||||
* string and is not null, then it is coverted to a string.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @return A String value.
|
||||
*/
|
||||
public String optString(int index) {
|
||||
@@ -657,8 +714,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Get the optional string associated with an index. The defaultValue is
|
||||
* returned if the key is not found.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @return A String value.
|
||||
*/
|
||||
public String optString(int index, String defaultValue) {
|
||||
@@ -669,7 +728,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Append a boolean value. This increases the array's length by one.
|
||||
*
|
||||
* @param value A boolean value.
|
||||
* @param value
|
||||
* A boolean value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(boolean value) {
|
||||
@@ -681,7 +741,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Put a value in the JSONArray, where the value will be a JSONArray which
|
||||
* is produced from a Collection.
|
||||
*
|
||||
* @param value A Collection value.
|
||||
* @param value
|
||||
* A Collection value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(Collection<Object> value) {
|
||||
@@ -692,9 +753,11 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Append a double value. This increases the array's length by one.
|
||||
*
|
||||
* @param value A double value.
|
||||
* @param value
|
||||
* A double value.
|
||||
* @return this.
|
||||
* @throws JSONException if the value is not finite.
|
||||
* @throws JSONException
|
||||
* if the value is not finite.
|
||||
*/
|
||||
public JSONArray put(double value) throws JSONException {
|
||||
Double d = Double.valueOf(value);
|
||||
@@ -706,7 +769,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Append an int value. This increases the array's length by one.
|
||||
*
|
||||
* @param value An int value.
|
||||
* @param value
|
||||
* An int value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(int value) {
|
||||
@@ -717,7 +781,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Append an long value. This increases the array's length by one.
|
||||
*
|
||||
* @param value A long value.
|
||||
* @param value
|
||||
* A long value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(long value) {
|
||||
@@ -729,7 +794,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Put a value in the JSONArray, where the value will be a JSONObject which
|
||||
* is produced from a Map.
|
||||
*
|
||||
* @param value A Map value.
|
||||
* @param value
|
||||
* A Map value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(Map<String, Object> value) {
|
||||
@@ -740,9 +806,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Append an object value. This increases the array's length by one.
|
||||
*
|
||||
* @param value An object value. The value should be a Boolean, Double,
|
||||
* Integer, JSONArray, JSONObject, Long, or String, or the
|
||||
* JSONObject.NULL object.
|
||||
* @param value
|
||||
* An object value. The value should be a Boolean, Double,
|
||||
* Integer, JSONArray, JSONObject, Long, or String, or the
|
||||
* JSONObject.NULL object.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(Object value) {
|
||||
@@ -755,10 +822,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
* than the length of the JSONArray, then null elements will be added as
|
||||
* necessary to pad it out.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value A boolean value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* A boolean value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative.
|
||||
* @throws JSONException
|
||||
* If the index is negative.
|
||||
*/
|
||||
public JSONArray put(int index, boolean value) throws JSONException {
|
||||
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
|
||||
@@ -769,10 +839,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Put a value in the JSONArray, where the value will be a JSONArray which
|
||||
* is produced from a Collection.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value A Collection value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* A Collection value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative or if the value is not finite.
|
||||
* @throws JSONException
|
||||
* If the index is negative or if the value is not finite.
|
||||
*/
|
||||
public JSONArray put(int index, Collection<Object> value) throws JSONException {
|
||||
this.put(index, new JSONArray(value));
|
||||
@@ -784,10 +857,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
* the JSONArray, then null elements will be added as necessary to pad it
|
||||
* out.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value A double value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* A double value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative or if the value is not finite.
|
||||
* @throws JSONException
|
||||
* If the index is negative or if the value is not finite.
|
||||
*/
|
||||
public JSONArray put(int index, double value) throws JSONException {
|
||||
this.put(index, Double.valueOf(value));
|
||||
@@ -799,10 +875,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
* the JSONArray, then null elements will be added as necessary to pad it
|
||||
* out.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value An int value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* An int value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative.
|
||||
* @throws JSONException
|
||||
* If the index is negative.
|
||||
*/
|
||||
public JSONArray put(int index, int value) throws JSONException {
|
||||
this.put(index, Integer.valueOf(value));
|
||||
@@ -814,10 +893,13 @@ public class JSONArray implements Iterable<Object> {
|
||||
* the JSONArray, then null elements will be added as necessary to pad it
|
||||
* out.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value A long value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* A long value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative.
|
||||
* @throws JSONException
|
||||
* If the index is negative.
|
||||
*/
|
||||
public JSONArray put(int index, long value) throws JSONException {
|
||||
this.put(index, Long.valueOf(value));
|
||||
@@ -828,11 +910,14 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Put a value in the JSONArray, where the value will be a JSONObject that
|
||||
* is produced from a Map.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value The Map value.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* The Map value.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative or if the the value is an invalid
|
||||
* number.
|
||||
* @throws JSONException
|
||||
* If the index is negative or if the the value is an invalid
|
||||
* number.
|
||||
*/
|
||||
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
|
||||
this.put(index, new JSONObject(value));
|
||||
@@ -844,23 +929,26 @@ public class JSONArray implements Iterable<Object> {
|
||||
* than the length of the JSONArray, then null elements will be added as
|
||||
* necessary to pad it out.
|
||||
*
|
||||
* @param index The subscript.
|
||||
* @param value The value to put into the array. The value should be a
|
||||
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or
|
||||
* String, or the JSONObject.NULL object.
|
||||
* @param index
|
||||
* The subscript.
|
||||
* @param value
|
||||
* The value to put into the array. The value should be a
|
||||
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or
|
||||
* String, or the JSONObject.NULL object.
|
||||
* @return this.
|
||||
* @throws JSONException If the index is negative or if the the value is an invalid
|
||||
* number.
|
||||
* @throws JSONException
|
||||
* If the index is negative or if the the value is an invalid
|
||||
* number.
|
||||
*/
|
||||
public JSONArray put(int index, Object value) throws JSONException {
|
||||
JSONObject.testValidity(value);
|
||||
if (index < 0) {
|
||||
if(index < 0) {
|
||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
||||
}
|
||||
if (index < this.length()) {
|
||||
if(index < this.length()) {
|
||||
this.myArrayList.set(index, value);
|
||||
} else {
|
||||
while (index != this.length()) {
|
||||
while(index != this.length()) {
|
||||
this.put(JSONObject.NULL);
|
||||
}
|
||||
this.put(value);
|
||||
@@ -871,7 +959,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
/**
|
||||
* Remove an index and close the hole.
|
||||
*
|
||||
* @param index The index of the element to be removed.
|
||||
* @param index
|
||||
* The index of the element to be removed.
|
||||
* @return The value that was associated with the index, or null if there
|
||||
* was no value.
|
||||
*/
|
||||
@@ -883,30 +972,31 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Determine if two JSONArrays are similar. They must contain similar
|
||||
* sequences.
|
||||
*
|
||||
* @param other The other JSONArray
|
||||
* @param other
|
||||
* The other JSONArray
|
||||
* @return true if they are equal
|
||||
*/
|
||||
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
|
||||
public boolean similar(Object other) {
|
||||
if (!(other instanceof JSONArray)) {
|
||||
if(!(other instanceof JSONArray)) {
|
||||
return false;
|
||||
}
|
||||
int len = this.length();
|
||||
if (len != ((JSONArray) other).length()) {
|
||||
if(len != ((JSONArray) other).length()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < len; i += 1) {
|
||||
for(int i = 0; i < len; i += 1) {
|
||||
Object valueThis = this.get(i);
|
||||
Object valueOther = ((JSONArray) other).get(i);
|
||||
if (valueThis instanceof JSONObject) {
|
||||
if (!((JSONObject) valueThis).similar(valueOther)) {
|
||||
if(valueThis instanceof JSONObject) {
|
||||
if(!((JSONObject) valueThis).similar(valueOther)) {
|
||||
return false;
|
||||
}
|
||||
} else if (valueThis instanceof JSONArray) {
|
||||
if (!((JSONArray) valueThis).similar(valueOther)) {
|
||||
} else if(valueThis instanceof JSONArray) {
|
||||
if(!((JSONArray) valueThis).similar(valueOther)) {
|
||||
return false;
|
||||
}
|
||||
} else if (!valueThis.equals(valueOther)) {
|
||||
} else if(!valueThis.equals(valueOther)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -917,18 +1007,20 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Produce a JSONObject by combining a JSONArray of names with the values of
|
||||
* this JSONArray.
|
||||
*
|
||||
* @param names A JSONArray containing a list of key strings. These will be
|
||||
* paired with the values.
|
||||
* @param names
|
||||
* A JSONArray containing a list of key strings. These will be
|
||||
* paired with the values.
|
||||
* @return A JSONObject, or null if there are no names or if this JSONArray
|
||||
* has no values.
|
||||
* @throws JSONException If any of the names are null.
|
||||
* @throws JSONException
|
||||
* If any of the names are null.
|
||||
*/
|
||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
||||
if (names == null || names.length() == 0 || this.length() == 0) {
|
||||
if(names == null || names.length() == 0 || this.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
JSONObject jo = new JSONObject();
|
||||
for (int i = 0; i < names.length(); i += 1) {
|
||||
for(int i = 0; i < names.length(); i += 1) {
|
||||
jo.put(names.getString(i), this.opt(i));
|
||||
}
|
||||
return jo;
|
||||
@@ -949,7 +1041,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
public String toString() {
|
||||
try {
|
||||
return this.toString(0);
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
return null;
|
||||
}
|
||||
@@ -959,16 +1051,16 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Make a prettyprinted JSON text of this JSONArray. Warning: This method
|
||||
* assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param indentFactor The number of spaces to add to each level of indentation.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @return a printable, displayable, transmittable representation of the
|
||||
* object, beginning with <code>[</code> <small>(left
|
||||
* bracket)</small> and ending with <code>]</code>
|
||||
* <small>(right bracket)</small>.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
synchronized (sw.getBuffer()) {
|
||||
synchronized(sw.getBuffer()) {
|
||||
return this.write(sw, indentFactor, 0).toString();
|
||||
}
|
||||
}
|
||||
@@ -980,7 +1072,6 @@ public class JSONArray implements Iterable<Object> {
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public Writer write(Writer writer) throws JSONException {
|
||||
return this.write(writer, 0, 0);
|
||||
@@ -992,10 +1083,11 @@ public class JSONArray implements Iterable<Object> {
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param indentFactor The number of spaces to add to each level of indentation.
|
||||
* @param indent The indention of the top level.
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @param indent
|
||||
* The indention of the top level.
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
||||
try {
|
||||
@@ -1003,30 +1095,30 @@ public class JSONArray implements Iterable<Object> {
|
||||
int length = this.length();
|
||||
writer.write('[');
|
||||
|
||||
if (length == 1) {
|
||||
if(length == 1) {
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(0), indentFactor, indent);
|
||||
} else if (length != 0) {
|
||||
} else if(length != 0) {
|
||||
final int newindent = indent + indentFactor;
|
||||
|
||||
for (int i = 0; i < length; i += 1) {
|
||||
if (commanate) {
|
||||
for(int i = 0; i < length; i += 1) {
|
||||
if(commanate) {
|
||||
writer.write(',');
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
if(indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, newindent);
|
||||
JSONObject.writeValue(writer, this.myArrayList.get(i), indentFactor, newindent);
|
||||
commanate = true;
|
||||
}
|
||||
if (indentFactor > 0) {
|
||||
if(indentFactor > 0) {
|
||||
writer.write('\n');
|
||||
}
|
||||
JSONObject.indent(writer, indent);
|
||||
}
|
||||
writer.write(']');
|
||||
return writer;
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,8 @@ public class JSONException extends RuntimeException {
|
||||
/**
|
||||
* Constructs a JSONException with an explanatory message.
|
||||
*
|
||||
* @param message Detail about the reason for the exception.
|
||||
* @param message
|
||||
* Detail about the reason for the exception.
|
||||
*/
|
||||
public JSONException(String message) {
|
||||
super(message);
|
||||
@@ -41,7 +42,8 @@ public class JSONException extends RuntimeException {
|
||||
/**
|
||||
* Constructs a new JSONException with the specified cause.
|
||||
*
|
||||
* @param cause The cause.
|
||||
* @param cause
|
||||
* The cause.
|
||||
*/
|
||||
public JSONException(Throwable cause) {
|
||||
super(cause.getMessage());
|
||||
|
||||
@@ -34,10 +34,13 @@ public class JSONML {
|
||||
/**
|
||||
* Parse XML values and store them in a JSONArray.
|
||||
*
|
||||
* @param x The XMLTokener containing the source string.
|
||||
* @param arrayForm true if array form, false if object form.
|
||||
* @param ja The JSONArray that is containing the current tag or null if we
|
||||
* are at the outermost level.
|
||||
* @param x
|
||||
* The XMLTokener containing the source string.
|
||||
* @param arrayForm
|
||||
* true if array form, false if object form.
|
||||
* @param ja
|
||||
* The JSONArray that is containing the current tag or null if we
|
||||
* are at the outermost level.
|
||||
* @return A JSONArray if the value is the outermost tag, otherwise null.
|
||||
*/
|
||||
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja) throws JSONException {
|
||||
@@ -56,41 +59,41 @@ public class JSONML {
|
||||
// <! ... >
|
||||
// <? ... ?>
|
||||
|
||||
while (true) {
|
||||
if (!x.more()) {
|
||||
while(true) {
|
||||
if(!x.more()) {
|
||||
throw x.syntaxError("Bad XML");
|
||||
}
|
||||
token = x.nextContent();
|
||||
if (token == XML.LT) {
|
||||
if(token == XML.LT) {
|
||||
token = x.nextToken();
|
||||
if (token instanceof Character) {
|
||||
if (token == XML.SLASH) {
|
||||
if(token instanceof Character) {
|
||||
if(token == XML.SLASH) {
|
||||
|
||||
// Close tag </
|
||||
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
if(!(token instanceof String)) {
|
||||
throw new JSONException("Expected a closing name instead of '" + token + "'.");
|
||||
}
|
||||
if (x.nextToken() != XML.GT) {
|
||||
if(x.nextToken() != XML.GT) {
|
||||
throw x.syntaxError("Misshaped close tag");
|
||||
}
|
||||
return token;
|
||||
} else if (token == XML.BANG) {
|
||||
} else if(token == XML.BANG) {
|
||||
|
||||
// <!
|
||||
|
||||
c = x.next();
|
||||
if (c == '-') {
|
||||
if (x.next() == '-') {
|
||||
if(c == '-') {
|
||||
if(x.next() == '-') {
|
||||
x.skipPast("-->");
|
||||
} else {
|
||||
x.back();
|
||||
}
|
||||
} else if (c == '[') {
|
||||
} else if(c == '[') {
|
||||
token = x.nextToken();
|
||||
if (token.equals("CDATA") && x.next() == '[') {
|
||||
if (ja != null) {
|
||||
if(token.equals("CDATA") && x.next() == '[') {
|
||||
if(ja != null) {
|
||||
ja.put(x.nextCDATA());
|
||||
}
|
||||
} else {
|
||||
@@ -100,16 +103,16 @@ public class JSONML {
|
||||
i = 1;
|
||||
do {
|
||||
token = x.nextMeta();
|
||||
if (token == null) {
|
||||
if(token == null) {
|
||||
throw x.syntaxError("Missing '>' after '<!'.");
|
||||
} else if (token == XML.LT) {
|
||||
} else if(token == XML.LT) {
|
||||
i += 1;
|
||||
} else if (token == XML.GT) {
|
||||
} else if(token == XML.GT) {
|
||||
i -= 1;
|
||||
}
|
||||
} while (i > 0);
|
||||
} while(i > 0);
|
||||
}
|
||||
} else if (token == XML.QUEST) {
|
||||
} else if(token == XML.QUEST) {
|
||||
|
||||
// <?
|
||||
|
||||
@@ -121,45 +124,45 @@ public class JSONML {
|
||||
// Open tag <
|
||||
|
||||
} else {
|
||||
if (!(token instanceof String)) {
|
||||
if(!(token instanceof String)) {
|
||||
throw x.syntaxError("Bad tagName '" + token + "'.");
|
||||
}
|
||||
tagName = (String) token;
|
||||
newja = new JSONArray();
|
||||
newjo = new JSONObject();
|
||||
if (arrayForm) {
|
||||
if(arrayForm) {
|
||||
newja.put(tagName);
|
||||
if (ja != null) {
|
||||
if(ja != null) {
|
||||
ja.put(newja);
|
||||
}
|
||||
} else {
|
||||
newjo.put("tagName", tagName);
|
||||
if (ja != null) {
|
||||
if(ja != null) {
|
||||
ja.put(newjo);
|
||||
}
|
||||
}
|
||||
token = null;
|
||||
for (; ; ) {
|
||||
if (token == null) {
|
||||
for(; ; ) {
|
||||
if(token == null) {
|
||||
token = x.nextToken();
|
||||
}
|
||||
if (token == null) {
|
||||
if(token == null) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (!(token instanceof String)) {
|
||||
if(!(token instanceof String)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// attribute = value
|
||||
|
||||
attribute = (String) token;
|
||||
if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
|
||||
if(!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
|
||||
throw x.syntaxError("Reserved attribute.");
|
||||
}
|
||||
token = x.nextToken();
|
||||
if (token == XML.EQ) {
|
||||
if(token == XML.EQ) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
if(!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
newjo.accumulate(attribute, XML.stringToValue((String) token));
|
||||
@@ -168,18 +171,18 @@ public class JSONML {
|
||||
newjo.accumulate(attribute, "");
|
||||
}
|
||||
}
|
||||
if (arrayForm && newjo.length() > 0) {
|
||||
if(arrayForm && newjo.length() > 0) {
|
||||
newja.put(newjo);
|
||||
}
|
||||
|
||||
// Empty tag <.../>
|
||||
|
||||
if (token == XML.SLASH) {
|
||||
if (x.nextToken() != XML.GT) {
|
||||
if(token == XML.SLASH) {
|
||||
if(x.nextToken() != XML.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (ja == null) {
|
||||
if (arrayForm) {
|
||||
if(ja == null) {
|
||||
if(arrayForm) {
|
||||
return newja;
|
||||
} else {
|
||||
return newjo;
|
||||
@@ -189,20 +192,20 @@ public class JSONML {
|
||||
// Content, between <...> and </...>
|
||||
|
||||
} else {
|
||||
if (token != XML.GT) {
|
||||
if(token != XML.GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
closeTag = (String) parse(x, arrayForm, newja);
|
||||
if (closeTag != null) {
|
||||
if (!closeTag.equals(tagName)) {
|
||||
if(closeTag != null) {
|
||||
if(!closeTag.equals(tagName)) {
|
||||
throw x.syntaxError("Mismatched '" + tagName + "' and '" + closeTag + "'");
|
||||
}
|
||||
tagName = null;
|
||||
if (!arrayForm && newja.length() > 0) {
|
||||
if(!arrayForm && newja.length() > 0) {
|
||||
newjo.put("childNodes", newja);
|
||||
}
|
||||
if (ja == null) {
|
||||
if (arrayForm) {
|
||||
if(ja == null) {
|
||||
if(arrayForm) {
|
||||
return newja;
|
||||
} else {
|
||||
return newjo;
|
||||
@@ -212,7 +215,7 @@ public class JSONML {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (ja != null) {
|
||||
if(ja != null) {
|
||||
ja.put(token instanceof String ? XML.stringToValue((String) token) : token);
|
||||
}
|
||||
}
|
||||
@@ -228,7 +231,8 @@ public class JSONML {
|
||||
* JSONArrays will represent the child tags. Comments, prologs, DTDs, and
|
||||
* <code><[ [ ]]></code> are ignored.
|
||||
*
|
||||
* @param string The source string.
|
||||
* @param string
|
||||
* The source string.
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
*/
|
||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||
@@ -244,7 +248,8 @@ public class JSONML {
|
||||
* JSONArrays will represent the child content and tags. Comments, prologs,
|
||||
* DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
*
|
||||
* @param x An XMLTokener.
|
||||
* @param x
|
||||
* An XMLTokener.
|
||||
* @return A JSONArray containing the structured data from the XML string.
|
||||
*/
|
||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||
@@ -261,7 +266,8 @@ public class JSONML {
|
||||
* <p>
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
*
|
||||
* @param x An XMLTokener of the XML source text.
|
||||
* @param x
|
||||
* An XMLTokener of the XML source text.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
*/
|
||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||
@@ -278,7 +284,8 @@ public class JSONML {
|
||||
* <p>
|
||||
* Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
|
||||
*
|
||||
* @param string The XML source text.
|
||||
* @param string
|
||||
* The XML source text.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
@@ -288,7 +295,8 @@ public class JSONML {
|
||||
/**
|
||||
* Reverse the JSONML transformation, making an XML text from a JSONArray.
|
||||
*
|
||||
* @param ja A JSONArray.
|
||||
* @param ja
|
||||
* A JSONArray.
|
||||
* @return An XML string.
|
||||
*/
|
||||
public static String toString(JSONArray ja) throws JSONException {
|
||||
@@ -311,18 +319,18 @@ public class JSONML {
|
||||
sb.append(tagName);
|
||||
|
||||
object = ja.opt(1);
|
||||
if (object instanceof JSONObject) {
|
||||
if(object instanceof JSONObject) {
|
||||
i = 2;
|
||||
jo = (JSONObject) object;
|
||||
|
||||
// Emit the attributes
|
||||
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
key = keys.next();
|
||||
XML.noSpace(key);
|
||||
value = jo.optString(key);
|
||||
if (value != null) {
|
||||
if(value != null) {
|
||||
sb.append(' ');
|
||||
sb.append(XML.escape(key));
|
||||
sb.append('=');
|
||||
@@ -338,25 +346,25 @@ public class JSONML {
|
||||
// Emit content in body
|
||||
|
||||
length = ja.length();
|
||||
if (i >= length) {
|
||||
if(i >= length) {
|
||||
sb.append('/');
|
||||
} else {
|
||||
sb.append('>');
|
||||
do {
|
||||
object = ja.get(i);
|
||||
i += 1;
|
||||
if (object != null) {
|
||||
if (object instanceof String) {
|
||||
if(object != null) {
|
||||
if(object instanceof String) {
|
||||
sb.append(XML.escape(object.toString()));
|
||||
} else if (object instanceof JSONObject) {
|
||||
} else if(object instanceof JSONObject) {
|
||||
sb.append(toString((JSONObject) object));
|
||||
} else if (object instanceof JSONArray) {
|
||||
} else if(object instanceof JSONArray) {
|
||||
sb.append(toString((JSONArray) object));
|
||||
} else {
|
||||
sb.append(object);
|
||||
}
|
||||
}
|
||||
} while (i < length);
|
||||
} while(i < length);
|
||||
sb.append('<');
|
||||
sb.append('/');
|
||||
sb.append(tagName);
|
||||
@@ -371,7 +379,8 @@ public class JSONML {
|
||||
* then it must have a "childNodes" property containing an array of objects.
|
||||
* The other properties are attributes with string values.
|
||||
*
|
||||
* @param jo A JSONObject.
|
||||
* @param jo
|
||||
* A JSONObject.
|
||||
* @return An XML string.
|
||||
*/
|
||||
public static String toString(JSONObject jo) throws JSONException {
|
||||
@@ -388,7 +397,7 @@ public class JSONML {
|
||||
// Emit <tagName
|
||||
|
||||
tagName = jo.optString("tagName");
|
||||
if (tagName == null) {
|
||||
if(tagName == null) {
|
||||
return XML.escape(jo.toString());
|
||||
}
|
||||
XML.noSpace(tagName);
|
||||
@@ -399,12 +408,12 @@ public class JSONML {
|
||||
// Emit the attributes
|
||||
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
key = keys.next();
|
||||
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
|
||||
if(!"tagName".equals(key) && !"childNodes".equals(key)) {
|
||||
XML.noSpace(key);
|
||||
value = jo.optString(key);
|
||||
if (value != null) {
|
||||
if(value != null) {
|
||||
sb.append(' ');
|
||||
sb.append(XML.escape(key));
|
||||
sb.append('=');
|
||||
@@ -418,19 +427,19 @@ public class JSONML {
|
||||
// Emit content in body
|
||||
|
||||
ja = jo.optJSONArray("childNodes");
|
||||
if (ja == null) {
|
||||
if(ja == null) {
|
||||
sb.append('/');
|
||||
} else {
|
||||
sb.append('>');
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
object = ja.get(i);
|
||||
if (object != null) {
|
||||
if (object instanceof String) {
|
||||
if(object != null) {
|
||||
if(object instanceof String) {
|
||||
sb.append(XML.escape(object.toString()));
|
||||
} else if (object instanceof JSONObject) {
|
||||
} else if(object instanceof JSONObject) {
|
||||
sb.append(toString((JSONObject) object));
|
||||
} else if (object instanceof JSONArray) {
|
||||
} else if(object instanceof JSONArray) {
|
||||
sb.append(toString((JSONArray) object));
|
||||
} else {
|
||||
sb.append(object);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -50,7 +50,8 @@ public class JSONTokener {
|
||||
/**
|
||||
* Construct a JSONTokener from a Reader.
|
||||
*
|
||||
* @param reader A reader.
|
||||
* @param reader
|
||||
* A reader.
|
||||
*/
|
||||
public JSONTokener(Reader reader) {
|
||||
this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
|
||||
@@ -65,7 +66,8 @@ public class JSONTokener {
|
||||
/**
|
||||
* Construct a JSONTokener from an InputStream.
|
||||
*
|
||||
* @param inputStream The source.
|
||||
* @param inputStream
|
||||
* The source.
|
||||
*/
|
||||
public JSONTokener(InputStream inputStream) throws JSONException {
|
||||
this(new InputStreamReader(inputStream));
|
||||
@@ -74,7 +76,8 @@ public class JSONTokener {
|
||||
/**
|
||||
* Construct a JSONTokener from a string.
|
||||
*
|
||||
* @param s A source string.
|
||||
* @param s
|
||||
* A source string.
|
||||
*/
|
||||
public JSONTokener(String s) {
|
||||
this(new StringReader(s));
|
||||
@@ -83,18 +86,19 @@ public class JSONTokener {
|
||||
/**
|
||||
* Get the hex value of a character (base16).
|
||||
*
|
||||
* @param c A character between '0' and '9' or between 'A' and 'F' or
|
||||
* between 'a' and 'f'.
|
||||
* @param c
|
||||
* A character between '0' and '9' or between 'A' and 'F' or
|
||||
* between 'a' and 'f'.
|
||||
* @return An int between 0 and 15, or -1 if c was not a hex digit.
|
||||
*/
|
||||
public static int dehexchar(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
if(c >= '0' && c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F') {
|
||||
if(c >= 'A' && c <= 'F') {
|
||||
return c - ('A' - 10);
|
||||
}
|
||||
if (c >= 'a' && c <= 'f') {
|
||||
if(c >= 'a' && c <= 'f') {
|
||||
return c - ('a' - 10);
|
||||
}
|
||||
return -1;
|
||||
@@ -106,7 +110,7 @@ public class JSONTokener {
|
||||
* next number or identifier.
|
||||
*/
|
||||
public void back() throws JSONException {
|
||||
if (this.usePrevious || this.index <= 0) {
|
||||
if(this.usePrevious || this.index <= 0) {
|
||||
throw new JSONException("Stepping back two steps is not supported");
|
||||
}
|
||||
this.index -= 1;
|
||||
@@ -127,7 +131,7 @@ public class JSONTokener {
|
||||
*/
|
||||
public boolean more() throws JSONException {
|
||||
this.next();
|
||||
if (this.end()) {
|
||||
if(this.end()) {
|
||||
return false;
|
||||
}
|
||||
this.back();
|
||||
@@ -141,27 +145,27 @@ public class JSONTokener {
|
||||
*/
|
||||
public char next() throws JSONException {
|
||||
int c;
|
||||
if (this.usePrevious) {
|
||||
if(this.usePrevious) {
|
||||
this.usePrevious = false;
|
||||
c = this.previous;
|
||||
} else {
|
||||
try {
|
||||
c = this.reader.read();
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
|
||||
if (c <= 0) { // End of stream
|
||||
if(c <= 0) { // End of stream
|
||||
this.eof = true;
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
this.index += 1;
|
||||
if (this.previous == '\r') {
|
||||
if(this.previous == '\r') {
|
||||
this.line += 1;
|
||||
this.character = c == '\n' ? 0 : 1;
|
||||
} else if (c == '\n') {
|
||||
} else if(c == '\n') {
|
||||
this.line += 1;
|
||||
this.character = 0;
|
||||
} else {
|
||||
@@ -175,13 +179,15 @@ public class JSONTokener {
|
||||
* Consume the next character, and check that it matches a specified
|
||||
* character.
|
||||
*
|
||||
* @param c The character to match.
|
||||
* @param c
|
||||
* The character to match.
|
||||
* @return The character.
|
||||
* @throws JSONException if the character does not match.
|
||||
* @throws JSONException
|
||||
* if the character does not match.
|
||||
*/
|
||||
public char next(char c) throws JSONException {
|
||||
char n = this.next();
|
||||
if (n != c) {
|
||||
if(n != c) {
|
||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" + n + "'");
|
||||
}
|
||||
return n;
|
||||
@@ -190,22 +196,24 @@ public class JSONTokener {
|
||||
/**
|
||||
* Get the next n characters.
|
||||
*
|
||||
* @param n The number of characters to take.
|
||||
* @param n
|
||||
* The number of characters to take.
|
||||
* @return A string of n characters.
|
||||
* @throws JSONException Substring bounds error if there are not n characters
|
||||
* remaining in the source string.
|
||||
* @throws JSONException
|
||||
* Substring bounds error if there are not n characters
|
||||
* remaining in the source string.
|
||||
*/
|
||||
public String next(int n) throws JSONException {
|
||||
if (n == 0) {
|
||||
if(n == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char[] chars = new char[n];
|
||||
int pos = 0;
|
||||
|
||||
while (pos < n) {
|
||||
while(pos < n) {
|
||||
chars[pos] = this.next();
|
||||
if (this.end()) {
|
||||
if(this.end()) {
|
||||
throw this.syntaxError("Substring bounds error");
|
||||
}
|
||||
pos += 1;
|
||||
@@ -217,12 +225,11 @@ public class JSONTokener {
|
||||
* Get the next char in the string, skipping whitespace.
|
||||
*
|
||||
* @return A character, or 0 if there are no more characters.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public char nextClean() throws JSONException {
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
char c = this.next();
|
||||
if (c == 0 || c > ' ') {
|
||||
if(c == 0 || c > ' ') {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -233,25 +240,27 @@ public class JSONTokener {
|
||||
* processing is done. The formal JSON format does not allow strings in
|
||||
* single quotes, but an implementation is allowed to accept them.
|
||||
*
|
||||
* @param quote The quoting character, either <code>"</code>
|
||||
* <small>(double quote)</small> or <code>'</code>
|
||||
* <small>(single quote)</small>.
|
||||
* @param quote
|
||||
* The quoting character, either <code>"</code>
|
||||
* <small>(double quote)</small> or <code>'</code>
|
||||
* <small>(single quote)</small>.
|
||||
* @return A String.
|
||||
* @throws JSONException Unterminated string.
|
||||
* @throws JSONException
|
||||
* Unterminated string.
|
||||
*/
|
||||
public String nextString(char quote) throws JSONException {
|
||||
char c;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case 0:
|
||||
case '\n':
|
||||
case '\r':
|
||||
throw this.syntaxError("Unterminated string");
|
||||
case '\\':
|
||||
c = this.next();
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case 'b':
|
||||
sb.append('\b');
|
||||
break;
|
||||
@@ -281,7 +290,7 @@ public class JSONTokener {
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (c == quote) {
|
||||
if(c == quote) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
@@ -293,15 +302,16 @@ public class JSONTokener {
|
||||
* Get the text up but not including the specified character or the end of
|
||||
* line, whichever comes first.
|
||||
*
|
||||
* @param delimiter A delimiter character.
|
||||
* @param delimiter
|
||||
* A delimiter character.
|
||||
* @return A string.
|
||||
*/
|
||||
public String nextTo(char delimiter) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
char c = this.next();
|
||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
if(c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||
if(c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
@@ -314,16 +324,17 @@ public class JSONTokener {
|
||||
* Get the text up but not including one of the specified delimiter
|
||||
* characters or the end of line, whichever comes first.
|
||||
*
|
||||
* @param delimiters A set of delimiter characters.
|
||||
* @param delimiters
|
||||
* A set of delimiter characters.
|
||||
* @return A string, trimmed.
|
||||
*/
|
||||
public String nextTo(String delimiters) throws JSONException {
|
||||
char c;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = this.next();
|
||||
if (delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') {
|
||||
if (c != 0) {
|
||||
if(delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') {
|
||||
if(c != 0) {
|
||||
this.back();
|
||||
}
|
||||
return sb.toString().trim();
|
||||
@@ -337,13 +348,14 @@ public class JSONTokener {
|
||||
* JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
|
||||
*
|
||||
* @return An object.
|
||||
* @throws JSONException If syntax error.
|
||||
* @throws JSONException
|
||||
* If syntax error.
|
||||
*/
|
||||
public Object nextValue() throws JSONException {
|
||||
char c = this.nextClean();
|
||||
String string;
|
||||
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case '"':
|
||||
case '\'':
|
||||
return this.nextString(c);
|
||||
@@ -365,14 +377,14 @@ public class JSONTokener {
|
||||
*/
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
while(c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||
sb.append(c);
|
||||
c = this.next();
|
||||
}
|
||||
this.back();
|
||||
|
||||
string = sb.toString().trim();
|
||||
if ("".equals(string)) {
|
||||
if("".equals(string)) {
|
||||
throw this.syntaxError("Missing value");
|
||||
}
|
||||
return JSONObject.stringToValue(string);
|
||||
@@ -382,7 +394,8 @@ public class JSONTokener {
|
||||
* Skip characters until the next character is the requested character. If
|
||||
* the requested character is not found, no characters are skipped.
|
||||
*
|
||||
* @param to A character to skip to.
|
||||
* @param to
|
||||
* A character to skip to.
|
||||
* @return The requested character, or zero if the requested character is
|
||||
* not found.
|
||||
*/
|
||||
@@ -395,15 +408,15 @@ public class JSONTokener {
|
||||
this.reader.mark(1000000);
|
||||
do {
|
||||
c = this.next();
|
||||
if (c == 0) {
|
||||
if(c == 0) {
|
||||
this.reader.reset();
|
||||
this.index = startIndex;
|
||||
this.character = startCharacter;
|
||||
this.line = startLine;
|
||||
return c;
|
||||
}
|
||||
} while (c != to);
|
||||
} catch (IOException e) {
|
||||
} while(c != to);
|
||||
} catch(IOException e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
@@ -414,7 +427,8 @@ public class JSONTokener {
|
||||
/**
|
||||
* Make a JSONException to signal a syntax error.
|
||||
*
|
||||
* @param message The error message.
|
||||
* @param message
|
||||
* The error message.
|
||||
* @return A JSONException object, suitable for throwing
|
||||
*/
|
||||
public JSONException syntaxError(String message) {
|
||||
|
||||
@@ -96,25 +96,27 @@ public class JSONWriter {
|
||||
/**
|
||||
* Append a value.
|
||||
*
|
||||
* @param string A string value.
|
||||
* @param string
|
||||
* A string value.
|
||||
* @return this
|
||||
* @throws JSONException If the value is out of sequence.
|
||||
* @throws JSONException
|
||||
* If the value is out of sequence.
|
||||
*/
|
||||
private JSONWriter append(String string) throws JSONException {
|
||||
if (string == null) {
|
||||
if(string == null) {
|
||||
throw new JSONException("Null pointer");
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
if(this.mode == 'o' || this.mode == 'a') {
|
||||
try {
|
||||
if (this.comma && this.mode == 'a') {
|
||||
if(this.comma && this.mode == 'a') {
|
||||
this.writer.write(',');
|
||||
}
|
||||
this.writer.write(string);
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
if (this.mode == 'o') {
|
||||
if(this.mode == 'o') {
|
||||
this.mode = 'k';
|
||||
}
|
||||
this.comma = true;
|
||||
@@ -129,12 +131,13 @@ public class JSONWriter {
|
||||
* <code>endArray</code> method must be called to mark the array's end.
|
||||
*
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is started in
|
||||
* the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
* @throws JSONException
|
||||
* If the nesting is too deep, or if the object is started in
|
||||
* the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter array() throws JSONException {
|
||||
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
|
||||
if(this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
|
||||
this.push(null);
|
||||
this.append("[");
|
||||
this.comma = false;
|
||||
@@ -146,19 +149,22 @@ public class JSONWriter {
|
||||
/**
|
||||
* End something.
|
||||
*
|
||||
* @param mode Mode
|
||||
* @param c Closing character
|
||||
* @param mode
|
||||
* Mode
|
||||
* @param c
|
||||
* Closing character
|
||||
* @return this
|
||||
* @throws JSONException If unbalanced.
|
||||
* @throws JSONException
|
||||
* If unbalanced.
|
||||
*/
|
||||
private JSONWriter end(char mode, char c) throws JSONException {
|
||||
if (this.mode != mode) {
|
||||
if(this.mode != mode) {
|
||||
throw new JSONException(mode == 'a' ? "Misplaced endArray." : "Misplaced endObject.");
|
||||
}
|
||||
this.pop(mode);
|
||||
try {
|
||||
this.writer.write(c);
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
@@ -171,7 +177,8 @@ public class JSONWriter {
|
||||
* <code>array</code>.
|
||||
*
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
* @throws JSONException
|
||||
* If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endArray() throws JSONException {
|
||||
return this.end('a', ']');
|
||||
@@ -182,7 +189,8 @@ public class JSONWriter {
|
||||
* <code>object</code>.
|
||||
*
|
||||
* @return this
|
||||
* @throws JSONException If incorrectly nested.
|
||||
* @throws JSONException
|
||||
* If incorrectly nested.
|
||||
*/
|
||||
public JSONWriter endObject() throws JSONException {
|
||||
return this.end('k', '}');
|
||||
@@ -192,19 +200,21 @@ 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 string 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.
|
||||
* @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 string) throws JSONException {
|
||||
if (string == null) {
|
||||
if(string == null) {
|
||||
throw new JSONException("Null key.");
|
||||
}
|
||||
if (this.mode == 'k') {
|
||||
if(this.mode == 'k') {
|
||||
try {
|
||||
this.stack[this.top - 1].putOnce(string, Boolean.TRUE);
|
||||
if (this.comma) {
|
||||
if(this.comma) {
|
||||
this.writer.write(',');
|
||||
}
|
||||
this.writer.write(JSONObject.quote(string));
|
||||
@@ -212,7 +222,7 @@ public class JSONWriter {
|
||||
this.comma = false;
|
||||
this.mode = 'o';
|
||||
return this;
|
||||
} catch (IOException e) {
|
||||
} catch(IOException e) {
|
||||
Iris.reportError(e);
|
||||
throw new JSONException(e);
|
||||
}
|
||||
@@ -226,15 +236,16 @@ public class JSONWriter {
|
||||
* <code>endObject</code> method must be called to mark the object's end.
|
||||
*
|
||||
* @return this
|
||||
* @throws JSONException If the nesting is too deep, or if the object is started in
|
||||
* the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
* @throws JSONException
|
||||
* If the nesting is too deep, or if the object is started in
|
||||
* the wrong place (for example as a key or after the end of the
|
||||
* outermost array or object).
|
||||
*/
|
||||
public JSONWriter object() throws JSONException {
|
||||
if (this.mode == 'i') {
|
||||
if(this.mode == 'i') {
|
||||
this.mode = 'o';
|
||||
}
|
||||
if (this.mode == 'o' || this.mode == 'a') {
|
||||
if(this.mode == 'o' || this.mode == 'a') {
|
||||
this.append("{");
|
||||
this.push(new JSONObject());
|
||||
this.comma = false;
|
||||
@@ -247,15 +258,17 @@ public class JSONWriter {
|
||||
/**
|
||||
* Pop an array or object scope.
|
||||
*
|
||||
* @param c The scope to close.
|
||||
* @throws JSONException If nesting is wrong.
|
||||
* @param c
|
||||
* The scope to close.
|
||||
* @throws JSONException
|
||||
* If nesting is wrong.
|
||||
*/
|
||||
private void pop(char c) throws JSONException {
|
||||
if (this.top <= 0) {
|
||||
if(this.top <= 0) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
char m = this.stack[this.top - 1] == null ? 'a' : 'k';
|
||||
if (m != c) {
|
||||
if(m != c) {
|
||||
throw new JSONException("Nesting error.");
|
||||
}
|
||||
this.top -= 1;
|
||||
@@ -265,11 +278,13 @@ public class JSONWriter {
|
||||
/**
|
||||
* Push an array or object scope.
|
||||
*
|
||||
* @param jo The scope to open.
|
||||
* @throws JSONException If nesting is too deep.
|
||||
* @param jo
|
||||
* The scope to open.
|
||||
* @throws JSONException
|
||||
* If nesting is too deep.
|
||||
*/
|
||||
private void push(JSONObject jo) throws JSONException {
|
||||
if (this.top >= maxdepth) {
|
||||
if(this.top >= maxdepth) {
|
||||
throw new JSONException("Nesting too deep.");
|
||||
}
|
||||
this.stack[this.top] = jo;
|
||||
@@ -281,7 +296,8 @@ public class JSONWriter {
|
||||
* Append either the value <code>true</code> or the value <code>false</code>
|
||||
* .
|
||||
*
|
||||
* @param b A boolean.
|
||||
* @param b
|
||||
* A boolean.
|
||||
* @return this
|
||||
*/
|
||||
public JSONWriter value(boolean b) throws JSONException {
|
||||
@@ -291,9 +307,11 @@ public class JSONWriter {
|
||||
/**
|
||||
* Append a double value.
|
||||
*
|
||||
* @param d A double.
|
||||
* @param d
|
||||
* A double.
|
||||
* @return this
|
||||
* @throws JSONException If the number is not finite.
|
||||
* @throws JSONException
|
||||
* If the number is not finite.
|
||||
*/
|
||||
public JSONWriter value(double d) throws JSONException {
|
||||
return this.value(Double.valueOf(d));
|
||||
@@ -302,7 +320,8 @@ public class JSONWriter {
|
||||
/**
|
||||
* Append a long value.
|
||||
*
|
||||
* @param l A long.
|
||||
* @param l
|
||||
* A long.
|
||||
* @return this
|
||||
*/
|
||||
public JSONWriter value(long l) throws JSONException {
|
||||
@@ -312,11 +331,13 @@ public class JSONWriter {
|
||||
/**
|
||||
* Append an object value.
|
||||
*
|
||||
* @param object The object to append. It can be null, or a Boolean, Number,
|
||||
* String, JSONObject, or JSONArray, or an object that implements
|
||||
* JSONString.
|
||||
* @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.
|
||||
* @throws JSONException
|
||||
* If the value is out of sequence.
|
||||
*/
|
||||
public JSONWriter value(Object object) throws JSONException {
|
||||
return this.append(JSONObject.valueToString(object));
|
||||
|
||||
@@ -87,14 +87,15 @@ public class XML {
|
||||
* " <small>(double quote)</small> is replaced by &quot;
|
||||
* </pre>
|
||||
*
|
||||
* @param string The string to be escaped.
|
||||
* @param string
|
||||
* The string to be escaped.
|
||||
* @return The escaped string.
|
||||
*/
|
||||
public static String escape(String string) {
|
||||
StringBuilder sb = new StringBuilder(string.length());
|
||||
for (int i = 0, length = string.length(); i < length; i++) {
|
||||
for(int i = 0, length = string.length(); i < length; i++) {
|
||||
char c = string.charAt(i);
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case '&' -> sb.append("&");
|
||||
case '<' -> sb.append("<");
|
||||
case '>' -> sb.append(">");
|
||||
@@ -110,15 +111,16 @@ public class XML {
|
||||
* Throw an exception if the string contains whitespace. Whitespace is not
|
||||
* allowed in tagNames and attributes.
|
||||
*
|
||||
* @param string A string.
|
||||
* @param string
|
||||
* A string.
|
||||
*/
|
||||
public static void noSpace(String string) throws JSONException {
|
||||
int i, length = string.length();
|
||||
if (length == 0) {
|
||||
if(length == 0) {
|
||||
throw new JSONException("Empty string.");
|
||||
}
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (Character.isWhitespace(string.charAt(i))) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
if(Character.isWhitespace(string.charAt(i))) {
|
||||
throw new JSONException("'" + string + "' contains a space character.");
|
||||
}
|
||||
}
|
||||
@@ -127,9 +129,12 @@ public class XML {
|
||||
/**
|
||||
* Scan the content following the named tag, attaching it to the context.
|
||||
*
|
||||
* @param x The XMLTokener containing the source string.
|
||||
* @param context The JSONObject that will include the new material.
|
||||
* @param name The tag name.
|
||||
* @param x
|
||||
* The XMLTokener containing the source string.
|
||||
* @param context
|
||||
* The JSONObject that will include the new material.
|
||||
* @param name
|
||||
* The tag name.
|
||||
* @return true if the close tag is processed.
|
||||
*/
|
||||
private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException {
|
||||
@@ -154,20 +159,20 @@ public class XML {
|
||||
|
||||
// <!
|
||||
|
||||
if (token == BANG) {
|
||||
if(token == BANG) {
|
||||
c = x.next();
|
||||
if (c == '-') {
|
||||
if (x.next() == '-') {
|
||||
if(c == '-') {
|
||||
if(x.next() == '-') {
|
||||
x.skipPast("-->");
|
||||
return false;
|
||||
}
|
||||
x.back();
|
||||
} else if (c == '[') {
|
||||
} else if(c == '[') {
|
||||
token = x.nextToken();
|
||||
if ("CDATA".equals(token)) {
|
||||
if (x.next() == '[') {
|
||||
if("CDATA".equals(token)) {
|
||||
if(x.next() == '[') {
|
||||
string = x.nextCDATA();
|
||||
if (string.length() > 0) {
|
||||
if(string.length() > 0) {
|
||||
context.accumulate("content", string);
|
||||
}
|
||||
return false;
|
||||
@@ -178,38 +183,38 @@ public class XML {
|
||||
i = 1;
|
||||
do {
|
||||
token = x.nextMeta();
|
||||
if (token == null) {
|
||||
if(token == null) {
|
||||
throw x.syntaxError("Missing '>' after '<!'.");
|
||||
} else if (token == LT) {
|
||||
} else if(token == LT) {
|
||||
i += 1;
|
||||
} else if (token == GT) {
|
||||
} else if(token == GT) {
|
||||
i -= 1;
|
||||
}
|
||||
} while (i > 0);
|
||||
} while(i > 0);
|
||||
return false;
|
||||
} else if (token == QUEST) {
|
||||
} else if(token == QUEST) {
|
||||
|
||||
// <?
|
||||
|
||||
x.skipPast("?>");
|
||||
return false;
|
||||
} else if (token == SLASH) {
|
||||
} else if(token == SLASH) {
|
||||
|
||||
// Close tag </
|
||||
|
||||
token = x.nextToken();
|
||||
if (name == null) {
|
||||
if(name == null) {
|
||||
throw x.syntaxError("Mismatched close tag " + token);
|
||||
}
|
||||
if (!token.equals(name)) {
|
||||
if(!token.equals(name)) {
|
||||
throw x.syntaxError("Mismatched " + name + " and " + token);
|
||||
}
|
||||
if (x.nextToken() != GT) {
|
||||
if(x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped close tag");
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if (token instanceof Character) {
|
||||
} else if(token instanceof Character) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
|
||||
// Open tag <
|
||||
@@ -218,19 +223,19 @@ public class XML {
|
||||
tagName = (String) token;
|
||||
token = null;
|
||||
jsonobject = new JSONObject();
|
||||
for (; ; ) {
|
||||
if (token == null) {
|
||||
for(; ; ) {
|
||||
if(token == null) {
|
||||
token = x.nextToken();
|
||||
}
|
||||
|
||||
// attribute = value
|
||||
|
||||
if (token instanceof String) {
|
||||
if(token instanceof String) {
|
||||
string = (String) token;
|
||||
token = x.nextToken();
|
||||
if (token == EQ) {
|
||||
if(token == EQ) {
|
||||
token = x.nextToken();
|
||||
if (!(token instanceof String)) {
|
||||
if(!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
jsonobject.accumulate(string, XML.stringToValue((String) token));
|
||||
@@ -241,11 +246,11 @@ public class XML {
|
||||
|
||||
// Empty tag <.../>
|
||||
|
||||
} else if (token == SLASH) {
|
||||
if (x.nextToken() != GT) {
|
||||
} else if(token == SLASH) {
|
||||
if(x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (jsonobject.length() > 0) {
|
||||
if(jsonobject.length() > 0) {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
} else {
|
||||
context.accumulate(tagName, "");
|
||||
@@ -254,27 +259,27 @@ public class XML {
|
||||
|
||||
// Content, between <...> and </...>
|
||||
|
||||
} else if (token == GT) {
|
||||
for (; ; ) {
|
||||
} else if(token == GT) {
|
||||
for(; ; ) {
|
||||
token = x.nextContent();
|
||||
if (token == null) {
|
||||
if (tagName != null) {
|
||||
if(token == null) {
|
||||
if(tagName != null) {
|
||||
throw x.syntaxError("Unclosed tag " + tagName);
|
||||
}
|
||||
return false;
|
||||
} else if (token instanceof String) {
|
||||
} else if(token instanceof String) {
|
||||
string = (String) token;
|
||||
if (string.length() > 0) {
|
||||
if(string.length() > 0) {
|
||||
jsonobject.accumulate("content", XML.stringToValue(string));
|
||||
}
|
||||
|
||||
// Nested element
|
||||
|
||||
} else if (token == LT) {
|
||||
if (parse(x, jsonobject, tagName)) {
|
||||
if (jsonobject.length() == 0) {
|
||||
} 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) {
|
||||
} else if(jsonobject.length() == 1 && jsonobject.opt("content") != null) {
|
||||
context.accumulate(tagName, jsonobject.opt("content"));
|
||||
} else {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
@@ -297,17 +302,18 @@ public class XML {
|
||||
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
|
||||
* points.
|
||||
*
|
||||
* @param string A String.
|
||||
* @param string
|
||||
* A String.
|
||||
* @return A simple JSON value.
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
if ("true".equalsIgnoreCase(string)) {
|
||||
if("true".equalsIgnoreCase(string)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if ("false".equalsIgnoreCase(string)) {
|
||||
if("false".equalsIgnoreCase(string)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if ("null".equalsIgnoreCase(string)) {
|
||||
if("null".equalsIgnoreCase(string)) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
@@ -317,20 +323,20 @@ public class XML {
|
||||
|
||||
try {
|
||||
char initial = string.charAt(0);
|
||||
if (initial == '-' || (initial >= '0' && initial <= '9')) {
|
||||
if(initial == '-' || (initial >= '0' && initial <= '9')) {
|
||||
Long value = Long.valueOf(string);
|
||||
if (value.toString().equals(string)) {
|
||||
if(value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
} catch(Exception ignore) {
|
||||
Iris.reportError(ignore);
|
||||
try {
|
||||
Double value = Double.valueOf(string);
|
||||
if (value.toString().equals(string)) {
|
||||
if(value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch(Exception e) {
|
||||
Iris.reportError(e);
|
||||
}
|
||||
}
|
||||
@@ -348,13 +354,14 @@ public class XML {
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* are ignored.
|
||||
*
|
||||
* @param string The source string.
|
||||
* @param string
|
||||
* The source string.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
*/
|
||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||
JSONObject jo = new JSONObject();
|
||||
XMLTokener x = new XMLTokener(string);
|
||||
while (x.more() && x.skipPast("<")) {
|
||||
while(x.more() && x.skipPast("<")) {
|
||||
parse(x, jo, null);
|
||||
}
|
||||
return jo;
|
||||
@@ -363,7 +370,8 @@ public class XML {
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||
*
|
||||
* @param object A JSONObject.
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @return A string.
|
||||
*/
|
||||
public static String toString(Object object) throws JSONException {
|
||||
@@ -373,8 +381,10 @@ public class XML {
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||
*
|
||||
* @param object A JSONObject.
|
||||
* @param tagName The optional name of the enclosing tag.
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param tagName
|
||||
* The optional name of the enclosing tag.
|
||||
* @return A string.
|
||||
*/
|
||||
public static String toString(Object object, String tagName) throws JSONException {
|
||||
@@ -387,11 +397,11 @@ public class XML {
|
||||
int length;
|
||||
String string;
|
||||
Object value;
|
||||
if (object instanceof JSONObject) {
|
||||
if(object instanceof JSONObject) {
|
||||
|
||||
// Emit <tagName>
|
||||
|
||||
if (tagName != null) {
|
||||
if(tagName != null) {
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
@@ -401,22 +411,22 @@ public class XML {
|
||||
|
||||
jo = (JSONObject) object;
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
while(keys.hasNext()) {
|
||||
key = keys.next();
|
||||
value = jo.opt(key);
|
||||
if (value == null) {
|
||||
if(value == null) {
|
||||
value = "";
|
||||
}
|
||||
string = value instanceof String ? (String) value : null;
|
||||
|
||||
// Emit content in body
|
||||
|
||||
if ("content".equals(key)) {
|
||||
if (value instanceof JSONArray) {
|
||||
if("content".equals(key)) {
|
||||
if(value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (i > 0) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
if(i > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
sb.append(escape(ja.get(i).toString()));
|
||||
@@ -427,12 +437,12 @@ public class XML {
|
||||
|
||||
// Emit an array of similar keys
|
||||
|
||||
} else if (value instanceof JSONArray) {
|
||||
} else if(value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
value = ja.get(i);
|
||||
if (value instanceof JSONArray) {
|
||||
if(value instanceof JSONArray) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
@@ -444,7 +454,7 @@ public class XML {
|
||||
sb.append(toString(value, key));
|
||||
}
|
||||
}
|
||||
} else if ("".equals(value)) {
|
||||
} else if("".equals(value)) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
@@ -455,7 +465,7 @@ public class XML {
|
||||
sb.append(toString(value, key));
|
||||
}
|
||||
}
|
||||
if (tagName != null) {
|
||||
if(tagName != null) {
|
||||
|
||||
// Emit the </tagname> close tag
|
||||
|
||||
@@ -470,13 +480,13 @@ public class XML {
|
||||
// where XML is lacking, synthesize an <array> element.
|
||||
|
||||
} else {
|
||||
if (object.getClass().isArray()) {
|
||||
if(object.getClass().isArray()) {
|
||||
object = new JSONArray(object);
|
||||
}
|
||||
if (object instanceof JSONArray) {
|
||||
if(object instanceof JSONArray) {
|
||||
ja = (JSONArray) object;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
@@ -46,7 +46,8 @@ public class XMLTokener extends JSONTokener {
|
||||
/**
|
||||
* Construct an XMLTokener from a string.
|
||||
*
|
||||
* @param s A source string.
|
||||
* @param s
|
||||
* A source string.
|
||||
*/
|
||||
public XMLTokener(String s) {
|
||||
super(s);
|
||||
@@ -56,20 +57,21 @@ public class XMLTokener extends JSONTokener {
|
||||
* Get the text in the CDATA block.
|
||||
*
|
||||
* @return The string up to the <code>]]></code>.
|
||||
* @throws JSONException If the <code>]]></code> is not found.
|
||||
* @throws JSONException
|
||||
* If the <code>]]></code> is not found.
|
||||
*/
|
||||
public String nextCDATA() throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = next();
|
||||
if (end()) {
|
||||
if(end()) {
|
||||
throw syntaxError("Unclosed CDATA");
|
||||
}
|
||||
sb.append(c);
|
||||
i = sb.length() - 3;
|
||||
if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
|
||||
if(i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
|
||||
sb.setLength(i);
|
||||
return sb.toString();
|
||||
}
|
||||
@@ -89,20 +91,20 @@ public class XMLTokener extends JSONTokener {
|
||||
StringBuilder sb;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
if (c == 0) {
|
||||
} while(Character.isWhitespace(c));
|
||||
if(c == 0) {
|
||||
return null;
|
||||
}
|
||||
if (c == '<') {
|
||||
if(c == '<') {
|
||||
return XML.LT;
|
||||
}
|
||||
sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
if (c == '<' || c == 0) {
|
||||
for(; ; ) {
|
||||
if(c == '<' || c == 0) {
|
||||
back();
|
||||
return sb.toString().trim();
|
||||
}
|
||||
if (c == '&') {
|
||||
if(c == '&') {
|
||||
sb.append(nextEntity(c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
@@ -115,17 +117,19 @@ public class XMLTokener extends JSONTokener {
|
||||
* Return the next entity. These entities are translated to Characters:
|
||||
* <code>& ' > < "</code>.
|
||||
*
|
||||
* @param ampersand 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.
|
||||
* @throws JSONException
|
||||
* If missing ';' in XML entity.
|
||||
*/
|
||||
public Object nextEntity(char ampersand) throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
char c = next();
|
||||
if (Character.isLetterOrDigit(c) || c == '#') {
|
||||
if(Character.isLetterOrDigit(c) || c == '#') {
|
||||
sb.append(Character.toLowerCase(c));
|
||||
} else if (c == ';') {
|
||||
} else if(c == ';') {
|
||||
break;
|
||||
} else {
|
||||
throw syntaxError("Missing ';' in XML entity: &" + sb);
|
||||
@@ -143,16 +147,17 @@ public class XMLTokener extends JSONTokener {
|
||||
* @return Syntax characters (<code>< > / = ! ?</code>) are returned as
|
||||
* Character, and strings and names are returned as Boolean. We
|
||||
* don't care what the values actually are.
|
||||
* @throws JSONException If a string is not properly closed or if the XML is badly
|
||||
* structured.
|
||||
* @throws JSONException
|
||||
* If a string is not properly closed or if the XML is badly
|
||||
* structured.
|
||||
*/
|
||||
public Object nextMeta() throws JSONException {
|
||||
char c;
|
||||
char q;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
switch (c) {
|
||||
} while(Character.isWhitespace(c));
|
||||
switch(c) {
|
||||
case 0:
|
||||
throw syntaxError("Misshaped meta tag");
|
||||
case '<':
|
||||
@@ -170,22 +175,22 @@ public class XMLTokener extends JSONTokener {
|
||||
case '"':
|
||||
case '\'':
|
||||
q = c;
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
if(c == 0) {
|
||||
throw syntaxError("Unterminated string");
|
||||
}
|
||||
if (c == q) {
|
||||
if(c == q) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
default:
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = next();
|
||||
if (Character.isWhitespace(c)) {
|
||||
if(Character.isWhitespace(c)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case 0, '<', '>', '/', '=', '!', '?', '"', '\'' -> {
|
||||
back();
|
||||
return Boolean.TRUE;
|
||||
@@ -201,7 +206,8 @@ public class XMLTokener extends JSONTokener {
|
||||
* string wrapped in single quotes or double quotes, or it may be a name.
|
||||
*
|
||||
* @return a String or a Character.
|
||||
* @throws JSONException If the XML is not well formed.
|
||||
* @throws JSONException
|
||||
* If the XML is not well formed.
|
||||
*/
|
||||
public Object nextToken() throws JSONException {
|
||||
char c;
|
||||
@@ -209,8 +215,8 @@ public class XMLTokener extends JSONTokener {
|
||||
StringBuilder sb;
|
||||
do {
|
||||
c = next();
|
||||
} while (Character.isWhitespace(c));
|
||||
switch (c) {
|
||||
} while(Character.isWhitespace(c));
|
||||
switch(c) {
|
||||
case 0:
|
||||
throw syntaxError("Misshaped element");
|
||||
case '<':
|
||||
@@ -232,15 +238,15 @@ public class XMLTokener extends JSONTokener {
|
||||
case '\'':
|
||||
q = c;
|
||||
sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
if(c == 0) {
|
||||
throw syntaxError("Unterminated string");
|
||||
}
|
||||
if (c == q) {
|
||||
if(c == q) {
|
||||
return sb.toString();
|
||||
}
|
||||
if (c == '&') {
|
||||
if(c == '&') {
|
||||
sb.append(nextEntity(c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
@@ -251,13 +257,13 @@ public class XMLTokener extends JSONTokener {
|
||||
// Name
|
||||
|
||||
sb = new StringBuilder();
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
sb.append(c);
|
||||
c = next();
|
||||
if (Character.isWhitespace(c)) {
|
||||
if(Character.isWhitespace(c)) {
|
||||
return sb.toString();
|
||||
}
|
||||
switch (c) {
|
||||
switch(c) {
|
||||
case 0:
|
||||
return sb.toString();
|
||||
case '>':
|
||||
@@ -282,7 +288,8 @@ public class XMLTokener extends JSONTokener {
|
||||
* Skip characters until past the requested string. If it is not found, we
|
||||
* are left at the end of the source with a result of false.
|
||||
*
|
||||
* @param to A string to skip past.
|
||||
* @param to
|
||||
* A string to skip past.
|
||||
*/
|
||||
public boolean skipPast(String to) throws JSONException {
|
||||
boolean b;
|
||||
@@ -298,9 +305,9 @@ public class XMLTokener extends JSONTokener {
|
||||
* string. If we reach an early end, bail.
|
||||
*/
|
||||
|
||||
for (i = 0; i < length; i += 1) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
if(c == 0) {
|
||||
return false;
|
||||
}
|
||||
circle[i] = c;
|
||||
@@ -308,26 +315,26 @@ public class XMLTokener extends JSONTokener {
|
||||
|
||||
/* We will loop, possibly for all of the remaining characters. */
|
||||
|
||||
for (; ; ) {
|
||||
for(; ; ) {
|
||||
j = offset;
|
||||
b = true;
|
||||
|
||||
/* Compare the circle buffer with the to string. */
|
||||
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (circle[j] != to.charAt(i)) {
|
||||
for(i = 0; i < length; i += 1) {
|
||||
if(circle[j] != to.charAt(i)) {
|
||||
b = false;
|
||||
break;
|
||||
}
|
||||
j += 1;
|
||||
if (j >= length) {
|
||||
if(j >= length) {
|
||||
j -= length;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we exit the loop with b intact, then victory is ours. */
|
||||
|
||||
if (b) {
|
||||
if(b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -336,7 +343,7 @@ public class XMLTokener extends JSONTokener {
|
||||
*/
|
||||
|
||||
c = next();
|
||||
if (c == 0) {
|
||||
if(c == 0) {
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
@@ -345,7 +352,7 @@ public class XMLTokener extends JSONTokener {
|
||||
*/
|
||||
circle[offset] = c;
|
||||
offset += 1;
|
||||
if (offset >= length) {
|
||||
if(offset >= length) {
|
||||
offset -= length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user