/** * Determine if the source string still contains characters that next() * can consume.<p> * * @return true if not yet at the end of the source * @throws JSONException if something goes wrong */ public boolean more() throws JSONException { char nextChar = next(); if (nextChar == 0) { return false; } back(); return true; }
/** * Get the next value. The value can be wrapped in quotes. The value can * be empty.<p> * * @param x A JSONTokener of the source text * @return The value string, or null if empty * @throws JSONException if the quoted string is badly formed */ private static String getValue(JSONTokener x) throws JSONException { char c; do { c = x.next(); } while ((c == ' ') || (c == '\t')); switch (c) { case 0: return null; case '"': case '\'': return x.nextString(c); case ',': x.back(); return ""; default: x.back(); return x.nextTo(','); } } }
if (x.next() != '>') { x.back();
/** * Get the next value. The value can be wrapped in quotes. The value can * be empty.<p> * * @param x A JSONTokener of the source text * @return The value string, or null if empty * @throws JSONException if the quoted string is badly formed */ private static String getValue(JSONTokener x) throws JSONException { char c; do { c = x.next(); } while (c == ' ' || c == '\t'); switch (c) { case 0: return null; case '"': case '\'': return x.nextString(c); case ',': x.back(); return ""; default: x.back(); return x.nextTo(','); } } }
if (x.next() != '>') { x.back();
/** * Consume the next character, and check that it matches a specified * character.<p> * * @param c the character to match * @return the character * @throws JSONException if the character does not match */ public char next(char c) throws JSONException { char n = next(); if (n != c) { throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'"); } return n; }
/** * Determine if the source string still contains characters that next() * can consume.<p> * * @return true if not yet at the end of the source * @throws JSONException if something goes wrong */ public boolean more() throws JSONException { char nextChar = next(); if (nextChar == 0) { return false; } back(); return true; }
/** * Consume the next character, and check that it matches a specified * character.<p> * * @param c the character to match * @return the character * @throws JSONException if the character does not match */ public char next(char c) throws JSONException { char n = next(); if (n != c) { throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'"); } return n; }
char c = next(); if (c == '/') { switch (next()) { case '/': do { c = next(); } while ((c != '\n') && (c != '\r') && (c != 0)); break; case '*': for (;;) { c = next(); if (c == 0) { throw syntaxError("Unclosed comment"); if (next() == '/') { break; c = next(); } while ((c != '\n') && (c != '\r') && (c != 0)); } else if ((c == 0) || (c > ' ')) {
/** * Get the text up but not including one of the specified delimiter * characters or the end of line, whichever comes first.<p> * * @param delimiters a set of delimiter characters * @return a string, trimmed * @throws JSONException if something goes wrong */ public String nextTo(String delimiters) throws JSONException { char c; StringBuffer sb = new StringBuffer(); for (;;) { c = next(); if ((delimiters.indexOf(c) >= 0) || (c == 0) || (c == '\n') || (c == '\r')) { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
char c = next(); if (c == '/') { switch (next()) { case '/': do { c = next(); } while ((c != '\n') && (c != '\r') && (c != 0)); break; case '*': for (;;) { c = next(); if (c == 0) { throw syntaxError("Unclosed comment"); if (next() == '/') { break; c = next(); } while ((c != '\n') && (c != '\r') && (c != 0)); } else if ((c == 0) || (c > ' ')) {
/** * Get the text up but not including the specified character or the * end of line, whichever comes first.<p> * * @param d a delimiter character * @return a string * @throws JSONException if something goes wrong */ public String nextTo(char d) throws JSONException { StringBuffer sb = new StringBuffer(); for (;;) { char c = next(); if ((c == d) || (c == 0) || (c == '\n') || (c == '\r')) { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
/** * Get the text up but not including the specified character or the * end of line, whichever comes first.<p> * * @param d a delimiter character * @return a string * @throws JSONException if something goes wrong */ public String nextTo(char d) throws JSONException { StringBuffer sb = new StringBuffer(); for (;;) { char c = next(); if ((c == d) || (c == 0) || (c == '\n') || (c == '\r')) { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
/** * Get the text up but not including one of the specified delimiter * characters or the end of line, whichever comes first.<p> * * @param delimiters a set of delimiter characters * @return a string, trimmed * @throws JSONException if something goes wrong */ public String nextTo(String delimiters) throws JSONException { char c; StringBuffer sb = new StringBuffer(); for (;;) { c = next(); if ((delimiters.indexOf(c) >= 0) || (c == 0) || (c == '\n') || (c == '\r')) { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
StringBuffer sb = new StringBuffer(); for (;;) { c = next(); switch (c) { case 0: throw syntaxError("Unterminated string"); case '\\': c = next(); switch (c) { case 'b': break; case 'u': sb.append((char)Integer.parseInt(next(4), 16)); break; case 'x': sb.append((char)Integer.parseInt(next(2), 16)); break; default:
StringBuffer sb = new StringBuffer(); for (;;) { c = next(); switch (c) { case 0: throw syntaxError("Unterminated string"); case '\\': c = next(); switch (c) { case 'b': break; case 'u': sb.append((char)Integer.parseInt(next(4), 16)); break; case 'x': sb.append((char)Integer.parseInt(next(2), 16)); break; default:
/** * Skip characters until the next character is the requested character. * If the requested character is not found, no characters are skipped.<p> * * @param to a character to skip to * @return the requested character, or zero if the requested character * is not found * @throws JSONException if something goes wrong */ public char skipTo(char to) throws JSONException { char c; try { int startIndex = this.m_index; m_reader.mark(Integer.MAX_VALUE); do { c = next(); if (c == 0) { m_reader.reset(); this.m_index = startIndex; return c; } } while (c != to); } catch (IOException exc) { throw new JSONException(exc); } back(); return c; }
/** * Skip characters until the next character is the requested character. * If the requested character is not found, no characters are skipped.<p> * * @param to a character to skip to * @return the requested character, or zero if the requested character * is not found * @throws JSONException if something goes wrong */ public char skipTo(char to) throws JSONException { char c; try { int startIndex = m_index; m_reader.mark(Integer.MAX_VALUE); do { c = next(); if (c == 0) { m_reader.reset(); m_index = startIndex; return c; } } while (c != to); } catch (IOException exc) { throw new JSONException(exc); } back(); return c; }
/** * Produce a JSONArray of strings from a row of comma delimited values.<p> * * @param x A JSONTokener of the source text * @return A JSONArray of strings * @throws JSONException if something goes wrong */ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { JSONArray ja = new JSONArray(); for (;;) { String value = getValue(x); if (value == null || (ja.length() == 0 && value.length() == 0)) { return null; } ja.put(value); for (;;) { char c = x.next(); if (c == ',') { break; } if (c != ' ') { if (c == '\n' || c == '\r' || c == 0) { return ja; } throw x.syntaxError("Bad character '" + c + "' (" + (int)c + ")."); } } } }
/** * Produce a JSONArray of strings from a row of comma delimited values.<p> * * @param x A JSONTokener of the source text * @return A JSONArray of strings * @throws JSONException if something goes wrong */ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { JSONArray ja = new JSONArray(); for (;;) { String value = getValue(x); if ((value == null) || ((ja.length() == 0) && (value.length() == 0))) { return null; } ja.put(value); for (;;) { char c = x.next(); if (c == ',') { break; } if (c != ' ') { if ((c == '\n') || (c == '\r') || (c == 0)) { return ja; } throw x.syntaxError("Bad character '" + c + "' (" + (int)c + ")."); } } } }