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

Season's Greetings

This commit is contained in:
Douglas Crockford 2010-12-25 14:12:07 -08:00
parent f284477cff
commit 87c4b1d59d
13 changed files with 562 additions and 566 deletions

254
XML.java
View file

@ -31,7 +31,7 @@ import java.util.Iterator;
* This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text.
* @author JSON.org
* @version 2010-12-23
* @version 2010-12-24
*/
public class XML {
@ -75,7 +75,7 @@ public class XML {
*/
public static String escape(String string) {
StringBuffer sb = new StringBuffer();
for (int i = 0, len = string.length(); i < len; i++) {
for (int i = 0, length = string.length(); i < length; i++) {
char c = string.charAt(i);
switch (c) {
case '&':
@ -128,10 +128,10 @@ public class XML {
String name) throws JSONException {
char c;
int i;
String n;
JSONObject o = null;
String s;
Object t;
JSONObject jsonobject = null;
String string;
String tagName;
Object token;
// Test for and skip past these forms:
// <!-- ... -->
@ -143,11 +143,11 @@ public class XML {
// <=
// <<
t = x.nextToken();
token = x.nextToken();
// <!
if (t == BANG) {
if (token == BANG) {
c = x.next();
if (c == '-') {
if (x.next() == '-') {
@ -156,12 +156,12 @@ public class XML {
}
x.back();
} else if (c == '[') {
t = x.nextToken();
if (t.equals("CDATA")) {
token = x.nextToken();
if (token.equals("CDATA")) {
if (x.next() == '[') {
s = x.nextCDATA();
if (s.length() > 0) {
context.accumulate("content", s);
string = x.nextCDATA();
if (string.length() > 0) {
context.accumulate("content", string);
}
return false;
}
@ -170,108 +170,111 @@ public class XML {
}
i = 1;
do {
t = x.nextMeta();
if (t == null) {
token = x.nextMeta();
if (token == null) {
throw x.syntaxError("Missing '>' after '<!'.");
} else if (t == LT) {
} else if (token == LT) {
i += 1;
} else if (t == GT) {
} else if (token == GT) {
i -= 1;
}
} while (i > 0);
return false;
} else if (t == QUEST) {
} else if (token == QUEST) {
// <?
x.skipPast("?>");
return false;
} else if (t == SLASH) {
} else if (token == SLASH) {
// Close tag </
t = x.nextToken();
token = x.nextToken();
if (name == null) {
throw x.syntaxError("Mismatched close tag" + t);
throw x.syntaxError("Mismatched close tag " + token);
}
if (!t.equals(name)) {
throw x.syntaxError("Mismatched " + name + " and " + t);
if (!token.equals(name)) {
throw x.syntaxError("Mismatched " + name + " and " + token);
}
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped close tag");
}
return true;
} else if (t instanceof Character) {
} else if (token instanceof Character) {
throw x.syntaxError("Misshaped tag");
// Open tag <
} else {
n = (String)t;
t = null;
o = new JSONObject();
tagName = (String)token;
token = null;
jsonobject = new JSONObject();
for (;;) {
if (t == null) {
t = x.nextToken();
if (token == null) {
token = x.nextToken();
}
// attribute = value
if (t instanceof String) {
s = (String)t;
t = x.nextToken();
if (t == EQ) {
t = x.nextToken();
if (!(t instanceof String)) {
if (token instanceof String) {
string = (String)token;
token = x.nextToken();
if (token == EQ) {
token = x.nextToken();
if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
}
o.accumulate(s, XML.stringToValue((String)t));
t = null;
jsonobject.accumulate(string,
XML.stringToValue((String)token));
token = null;
} else {
o.accumulate(s, "");
jsonobject.accumulate(string, "");
}
// Empty tag <.../>
} else if (t == SLASH) {
} else if (token == SLASH) {
if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped tag");
}
if (o.length() > 0) {
context.accumulate(n, o);
if (jsonobject.length() > 0) {
context.accumulate(tagName, jsonobject);
} else {
context.accumulate(n, "");
context.accumulate(tagName, "");
}
return false;
// Content, between <...> and </...>
} else if (t == GT) {
} else if (token == GT) {
for (;;) {
t = x.nextContent();
if (t == null) {
if (n != null) {
throw x.syntaxError("Unclosed tag " + n);
token = x.nextContent();
if (token == null) {
if (tagName != null) {
throw x.syntaxError("Unclosed tag " + tagName);
}
return false;
} else if (t instanceof String) {
s = (String)t;
if (s.length() > 0) {
o.accumulate("content", XML.stringToValue(s));
} else if (token instanceof String) {
string = (String)token;
if (string.length() > 0) {
jsonobject.accumulate("content",
XML.stringToValue(string));
}
// Nested element
} else if (t == LT) {
if (parse(x, o, n)) {
if (o.length() == 0) {
context.accumulate(n, "");
} else if (o.length() == 1 &&
o.opt("content") != null) {
context.accumulate(n, o.opt("content"));
} else if (token == LT) {
if (parse(x, jsonobject, tagName)) {
if (jsonobject.length() == 0) {
context.accumulate(tagName, "");
} else if (jsonobject.length() == 1 &&
jsonobject.opt("content") != null) {
context.accumulate(tagName,
jsonobject.opt("content"));
} else {
context.accumulate(n, o);
context.accumulate(tagName, jsonobject);
}
return false;
}
@ -354,142 +357,141 @@ public class XML {
* @throws JSONException
*/
public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject o = new JSONObject();
JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(string);
while (x.more() && x.skipPast("<")) {
parse(x, o, null);
parse(x, jo, null);
}
return o;
return jo;
}
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
* @param o A JSONObject.
* @param object A JSONObject.
* @return A string.
* @throws JSONException
*/
public static String toString(Object o) throws JSONException {
return toString(o, null);
public static String toString(Object object) throws JSONException {
return toString(object, null);
}
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
* @param o A JSONObject.
* @param object A JSONObject.
* @param tagName The optional name of the enclosing tag.
* @return A string.
* @throws JSONException
*/
public static String toString(Object o, String tagName)
public static String toString(Object object, String tagName)
throws JSONException {
StringBuffer b = new StringBuffer();
StringBuffer sb = new StringBuffer();
int i;
JSONArray ja;
JSONObject jo;
String k;
String key;
Iterator keys;
int len;
String s;
Object v;
if (o instanceof JSONObject) {
int length;
String string;
Object value;
if (object instanceof JSONObject) {
// Emit <tagName>
if (tagName != null) {
b.append('<');
b.append(tagName);
b.append('>');
sb.append('<');
sb.append(tagName);
sb.append('>');
}
// Loop thru the keys.
jo = (JSONObject)o;
jo = (JSONObject)object;
keys = jo.keys();
while (keys.hasNext()) {
k = keys.next().toString();
v = jo.opt(k);
if (v == null) {
v = "";
key = keys.next().toString();
value = jo.opt(key);
if (value == null) {
value = "";
}
if (v instanceof String) {
s = (String)v;
if (value instanceof String) {
string = (String)value;
} else {
s = null;
string = null;
}
// Emit content in body
if (k.equals("content")) {
if (v instanceof JSONArray) {
ja = (JSONArray)v;
len = ja.length();
for (i = 0; i < len; i += 1) {
if (key.equals("content")) {
if (value instanceof JSONArray) {
ja = (JSONArray)value;
length = ja.length();
for (i = 0; i < length; i += 1) {
if (i > 0) {
b.append('\n');
sb.append('\n');
}
b.append(escape(ja.get(i).toString()));
sb.append(escape(ja.get(i).toString()));
}
} else {
b.append(escape(v.toString()));
sb.append(escape(value.toString()));
}
// Emit an array of similar keys
} else if (v instanceof JSONArray) {
ja = (JSONArray)v;
len = ja.length();
for (i = 0; i < len; i += 1) {
v = ja.get(i);
if (v instanceof JSONArray) {
b.append('<');
b.append(k);
b.append('>');
b.append(toString(v));
b.append("</");
b.append(k);
b.append('>');
} 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 {
b.append(toString(v, k));
sb.append(toString(value, key));
}
}
} else if (v.equals("")) {
b.append('<');
b.append(k);
b.append("/>");
} else if (value.equals("")) {
sb.append('<');
sb.append(key);
sb.append("/>");
// Emit a new tag <k>
} else {
b.append(toString(v, k));
sb.append(toString(value, key));
}
}
if (tagName != null) {
// Emit the </tagname> close tag
b.append("</");
b.append(tagName);
b.append('>');
sb.append("</");
sb.append(tagName);
sb.append('>');
}
return b.toString();
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 (o instanceof JSONArray) {
ja = (JSONArray)o;
len = ja.length();
for (i = 0; i < len; ++i) {
v = ja.opt(i);
b.append(toString(v, (tagName == null) ? "array" : tagName));
} else 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 b.toString();
return sb.toString();
} else {
s = (o == null) ? "null" : escape(o.toString());
return (tagName == null) ? "\"" + s + "\"" :
(s.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + s + "</" + tagName + ">";
string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\"" :
(string.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + string + "</" + tagName + ">";
}
}
}