From e7e6ed9205b7aa127a70d3cf4e0275e7dc3d4f8b Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 23 Jun 2017 13:40:41 -0400 Subject: [PATCH 1/3] Fixes position reports on errors --- CDL.java | 11 +++--- JSONTokener.java | 93 ++++++++++++++++++++++++++++-------------------- XMLTokener.java | 11 +++--- 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/CDL.java b/CDL.java index 6a82764db..1c7df3223 100644 --- a/CDL.java +++ b/CDL.java @@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal 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 SOFTWARE. -*/ + */ /** * This provides static methods to convert comma delimited text into a @@ -70,9 +70,12 @@ private static String getValue(JSONTokener x) throws JSONException { c = x.next(); if (c == q) { //Handle escaped double-quote - if(x.next() != '\"') - { - x.back(); + char nextC = x.next(); + if(nextC != '\"') { + // if our quote was the end of the file, don't step + if(nextC > 0) { + x.back(); + } break; } } diff --git a/JSONTokener.java b/JSONTokener.java index b244d1371..956efcf33 100644 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal 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 SOFTWARE. -*/ + */ /** * A JSONTokener takes a source string and extracts characters and tokens from @@ -39,7 +39,7 @@ of this software and associated documentation files (the "Software"), to deal * @version 2014-05-03 */ public class JSONTokener { - /** current read character. */ + /** current read character position on the current line. */ private long character; /** flag to indicate if the end of the input has been found. */ private boolean eof; @@ -47,7 +47,7 @@ public class JSONTokener { private long index; /** current line of the input. */ private long line; - /** previous index of the input. */ + /** previous character read from the input. */ private char previous; /** Reader for the input. */ private final Reader reader; @@ -62,8 +62,8 @@ public class JSONTokener { */ public JSONTokener(Reader reader) { this.reader = reader.markSupported() - ? reader - : new BufferedReader(reader); + ? reader + : new BufferedReader(reader); this.eof = false; this.usePrevious = false; this.previous = 0; @@ -103,8 +103,8 @@ public void back() throws JSONException { if (this.usePrevious || this.index <= 0) { throw new JSONException("Stepping back two steps is not supported"); } - this.index -= 1; - this.character -= 1; + this.index--; + this.character--; this.usePrevious = true; this.eof = false; } @@ -145,11 +145,23 @@ public boolean end() { * or backward while checking for more data. */ public boolean more() throws JSONException { - this.next(); - if (this.end()) { - return false; + if(this.usePrevious) { + return true; + } + try { + this.reader.mark(1); + } catch (IOException e) { + throw new JSONException("Unable to preserve stream position", e); + } + try { + if(this.reader.read()<0) { + this.eof = true; + return false; + } + this.reader.reset(); + } catch (IOException e) { + throw new JSONException("Unable to read the next character from the stream", e); } - this.back(); return true; } @@ -174,7 +186,7 @@ public char next() throws JSONException { if (c <= 0) { // End of stream this.eof = true; - c = 0; + return 0; } } this.index += 1; @@ -202,8 +214,11 @@ public char next() throws JSONException { public char next(char c) throws JSONException { char n = this.next(); if (n != c) { - throw this.syntaxError("Expected '" + c + "' and instead saw '" + - n + "'"); + if(n > 0) { + throw this.syntaxError("Expected '" + c + "' and instead saw '" + + n + "'"); + } + throw this.syntaxError("Expected '" + c + "' and instead saw ''"); } return n; } @@ -218,23 +233,23 @@ public char next(char c) throws JSONException { * Substring bounds error if there are not * n characters remaining in the source string. */ - public String next(int n) throws JSONException { - if (n == 0) { - return ""; - } + public String next(int n) throws JSONException { + if (n == 0) { + return ""; + } - char[] chars = new char[n]; - int pos = 0; + char[] chars = new char[n]; + int pos = 0; - while (pos < n) { - chars[pos] = this.next(); - if (this.end()) { - throw this.syntaxError("Substring bounds error"); - } - pos += 1; - } - return new String(chars); - } + while (pos < n) { + chars[pos] = this.next(); + if (this.end()) { + throw this.syntaxError("Substring bounds error"); + } + pos += 1; + } + return new String(chars); + } /** @@ -378,15 +393,15 @@ public Object nextValue() throws JSONException { String string; switch (c) { - case '"': - case '\'': - return this.nextString(c); - case '{': - this.back(); - return new JSONObject(this); - case '[': - this.back(); - return new JSONArray(this); + case '"': + case '\'': + return this.nextString(c); + case '{': + this.back(); + return new JSONObject(this); + case '[': + this.back(); + return new JSONArray(this); } /* @@ -476,6 +491,6 @@ public JSONException syntaxError(String message, Throwable causedBy) { @Override public String toString() { return " at " + this.index + " [character " + this.character + " line " + - this.line + "]"; + this.line + "]"; } } diff --git a/XMLTokener.java b/XMLTokener.java index e45e747dc..1c5f2b59d 100644 --- a/XMLTokener.java +++ b/XMLTokener.java @@ -64,11 +64,8 @@ public String nextCDATA() throws JSONException { char c; int i; StringBuilder sb = new StringBuilder(); - for (;;) { + while (more()) { c = next(); - if (end()) { - throw syntaxError("Unclosed CDATA"); - } sb.append(c); i = sb.length() - 3; if (i >= 0 && sb.charAt(i) == ']' && @@ -77,6 +74,7 @@ public String nextCDATA() throws JSONException { return sb.toString(); } } + throw syntaxError("Unclosed CDATA"); } @@ -103,7 +101,10 @@ public Object nextContent() throws JSONException { } sb = new StringBuilder(); for (;;) { - if (c == '<' || c == 0) { + if (c == 0) { + return sb.toString().trim(); + } + if (c == '<') { back(); return sb.toString().trim(); } From 52845366bd4f6ae6a947aea5169d682679f6914a Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Fri, 23 Jun 2017 23:27:28 -0400 Subject: [PATCH 2/3] Fixes more position errors from stepping to new lines and then back. --- JSONTokener.java | 70 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/JSONTokener.java b/JSONTokener.java index 956efcf33..741e78b42 100644 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -53,10 +53,12 @@ public class JSONTokener { private final Reader reader; /** flag to indicate that a previous character was requested. */ private boolean usePrevious; + /** the number of characters read in the previous line. */ + private long characterPreviousLine; /** - * Construct a JSONTokener from a Reader. + * Construct a JSONTokener from a Reader. The caller must close the Reader. * * @param reader A reader. */ @@ -69,12 +71,13 @@ public JSONTokener(Reader reader) { this.previous = 0; this.index = 0; this.character = 1; + this.characterPreviousLine = 0; this.line = 1; } /** - * Construct a JSONTokener from an InputStream. + * Construct a JSONTokener from an InputStream. The caller must close the input stream. * @param inputStream The source. */ public JSONTokener(InputStream inputStream) { @@ -103,12 +106,23 @@ public void back() throws JSONException { if (this.usePrevious || this.index <= 0) { throw new JSONException("Stepping back two steps is not supported"); } - this.index--; - this.character--; + this.decrementIndexes(); this.usePrevious = true; this.eof = false; } + /** + * Decrements the indexes for the {@link #back()} method based on the previous character read. + */ + private void decrementIndexes() { + this.index--; + if(this.previous=='\r' || this.previous == '\n') { + this.line--; + this.character=this.characterPreviousLine ; + } else if(this.character > 0){ + this.character--; + } + } /** * Get the hex value of a character (base16). @@ -183,26 +197,39 @@ public char next() throws JSONException { } catch (IOException exception) { throw new JSONException(exception); } - - if (c <= 0) { // End of stream - this.eof = true; - return 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; + if (c <= 0) { // End of stream + this.eof = true; + return 0; } + this.incrementIndexes(c); this.previous = (char) c; return this.previous; } + /** + * Increments the internal indexes according to the previous character + * read and the character passed as the current character. + * @param c the current character read. + */ + private void incrementIndexes(int c) { + if(c > 0) { + this.index++; + if(c=='\r') { + this.line++; + this.characterPreviousLine = this.character; + this.character=0; + }else if (c=='\n') { + if(this.previous != '\r') { + this.line++; + this.characterPreviousLine = this.character; + } + this.character=0; + } else { + this.character++; + } + } + } /** * Consume the next character, and check that it matches a specified @@ -447,13 +474,17 @@ public char skipTo(char to) throws JSONException { do { c = this.next(); if (c == 0) { + // in some readers, reset() may throw an exception if + // the remaining portion of the input is greater than + // the mark size (1,000,000 above). this.reader.reset(); this.index = startIndex; this.character = startCharacter; this.line = startLine; - return c; + return 0; } } while (c != to); + this.reader.mark(1); } catch (IOException exception) { throw new JSONException(exception); } @@ -461,7 +492,6 @@ public char skipTo(char to) throws JSONException { return c; } - /** * Make a JSONException to signal a syntax error. * From 16baa323cff49ff9000bc7aa0a90791be84d5627 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Mon, 26 Jun 2017 10:32:02 -0400 Subject: [PATCH 3/3] adds comments --- JSONTokener.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/JSONTokener.java b/JSONTokener.java index 741e78b42..36bce45c2 100644 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -144,6 +144,8 @@ public static int dehexchar(char c) { } /** + * Checks if the end of the input has been reached. + * * @return true if at the end of the file and we didn't step back */ public boolean end() { @@ -168,7 +170,8 @@ public boolean more() throws JSONException { throw new JSONException("Unable to preserve stream position", e); } try { - if(this.reader.read()<0) { + // -1 is EOF, but next() can not consume the null character '\0' + if(this.reader.read() <= 0) { this.eof = true; return false; }