diff options
Diffstat (limited to 'src/org/json/simple/parser/ParseException.java')
-rw-r--r-- | src/org/json/simple/parser/ParseException.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/org/json/simple/parser/ParseException.java b/src/org/json/simple/parser/ParseException.java new file mode 100644 index 0000000..df1b1d1 --- /dev/null +++ b/src/org/json/simple/parser/ParseException.java | |||
@@ -0,0 +1,92 @@ | |||
1 | package org.json.simple.parser; | ||
2 | |||
3 | /** | ||
4 | * ParseException explains why and where the error occurs in source JSON text. | ||
5 | * | ||
6 | * @author FangYidong<fangyidong@yahoo.com.cn> | ||
7 | * | ||
8 | */ | ||
9 | public class ParseException extends Exception { | ||
10 | private static final long serialVersionUID = -7880698968187728547L; | ||
11 | |||
12 | public static final int ERROR_UNEXPECTED_CHAR = 0; | ||
13 | public static final int ERROR_UNEXPECTED_TOKEN = 1; | ||
14 | public static final int ERROR_UNEXPECTED_EXCEPTION = 2; | ||
15 | |||
16 | private int errorType; | ||
17 | private Object unexpectedObject; | ||
18 | private int position; | ||
19 | |||
20 | public ParseException(final int errorType) { | ||
21 | this(-1, errorType, null); | ||
22 | } | ||
23 | |||
24 | public ParseException(final int errorType, final Object unexpectedObject) { | ||
25 | this(-1, errorType, unexpectedObject); | ||
26 | } | ||
27 | |||
28 | public ParseException(final int position, final int errorType, final Object unexpectedObject) { | ||
29 | this.position = position; | ||
30 | this.errorType = errorType; | ||
31 | this.unexpectedObject = unexpectedObject; | ||
32 | } | ||
33 | |||
34 | public int getErrorType() { | ||
35 | return this.errorType; | ||
36 | } | ||
37 | |||
38 | public void setErrorType(final int errorType) { | ||
39 | this.errorType = errorType; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * @see org.json.simple.parser.JSONParser#getPosition() | ||
44 | * | ||
45 | * @return The character position (starting with 0) of the input where the | ||
46 | * error occurs. | ||
47 | */ | ||
48 | public int getPosition() { | ||
49 | return this.position; | ||
50 | } | ||
51 | |||
52 | public void setPosition(final int position) { | ||
53 | this.position = position; | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * @see org.json.simple.parser.Yytoken | ||
58 | * | ||
59 | * @return One of the following base on the value of errorType: | ||
60 | * ERROR_UNEXPECTED_CHAR java.lang.Character ERROR_UNEXPECTED_TOKEN | ||
61 | * org.json.simple.parser.Yytoken ERROR_UNEXPECTED_EXCEPTION | ||
62 | * java.lang.Exception | ||
63 | */ | ||
64 | public Object getUnexpectedObject() { | ||
65 | return this.unexpectedObject; | ||
66 | } | ||
67 | |||
68 | public void setUnexpectedObject(final Object unexpectedObject) { | ||
69 | this.unexpectedObject = unexpectedObject; | ||
70 | } | ||
71 | |||
72 | @Override | ||
73 | public String getMessage() { | ||
74 | final StringBuffer sb = new StringBuffer(); | ||
75 | |||
76 | switch (this.errorType) { | ||
77 | case ERROR_UNEXPECTED_CHAR: | ||
78 | sb.append("Unexpected character (").append(this.unexpectedObject).append(") at position ").append(this.position).append("."); | ||
79 | break; | ||
80 | case ERROR_UNEXPECTED_TOKEN: | ||
81 | sb.append("Unexpected token ").append(this.unexpectedObject).append(" at position ").append(this.position).append("."); | ||
82 | break; | ||
83 | case ERROR_UNEXPECTED_EXCEPTION: | ||
84 | sb.append("Unexpected exception at position ").append(this.position).append(": ").append(this.unexpectedObject); | ||
85 | break; | ||
86 | default: | ||
87 | sb.append("Unkown error at position ").append(this.position).append("."); | ||
88 | break; | ||
89 | } | ||
90 | return sb.toString(); | ||
91 | } | ||
92 | } | ||