1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

entityify

This commit is contained in:
Douglas Crockford 2012-10-26 01:52:58 -07:00
parent 7c14a3d8c2
commit f803a5db3e

View file

@ -31,11 +31,11 @@ import java.util.Iterator;
* This provides static methods to convert an XML text into a JSONObject, * This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text. * and to covert a JSONObject into an XML text.
* @author JSON.org * @author JSON.org
* @version 2011-02-11 * @version 2012-10-26
*/ */
public class XML { public class XML {
/** The Character '&'. */ /** The Character '&'. */
public static final Character AMP = new Character('&'); public static final Character AMP = new Character('&');
/** The Character '''. */ /** The Character '''. */
@ -50,7 +50,7 @@ public class XML {
/** The Character '>'. */ /** The Character '>'. */
public static final Character GT = new Character('>'); public static final Character GT = new Character('>');
/** The Character '<'. */ /** The Character '&lt;'. */
public static final Character LT = new Character('<'); public static final Character LT = new Character('<');
/** The Character '?'. */ /** The Character '?'. */
@ -99,9 +99,9 @@ public class XML {
} }
return sb.toString(); return sb.toString();
} }
/** /**
* Throw an exception if the string contains whitespace. * Throw an exception if the string contains whitespace.
* Whitespace is not allowed in tagNames and attributes. * Whitespace is not allowed in tagNames and attributes.
* @param string * @param string
* @throws JSONException * @throws JSONException
@ -113,7 +113,7 @@ public class XML {
} }
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1) {
if (Character.isWhitespace(string.charAt(i))) { if (Character.isWhitespace(string.charAt(i))) {
throw new JSONException("'" + string + throw new JSONException("'" + string +
"' contains a space character."); "' contains a space character.");
} }
} }
@ -196,7 +196,7 @@ public class XML {
token = x.nextToken(); token = x.nextToken();
if (name == null) { if (name == null) {
throw x.syntaxError("Mismatched close tag " + token); throw x.syntaxError("Mismatched close tag " + token);
} }
if (!token.equals(name)) { if (!token.equals(name)) {
throw x.syntaxError("Mismatched " + name + " and " + token); throw x.syntaxError("Mismatched " + name + " and " + token);
} }
@ -229,7 +229,7 @@ public class XML {
if (!(token instanceof String)) { if (!(token instanceof String)) {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
jsonobject.accumulate(string, jsonobject.accumulate(string,
XML.stringToValue((String)token)); XML.stringToValue((String)token));
token = null; token = null;
} else { } else {
@ -262,7 +262,7 @@ public class XML {
} else if (token instanceof String) { } else if (token instanceof String) {
string = (String)token; string = (String)token;
if (string.length() > 0) { if (string.length() > 0) {
jsonobject.accumulate("content", jsonobject.accumulate("content",
XML.stringToValue(string)); XML.stringToValue(string));
} }
@ -274,7 +274,7 @@ public class XML {
context.accumulate(tagName, ""); context.accumulate(tagName, "");
} else if (jsonobject.length() == 1 && } else if (jsonobject.length() == 1 &&
jsonobject.opt("content") != null) { jsonobject.opt("content") != null) {
context.accumulate(tagName, context.accumulate(tagName,
jsonobject.opt("content")); jsonobject.opt("content"));
} else { } else {
context.accumulate(tagName, jsonobject); context.accumulate(tagName, jsonobject);
@ -295,7 +295,7 @@ public class XML {
* Try to convert a string into a number, boolean, or null. If the string * 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 * can't be converted, return the string. This is much less ambitious than
* JSONObject.stringToValue, especially because it does not attempt to * JSONObject.stringToValue, especially because it does not attempt to
* convert plus forms, octal forms, hex forms, or E forms lacking decimal * convert plus forms, octal forms, hex forms, or E forms lacking decimal
* points. * points.
* @param string A String. * @param string A String.
* @return A simple JSON value. * @return A simple JSON value.
@ -317,7 +317,7 @@ public class XML {
return new Integer(0); return new Integer(0);
} }
// If it might be a number, try converting it. If that doesn't work, // If it might be a number, try converting it. If that doesn't work,
// return the string. // return the string.
try { try {
@ -347,7 +347,7 @@ public class XML {
return string; return string;
} }
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject. Some information may be lost in this transformation * JSONObject. Some information may be lost in this transformation
@ -505,4 +505,4 @@ public class XML {
} }
} }
} }
} }