mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #352 from johnjaylward/ErrorMessagePositionFixes
Error message position fixes
This commit is contained in:
commit
5024f2d210
3 changed files with 117 additions and 65 deletions
9
CDL.java
9
CDL.java
|
@ -22,7 +22,7 @@ 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides static methods to convert comma delimited text into a
|
* This provides static methods to convert comma delimited text into a
|
||||||
|
@ -70,9 +70,12 @@ public class CDL {
|
||||||
c = x.next();
|
c = x.next();
|
||||||
if (c == q) {
|
if (c == q) {
|
||||||
//Handle escaped double-quote
|
//Handle escaped double-quote
|
||||||
if(x.next() != '\"')
|
char nextC = x.next();
|
||||||
{
|
if(nextC != '\"') {
|
||||||
|
// if our quote was the end of the file, don't step
|
||||||
|
if(nextC > 0) {
|
||||||
x.back();
|
x.back();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A JSONTokener takes a source string and extracts characters and tokens from
|
* A JSONTokener takes a source string and extracts characters and tokens from
|
||||||
|
@ -39,7 +39,7 @@ SOFTWARE.
|
||||||
* @version 2014-05-03
|
* @version 2014-05-03
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
/** current read character. */
|
/** current read character position on the current line. */
|
||||||
private long character;
|
private long character;
|
||||||
/** flag to indicate if the end of the input has been found. */
|
/** flag to indicate if the end of the input has been found. */
|
||||||
private boolean eof;
|
private boolean eof;
|
||||||
|
@ -47,16 +47,18 @@ public class JSONTokener {
|
||||||
private long index;
|
private long index;
|
||||||
/** current line of the input. */
|
/** current line of the input. */
|
||||||
private long line;
|
private long line;
|
||||||
/** previous index of the input. */
|
/** previous character read from the input. */
|
||||||
private char previous;
|
private char previous;
|
||||||
/** Reader for the input. */
|
/** Reader for the input. */
|
||||||
private final Reader reader;
|
private final Reader reader;
|
||||||
/** flag to indicate that a previous character was requested. */
|
/** flag to indicate that a previous character was requested. */
|
||||||
private boolean usePrevious;
|
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.
|
* @param reader A reader.
|
||||||
*/
|
*/
|
||||||
|
@ -69,12 +71,13 @@ public class JSONTokener {
|
||||||
this.previous = 0;
|
this.previous = 0;
|
||||||
this.index = 0;
|
this.index = 0;
|
||||||
this.character = 1;
|
this.character = 1;
|
||||||
|
this.characterPreviousLine = 0;
|
||||||
this.line = 1;
|
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.
|
* @param inputStream The source.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(InputStream inputStream) {
|
public JSONTokener(InputStream inputStream) {
|
||||||
|
@ -103,12 +106,23 @@ public class JSONTokener {
|
||||||
if (this.usePrevious || this.index <= 0) {
|
if (this.usePrevious || this.index <= 0) {
|
||||||
throw new JSONException("Stepping back two steps is not supported");
|
throw new JSONException("Stepping back two steps is not supported");
|
||||||
}
|
}
|
||||||
this.index -= 1;
|
this.decrementIndexes();
|
||||||
this.character -= 1;
|
|
||||||
this.usePrevious = true;
|
this.usePrevious = true;
|
||||||
this.eof = false;
|
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).
|
* Get the hex value of a character (base16).
|
||||||
|
@ -130,6 +144,8 @@ public class JSONTokener {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 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
|
* @return true if at the end of the file and we didn't step back
|
||||||
*/
|
*/
|
||||||
public boolean end() {
|
public boolean end() {
|
||||||
|
@ -145,11 +161,24 @@ public class JSONTokener {
|
||||||
* or backward while checking for more data.
|
* or backward while checking for more data.
|
||||||
*/
|
*/
|
||||||
public boolean more() throws JSONException {
|
public boolean more() throws JSONException {
|
||||||
this.next();
|
if(this.usePrevious) {
|
||||||
if (this.end()) {
|
return true;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.reader.mark(1);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new JSONException("Unable to preserve stream position", e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// -1 is EOF, but next() can not consume the null character '\0'
|
||||||
|
if(this.reader.read() <= 0) {
|
||||||
|
this.eof = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this.back();
|
this.reader.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new JSONException("Unable to read the next character from the stream", e);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,26 +200,39 @@ public class JSONTokener {
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
throw new JSONException(exception);
|
throw new JSONException(exception);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (c <= 0) { // End of stream
|
if (c <= 0) { // End of stream
|
||||||
this.eof = true;
|
this.eof = true;
|
||||||
c = 0;
|
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;
|
|
||||||
}
|
}
|
||||||
|
this.incrementIndexes(c);
|
||||||
this.previous = (char) c;
|
this.previous = (char) c;
|
||||||
return this.previous;
|
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
|
* Consume the next character, and check that it matches a specified
|
||||||
|
@ -202,9 +244,12 @@ public class JSONTokener {
|
||||||
public char next(char c) throws JSONException {
|
public char next(char c) throws JSONException {
|
||||||
char n = this.next();
|
char n = this.next();
|
||||||
if (n != c) {
|
if (n != c) {
|
||||||
|
if(n > 0) {
|
||||||
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||||
n + "'");
|
n + "'");
|
||||||
}
|
}
|
||||||
|
throw this.syntaxError("Expected '" + c + "' and instead saw ''");
|
||||||
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,13 +477,17 @@ public class JSONTokener {
|
||||||
do {
|
do {
|
||||||
c = this.next();
|
c = this.next();
|
||||||
if (c == 0) {
|
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.reader.reset();
|
||||||
this.index = startIndex;
|
this.index = startIndex;
|
||||||
this.character = startCharacter;
|
this.character = startCharacter;
|
||||||
this.line = startLine;
|
this.line = startLine;
|
||||||
return c;
|
return 0;
|
||||||
}
|
}
|
||||||
} while (c != to);
|
} while (c != to);
|
||||||
|
this.reader.mark(1);
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
throw new JSONException(exception);
|
throw new JSONException(exception);
|
||||||
}
|
}
|
||||||
|
@ -446,7 +495,6 @@ public class JSONTokener {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a JSONException to signal a syntax error.
|
* Make a JSONException to signal a syntax error.
|
||||||
*
|
*
|
||||||
|
|
|
@ -64,11 +64,8 @@ public class XMLTokener extends JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
int i;
|
int i;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (;;) {
|
while (more()) {
|
||||||
c = next();
|
c = next();
|
||||||
if (end()) {
|
|
||||||
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) == ']' &&
|
||||||
|
@ -77,6 +74,7 @@ public class XMLTokener extends JSONTokener {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw syntaxError("Unclosed CDATA");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,7 +101,10 @@ public class XMLTokener extends JSONTokener {
|
||||||
}
|
}
|
||||||
sb = new StringBuilder();
|
sb = new StringBuilder();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (c == '<' || c == 0) {
|
if (c == 0) {
|
||||||
|
return sb.toString().trim();
|
||||||
|
}
|
||||||
|
if (c == '<') {
|
||||||
back();
|
back();
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue