From 91c6f09be82b81f5a6d553f83b711d4cdba42ec5 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 26 Oct 2015 18:17:37 -0400 Subject: [PATCH 1/3] Modifies XML output to be handled the same for a native java array as well as a JSONArray. --- XML.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/XML.java b/XML.java index 23a9919..a55223c 100755 --- a/XML.java +++ b/XML.java @@ -406,8 +406,11 @@ public class XML { value = jo.opt(key); if (value == null) { value = ""; + }else if(value.getClass().isArray()){ + value = new JSONArray(value); } string = value instanceof String ? (String)value : null; + // Emit content in body From 7886c96204c93c7cd4e9a9258b1bd40773fe11e9 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 26 Oct 2015 18:30:41 -0400 Subject: [PATCH 2/3] Changes JSONArray for loops to use the new iterators. --- XML.java | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/XML.java b/XML.java index a55223c..bad8737 100755 --- a/XML.java +++ b/XML.java @@ -379,14 +379,13 @@ public class XML { public static String toString(Object object, String tagName) throws JSONException { StringBuilder sb = new StringBuilder(); - int i; JSONArray ja; JSONObject jo; String key; Iterator keys; - int length; String string; Object value; + if (object instanceof JSONObject) { // Emit @@ -417,12 +416,13 @@ public class XML { if ("content".equals(key)) { if (value instanceof JSONArray) { ja = (JSONArray)value; - length = ja.length(); - for (i = 0; i < length; i += 1) { + int i = 0; + for (Object val : ja) { if (i > 0) { sb.append('\n'); } - sb.append(escape(ja.get(i).toString())); + sb.append(escape(val.toString())); + i++; } } else { sb.append(escape(value.toString())); @@ -432,19 +432,17 @@ public class XML { } 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) { + for (Object val : ja) { + if (val instanceof JSONArray) { sb.append('<'); sb.append(key); sb.append('>'); - sb.append(toString(value)); + sb.append(toString(val)); sb.append("'); } else { - sb.append(toString(value, key)); + sb.append(toString(val, key)); } } } else if ("".equals(value)) { @@ -479,9 +477,8 @@ public class XML { 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)); + for (Object val : ja) { + sb.append(toString(val, tagName == null ? "array" : tagName)); } return sb.toString(); } From 105426b53f7f4f74fefa26e4d1321b633cc19628 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 26 Oct 2015 18:35:40 -0400 Subject: [PATCH 3/3] Formatting --- XML.java | 228 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 118 insertions(+), 110 deletions(-) diff --git a/XML.java b/XML.java index bad8737..dee7544 100755 --- a/XML.java +++ b/XML.java @@ -27,49 +27,54 @@ SOFTWARE. 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. + * 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 2015-10-18 */ +@SuppressWarnings("boxing") public class XML { /** The Character '&'. */ - public static final Character AMP = '&'; + public static final Character AMP = '&'; /** The Character '''. */ - public static final Character APOS = '\''; + public static final Character APOS = '\''; /** The Character '!'. */ - public static final Character BANG = '!'; + public static final Character BANG = '!'; /** The Character '='. */ - public static final Character EQ = '='; + public static final Character EQ = '='; /** The Character '>'. */ - public static final Character GT = '>'; + public static final Character GT = '>'; /** The Character '<'. */ - public static final Character LT = '<'; + public static final Character LT = '<'; /** The Character '?'. */ public static final Character QUEST = '?'; /** The Character '"'. */ - public static final Character QUOT = '"'; + public static final Character QUOT = '"'; /** The Character '/'. */ public static final Character SLASH = '/'; /** * Replace special characters with XML escapes: + * *
      * & (ampersand) is replaced by &amp;
      * < (less than) is replaced by &lt;
      * > (greater than) is replaced by &gt;
      * " (double quote) is replaced by &quot;
      * 
- * @param string The string to be escaped. + * + * @param string + * The string to be escaped. * @return The escaped string. */ public static String escape(String string) { @@ -100,9 +105,11 @@ public class XML { } /** - * Throw an exception if the string contains whitespace. - * Whitespace is not allowed in tagNames and attributes. - * @param string A string. + * Throw an exception if the string contains whitespace. Whitespace is not + * allowed in tagNames and attributes. + * + * @param string + * A string. * @throws JSONException */ public static void noSpace(String string) throws JSONException { @@ -112,42 +119,46 @@ public class XML { } for (i = 0; i < length; i += 1) { if (Character.isWhitespace(string.charAt(i))) { - throw new JSONException("'" + string + - "' contains a space character."); + 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. + * + * @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 { - char c; - int i; + private static boolean parse(XMLTokener x, JSONObject context, String name) + throws JSONException { + char c; + int i; JSONObject jsonobject = null; - String string; - String tagName; - Object token; + String string; + String tagName; + Object token; -// Test for and skip past these forms: -// -// -// -// -// Report errors for these forms: -// <> -// <= -// << + // Test for and skip past these forms: + // + // + // + // + // Report errors for these forms: + // <> + // <= + // << token = x.nextToken(); -// "); return false; } else if (token == SLASH) { -// Close tag } else if (token == SLASH) { + // Empty tag <.../> if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } @@ -248,9 +257,8 @@ public class XML { } return false; -// Content, between <...> and - } else if (token == GT) { + // Content, between <...> and for (;;) { token = x.nextContent(); if (token == null) { @@ -259,20 +267,19 @@ public class XML { } return false; } else if (token instanceof String) { - string = (String)token; + string = (String) token; if (string.length() > 0) { jsonobject.accumulate("content", XML.stringToValue(string)); } -// Nested element - } else if (token == LT) { + // Nested element 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 { @@ -289,14 +296,15 @@ public class XML { } } - /** * 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. + * + * @param string + * A String. * @return A simple JSON value. */ public static Object stringToValue(String string) { @@ -310,9 +318,8 @@ public class XML { 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. - + // 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')) { @@ -321,30 +328,31 @@ public class XML { return value; } } - } catch (Exception ignore) { + } catch (Exception ignore) { try { Double value = new Double(string); if (value.toString().equals(string)) { return value; } - } catch (Exception ignoreAlso) { + } catch (Exception ignoreAlso) { } } return string; } - /** * Convert a well-formed (but not necessarily valid) XML string into a - * JSONObject. Some information may be lost in this transformation - * because JSON is a data format and XML is a document format. XML uses - * elements, attributes, and content text, while JSON uses unordered - * collections of name/value pairs and arrays of values. JSON does not - * does not like to distinguish between elements and attributes. - * Sequences of similar elements are represented as JSONArrays. Content - * text may be placed in a "content" member. Comments, prologs, DTDs, and - * <[ [ ]]> are ignored. - * @param string The source string. + * JSONObject. Some information may be lost in this transformation because + * JSON is a data format and XML is a document format. XML uses elements, + * attributes, and content text, while JSON uses unordered collections of + * name/value pairs and arrays of values. JSON does not does not like to + * distinguish between elements and attributes. Sequences of similar + * elements are represented as JSONArrays. Content text may be placed in a + * "content" member. Comments, prologs, DTDs, and <[ [ ]]> + * are ignored. + * + * @param string + * The source string. * @return A JSONObject containing the structured data from the XML string. * @throws JSONException */ @@ -357,65 +365,64 @@ public class XML { return jo; } - /** * Convert a JSONObject into a well-formed, element-normal XML string. - * @param object A JSONObject. - * @return A string. - * @throws JSONException + * + * @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. + * + * @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(); - JSONArray ja; - JSONObject jo; - String key; - Iterator keys; - String string; - Object value; - + StringBuilder sb = new StringBuilder(); + JSONArray ja; + JSONObject jo; + String key; + Iterator keys; + String string; + Object value; + if (object instanceof JSONObject) { -// Emit - + // Emit if (tagName != null) { sb.append('<'); sb.append(tagName); sb.append('>'); } -// Loop thru the keys. - - jo = (JSONObject)object; + // Loop thru the keys. + jo = (JSONObject) object; keys = jo.keys(); while (keys.hasNext()) { key = keys.next(); value = jo.opt(key); if (value == null) { value = ""; - }else if(value.getClass().isArray()){ + } else if (value.getClass().isArray()) { value = new JSONArray(value); } - string = value instanceof String ? (String)value : null; - - -// Emit content in body + string = value instanceof String ? (String) value : null; + // Emit content in body if ("content".equals(key)) { if (value instanceof JSONArray) { - ja = (JSONArray)value; + ja = (JSONArray) value; int i = 0; for (Object val : ja) { if (i > 0) { @@ -428,10 +435,10 @@ public class XML { sb.append(escape(value.toString())); } -// Emit an array of similar keys + // Emit an array of similar keys } else if (value instanceof JSONArray) { - ja = (JSONArray)value; + ja = (JSONArray) value; for (Object val : ja) { if (val instanceof JSONArray) { sb.append('<'); @@ -450,7 +457,7 @@ public class XML { sb.append(key); sb.append("/>"); -// Emit a new tag + // Emit a new tag } else { sb.append(toString(value, key)); @@ -458,35 +465,36 @@ public class XML { } if (tagName != null) { -// Emit the close tag - + // Emit the close tag 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 element. - } - if(object!=null){ + + if (object != null) { if (object.getClass().isArray()) { object = new JSONArray(object); } - + if (object instanceof JSONArray) { - ja = (JSONArray)object; + ja = (JSONArray) object; for (Object val : ja) { + // XML does not have good support for arrays. If an array + // appears in a place where XML is lacking, synthesize an + // element. sb.append(toString(val, tagName == null ? "array" : tagName)); } return sb.toString(); } } + string = (object == null) ? "null" : escape(object.toString()); - return (tagName == null) ? "\"" + string + "\"" : - (string.length() == 0) ? "<" + tagName + "/>" : - "<" + tagName + ">" + string + ""; - + return (tagName == null) ? "\"" + string + "\"" + : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + + ">" + string + ""; + } }