Skip to content

Commit e7e6ed9

Browse files
author
John J. Aylward
committed
Fixes position reports on errors
1 parent 1add124 commit e7e6ed9

3 files changed

Lines changed: 67 additions & 48 deletions

File tree

CDL.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ of this software and associated documentation files (the "Software"), to deal
2222
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2323
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
SOFTWARE.
25-
*/
25+
*/
2626

2727
/**
2828
* This provides static methods to convert comma delimited text into a
@@ -70,9 +70,12 @@ private static String getValue(JSONTokener x) throws JSONException {
7070
c = x.next();
7171
if (c == q) {
7272
//Handle escaped double-quote
73-
if(x.next() != '\"')
74-
{
75-
x.back();
73+
char nextC = x.next();
74+
if(nextC != '\"') {
75+
// if our quote was the end of the file, don't step
76+
if(nextC > 0) {
77+
x.back();
78+
}
7679
break;
7780
}
7881
}

JSONTokener.java

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3030
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3131
SOFTWARE.
32-
*/
32+
*/
3333

3434
/**
3535
* A JSONTokener takes a source string and extracts characters and tokens from
@@ -39,15 +39,15 @@ of this software and associated documentation files (the "Software"), to deal
3939
* @version 2014-05-03
4040
*/
4141
public class JSONTokener {
42-
/** current read character. */
42+
/** current read character position on the current line. */
4343
private long character;
4444
/** flag to indicate if the end of the input has been found. */
4545
private boolean eof;
4646
/** current read index of the input. */
4747
private long index;
4848
/** current line of the input. */
4949
private long line;
50-
/** previous index of the input. */
50+
/** previous character read from the input. */
5151
private char previous;
5252
/** Reader for the input. */
5353
private final Reader reader;
@@ -62,8 +62,8 @@ public class JSONTokener {
6262
*/
6363
public JSONTokener(Reader reader) {
6464
this.reader = reader.markSupported()
65-
? reader
66-
: new BufferedReader(reader);
65+
? reader
66+
: new BufferedReader(reader);
6767
this.eof = false;
6868
this.usePrevious = false;
6969
this.previous = 0;
@@ -103,8 +103,8 @@ public void back() throws JSONException {
103103
if (this.usePrevious || this.index <= 0) {
104104
throw new JSONException("Stepping back two steps is not supported");
105105
}
106-
this.index -= 1;
107-
this.character -= 1;
106+
this.index--;
107+
this.character--;
108108
this.usePrevious = true;
109109
this.eof = false;
110110
}
@@ -145,11 +145,23 @@ public boolean end() {
145145
* or backward while checking for more data.
146146
*/
147147
public boolean more() throws JSONException {
148-
this.next();
149-
if (this.end()) {
150-
return false;
148+
if(this.usePrevious) {
149+
return true;
150+
}
151+
try {
152+
this.reader.mark(1);
153+
} catch (IOException e) {
154+
throw new JSONException("Unable to preserve stream position", e);
155+
}
156+
try {
157+
if(this.reader.read()<0) {
158+
this.eof = true;
159+
return false;
160+
}
161+
this.reader.reset();
162+
} catch (IOException e) {
163+
throw new JSONException("Unable to read the next character from the stream", e);
151164
}
152-
this.back();
153165
return true;
154166
}
155167

@@ -174,7 +186,7 @@ public char next() throws JSONException {
174186

175187
if (c <= 0) { // End of stream
176188
this.eof = true;
177-
c = 0;
189+
return 0;
178190
}
179191
}
180192
this.index += 1;
@@ -202,8 +214,11 @@ public char next() throws JSONException {
202214
public char next(char c) throws JSONException {
203215
char n = this.next();
204216
if (n != c) {
205-
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
206-
n + "'");
217+
if(n > 0) {
218+
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
219+
n + "'");
220+
}
221+
throw this.syntaxError("Expected '" + c + "' and instead saw ''");
207222
}
208223
return n;
209224
}
@@ -218,23 +233,23 @@ public char next(char c) throws JSONException {
218233
* Substring bounds error if there are not
219234
* n characters remaining in the source string.
220235
*/
221-
public String next(int n) throws JSONException {
222-
if (n == 0) {
223-
return "";
224-
}
236+
public String next(int n) throws JSONException {
237+
if (n == 0) {
238+
return "";
239+
}
225240

226-
char[] chars = new char[n];
227-
int pos = 0;
241+
char[] chars = new char[n];
242+
int pos = 0;
228243

229-
while (pos < n) {
230-
chars[pos] = this.next();
231-
if (this.end()) {
232-
throw this.syntaxError("Substring bounds error");
233-
}
234-
pos += 1;
235-
}
236-
return new String(chars);
237-
}
244+
while (pos < n) {
245+
chars[pos] = this.next();
246+
if (this.end()) {
247+
throw this.syntaxError("Substring bounds error");
248+
}
249+
pos += 1;
250+
}
251+
return new String(chars);
252+
}
238253

239254

240255
/**
@@ -378,15 +393,15 @@ public Object nextValue() throws JSONException {
378393
String string;
379394

380395
switch (c) {
381-
case '"':
382-
case '\'':
383-
return this.nextString(c);
384-
case '{':
385-
this.back();
386-
return new JSONObject(this);
387-
case '[':
388-
this.back();
389-
return new JSONArray(this);
396+
case '"':
397+
case '\'':
398+
return this.nextString(c);
399+
case '{':
400+
this.back();
401+
return new JSONObject(this);
402+
case '[':
403+
this.back();
404+
return new JSONArray(this);
390405
}
391406

392407
/*
@@ -476,6 +491,6 @@ public JSONException syntaxError(String message, Throwable causedBy) {
476491
@Override
477492
public String toString() {
478493
return " at " + this.index + " [character " + this.character + " line " +
479-
this.line + "]";
494+
this.line + "]";
480495
}
481496
}

XMLTokener.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ public String nextCDATA() throws JSONException {
6464
char c;
6565
int i;
6666
StringBuilder sb = new StringBuilder();
67-
for (;;) {
67+
while (more()) {
6868
c = next();
69-
if (end()) {
70-
throw syntaxError("Unclosed CDATA");
71-
}
7269
sb.append(c);
7370
i = sb.length() - 3;
7471
if (i >= 0 && sb.charAt(i) == ']' &&
@@ -77,6 +74,7 @@ public String nextCDATA() throws JSONException {
7774
return sb.toString();
7875
}
7976
}
77+
throw syntaxError("Unclosed CDATA");
8078
}
8179

8280

@@ -103,7 +101,10 @@ public Object nextContent() throws JSONException {
103101
}
104102
sb = new StringBuilder();
105103
for (;;) {
106-
if (c == '<' || c == 0) {
104+
if (c == 0) {
105+
return sb.toString().trim();
106+
}
107+
if (c == '<') {
107108
back();
108109
return sb.toString().trim();
109110
}

0 commit comments

Comments
 (0)