mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-22 21:26:45 +01:00
Use relocated org.json instead of including the source files
This commit is contained in:
parent
cb05d34d20
commit
dada7dbb86
@ -1,815 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with
|
|
||||||
* commas separating the values. The internal form is an object having {@code get} and {@code opt} methods for
|
|
||||||
* accessing the values by index, and {@code put} methods for adding or replacing values. The values can be any of
|
|
||||||
* these types: {@code Boolean}, {@code JSONArray}, {@code JSONObject}, {@code Number},
|
|
||||||
* {@code String}, or the {@code JSONObject.NULL object}.
|
|
||||||
*
|
|
||||||
* <p>The constructor can convert a JSON text into a Java object. The {@code toString} method converts to JSON text.
|
|
||||||
*
|
|
||||||
* <p>A {@code get} method returns a value if one can be found, and throws an exception if one cannot be found. An
|
|
||||||
* {@code opt} method returns a default value instead of throwing an exception, and so is useful for obtaining
|
|
||||||
* optional values.
|
|
||||||
*
|
|
||||||
* <p>The generic {@code get()} and {@code opt()} methods return an object which you can cast or query for type.
|
|
||||||
* There are also typed {@code get} and {@code opt} methods that do type checking and type coercion for you.
|
|
||||||
*
|
|
||||||
* <p>The texts produced by the {@code toString} methods strictly conform to JSON syntax rules. The constructors are
|
|
||||||
* more forgiving in the texts they will accept: <ul> <li>An extra {@code ,} <small>(comma)</small> may appear
|
|
||||||
* just before the closing bracket.</li> <li>The {@code null} value will be inserted when there is {@code ,}
|
|
||||||
* <small>(comma)</small> elision.</li> <li>Strings may be quoted with {@code '} <small>(single
|
|
||||||
* quote)</small>.</li> <li>Strings do not need to be quoted at all if they do not begin with a quote or single quote,
|
|
||||||
* and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: {@code { }
|
|
||||||
* [ ] / \ : , #} and if they do not look like numbers and if they are not the reserved words {@code true},
|
|
||||||
* {@code false}, or {@code null}.</li> </ul>
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
public class JSONArray {
|
|
||||||
/**
|
|
||||||
* The arrayList where the JSONArray's properties are kept.
|
|
||||||
*/
|
|
||||||
private final ArrayList<Object> myArrayList;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct an empty JSONArray.
|
|
||||||
*/
|
|
||||||
public JSONArray() {
|
|
||||||
this.myArrayList = new ArrayList<Object>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a JSONTokener.
|
|
||||||
*
|
|
||||||
* @param x A JSONTokener
|
|
||||||
* @throws JSONException If there is a syntax error.
|
|
||||||
*/
|
|
||||||
public JSONArray(JSONTokener x) throws JSONException {
|
|
||||||
this();
|
|
||||||
if (x.nextClean() != '[') {
|
|
||||||
throw x.syntaxError("A JSONArray text must start with '['");
|
|
||||||
}
|
|
||||||
if (x.nextClean() != ']') {
|
|
||||||
x.back();
|
|
||||||
for (; ; ) {
|
|
||||||
if (x.nextClean() == ',') {
|
|
||||||
x.back();
|
|
||||||
this.myArrayList.add(JSONObject.NULL);
|
|
||||||
} else {
|
|
||||||
x.back();
|
|
||||||
this.myArrayList.add(x.nextValue());
|
|
||||||
}
|
|
||||||
switch (x.nextClean()) {
|
|
||||||
case ',':
|
|
||||||
if (x.nextClean() == ']') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
x.back();
|
|
||||||
break;
|
|
||||||
case ']':
|
|
||||||
return;
|
|
||||||
default:
|
|
||||||
throw x.syntaxError("Expected a ',' or ']'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a source JSON text.
|
|
||||||
*
|
|
||||||
* @param source A string that begins with {@code [} <small>(left bracket)</small> and ends with
|
|
||||||
* {@code ]} <small>(right bracket)</small>.
|
|
||||||
* @throws JSONException If there is a syntax error.
|
|
||||||
*/
|
|
||||||
public JSONArray(String source) throws JSONException {
|
|
||||||
this(new JSONTokener(source));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from a Collection.
|
|
||||||
*
|
|
||||||
* @param collection A Collection.
|
|
||||||
*/
|
|
||||||
public JSONArray(Collection<Object> collection) {
|
|
||||||
this.myArrayList = new ArrayList<Object>();
|
|
||||||
if (collection != null) {
|
|
||||||
for (Object aCollection : collection) {
|
|
||||||
this.myArrayList.add(JSONObject.wrap(aCollection));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONArray from an array
|
|
||||||
*
|
|
||||||
* @throws JSONException If not an array.
|
|
||||||
*/
|
|
||||||
public JSONArray(Object array) throws JSONException {
|
|
||||||
this();
|
|
||||||
if (array.getClass().isArray()) {
|
|
||||||
int length = Array.getLength(array);
|
|
||||||
for (int i = 0; i < length; i += 1) {
|
|
||||||
this.put(JSONObject.wrap(Array.get(array, i)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new JSONException(
|
|
||||||
"JSONArray initial value should be a string or collection or array.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the object value associated with an index.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public Object get(int index) throws JSONException {
|
|
||||||
Object object = opt(index);
|
|
||||||
if (object == null) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
|
||||||
}
|
|
||||||
return 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.
|
|
||||||
* @return The truth.
|
|
||||||
* @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 = get(index);
|
|
||||||
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"))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a boolean.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the double value associated with an index.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public double getDouble(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number ?
|
|
||||||
((Number) object).doubleValue() :
|
|
||||||
Double.parseDouble((String) object);
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the int value associated with an index.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public int getInt(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number ?
|
|
||||||
((Number) object).intValue() :
|
|
||||||
Integer.parseInt((String) object);
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the JSONArray associated with an index.
|
|
||||||
*
|
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
public JSONArray getJSONArray(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
if (object instanceof JSONArray) {
|
|
||||||
return (JSONArray) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a JSONArray.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the JSONObject associated with an index.
|
|
||||||
*
|
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
public JSONObject getJSONObject(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
if (object instanceof JSONObject) {
|
|
||||||
return (JSONObject) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a JSONObject.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the long value associated with an index.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public long getLong(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
try {
|
|
||||||
return object instanceof Number ?
|
|
||||||
((Number) object).longValue() :
|
|
||||||
Long.parseLong((String) object);
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] is not a number.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the string associated with an index.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public String getString(int index) throws JSONException {
|
|
||||||
Object object = get(index);
|
|
||||||
if (object instanceof String) {
|
|
||||||
return (String) object;
|
|
||||||
}
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not a string.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the value is null.
|
|
||||||
*
|
|
||||||
* @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) {
|
|
||||||
return JSONObject.NULL.equals(opt(index));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a string from the contents of this JSONArray. The {@code separator} 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.
|
|
||||||
* @return a string.
|
|
||||||
* @throws JSONException If the array contains an invalid number.
|
|
||||||
*/
|
|
||||||
public String join(String separator) throws JSONException {
|
|
||||||
int len = length();
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (int i = 0; i < len; i += 1) {
|
|
||||||
if (i > 0) {
|
|
||||||
sb.append(separator);
|
|
||||||
}
|
|
||||||
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the number of elements in the JSONArray, included nulls.
|
|
||||||
*
|
|
||||||
* @return The length (or size).
|
|
||||||
*/
|
|
||||||
public int length() {
|
|
||||||
return this.myArrayList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional object value associated with an index.
|
|
||||||
*
|
|
||||||
* @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) {
|
|
||||||
return ((index < 0) || (index >= length())) ? null : this.myArrayList.get(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional boolean value associated with an index. It returns false 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.
|
|
||||||
* @return The truth.
|
|
||||||
*/
|
|
||||||
public boolean optBoolean(int index) {
|
|
||||||
return this.optBoolean(index, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional boolean value associated with an index. It returns the 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.
|
|
||||||
* @return The truth.
|
|
||||||
*/
|
|
||||||
public boolean optBoolean(int index, boolean defaultValue) {
|
|
||||||
try {
|
|
||||||
return getBoolean(index);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional double value associated with an index. NaN 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.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public double optDouble(int index) {
|
|
||||||
return this.optDouble(index, Double.NaN);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional double value associated with an index. The 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 subscript
|
|
||||||
* @param defaultValue The default value.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public double optDouble(int index, double defaultValue) {
|
|
||||||
try {
|
|
||||||
return getDouble(index);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional int value associated with an index. Zero 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.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public int optInt(int index) {
|
|
||||||
return this.optInt(index, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional int value associated with an index. The 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.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public int optInt(int index, int defaultValue) {
|
|
||||||
try {
|
|
||||||
return getInt(index);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional JSONArray associated with an index.
|
|
||||||
*
|
|
||||||
* @param index subscript
|
|
||||||
* @return A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.
|
|
||||||
*/
|
|
||||||
public JSONArray optJSONArray(int index) {
|
|
||||||
Object o = opt(index);
|
|
||||||
return o instanceof JSONArray ? (JSONArray) o : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional JSONObject associated with an index. Null is returned if 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.
|
|
||||||
* @return A JSONObject value.
|
|
||||||
*/
|
|
||||||
public JSONObject optJSONObject(int index) {
|
|
||||||
Object o = opt(index);
|
|
||||||
return o instanceof JSONObject ? (JSONObject) o : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional long value associated with an index. Zero 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.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public long optLong(int index) {
|
|
||||||
return this.optLong(index, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional long value associated with an index. The 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.
|
|
||||||
* @return The value.
|
|
||||||
*/
|
|
||||||
public long optLong(int index, long defaultValue) {
|
|
||||||
try {
|
|
||||||
return getLong(index);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the optional string value associated with an index. It returns an
|
|
||||||
* empty string if there is no value at that index. If the value is not a
|
|
||||||
* string and is not null, then it is converted to a string.
|
|
||||||
*
|
|
||||||
* @param index The index must be between 0 and length() - 1.
|
|
||||||
* @return A String value.
|
|
||||||
*/
|
|
||||||
public String optString(int index) {
|
|
||||||
return this.optString(index, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return A String value.
|
|
||||||
*/
|
|
||||||
public String optString(int index, String defaultValue) {
|
|
||||||
Object object = opt(index);
|
|
||||||
return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a boolean value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A boolean value.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(boolean value) {
|
|
||||||
this.put(value ? Boolean.TRUE : Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONArray which
|
|
||||||
* is produced from a Collection.
|
|
||||||
*
|
|
||||||
* @param value A Collection value.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Collection<Object> value) {
|
|
||||||
this.put(new JSONArray(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append a double value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A double value.
|
|
||||||
* @return this.
|
|
||||||
* @throws JSONException if the value is not finite.
|
|
||||||
*/
|
|
||||||
public JSONArray put(double value) throws JSONException {
|
|
||||||
Double d = value;
|
|
||||||
JSONObject.testValidity(d);
|
|
||||||
this.put(d);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append an int value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value An int value.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int value) {
|
|
||||||
this.put(Integer.valueOf(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Append an long value. This increases the array's length by one.
|
|
||||||
*
|
|
||||||
* @param value A long value.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(long value) {
|
|
||||||
this.put(Long.valueOf(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put a value in the JSONArray, where the value will be a JSONObject which
|
|
||||||
* is produced from a Map.
|
|
||||||
*
|
|
||||||
* @param value A Map value.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Map<String, Object> value) {
|
|
||||||
this.put(new JSONObject(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return this.
|
|
||||||
*/
|
|
||||||
public JSONArray put(Object value) {
|
|
||||||
this.myArrayList.add(value);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a boolean value in the JSONArray. If the index is greater 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.
|
|
||||||
* @return this.
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, boolean value) throws JSONException {
|
|
||||||
this.put(index, value ? Boolean.TRUE : Boolean.FALSE);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return this.
|
|
||||||
* @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));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a double value. If the index is greater 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 double value.
|
|
||||||
* @return this.
|
|
||||||
* @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, new Double(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace an int value. If the index is greater than the length of the JSONArray, then null elements will be
|
|
||||||
* added as necessary to pad it out.
|
|
||||||
*
|
|
||||||
* @param index The subscript.
|
|
||||||
* @param value An int value.
|
|
||||||
* @return this.
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, int value) throws JSONException {
|
|
||||||
this.put(index, Integer.valueOf(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace a long value. If the index is greater 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 long value.
|
|
||||||
* @return this.
|
|
||||||
* @throws JSONException If the index is negative.
|
|
||||||
*/
|
|
||||||
public JSONArray put(int index, long value) throws JSONException {
|
|
||||||
this.put(index, Long.valueOf(value));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return this.
|
|
||||||
* @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));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put or replace an object value in the JSONArray. If the index is greater 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.
|
|
||||||
* @return this.
|
|
||||||
* @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) {
|
|
||||||
throw new JSONException("JSONArray[" + index + "] not found.");
|
|
||||||
}
|
|
||||||
if (index < length()) {
|
|
||||||
this.myArrayList.set(index, value);
|
|
||||||
} else {
|
|
||||||
while (index != length()) {
|
|
||||||
this.put(JSONObject.NULL);
|
|
||||||
}
|
|
||||||
this.put(value);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an index and close the hole.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public Object remove(int index) {
|
|
||||||
return (index >= 0) && (index < length()) ? this.myArrayList.remove(index) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if two JSONArrays are similar. They must contain similar sequences.
|
|
||||||
*
|
|
||||||
* @param other The other JSONArray
|
|
||||||
* @return true if they are equal
|
|
||||||
*/
|
|
||||||
public boolean similar(Object other) {
|
|
||||||
if (!(other instanceof JSONArray)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
int len = length();
|
|
||||||
if (len != ((JSONArray) other).length()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < len; i += 1) {
|
|
||||||
Object valueThis = get(i);
|
|
||||||
Object valueOther = ((JSONArray) other).get(i);
|
|
||||||
if (valueThis instanceof JSONObject) {
|
|
||||||
if (!((JSONObject) valueThis).similar(valueOther)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (valueThis instanceof JSONArray) {
|
|
||||||
if (!((JSONArray) valueThis).similar(valueOther)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (!valueThis.equals(valueOther)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public JSONObject toJSONObject(JSONArray names) throws JSONException {
|
|
||||||
if ((names == null) || (names.length() == 0) || (length() == 0)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject();
|
|
||||||
for (int i = 0; i < names.length(); i += 1) {
|
|
||||||
jo.put(names.getString(i), opt(i));
|
|
||||||
}
|
|
||||||
return jo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a JSON text of this JSONArray. For compactness, no unnecessary whitespace is added. If it is not possible to
|
|
||||||
* produce a syntactically correct JSON text then null will be returned instead. This could occur if the array
|
|
||||||
* contains an invalid number.
|
|
||||||
* <p>
|
|
||||||
* Warning: This method assumes that the data structure is acyclical.
|
|
||||||
*
|
|
||||||
* @return a printable, displayable, transmittable representation of the array.
|
|
||||||
*/
|
|
||||||
@Override public String toString() {
|
|
||||||
try {
|
|
||||||
return this.toString(0);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return a printable, displayable, transmittable representation of the object, beginning with
|
|
||||||
* {@code [} <small>(left bracket)</small> and ending with {@code ]} <small>(right
|
|
||||||
* bracket)</small>.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public String toString(int indentFactor) throws JSONException {
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
synchronized (sw.getBuffer()) {
|
|
||||||
return this.write(sw, indentFactor, 0).toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
|
|
||||||
* <p>
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.
|
|
||||||
* <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.
|
|
||||||
* @return The writer.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
|
|
||||||
try {
|
|
||||||
boolean commanate = false;
|
|
||||||
int length = length();
|
|
||||||
writer.write('[');
|
|
||||||
if (length == 1) {
|
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(0), indentFactor, indent);
|
|
||||||
} else if (length != 0) {
|
|
||||||
int newindent = indent + indentFactor;
|
|
||||||
for (int i = 0; i < length; i += 1) {
|
|
||||||
if (commanate) {
|
|
||||||
writer.write(',');
|
|
||||||
}
|
|
||||||
if (indentFactor > 0) {
|
|
||||||
writer.write('\n');
|
|
||||||
}
|
|
||||||
JSONObject.indent(writer, newindent);
|
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(i), indentFactor, newindent);
|
|
||||||
commanate = true;
|
|
||||||
}
|
|
||||||
if (indentFactor > 0) {
|
|
||||||
writer.write('\n');
|
|
||||||
}
|
|
||||||
JSONObject.indent(writer, indent);
|
|
||||||
}
|
|
||||||
writer.write(']');
|
|
||||||
return writer;
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new JSONException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
public class JSONException extends RuntimeException {
|
|
||||||
private static final long serialVersionUID = 0;
|
|
||||||
private Throwable cause;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a JSONException with an explanatory message.
|
|
||||||
*
|
|
||||||
* @param message Detail about the reason for the exception.
|
|
||||||
*/
|
|
||||||
public JSONException(final String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new JSONException with the specified cause.
|
|
||||||
*
|
|
||||||
* @param cause The cause.
|
|
||||||
*/
|
|
||||||
public JSONException(final Throwable cause) {
|
|
||||||
super(cause.getMessage());
|
|
||||||
this.cause = cause;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the cause of this exception or null if the cause is nonexistent or unknown.
|
|
||||||
*
|
|
||||||
* @return the cause of this exception or null if the cause is nonexistent or unknown.
|
|
||||||
*/
|
|
||||||
@Override public Throwable getCause() {
|
|
||||||
return cause;
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,16 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The <code>JSONString</code> interface allows a <code>toJSONString()</code> method so that a class can change the
|
|
||||||
* behavior of <code>JSONObject.toString()</code>, <code>JSONArray.toString()</code>, and
|
|
||||||
* <code>JSONWriter.value(</code>Object<code>)</code>. The <code>toJSONString</code> method will be used instead of the
|
|
||||||
* default behavior of using the Object's <code>toString()</code> method and quoting the result.
|
|
||||||
*/
|
|
||||||
public interface JSONString {
|
|
||||||
/**
|
|
||||||
* The <code>toJSONString</code> method allows a class to produce its own JSON serialization.
|
|
||||||
*
|
|
||||||
* @return A strictly syntactically correct JSON text.
|
|
||||||
*/
|
|
||||||
String toJSONString();
|
|
||||||
}
|
|
@ -1,389 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and
|
|
||||||
* JSONArray constructors to parse JSON source strings.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
public class JSONTokener {
|
|
||||||
private final Reader reader;
|
|
||||||
private long character;
|
|
||||||
private boolean eof;
|
|
||||||
private long index;
|
|
||||||
private long line;
|
|
||||||
private char previous;
|
|
||||||
private boolean usePrevious;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from a Reader.
|
|
||||||
*
|
|
||||||
* @param reader A reader.
|
|
||||||
*/
|
|
||||||
public JSONTokener(final Reader reader) {
|
|
||||||
this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
|
|
||||||
eof = false;
|
|
||||||
usePrevious = false;
|
|
||||||
previous = 0;
|
|
||||||
index = 0;
|
|
||||||
character = 1;
|
|
||||||
line = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from an InputStream.
|
|
||||||
*
|
|
||||||
* @param inputStream The source.
|
|
||||||
*/
|
|
||||||
public JSONTokener(final InputStream inputStream) throws JSONException {
|
|
||||||
this(new InputStreamReader(inputStream));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a JSONTokener from a string.
|
|
||||||
*
|
|
||||||
* @param s A source string.
|
|
||||||
*/
|
|
||||||
public JSONTokener(final String s) {
|
|
||||||
this(new StringReader(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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'.
|
|
||||||
* @return An int between 0 and 15, or -1 if c was not a hex digit.
|
|
||||||
*/
|
|
||||||
public static int dehexchar(final char c) {
|
|
||||||
if ((c >= '0') && (c <= '9')) {
|
|
||||||
return c - '0';
|
|
||||||
}
|
|
||||||
if ((c >= 'A') && (c <= 'F')) {
|
|
||||||
return c - ('A' - 10);
|
|
||||||
}
|
|
||||||
if ((c >= 'a') && (c <= 'f')) {
|
|
||||||
return c - ('a' - 10);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter
|
|
||||||
* before attempting to parse the next number or identifier.
|
|
||||||
*/
|
|
||||||
public void back() throws JSONException {
|
|
||||||
if (usePrevious || (index <= 0)) {
|
|
||||||
throw new JSONException("Stepping back two steps is not supported");
|
|
||||||
}
|
|
||||||
index -= 1;
|
|
||||||
character -= 1;
|
|
||||||
usePrevious = true;
|
|
||||||
eof = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean end() {
|
|
||||||
return eof && !usePrevious;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the source string still contains characters that next() can consume.
|
|
||||||
*
|
|
||||||
* @return true if not yet at the end of the source.
|
|
||||||
*/
|
|
||||||
public boolean more() throws JSONException {
|
|
||||||
this.next();
|
|
||||||
if (end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
back();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next character in the source string.
|
|
||||||
*
|
|
||||||
* @return The next character, or 0 if past the end of the source string.
|
|
||||||
*/
|
|
||||||
public char next() throws JSONException {
|
|
||||||
int c;
|
|
||||||
if (usePrevious) {
|
|
||||||
usePrevious = false;
|
|
||||||
c = previous;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
c = reader.read();
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
throw new JSONException(exception);
|
|
||||||
}
|
|
||||||
if (c <= 0) { // End of stream
|
|
||||||
eof = true;
|
|
||||||
c = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index += 1;
|
|
||||||
if (previous == '\r') {
|
|
||||||
line += 1;
|
|
||||||
character = c == '\n' ? 0 : 1;
|
|
||||||
} else if (c == '\n') {
|
|
||||||
line += 1;
|
|
||||||
character = 0;
|
|
||||||
} else {
|
|
||||||
character += 1;
|
|
||||||
}
|
|
||||||
previous = (char) c;
|
|
||||||
return previous;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Consume the next character, and check that it matches a specified character.
|
|
||||||
*
|
|
||||||
* @param c The character to match.
|
|
||||||
* @return The character.
|
|
||||||
* @throws JSONException if the character does not match.
|
|
||||||
*/
|
|
||||||
public char next(final char c) throws JSONException {
|
|
||||||
final char n = this.next();
|
|
||||||
if (n != c) {
|
|
||||||
throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'");
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next n characters.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public String next(final int n) throws JSONException {
|
|
||||||
if (n == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
final char[] chars = new char[n];
|
|
||||||
int pos = 0;
|
|
||||||
while (pos < n) {
|
|
||||||
chars[pos] = this.next();
|
|
||||||
if (end()) {
|
|
||||||
throw syntaxError("Substring bounds error");
|
|
||||||
}
|
|
||||||
pos += 1;
|
|
||||||
}
|
|
||||||
return new String(chars);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 (; ; ) {
|
|
||||||
final char c = this.next();
|
|
||||||
if ((c == 0) || (c > ' ')) {
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the characters up to the next close quote character. Backslash 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>.
|
|
||||||
* @return A String.
|
|
||||||
* @throws JSONException Unterminated string.
|
|
||||||
*/
|
|
||||||
public String nextString(final char quote) throws JSONException {
|
|
||||||
char c;
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
c = this.next();
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
case '\n':
|
|
||||||
case '\r':
|
|
||||||
throw syntaxError("Unterminated string");
|
|
||||||
case '\\':
|
|
||||||
c = this.next();
|
|
||||||
switch (c) {
|
|
||||||
case 'b':
|
|
||||||
sb.append('\b');
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
sb.append('\t');
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
sb.append('\n');
|
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
sb.append('\f');
|
|
||||||
break;
|
|
||||||
case 'r':
|
|
||||||
sb.append('\r');
|
|
||||||
break;
|
|
||||||
case 'u':
|
|
||||||
sb.append((char) Integer.parseInt(this.next(4), 16));
|
|
||||||
break;
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
case '\\':
|
|
||||||
case '/':
|
|
||||||
sb.append(c);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw syntaxError("Illegal escape.");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (c == quote) {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the text up but not including the specified character or the end of line, whichever comes first.
|
|
||||||
*
|
|
||||||
* @param delimiter A delimiter character.
|
|
||||||
* @return A string.
|
|
||||||
*/
|
|
||||||
public String nextTo(final char delimiter) throws JSONException {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
final char c = this.next();
|
|
||||||
if ((c == delimiter) || (c == 0) || (c == '\n') || (c == '\r')) {
|
|
||||||
if (c != 0) {
|
|
||||||
back();
|
|
||||||
}
|
|
||||||
return sb.toString().trim();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return A string, trimmed.
|
|
||||||
*/
|
|
||||||
public String nextTo(final String delimiters) throws JSONException {
|
|
||||||
char c;
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
c = this.next();
|
|
||||||
if ((delimiters.indexOf(c) >= 0) || (c == 0) || (c == '\n') || (c == '\r')) {
|
|
||||||
if (c != 0) {
|
|
||||||
back();
|
|
||||||
}
|
|
||||||
return sb.toString().trim();
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
|
|
||||||
* JSONObject.NULL object.
|
|
||||||
*
|
|
||||||
* @return An object.
|
|
||||||
* @throws JSONException If syntax error.
|
|
||||||
*/
|
|
||||||
public Object nextValue() throws JSONException {
|
|
||||||
char c = nextClean();
|
|
||||||
String string;
|
|
||||||
switch (c) {
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
return nextString(c);
|
|
||||||
case '{':
|
|
||||||
back();
|
|
||||||
return new JSONObject(this);
|
|
||||||
case '[':
|
|
||||||
back();
|
|
||||||
return new JSONArray(this);
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* Handle unquoted text. This could be the values true, false, or
|
|
||||||
* null, or it can be a number. An implementation (such as this one)
|
|
||||||
* is allowed to also accept non-standard forms.
|
|
||||||
* Accumulate characters until we reach the end of the text or a
|
|
||||||
* formatting character.
|
|
||||||
*/
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
while ((c >= ' ') && (",:]}/\\\"[{;=#".indexOf(c) < 0)) {
|
|
||||||
sb.append(c);
|
|
||||||
c = this.next();
|
|
||||||
}
|
|
||||||
back();
|
|
||||||
string = sb.toString().trim();
|
|
||||||
if (string.isEmpty()) {
|
|
||||||
throw syntaxError("Missing value");
|
|
||||||
}
|
|
||||||
return JSONObject.stringToValue(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return The requested character, or zero if the requested character is not found.
|
|
||||||
*/
|
|
||||||
public char skipTo(final char to) throws JSONException {
|
|
||||||
char c;
|
|
||||||
try {
|
|
||||||
final long startIndex = index;
|
|
||||||
final long startCharacter = character;
|
|
||||||
final long startLine = line;
|
|
||||||
reader.mark(1000000);
|
|
||||||
do {
|
|
||||||
c = this.next();
|
|
||||||
if (c == 0) {
|
|
||||||
reader.reset();
|
|
||||||
index = startIndex;
|
|
||||||
character = startCharacter;
|
|
||||||
line = startLine;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
} while (c != to);
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
throw new JSONException(exception);
|
|
||||||
}
|
|
||||||
back();
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a JSONException to signal a syntax error.
|
|
||||||
*
|
|
||||||
* @param message The error message.
|
|
||||||
* @return A JSONException object, suitable for throwing
|
|
||||||
*/
|
|
||||||
public JSONException syntaxError(final String message) {
|
|
||||||
return new JSONException(message + toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make a printable string of this JSONTokener.
|
|
||||||
*
|
|
||||||
* @return " at {index} [character {character} line {line}]"
|
|
||||||
*/
|
|
||||||
@Override public String toString() {
|
|
||||||
return " at " + index + " [character " + character + " line " + line + "]";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a Property file data into JSONObject and back.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
public class Property {
|
|
||||||
/**
|
|
||||||
* Converts a property file object into a JSONObject. The property file object is a table of name value pairs.
|
|
||||||
*
|
|
||||||
* @param properties java.util.Properties
|
|
||||||
* @return JSONObject
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public static JSONObject toJSONObject(final java.util.Properties properties)
|
|
||||||
throws JSONException {
|
|
||||||
final JSONObject jo = new JSONObject();
|
|
||||||
if ((properties != null) && !properties.isEmpty()) {
|
|
||||||
final Enumeration enumProperties = properties.propertyNames();
|
|
||||||
while (enumProperties.hasMoreElements()) {
|
|
||||||
final String name = (String) enumProperties.nextElement();
|
|
||||||
jo.put(name, properties.getProperty(name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts the JSONObject into a property file object.
|
|
||||||
*
|
|
||||||
* @param jo JSONObject
|
|
||||||
* @return java.util.Properties
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public static Properties toProperties(final JSONObject jo) throws JSONException {
|
|
||||||
final Properties properties = new Properties();
|
|
||||||
if (jo != null) {
|
|
||||||
final Iterator<String> keys = jo.keys();
|
|
||||||
while (keys.hasNext()) {
|
|
||||||
final String name = keys.next();
|
|
||||||
properties.put(name, jo.getString(name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return properties;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,375 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This provides static methods to convert an XML text into a JSONObject, and to covert a JSONObject into an XML text.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
class XML {
|
|
||||||
|
|
||||||
static final Character AMP = '&';
|
|
||||||
static final Character APOS = '\'';
|
|
||||||
static final Character BANG = '!';
|
|
||||||
static final Character EQ = '=';
|
|
||||||
static final Character GT = '>';
|
|
||||||
static final Character LT = '<';
|
|
||||||
static final Character QUEST = '?';
|
|
||||||
static final Character QUOT = '"';
|
|
||||||
static final Character SLASH = '/';
|
|
||||||
|
|
||||||
static String escape(String string) {
|
|
||||||
StringBuilder sb = new StringBuilder(string.length());
|
|
||||||
for (int i = 0, length = string.length(); i < length; i++) {
|
|
||||||
char c = string.charAt(i);
|
|
||||||
switch (c) {
|
|
||||||
case '&':
|
|
||||||
sb.append("&");
|
|
||||||
break;
|
|
||||||
case '<':
|
|
||||||
sb.append("<");
|
|
||||||
break;
|
|
||||||
case '>':
|
|
||||||
sb.append(">");
|
|
||||||
break;
|
|
||||||
case '"':
|
|
||||||
sb.append(""");
|
|
||||||
break;
|
|
||||||
case '\'':
|
|
||||||
sb.append("'");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Throw an exception if the string contains whitespace. Whitespace is not allowed in tagNames and attributes.
|
|
||||||
*
|
|
||||||
* @param string A string.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
static void noSpace(String string) throws JSONException {
|
|
||||||
int length = string.length();
|
|
||||||
if (length == 0) {
|
|
||||||
throw new JSONException("Empty string.");
|
|
||||||
}
|
|
||||||
for (char c : string.toCharArray()) {
|
|
||||||
if (Character.isWhitespace(c)) {
|
|
||||||
throw new JSONException('\'' + string + "' contains a space character.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @return true if the close tag is processed.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
private static boolean parse(XMLTokener x, JSONObject context, String name)
|
|
||||||
throws JSONException {
|
|
||||||
// Test for and skip past these forms:
|
|
||||||
// <!-- ... -->
|
|
||||||
// <! ... >
|
|
||||||
// <![ ... ]]>
|
|
||||||
// <? ... ?>
|
|
||||||
// Report errors for these forms:
|
|
||||||
// <>
|
|
||||||
// <=
|
|
||||||
// <<
|
|
||||||
Object token = x.nextToken();
|
|
||||||
// <!
|
|
||||||
String string;
|
|
||||||
if (token == BANG) {
|
|
||||||
char c = x.next();
|
|
||||||
if (c == '-') {
|
|
||||||
if (x.next() == '-') {
|
|
||||||
x.skipPast("-->");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
x.back();
|
|
||||||
} else if (c == '[') {
|
|
||||||
token = x.nextToken();
|
|
||||||
if ("CDATA".equals(token)) {
|
|
||||||
if (x.next() == '[') {
|
|
||||||
string = x.nextCDATA();
|
|
||||||
if (!string.isEmpty()) {
|
|
||||||
context.accumulate("content", string);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw x.syntaxError("Expected 'CDATA['");
|
|
||||||
}
|
|
||||||
int i = 1;
|
|
||||||
do {
|
|
||||||
token = x.nextMeta();
|
|
||||||
if (token == null) {
|
|
||||||
throw x.syntaxError("Missing '>' after '<!'.");
|
|
||||||
} else if (token == LT) {
|
|
||||||
i += 1;
|
|
||||||
} else if (token == GT) {
|
|
||||||
i -= 1;
|
|
||||||
}
|
|
||||||
} while (i > 0);
|
|
||||||
return false;
|
|
||||||
} else if (token == QUEST) {
|
|
||||||
// <?
|
|
||||||
x.skipPast("?>");
|
|
||||||
return false;
|
|
||||||
} else if (token == SLASH) {
|
|
||||||
// Close tag </
|
|
||||||
token = x.nextToken();
|
|
||||||
if (name == null) {
|
|
||||||
throw x.syntaxError("Mismatched close tag " + token);
|
|
||||||
}
|
|
||||||
if (!token.equals(name)) {
|
|
||||||
throw x.syntaxError("Mismatched " + name + " and " + token);
|
|
||||||
}
|
|
||||||
if (x.nextToken() != GT) {
|
|
||||||
throw x.syntaxError("Misshaped close tag");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
} else if (token instanceof Character) {
|
|
||||||
throw x.syntaxError("Misshaped tag");
|
|
||||||
// Open tag <
|
|
||||||
} else {
|
|
||||||
String tagName = (String) token;
|
|
||||||
token = null;
|
|
||||||
JSONObject jsonobject = new JSONObject();
|
|
||||||
for (; ; ) {
|
|
||||||
if (token == null) {
|
|
||||||
token = x.nextToken();
|
|
||||||
}
|
|
||||||
// attribute = value
|
|
||||||
if (token instanceof String) {
|
|
||||||
string = (String) token;
|
|
||||||
token = x.nextToken();
|
|
||||||
if (token == EQ) {
|
|
||||||
token = x.nextToken();
|
|
||||||
if (!(token instanceof String)) {
|
|
||||||
throw x.syntaxError("Missing value");
|
|
||||||
}
|
|
||||||
jsonobject.accumulate(string, XML.stringToValue((String) token));
|
|
||||||
token = null;
|
|
||||||
} else {
|
|
||||||
jsonobject.accumulate(string, "");
|
|
||||||
}
|
|
||||||
// Empty tag <.../>
|
|
||||||
} else if (token == SLASH) {
|
|
||||||
if (x.nextToken() != GT) {
|
|
||||||
throw x.syntaxError("Misshaped tag");
|
|
||||||
}
|
|
||||||
if (jsonobject.length() > 0) {
|
|
||||||
context.accumulate(tagName, jsonobject);
|
|
||||||
} else {
|
|
||||||
context.accumulate(tagName, "");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
// Content, between <...> and </...>
|
|
||||||
} else if (token == GT) {
|
|
||||||
for (; ; ) {
|
|
||||||
token = x.nextContent();
|
|
||||||
if (token == null) {
|
|
||||||
if (tagName != null) {
|
|
||||||
throw x.syntaxError("Unclosed tag " + tagName);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} else if (token instanceof String) {
|
|
||||||
string = (String) token;
|
|
||||||
if (!string.isEmpty()) {
|
|
||||||
jsonobject.accumulate("content", XML.stringToValue(string));
|
|
||||||
}
|
|
||||||
// Nested element
|
|
||||||
} else if (token == LT) {
|
|
||||||
if (parse(x, jsonobject, tagName)) {
|
|
||||||
if (jsonobject.length() == 0) {
|
|
||||||
context.accumulate(tagName, "");
|
|
||||||
} else if ((jsonobject.length() == 1) && (jsonobject.opt("content")
|
|
||||||
!= null)) {
|
|
||||||
context.accumulate(tagName, jsonobject.opt("content"));
|
|
||||||
} else {
|
|
||||||
context.accumulate(tagName, jsonobject);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw x.syntaxError("Misshaped tag");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to convert a string into a number, boolean, or null. If the string can't be converted, return the string.
|
|
||||||
* This is much less ambitious than JSONObject.stringToValue, especially because it does not attempt to convert plus
|
|
||||||
* forms, octal forms, hex forms, or E forms lacking decimal points.
|
|
||||||
*
|
|
||||||
* @param string A String.
|
|
||||||
* @return A simple JSON value.
|
|
||||||
*/
|
|
||||||
static Object stringToValue(String string) {
|
|
||||||
if ("true".equalsIgnoreCase(string)) {
|
|
||||||
return Boolean.TRUE;
|
|
||||||
}
|
|
||||||
if ("false".equalsIgnoreCase(string)) {
|
|
||||||
return Boolean.FALSE;
|
|
||||||
}
|
|
||||||
if ("null".equalsIgnoreCase(string)) {
|
|
||||||
return JSONObject.NULL;
|
|
||||||
}
|
|
||||||
//If it might be a number, try converting it, first as a Long, and then as a Double. If that doesn't work, return the string.
|
|
||||||
try {
|
|
||||||
char initial = string.charAt(0);
|
|
||||||
if ((initial == '-') || ((initial >= '0') && (initial <= '9'))) {
|
|
||||||
Long value = Long.valueOf(string);
|
|
||||||
if (value.toString().equals(string)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException ignore) {
|
|
||||||
try {
|
|
||||||
Double value = Double.valueOf(string);
|
|
||||||
if (value.toString().equals(string)) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
|
||||||
JSONObject jo = new JSONObject();
|
|
||||||
XMLTokener x = new XMLTokener(string);
|
|
||||||
while (x.more() && x.skipPast("<")) {
|
|
||||||
parse(x, jo, null);
|
|
||||||
}
|
|
||||||
return jo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
|
||||||
*
|
|
||||||
* @param object A JSONObject.
|
|
||||||
* @return A string.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public static String toString(Object object) throws JSONException {
|
|
||||||
return toString(object, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
|
||||||
*
|
|
||||||
* @param object A JSONObject.
|
|
||||||
* @param tagName The optional name of the enclosing tag.
|
|
||||||
* @return A string.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public static String toString(Object object, String tagName) throws JSONException {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
int i;
|
|
||||||
JSONArray ja;
|
|
||||||
int length;
|
|
||||||
String string;
|
|
||||||
if (object instanceof JSONObject) {
|
|
||||||
// Emit <tagName>
|
|
||||||
if (tagName != null) {
|
|
||||||
sb.append('<');
|
|
||||||
sb.append(tagName);
|
|
||||||
sb.append('>');
|
|
||||||
}
|
|
||||||
// Loop thru the keys.
|
|
||||||
JSONObject jo = (JSONObject) object;
|
|
||||||
Iterator<String> keys = jo.keys();
|
|
||||||
while (keys.hasNext()) {
|
|
||||||
String key = keys.next();
|
|
||||||
Object value = jo.opt(key);
|
|
||||||
if (value == null) {
|
|
||||||
value = "";
|
|
||||||
}
|
|
||||||
string = value instanceof String ? (String) value : null;
|
|
||||||
// Emit content in body
|
|
||||||
if ("content".equals(key)) {
|
|
||||||
if (value instanceof JSONArray) {
|
|
||||||
ja = (JSONArray) value;
|
|
||||||
length = ja.length();
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
if (i > 0) {
|
|
||||||
sb.append('\n');
|
|
||||||
}
|
|
||||||
sb.append(escape(ja.get(i).toString()));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sb.append(escape(value.toString()));
|
|
||||||
}
|
|
||||||
// Emit an array of similar keys
|
|
||||||
} else if (value instanceof JSONArray) {
|
|
||||||
ja = (JSONArray) value;
|
|
||||||
length = ja.length();
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
value = ja.get(i);
|
|
||||||
if (value instanceof JSONArray) {
|
|
||||||
sb.append('<');
|
|
||||||
sb.append(key);
|
|
||||||
sb.append('>');
|
|
||||||
sb.append(toString(value));
|
|
||||||
sb.append("</");
|
|
||||||
sb.append(key);
|
|
||||||
sb.append('>');
|
|
||||||
} else {
|
|
||||||
sb.append(toString(value, key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if ("".equals(value)) {
|
|
||||||
sb.append('<');
|
|
||||||
sb.append(key);
|
|
||||||
sb.append("/>");
|
|
||||||
// Emit a new tag <k>
|
|
||||||
} else {
|
|
||||||
sb.append(toString(value, key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (tagName != null) {
|
|
||||||
// Emit the </tagname> close tag
|
|
||||||
sb.append("</");
|
|
||||||
sb.append(tagName);
|
|
||||||
sb.append('>');
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
// XML does not have good support for arrays. If an array appears in
|
|
||||||
// a place
|
|
||||||
// where XML is lacking, synthesize an <array> element.
|
|
||||||
} else {
|
|
||||||
if (object.getClass().isArray()) {
|
|
||||||
object = new JSONArray(object);
|
|
||||||
}
|
|
||||||
if (object instanceof JSONArray) {
|
|
||||||
ja = (JSONArray) object;
|
|
||||||
length = ja.length();
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
} else {
|
|
||||||
string = escape(object.toString());
|
|
||||||
return (tagName == null) ?
|
|
||||||
'"' + string + '"' :
|
|
||||||
string.isEmpty() ?
|
|
||||||
'<' + tagName + "/>" :
|
|
||||||
'<' + tagName + '>' + string + "</" + tagName + '>';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,315 +0,0 @@
|
|||||||
package com.github.intellectualsites.plotsquared.json;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The XMLTokener extends the JSONTokener to provide additional methods for the parsing of XML texts.
|
|
||||||
*
|
|
||||||
* @author JSON.org
|
|
||||||
* @version 2014-05-03
|
|
||||||
*/
|
|
||||||
public class XMLTokener extends JSONTokener {
|
|
||||||
/**
|
|
||||||
* The table of entity values. It initially contains Character values for amp, apos, gt, lt, quot.
|
|
||||||
*/
|
|
||||||
public static final HashMap<String, Character> entity;
|
|
||||||
|
|
||||||
static {
|
|
||||||
entity = new HashMap<>(8);
|
|
||||||
entity.put("amp", XML.AMP);
|
|
||||||
entity.put("apos", XML.APOS);
|
|
||||||
entity.put("gt", XML.GT);
|
|
||||||
entity.put("lt", XML.LT);
|
|
||||||
entity.put("quot", XML.QUOT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct an XMLTokener from a string.
|
|
||||||
*
|
|
||||||
* @param s A source string.
|
|
||||||
*/
|
|
||||||
public XMLTokener(final String s) {
|
|
||||||
super(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the text in the CDATA block.
|
|
||||||
*
|
|
||||||
* @return The string up to the <code>]]></code>.
|
|
||||||
* @throws JSONException If the <code>]]></code> is not found.
|
|
||||||
*/
|
|
||||||
public String nextCDATA() throws JSONException {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
char c = next();
|
|
||||||
if (end()) {
|
|
||||||
throw syntaxError("Unclosed CDATA");
|
|
||||||
}
|
|
||||||
sb.append(c);
|
|
||||||
int i = sb.length() - 3;
|
|
||||||
if ((i >= 0) && (sb.charAt(i) == ']') && (sb.charAt(i + 1) == ']') && (sb.charAt(i + 2)
|
|
||||||
== '>')) {
|
|
||||||
sb.setLength(i);
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next XML outer token, trimming whitespace. There are two kinds of tokens: the '<' character which begins
|
|
||||||
* a markup tag, and the content text between markup tags.
|
|
||||||
*
|
|
||||||
* @return A string, or a '<' Character, or null if there is no more source text.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public Object nextContent() throws JSONException {
|
|
||||||
char c;
|
|
||||||
do {
|
|
||||||
c = next();
|
|
||||||
} while (Character.isWhitespace(c));
|
|
||||||
if (c == 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (c == '<') {
|
|
||||||
return XML.LT;
|
|
||||||
}
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
if ((c == '<') || (c == 0)) {
|
|
||||||
back();
|
|
||||||
return sb.toString().trim();
|
|
||||||
}
|
|
||||||
if (c == '&') {
|
|
||||||
sb.append(nextEntity('&'));
|
|
||||||
} else {
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
c = next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the next entity. These entities are translated to Characters: <code>& " > <
|
|
||||||
* "</code>.
|
|
||||||
*
|
|
||||||
* @param ampersand An ampersand character.
|
|
||||||
* @return A Character or an entity String if the entity is not recognized.
|
|
||||||
* @throws JSONException If missing ';' in XML entity.
|
|
||||||
*/
|
|
||||||
public Object nextEntity(final char ampersand) throws JSONException {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
final char c = next();
|
|
||||||
if (Character.isLetterOrDigit(c) || (c == '#')) {
|
|
||||||
sb.append(Character.toLowerCase(c));
|
|
||||||
} else if (c == ';') {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
throw syntaxError("Missing ';' in XML entity: &" + sb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
final String string = sb.toString();
|
|
||||||
final Object object = entity.get(string);
|
|
||||||
return object != null ? object : ampersand + string + ';';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the next XML meta token. This is used for skipping over <!...> and <?...?> structures.
|
|
||||||
*
|
|
||||||
* @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.
|
|
||||||
*/
|
|
||||||
public Object nextMeta() throws JSONException {
|
|
||||||
char c;
|
|
||||||
do {
|
|
||||||
c = next();
|
|
||||||
} while (Character.isWhitespace(c));
|
|
||||||
char q;
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
throw syntaxError("Misshaped meta tag");
|
|
||||||
case '<':
|
|
||||||
return XML.LT;
|
|
||||||
case '>':
|
|
||||||
return XML.GT;
|
|
||||||
case '/':
|
|
||||||
return XML.SLASH;
|
|
||||||
case '=':
|
|
||||||
return XML.EQ;
|
|
||||||
case '!':
|
|
||||||
return XML.BANG;
|
|
||||||
case '?':
|
|
||||||
return XML.QUEST;
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
q = c;
|
|
||||||
for (; ; ) {
|
|
||||||
c = next();
|
|
||||||
if (c == 0) {
|
|
||||||
throw syntaxError("Unterminated string");
|
|
||||||
}
|
|
||||||
if (c == q) {
|
|
||||||
return Boolean.TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
for (; ; ) {
|
|
||||||
c = next();
|
|
||||||
if (Character.isWhitespace(c)) {
|
|
||||||
return Boolean.TRUE;
|
|
||||||
}
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
case '<':
|
|
||||||
case '>':
|
|
||||||
case '/':
|
|
||||||
case '=':
|
|
||||||
case '!':
|
|
||||||
case '?':
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
back();
|
|
||||||
return Boolean.TRUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the next XML Token. These tokens are found inside of angle brackets. It may be one of these characters:
|
|
||||||
* <code>/ >= ! ?</code> or it may be a 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.
|
|
||||||
*/
|
|
||||||
public Object nextToken() throws JSONException {
|
|
||||||
char c;
|
|
||||||
do {
|
|
||||||
c = next();
|
|
||||||
} while (Character.isWhitespace(c));
|
|
||||||
char q;
|
|
||||||
StringBuilder sb;
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
throw syntaxError("Misshaped element");
|
|
||||||
case '<':
|
|
||||||
throw syntaxError("Misplaced '<'");
|
|
||||||
case '>':
|
|
||||||
return XML.GT;
|
|
||||||
case '/':
|
|
||||||
return XML.SLASH;
|
|
||||||
case '=':
|
|
||||||
return XML.EQ;
|
|
||||||
case '!':
|
|
||||||
return XML.BANG;
|
|
||||||
case '?':
|
|
||||||
return XML.QUEST;
|
|
||||||
// Quoted string
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
q = c;
|
|
||||||
sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
c = next();
|
|
||||||
if (c == 0) {
|
|
||||||
throw syntaxError("Unterminated string");
|
|
||||||
}
|
|
||||||
if (c == q) {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
if (c == '&') {
|
|
||||||
sb.append(nextEntity('&'));
|
|
||||||
} else {
|
|
||||||
sb.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// Name
|
|
||||||
sb = new StringBuilder();
|
|
||||||
for (; ; ) {
|
|
||||||
sb.append(c);
|
|
||||||
c = next();
|
|
||||||
if (Character.isWhitespace(c)) {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
switch (c) {
|
|
||||||
case 0:
|
|
||||||
return sb.toString();
|
|
||||||
case '>':
|
|
||||||
case '/':
|
|
||||||
case '=':
|
|
||||||
case '!':
|
|
||||||
case '?':
|
|
||||||
case '[':
|
|
||||||
case ']':
|
|
||||||
back();
|
|
||||||
return sb.toString();
|
|
||||||
case '<':
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
throw syntaxError("Bad character in a name");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
* @throws JSONException
|
|
||||||
*/
|
|
||||||
public boolean skipPast(final String to) throws JSONException {
|
|
||||||
char c;
|
|
||||||
int i;
|
|
||||||
final int length = to.length();
|
|
||||||
final char[] circle = new char[length];
|
|
||||||
/*
|
|
||||||
* First fill the circle buffer with as many characters as are in the
|
|
||||||
* to string. If we reach an early end, bail.
|
|
||||||
*/
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
c = next();
|
|
||||||
if (c == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
circle[i] = c;
|
|
||||||
}
|
|
||||||
/* We will loop, possibly for all of the remaining characters. */
|
|
||||||
for (int offset = 0; ; ) {
|
|
||||||
int j = offset;
|
|
||||||
boolean b = true;
|
|
||||||
/* Compare the circle buffer with the to string. */
|
|
||||||
for (i = 0; i < length; i += 1) {
|
|
||||||
if (circle[j] != to.charAt(i)) {
|
|
||||||
b = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
j += 1;
|
|
||||||
if (j >= length) {
|
|
||||||
j -= length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* If we exit the loop with b intact, then victory is ours. */
|
|
||||||
if (b) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
/* Get the next character. If there isn't one, then defeat is ours. */
|
|
||||||
c = next();
|
|
||||||
if (c == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
* Shove the character in the circle buffer and advance the
|
|
||||||
* circle offset. The offset is mod n.
|
|
||||||
*/
|
|
||||||
circle[offset] = c;
|
|
||||||
offset += 1;
|
|
||||||
if (offset >= length) {
|
|
||||||
offset -= length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,5 @@
|
|||||||
package com.github.intellectualsites.plotsquared.plot.util;
|
package com.github.intellectualsites.plotsquared.plot.util;
|
||||||
|
|
||||||
import com.github.intellectualsites.plotsquared.json.JSONArray;
|
|
||||||
import com.github.intellectualsites.plotsquared.json.JSONException;
|
|
||||||
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
|
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
|
||||||
import com.github.intellectualsites.plotsquared.plot.config.Settings;
|
import com.github.intellectualsites.plotsquared.plot.config.Settings;
|
||||||
import com.github.intellectualsites.plotsquared.plot.generator.ClassicPlotWorld;
|
import com.github.intellectualsites.plotsquared.plot.generator.ClassicPlotWorld;
|
||||||
@ -26,6 +24,8 @@ import com.sk89q.worldedit.regions.CuboidRegion;
|
|||||||
import com.sk89q.worldedit.world.biome.BiomeType;
|
import com.sk89q.worldedit.world.biome.BiomeType;
|
||||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -75,6 +75,8 @@ subprojects {
|
|||||||
exclude(module: "mockito-core")
|
exclude(module: "mockito-core")
|
||||||
exclude(module: "dummypermscompat")
|
exclude(module: "dummypermscompat")
|
||||||
}
|
}
|
||||||
|
compile group: 'org.json', name: 'json', version: '20190722'
|
||||||
|
|
||||||
implementation("net.kyori:text-api:3.0.2")
|
implementation("net.kyori:text-api:3.0.2")
|
||||||
implementation("net.kyori:text-serializer-gson:3.0.2")
|
implementation("net.kyori:text-serializer-gson:3.0.2")
|
||||||
implementation("net.kyori:text-serializer-legacy:3.0.2")
|
implementation("net.kyori:text-serializer-legacy:3.0.2")
|
||||||
@ -111,6 +113,7 @@ subprojects {
|
|||||||
include(dependency("net.kyori:text-api:3.0.2"))
|
include(dependency("net.kyori:text-api:3.0.2"))
|
||||||
}
|
}
|
||||||
relocate("io.papermc.lib", "com.github.intellectualsites.plotsquared.bukkit.paperlib")
|
relocate("io.papermc.lib", "com.github.intellectualsites.plotsquared.bukkit.paperlib")
|
||||||
|
relocate("org.json", "com.github.intellectualsites.plotsquared.json")
|
||||||
// relocate('org.mcstats', 'com.plotsquared.stats')
|
// relocate('org.mcstats', 'com.plotsquared.stats')
|
||||||
archiveFileName = "${parent.name}-${project.name}-${parent.version}.jar"
|
archiveFileName = "${parent.name}-${project.name}-${parent.version}.jar"
|
||||||
destinationDirectory = file "../target"
|
destinationDirectory = file "../target"
|
||||||
|
Loading…
Reference in New Issue
Block a user