michael@0: package org.json.simple.parser; michael@0: michael@0: /** michael@0: * ParseException explains why and where the error occurs in source JSON text. michael@0: * michael@0: * @author FangYidong michael@0: * michael@0: */ michael@0: public class ParseException extends Exception { michael@0: private static final long serialVersionUID = -7880698968187728548L; michael@0: michael@0: public static final int ERROR_UNEXPECTED_CHAR = 0; michael@0: public static final int ERROR_UNEXPECTED_TOKEN = 1; michael@0: public static final int ERROR_UNEXPECTED_EXCEPTION = 2; michael@0: michael@0: private int errorType; michael@0: private Object unexpectedObject; michael@0: private int position; michael@0: michael@0: public ParseException(int errorType){ michael@0: this(-1, errorType, null); michael@0: } michael@0: michael@0: public ParseException(int errorType, Object unexpectedObject){ michael@0: this(-1, errorType, unexpectedObject); michael@0: } michael@0: michael@0: public ParseException(int position, int errorType, Object unexpectedObject){ michael@0: this.position = position; michael@0: this.errorType = errorType; michael@0: this.unexpectedObject = unexpectedObject; michael@0: } michael@0: michael@0: public int getErrorType() { michael@0: return errorType; michael@0: } michael@0: michael@0: public void setErrorType(int errorType) { michael@0: this.errorType = errorType; michael@0: } michael@0: michael@0: /** michael@0: * @see org.json.simple.parser.JSONParser#getPosition() michael@0: * michael@0: * @return The character position (starting with 0) of the input where the error occurs. michael@0: */ michael@0: public int getPosition() { michael@0: return position; michael@0: } michael@0: michael@0: public void setPosition(int position) { michael@0: this.position = position; michael@0: } michael@0: michael@0: /** michael@0: * @see org.json.simple.parser.Yytoken michael@0: * michael@0: * @return One of the following base on the value of errorType: michael@0: * ERROR_UNEXPECTED_CHAR java.lang.Character michael@0: * ERROR_UNEXPECTED_TOKEN org.json.simple.parser.Yytoken michael@0: * ERROR_UNEXPECTED_EXCEPTION java.lang.Exception michael@0: */ michael@0: public Object getUnexpectedObject() { michael@0: return unexpectedObject; michael@0: } michael@0: michael@0: public void setUnexpectedObject(Object unexpectedObject) { michael@0: this.unexpectedObject = unexpectedObject; michael@0: } michael@0: michael@0: public String toString(){ michael@0: StringBuffer sb = new StringBuffer(); michael@0: michael@0: switch(errorType){ michael@0: case ERROR_UNEXPECTED_CHAR: michael@0: sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position).append("."); michael@0: break; michael@0: case ERROR_UNEXPECTED_TOKEN: michael@0: sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position).append("."); michael@0: break; michael@0: case ERROR_UNEXPECTED_EXCEPTION: michael@0: sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject); michael@0: break; michael@0: default: michael@0: sb.append("Unkown error at position ").append(position).append("."); michael@0: break; michael@0: } michael@0: return sb.toString(); michael@0: } michael@0: }