From 9420bb7e8bbaf36d3d364cb7bb11449e8c3bbeed Mon Sep 17 00:00:00 2001 From: ajayk Date: Fri, 13 Jan 2012 13:10:10 +0530 Subject: [PATCH] AjayKemparaj: Tabs to spaces and minor java code fixups --- CDL.java | 90 ++++---- HTTP.java | 6 +- JSONException.java | 4 +- JSONML.java | 506 ++++++++++++++++++++++----------------------- JSONObject.java | 4 +- JSONString.java | 14 +- JSONTokener.java | 64 +++--- XML.java | 70 +++---- XMLTokener.java | 112 +++++----- 9 files changed, 435 insertions(+), 435 deletions(-) diff --git a/CDL.java b/CDL.java index a6b1787..fd5d476 100755 --- a/CDL.java +++ b/CDL.java @@ -64,18 +64,18 @@ public class CDL { return null; case '"': case '\'': - q = c; - sb = new StringBuffer(); - for (;;) { - c = x.next(); - if (c == q) { - break; - } + q = c; + sb = new StringBuffer(); + for (;;) { + c = x.next(); + if (c == q) { + break; + } if (c == 0 || c == '\n' || c == '\r') { throw x.syntaxError("Missing close quote '" + q + "'."); } sb.append(c); - } + } return sb.toString(); case ',': x.back(); @@ -98,7 +98,7 @@ public class CDL { String value = getValue(x); char c = x.next(); if (value == null || - (ja.length() == 0 && value.length() == 0 && c != ',')) { + (ja.length() == 0 && value.length() == 0 && c != ',')) { return null; } ja.put(value); @@ -135,43 +135,43 @@ public class CDL { } /** - * Produce a comma delimited text row from a JSONArray. Values containing - * the comma character will be quoted. Troublesome characters may be - * removed. - * @param ja A JSONArray of strings. - * @return A string ending in NEWLINE. - */ - public static String rowToString(JSONArray ja) { - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < ja.length(); i += 1) { - if (i > 0) { - sb.append(','); - } - Object object = ja.opt(i); - if (object != null) { - String string = object.toString(); - if (string.length() > 0 && (string.indexOf(',') >= 0 || - string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 || - string.indexOf(0) >= 0 || string.charAt(0) == '"')) { - sb.append('"'); - int length = string.length(); - for (int j = 0; j < length; j += 1) { - char c = string.charAt(j); - if (c >= ' ' && c != '"') { - sb.append(c); - } - } - sb.append('"'); - } else { - sb.append(string); - } - } - } - sb.append('\n'); - return sb.toString(); - } + * Produce a comma delimited text row from a JSONArray. Values containing + * the comma character will be quoted. Troublesome characters may be + * removed. + * @param ja A JSONArray of strings. + * @return A string ending in NEWLINE. + */ + public static String rowToString(JSONArray ja) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < ja.length(); i += 1) { + if (i > 0) { + sb.append(','); + } + Object object = ja.opt(i); + if (object != null) { + String string = object.toString(); + if (string.length() > 0 && (string.indexOf(',') >= 0 || + string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 || + string.indexOf(0) >= 0 || string.charAt(0) == '"')) { + sb.append('"'); + int length = string.length(); + for (int j = 0; j < length; j += 1) { + char c = string.charAt(j); + if (c >= ' ' && c != '"') { + sb.append(c); + } + } + sb.append('"'); + } else { + sb.append(string); + } + } + } + sb.append('\n'); + return sb.toString(); + } - /** + /** * Produce a JSONArray of JSONObjects from a comma delimited text string, * using the first row as a source of names. * @param string The comma delimited text. diff --git a/HTTP.java b/HTTP.java index 0ce7a21..43d04a8 100755 --- a/HTTP.java +++ b/HTTP.java @@ -148,9 +148,9 @@ public class HTTP { sb.append(CRLF); while (keys.hasNext()) { string = keys.next().toString(); - if (!string.equals("HTTP-Version") && !string.equals("Status-Code") && - !string.equals("Reason-Phrase") && !string.equals("Method") && - !string.equals("Request-URI") && !jo.isNull(string)) { + if (!"HTTP-Version".equals(string) && !"Status-Code".equals(string) && + !"Reason-Phrase".equals(string) && !"Method".equals(string) && + !"Request-URI".equals(string) && !jo.isNull(string)) { sb.append(string); sb.append(": "); sb.append(jo.getString(string)); diff --git a/JSONException.java b/JSONException.java index b498d4b..289b602 100755 --- a/JSONException.java +++ b/JSONException.java @@ -6,8 +6,8 @@ package org.json; * @version 2010-12-24 */ public class JSONException extends Exception { - private static final long serialVersionUID = 0; - private Throwable cause; + private static final long serialVersionUID = 0; + private Throwable cause; /** * Constructs a JSONException with an explanatory message. diff --git a/JSONML.java b/JSONML.java index c329179..d20a9c3 100755 --- a/JSONML.java +++ b/JSONML.java @@ -48,16 +48,16 @@ public class JSONML { private static Object parse( XMLTokener x, boolean arrayForm, - JSONArray ja + JSONArray ja ) throws JSONException { String attribute; char c; - String closeTag = null; + String closeTag = null; int i; JSONArray newja = null; JSONObject newjo = null; Object token; - String tagName = null; + String tagName = null; // Test for and skip past these forms: // @@ -69,166 +69,166 @@ public class JSONML { if (!x.more()) { throw x.syntaxError("Bad XML"); } - token = x.nextContent(); - if (token == XML.LT) { - token = x.nextToken(); - if (token instanceof Character) { - if (token == XML.SLASH) { + token = x.nextContent(); + if (token == XML.LT) { + token = x.nextToken(); + if (token instanceof Character) { + if (token == XML.SLASH) { // Close tag "); - } - x.back(); - } else if (c == '[') { - token = x.nextToken(); - if (token.equals("CDATA") && x.next() == '[') { - if (ja != null) { - ja.put(x.nextCDATA()); - } - } else { - throw x.syntaxError("Expected 'CDATA['"); - } - } else { - i = 1; - do { - token = x.nextMeta(); - if (token == null) { - throw x.syntaxError("Missing '>' after ' 0); - } - } else if (token == XML.QUEST) { + c = x.next(); + if (c == '-') { + if (x.next() == '-') { + x.skipPast("-->"); + } + x.back(); + } else if (c == '[') { + token = x.nextToken(); + if (token.equals("CDATA") && x.next() == '[') { + if (ja != null) { + ja.put(x.nextCDATA()); + } + } else { + throw x.syntaxError("Expected 'CDATA['"); + } + } else { + i = 1; + do { + token = x.nextMeta(); + if (token == null) { + throw x.syntaxError("Missing '>' after ' 0); + } + } else if (token == XML.QUEST) { // "); - } else { - throw x.syntaxError("Misshaped tag"); - } + x.skipPast("?>"); + } else { + throw x.syntaxError("Misshaped tag"); + } // Open tag < - } else { - if (!(token instanceof String)) { - throw x.syntaxError("Bad tagName '" + token + "'."); - } - tagName = (String)token; - newja = new JSONArray(); - newjo = new JSONObject(); - if (arrayForm) { - newja.put(tagName); - if (ja != null) { - ja.put(newja); - } - } else { - newjo.put("tagName", tagName); - if (ja != null) { - ja.put(newjo); - } - } - token = null; - for (;;) { - if (token == null) { - token = x.nextToken(); - } - if (token == null) { - throw x.syntaxError("Misshaped tag"); - } - if (!(token instanceof String)) { - break; - } + } else { + if (!(token instanceof String)) { + throw x.syntaxError("Bad tagName '" + token + "'."); + } + tagName = (String)token; + newja = new JSONArray(); + newjo = new JSONObject(); + if (arrayForm) { + newja.put(tagName); + if (ja != null) { + ja.put(newja); + } + } else { + newjo.put("tagName", tagName); + if (ja != null) { + ja.put(newjo); + } + } + token = null; + for (;;) { + if (token == null) { + token = x.nextToken(); + } + if (token == null) { + throw x.syntaxError("Misshaped tag"); + } + if (!(token instanceof String)) { + break; + } // attribute = value - attribute = (String)token; - if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) { + attribute = (String)token; + if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) { throw x.syntaxError("Reserved attribute."); - } - token = x.nextToken(); - if (token == XML.EQ) { - token = x.nextToken(); - if (!(token instanceof String)) { - throw x.syntaxError("Missing value"); - } - newjo.accumulate(attribute, XML.stringToValue((String)token)); - token = null; - } else { - newjo.accumulate(attribute, ""); - } - } + } + token = x.nextToken(); + if (token == XML.EQ) { + token = x.nextToken(); + if (!(token instanceof String)) { + throw x.syntaxError("Missing value"); + } + newjo.accumulate(attribute, XML.stringToValue((String)token)); + token = null; + } else { + newjo.accumulate(attribute, ""); + } + } if (arrayForm && newjo.length() > 0) { - newja.put(newjo); + newja.put(newjo); } // Empty tag <.../> - if (token == XML.SLASH) { - if (x.nextToken() != XML.GT) { - throw x.syntaxError("Misshaped tag"); - } - if (ja == null) { - if (arrayForm) { - return newja; - } else { - return newjo; - } - } + if (token == XML.SLASH) { + if (x.nextToken() != XML.GT) { + throw x.syntaxError("Misshaped tag"); + } + if (ja == null) { + if (arrayForm) { + return newja; + } else { + return newjo; + } + } // Content, between <...> and - } else { - if (token != XML.GT) { - throw x.syntaxError("Misshaped tag"); - } - closeTag = (String)parse(x, arrayForm, newja); - if (closeTag != null) { - if (!closeTag.equals(tagName)) { - throw x.syntaxError("Mismatched '" + tagName + - "' and '" + closeTag + "'"); - } - tagName = null; - if (!arrayForm && newja.length() > 0) { - newjo.put("childNodes", newja); - } - if (ja == null) { - if (arrayForm) { - return newja; - } else { - return newjo; - } - } - } - } - } - } else { - if (ja != null) { - ja.put(token instanceof String - ? XML.stringToValue((String)token) - : token); - } - } + } else { + if (token != XML.GT) { + throw x.syntaxError("Misshaped tag"); + } + closeTag = (String)parse(x, arrayForm, newja); + if (closeTag != null) { + if (!closeTag.equals(tagName)) { + throw x.syntaxError("Mismatched '" + tagName + + "' and '" + closeTag + "'"); + } + tagName = null; + if (!arrayForm && newja.length() > 0) { + newjo.put("childNodes", newja); + } + if (ja == null) { + if (arrayForm) { + return newja; + } else { + return newjo; + } + } + } + } + } + } else { + if (ja != null) { + ja.put(token instanceof String + ? XML.stringToValue((String)token) + : token); + } + } } } @@ -246,7 +246,7 @@ public class JSONML { * @throws JSONException */ public static JSONArray toJSONArray(String string) throws JSONException { - return toJSONArray(new XMLTokener(string)); + return toJSONArray(new XMLTokener(string)); } @@ -263,7 +263,7 @@ public class JSONML { * @throws JSONException */ public static JSONArray toJSONArray(XMLTokener x) throws JSONException { - return (JSONArray)parse(x, true, null); + return (JSONArray)parse(x, true, null); } @@ -281,7 +281,7 @@ public class JSONML { * @throws JSONException */ public static JSONObject toJSONObject(XMLTokener x) throws JSONException { - return (JSONObject)parse(x, false, null); + return (JSONObject)parse(x, false, null); } @@ -299,7 +299,7 @@ public class JSONML { * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { - return toJSONObject(new XMLTokener(string)); + return toJSONObject(new XMLTokener(string)); } @@ -310,75 +310,75 @@ public class JSONML { * @throws JSONException */ public static String toString(JSONArray ja) throws JSONException { - int i; - JSONObject jo; - String key; - Iterator keys; - int length; - Object object; - StringBuffer sb = new StringBuffer(); - String tagName; - String value; + int i; + JSONObject jo; + String key; + Iterator keys; + int length; + Object object; + StringBuffer sb = new StringBuffer(); + String tagName; + String value; // Emit = length) { - sb.append('/'); - sb.append('>'); - } else { - sb.append('>'); - do { - object = ja.get(i); - i += 1; - if (object != null) { - if (object instanceof String) { - sb.append(XML.escape(object.toString())); - } else if (object instanceof JSONObject) { - sb.append(toString((JSONObject)object)); - } else if (object instanceof JSONArray) { - sb.append(toString((JSONArray)object)); - } - } - } while (i < length); - sb.append('<'); - sb.append('/'); - sb.append(tagName); - sb.append('>'); - } + length = ja.length(); + if (i >= length) { + sb.append('/'); + sb.append('>'); + } else { + sb.append('>'); + do { + object = ja.get(i); + i += 1; + if (object != null) { + if (object instanceof String) { + sb.append(XML.escape(object.toString())); + } else if (object instanceof JSONObject) { + sb.append(toString((JSONObject)object)); + } else if (object instanceof JSONArray) { + sb.append(toString((JSONArray)object)); + } + } + } while (i < length); + sb.append('<'); + sb.append('/'); + sb.append(tagName); + sb.append('>'); + } return sb.toString(); } @@ -391,75 +391,75 @@ public class JSONML { * @return An XML string. * @throws JSONException */ - public static String toString(JSONObject jo) throws JSONException { - StringBuffer sb = new StringBuffer(); - int i; - JSONArray ja; - String key; - Iterator keys; - int length; - Object object; - String tagName; - String value; + public static String toString(JSONObject jo) throws JSONException { + StringBuffer sb = new StringBuffer(); + int i; + JSONArray ja; + String key; + Iterator keys; + int length; + Object object; + String tagName; + String value; //Emit '); - } else { - sb.append('>'); - length = ja.length(); - for (i = 0; i < length; i += 1) { - object = ja.get(i); - if (object != null) { - if (object instanceof String) { - sb.append(XML.escape(object.toString())); + ja = jo.optJSONArray("childNodes"); + if (ja == null) { + sb.append('/'); + sb.append('>'); + } else { + sb.append('>'); + length = ja.length(); + for (i = 0; i < length; i += 1) { + object = ja.get(i); + if (object != null) { + if (object instanceof String) { + sb.append(XML.escape(object.toString())); } else if (object instanceof JSONObject) { sb.append(toString((JSONObject)object)); - } else if (object instanceof JSONArray) { - sb.append(toString((JSONArray)object)); + } else if (object instanceof JSONArray) { + sb.append(toString((JSONArray)object)); } else { sb.append(object.toString()); - } - } - } - sb.append('<'); - sb.append('/'); - sb.append(tagName); - sb.append('>'); - } + } + } + } + sb.append('<'); + sb.append('/'); + sb.append(tagName); + sb.append('>'); + } return sb.toString(); } } \ No newline at end of file diff --git a/JSONObject.java b/JSONObject.java index c4ad550..f8ee359 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -967,8 +967,8 @@ public class JSONObject { String name = method.getName(); String key = ""; if (name.startsWith("get")) { - if (name.equals("getClass") || - name.equals("getDeclaringClass")) { + if ("getClass".equals(name) || + "getDeclaringClass".equals(name)) { key = ""; } else { key = name.substring(3); diff --git a/JSONString.java b/JSONString.java index 17f4384..41726b1 100755 --- a/JSONString.java +++ b/JSONString.java @@ -8,11 +8,11 @@ package org.json; * of using the Object's toString() method and quoting the result. */ public interface JSONString { - /** - * The toJSONString method allows a class to produce its own JSON - * serialization. - * - * @return A strictly syntactically correct JSON text. - */ - public String toJSONString(); + /** + * The toJSONString method allows a class to produce its own JSON + * serialization. + * + * @return A strictly syntactically correct JSON text. + */ + public String toJSONString(); } diff --git a/JSONTokener.java b/JSONTokener.java index 31525b5..f323f6e 100755 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -40,12 +40,12 @@ SOFTWARE. */ public class JSONTokener { - private int character; - private boolean eof; - private int index; - private int line; - private char previous; - private final Reader reader; + private int character; + private boolean eof; + private int index; + private int line; + private char previous; + private final Reader reader; private boolean usePrevious; @@ -121,7 +121,7 @@ public class JSONTokener { } public boolean end() { - return this.eof && !this.usePrevious; + return this.eof && !this.usePrevious; } @@ -148,31 +148,31 @@ public class JSONTokener { public char next() throws JSONException { int c; if (this.usePrevious) { - this.usePrevious = false; + this.usePrevious = false; c = this.previous; } else { - try { - c = this.reader.read(); - } catch (IOException exception) { - throw new JSONException(exception); - } + try { + c = this.reader.read(); + } catch (IOException exception) { + throw new JSONException(exception); + } - if (c <= 0) { // End of stream - this.eof = true; - c = 0; - } + if (c <= 0) { // End of stream + this.eof = true; + c = 0; + } } - this.index += 1; - if (this.previous == '\r') { - this.line += 1; - this.character = c == '\n' ? 0 : 1; - } else if (c == '\n') { - this.line += 1; - this.character = 0; - } else { - this.character += 1; - } - this.previous = (char) c; + this.index += 1; + if (this.previous == '\r') { + this.line += 1; + this.character = c == '\n' ? 0 : 1; + } else if (c == '\n') { + this.line += 1; + this.character = 0; + } else { + this.character += 1; + } + this.previous = (char) c; return this.previous; } @@ -283,8 +283,8 @@ public class JSONTokener { case '\'': case '\\': case '/': - sb.append(c); - break; + sb.append(c); + break; default: throw this.syntaxError("Illegal escape."); } @@ -383,7 +383,7 @@ public class JSONTokener { this.back(); string = sb.toString().trim(); - if (string.equals("")) { + if ("".equals(string)) { throw this.syntaxError("Missing value"); } return JSONObject.stringToValue(string); @@ -441,6 +441,6 @@ public class JSONTokener { */ public String toString() { return " at " + this.index + " [character " + this.character + " line " + - this.line + "]"; + this.line + "]"; } } \ No newline at end of file diff --git a/XML.java b/XML.java index e328191..82455b3 100755 --- a/XML.java +++ b/XML.java @@ -107,16 +107,16 @@ public class XML { * @throws JSONException */ public static void noSpace(String string) throws JSONException { - int i, length = string.length(); - if (length == 0) { - throw new JSONException("Empty string."); - } - for (i = 0; i < length; i += 1) { - if (Character.isWhitespace(string.charAt(i))) { - throw new JSONException("'" + string + - "' contains a space character."); - } - } + int i, length = string.length(); + if (length == 0) { + throw new JSONException("Empty string."); + } + for (i = 0; i < length; i += 1) { + if (Character.isWhitespace(string.charAt(i))) { + throw new JSONException("'" + string + + "' contains a space character."); + } + } } /** @@ -160,7 +160,7 @@ public class XML { x.back(); } else if (c == '[') { token = x.nextToken(); - if (token.equals("CDATA")) { + if ("CDATA".equals(token)) { if (x.next() == '[') { string = x.nextCDATA(); if (string.length() > 0) { @@ -193,7 +193,7 @@ public class XML { // Close tag 0) { context.accumulate(tagName, jsonobject); } else { - context.accumulate(tagName, ""); + context.accumulate(tagName, ""); } return false; @@ -263,7 +263,7 @@ public class XML { string = (String)token; if (string.length() > 0) { jsonobject.accumulate("content", - XML.stringToValue(string)); + XML.stringToValue(string)); } // Nested element @@ -275,7 +275,7 @@ public class XML { } else if (jsonobject.length() == 1 && jsonobject.opt("content") != null) { context.accumulate(tagName, - jsonobject.opt("content")); + jsonobject.opt("content")); } else { context.accumulate(tagName, jsonobject); } @@ -301,19 +301,19 @@ public class XML { * @return A simple JSON value. */ public static Object stringToValue(String string) { - if (string.equals("")) { + if ("".equals(string)) { return string; } - if (string.equalsIgnoreCase("true")) { + if ("true".equalsIgnoreCase(string)) { return Boolean.TRUE; } - if (string.equalsIgnoreCase("false")) { + if ("false".equalsIgnoreCase(string)) { return Boolean.FALSE; } - if (string.equalsIgnoreCase("null")) { + if ("null".equalsIgnoreCase(string)) { return JSONObject.NULL; } - if (string.equals("0")) { + if ("0".equals(string)) { return new Integer(0); } @@ -321,16 +321,16 @@ public class XML { // return the string. try { - char initial = string.charAt(0); - boolean negative = false; - if (initial == '-') { - initial = string.charAt(1); - negative = true; - } - if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') { - return string; - } - if ((initial >= '0' && initial <= '9')) { + char initial = string.charAt(0); + boolean negative = false; + if (initial == '-') { + initial = string.charAt(1); + negative = true; + } + if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') { + return string; + } + if ((initial >= '0' && initial <= '9')) { if (string.indexOf('.') >= 0) { return Double.valueOf(string); } else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) { @@ -341,7 +341,7 @@ public class XML { return myLong; } } - } + } } catch (Exception ignore) { } return string; @@ -419,7 +419,7 @@ public class XML { key = keys.next().toString(); value = jo.opt(key); if (value == null) { - value = ""; + value = ""; } if (value instanceof String) { string = (String)value; @@ -429,7 +429,7 @@ public class XML { // Emit content in body - if (key.equals("content")) { + if ("content".equals(key)) { if (value instanceof JSONArray) { ja = (JSONArray)value; length = ja.length(); @@ -462,7 +462,7 @@ public class XML { sb.append(toString(value, key)); } } - } else if (value.equals("")) { + } else if ("".equals(value)) { sb.append('<'); sb.append(key); sb.append("/>"); diff --git a/XMLTokener.java b/XMLTokener.java index 8762eae..07574b0 100755 --- a/XMLTokener.java +++ b/XMLTokener.java @@ -272,7 +272,7 @@ public class XMLTokener extends JSONTokener { } switch (c) { case 0: - return sb.toString(); + return sb.toString(); case '>': case '/': case '=': @@ -299,12 +299,12 @@ public class XMLTokener extends JSONTokener { * @throws JSONException */ public boolean skipPast(String to) throws JSONException { - boolean b; - char c; - int i; - int j; - int offset = 0; - int length = to.length(); + boolean b; + char c; + int i; + int j; + int offset = 0; + int length = to.length(); char[] circle = new char[length]; /* @@ -312,54 +312,54 @@ public class XMLTokener extends JSONTokener { * 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 (;;) { - j = offset; - b = true; - /* - * Compare the circle buffer with the to string. - */ - for (i = 0; i < length; i += 1) { - if (circle[j] != to.charAt(i)) { - 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; - } - } + 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 (;;) { + j = offset; + b = true; + /* + * Compare the circle buffer with the to string. + */ + for (i = 0; i < length; i += 1) { + if (circle[j] != to.charAt(i)) { + 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; + } + } } }