|
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 = -7880698968187728548L; |
|
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(int errorType){ |
|
21 this(-1, errorType, null); |
|
22 } |
|
23 |
|
24 public ParseException(int errorType, Object unexpectedObject){ |
|
25 this(-1, errorType, unexpectedObject); |
|
26 } |
|
27 |
|
28 public ParseException(int position, int errorType, Object unexpectedObject){ |
|
29 this.position = position; |
|
30 this.errorType = errorType; |
|
31 this.unexpectedObject = unexpectedObject; |
|
32 } |
|
33 |
|
34 public int getErrorType() { |
|
35 return errorType; |
|
36 } |
|
37 |
|
38 public void setErrorType(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 error occurs. |
|
46 */ |
|
47 public int getPosition() { |
|
48 return position; |
|
49 } |
|
50 |
|
51 public void setPosition(int position) { |
|
52 this.position = position; |
|
53 } |
|
54 |
|
55 /** |
|
56 * @see org.json.simple.parser.Yytoken |
|
57 * |
|
58 * @return One of the following base on the value of errorType: |
|
59 * ERROR_UNEXPECTED_CHAR java.lang.Character |
|
60 * ERROR_UNEXPECTED_TOKEN org.json.simple.parser.Yytoken |
|
61 * ERROR_UNEXPECTED_EXCEPTION java.lang.Exception |
|
62 */ |
|
63 public Object getUnexpectedObject() { |
|
64 return unexpectedObject; |
|
65 } |
|
66 |
|
67 public void setUnexpectedObject(Object unexpectedObject) { |
|
68 this.unexpectedObject = unexpectedObject; |
|
69 } |
|
70 |
|
71 public String toString(){ |
|
72 StringBuffer sb = new StringBuffer(); |
|
73 |
|
74 switch(errorType){ |
|
75 case ERROR_UNEXPECTED_CHAR: |
|
76 sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position).append("."); |
|
77 break; |
|
78 case ERROR_UNEXPECTED_TOKEN: |
|
79 sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position).append("."); |
|
80 break; |
|
81 case ERROR_UNEXPECTED_EXCEPTION: |
|
82 sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject); |
|
83 break; |
|
84 default: |
|
85 sb.append("Unkown error at position ").append(position).append("."); |
|
86 break; |
|
87 } |
|
88 return sb.toString(); |
|
89 } |
|
90 } |