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

whitespace

This commit is contained in:
Douglas Crockford 2012-11-13 14:39:02 -08:00
parent 216a4299f3
commit 96fceff148

View file

@ -1,365 +1,365 @@
package org.json; package org.json;
/* /*
Copyright (c) 2002 JSON.org Copyright (c) 2002 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
The Software shall be used for Good, not Evil. The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
/** /**
* The XMLTokener extends the JSONTokener to provide additional methods * The XMLTokener extends the JSONTokener to provide additional methods
* for the parsing of XML texts. * for the parsing of XML texts.
* @author JSON.org * @author JSON.org
* @version 2010-12-24 * @version 2012-11-13
*/ */
public class XMLTokener extends JSONTokener { public class XMLTokener extends JSONTokener {
/** The table of entity values. It initially contains Character values for /** The table of entity values. It initially contains Character values for
* amp, apos, gt, lt, quot. * amp, apos, gt, lt, quot.
*/ */
public static final java.util.HashMap entity; public static final java.util.HashMap entity;
static { static {
entity = new java.util.HashMap(8); entity = new java.util.HashMap(8);
entity.put("amp", XML.AMP); entity.put("amp", XML.AMP);
entity.put("apos", XML.APOS); entity.put("apos", XML.APOS);
entity.put("gt", XML.GT); entity.put("gt", XML.GT);
entity.put("lt", XML.LT); entity.put("lt", XML.LT);
entity.put("quot", XML.QUOT); entity.put("quot", XML.QUOT);
} }
/** /**
* Construct an XMLTokener from a string. * Construct an XMLTokener from a string.
* @param s A source string. * @param s A source string.
*/ */
public XMLTokener(String s) { public XMLTokener(String s) {
super(s); super(s);
} }
/** /**
* Get the text in the CDATA block. * Get the text in the CDATA block.
* @return The string up to the <code>]]&gt;</code>. * @return The string up to the <code>]]&gt;</code>.
* @throws JSONException If the <code>]]&gt;</code> is not found. * @throws JSONException If the <code>]]&gt;</code> is not found.
*/ */
public String nextCDATA() throws JSONException { public String nextCDATA() throws JSONException {
char c; char c;
int i; int i;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (;;) { for (;;) {
c = next(); c = next();
if (end()) { if (end()) {
throw syntaxError("Unclosed CDATA"); throw syntaxError("Unclosed CDATA");
} }
sb.append(c); sb.append(c);
i = sb.length() - 3; i = sb.length() - 3;
if (i >= 0 && sb.charAt(i) == ']' && if (i >= 0 && sb.charAt(i) == ']' &&
sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') { sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
sb.setLength(i); sb.setLength(i);
return sb.toString(); return sb.toString();
} }
} }
} }
/** /**
* Get the next XML outer token, trimming whitespace. There are two kinds * Get the next XML outer token, trimming whitespace. There are two kinds
* of tokens: the '<' character which begins a markup tag, and the content * of tokens: the '<' character which begins a markup tag, and the content
* text between markup tags. * text between markup tags.
* *
* @return A string, or a '<' Character, or null if there is no more * @return A string, or a '<' Character, or null if there is no more
* source text. * source text.
* @throws JSONException * @throws JSONException
*/ */
public Object nextContent() throws JSONException { public Object nextContent() throws JSONException {
char c; char c;
StringBuffer sb; StringBuffer sb;
do { do {
c = next(); c = next();
} while (Character.isWhitespace(c)); } while (Character.isWhitespace(c));
if (c == 0) { if (c == 0) {
return null; return null;
} }
if (c == '<') { if (c == '<') {
return XML.LT; return XML.LT;
} }
sb = new StringBuffer(); sb = new StringBuffer();
for (;;) { for (;;) {
if (c == '<' || c == 0) { if (c == '<' || c == 0) {
back(); back();
return sb.toString().trim(); return sb.toString().trim();
} }
if (c == '&') { if (c == '&') {
sb.append(nextEntity(c)); sb.append(nextEntity(c));
} else { } else {
sb.append(c); sb.append(c);
} }
c = next(); c = next();
} }
} }
/** /**
* Return the next entity. These entities are translated to Characters: * Return the next entity. These entities are translated to Characters:
* <code>&amp; &apos; &gt; &lt; &quot;</code>. * <code>&amp; &apos; &gt; &lt; &quot;</code>.
* @param ampersand An ampersand character. * @param ampersand An ampersand character.
* @return A Character or an entity String if the entity is not recognized. * @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity. * @throws JSONException If missing ';' in XML entity.
*/ */
public Object nextEntity(char ampersand) throws JSONException { public Object nextEntity(char ampersand) throws JSONException {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (;;) { for (;;) {
char c = next(); char c = next();
if (Character.isLetterOrDigit(c) || c == '#') { if (Character.isLetterOrDigit(c) || c == '#') {
sb.append(Character.toLowerCase(c)); sb.append(Character.toLowerCase(c));
} else if (c == ';') { } else if (c == ';') {
break; break;
} else { } else {
throw syntaxError("Missing ';' in XML entity: &" + sb); throw syntaxError("Missing ';' in XML entity: &" + sb);
} }
} }
String string = sb.toString(); String string = sb.toString();
Object object = entity.get(string); Object object = entity.get(string);
return object != null ? object : ampersand + string + ";"; return object != null ? object : ampersand + string + ";";
} }
/** /**
* Returns the next XML meta token. This is used for skipping over <!...> * Returns the next XML meta token. This is used for skipping over <!...>
* and <?...?> structures. * and <?...?> structures.
* @return Syntax characters (<code>< > / = ! ?</code>) are returned as * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
* Character, and strings and names are returned as Boolean. We don't care * Character, and strings and names are returned as Boolean. We don't care
* what the values actually are. * what the values actually are.
* @throws JSONException If a string is not properly closed or if the XML * @throws JSONException If a string is not properly closed or if the XML
* is badly structured. * is badly structured.
*/ */
public Object nextMeta() throws JSONException { public Object nextMeta() throws JSONException {
char c; char c;
char q; char q;
do { do {
c = next(); c = next();
} while (Character.isWhitespace(c)); } while (Character.isWhitespace(c));
switch (c) { switch (c) {
case 0: case 0:
throw syntaxError("Misshaped meta tag"); throw syntaxError("Misshaped meta tag");
case '<': case '<':
return XML.LT; return XML.LT;
case '>': case '>':
return XML.GT; return XML.GT;
case '/': case '/':
return XML.SLASH; return XML.SLASH;
case '=': case '=':
return XML.EQ; return XML.EQ;
case '!': case '!':
return XML.BANG; return XML.BANG;
case '?': case '?':
return XML.QUEST; return XML.QUEST;
case '"': case '"':
case '\'': case '\'':
q = c; q = c;
for (;;) { for (;;) {
c = next(); c = next();
if (c == 0) { if (c == 0) {
throw syntaxError("Unterminated string"); throw syntaxError("Unterminated string");
} }
if (c == q) { if (c == q) {
return Boolean.TRUE; return Boolean.TRUE;
} }
} }
default: default:
for (;;) { for (;;) {
c = next(); c = next();
if (Character.isWhitespace(c)) { if (Character.isWhitespace(c)) {
return Boolean.TRUE; return Boolean.TRUE;
} }
switch (c) { switch (c) {
case 0: case 0:
case '<': case '<':
case '>': case '>':
case '/': case '/':
case '=': case '=':
case '!': case '!':
case '?': case '?':
case '"': case '"':
case '\'': case '\'':
back(); back();
return Boolean.TRUE; return Boolean.TRUE;
} }
} }
} }
} }
/** /**
* Get the next XML Token. These tokens are found inside of angle * Get the next XML Token. These tokens are found inside of angle
* brackets. It may be one of these characters: <code>/ > = ! ?</code> or it * 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 * may be a string wrapped in single quotes or double quotes, or it may be a
* name. * name.
* @return a String or a Character. * @return a String or a Character.
* @throws JSONException If the XML is not well formed. * @throws JSONException If the XML is not well formed.
*/ */
public Object nextToken() throws JSONException { public Object nextToken() throws JSONException {
char c; char c;
char q; char q;
StringBuffer sb; StringBuffer sb;
do { do {
c = next(); c = next();
} while (Character.isWhitespace(c)); } while (Character.isWhitespace(c));
switch (c) { switch (c) {
case 0: case 0:
throw syntaxError("Misshaped element"); throw syntaxError("Misshaped element");
case '<': case '<':
throw syntaxError("Misplaced '<'"); throw syntaxError("Misplaced '<'");
case '>': case '>':
return XML.GT; return XML.GT;
case '/': case '/':
return XML.SLASH; return XML.SLASH;
case '=': case '=':
return XML.EQ; return XML.EQ;
case '!': case '!':
return XML.BANG; return XML.BANG;
case '?': case '?':
return XML.QUEST; return XML.QUEST;
// Quoted string // Quoted string
case '"': case '"':
case '\'': case '\'':
q = c; q = c;
sb = new StringBuffer(); sb = new StringBuffer();
for (;;) { for (;;) {
c = next(); c = next();
if (c == 0) { if (c == 0) {
throw syntaxError("Unterminated string"); throw syntaxError("Unterminated string");
} }
if (c == q) { if (c == q) {
return sb.toString(); return sb.toString();
} }
if (c == '&') { if (c == '&') {
sb.append(nextEntity(c)); sb.append(nextEntity(c));
} else { } else {
sb.append(c); sb.append(c);
} }
} }
default: default:
// Name // Name
sb = new StringBuffer(); sb = new StringBuffer();
for (;;) { for (;;) {
sb.append(c); sb.append(c);
c = next(); c = next();
if (Character.isWhitespace(c)) { if (Character.isWhitespace(c)) {
return sb.toString(); return sb.toString();
} }
switch (c) { switch (c) {
case 0: case 0:
return sb.toString(); return sb.toString();
case '>': case '>':
case '/': case '/':
case '=': case '=':
case '!': case '!':
case '?': case '?':
case '[': case '[':
case ']': case ']':
back(); back();
return sb.toString(); return sb.toString();
case '<': case '<':
case '"': case '"':
case '\'': case '\'':
throw syntaxError("Bad character in a name"); throw syntaxError("Bad character in a name");
} }
} }
} }
} }
/** /**
* Skip characters until past the requested string. * 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. * 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. * @param to A string to skip past.
* @throws JSONException * @throws JSONException
*/ */
public boolean skipPast(String to) throws JSONException { public boolean skipPast(String to) throws JSONException {
boolean b; boolean b;
char c; char c;
int i; int i;
int j; int j;
int offset = 0; int offset = 0;
int length = to.length(); int length = to.length();
char[] circle = new char[length]; char[] circle = new char[length];
/* /*
* First fill the circle buffer with as many characters as are in the * First fill the circle buffer with as many characters as are in the
* to string. If we reach an early end, bail. * to string. If we reach an early end, bail.
*/ */
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1) {
c = next(); c = next();
if (c == 0) { if (c == 0) {
return false; return false;
} }
circle[i] = c; circle[i] = c;
} }
/*
* We will loop, possibly for all of the remaining characters. /* We will loop, possibly for all of the remaining characters. */
*/
for (;;) { for (;;) {
j = offset; j = offset;
b = true; b = true;
/*
* Compare the circle buffer with the to string. /* Compare the circle buffer with the to string. */
*/
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1) {
if (circle[j] != to.charAt(i)) { if (circle[j] != to.charAt(i)) {
b = false; b = false;
break; break;
} }
j += 1; j += 1;
if (j >= length) { if (j >= length) {
j -= length; j -= length;
} }
} }
/*
* If we exit the loop with b intact, then victory is ours. /* If we exit the loop with b intact, then victory is ours. */
*/
if (b) { if (b) {
return true; return true;
} }
/*
* Get the next character. If there isn't one, then defeat is ours. /* Get the next character. If there isn't one, then defeat is ours. */
*/
c = next(); c = next();
if (c == 0) { if (c == 0) {
return false; return false;
} }
/* /*
* Shove the character in the circle buffer and advance the * Shove the character in the circle buffer and advance the
* circle offset. The offset is mod n. * circle offset. The offset is mod n.
*/ */
circle[offset] = c; circle[offset] = c;
offset += 1; offset += 1;
if (offset >= length) { if (offset >= length) {
offset -= length; offset -= length;
} }
} }
} }
} }