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