001/* ============ 002 * Orson Charts 003 * ============ 004 * 005 * (C)opyright 2013, 2014, by Object Refinery Limited. 006 * 007 * http://www.object-refinery.com/orsoncharts/index.html 008 * 009 * JSON.simple 010 * ----------- 011 * The code in this file originates from the JSON.simple project by 012 * FangYidong<fangyidong@yahoo.com.cn>: 013 * 014 * https://code.google.com/p/json-simple/ 015 * 016 * which is licensed under the Apache Software License version 2.0. 017 * 018 * It has been modified locally and repackaged under 019 * com.orsoncharts.util.json.* to avoid conflicts with any other version that 020 * may be present on the classpath. 021 * 022 */ 023 024package com.orsoncharts.util.json.parser; 025 026/** 027 * ParseException explains why and where the error occurs in source JSON text. 028 * 029 */ 030public class ParseException extends Exception { 031 032 private static final long serialVersionUID = -7880698968187728548L; 033 034 public static final int ERROR_UNEXPECTED_CHAR = 0; 035 public static final int ERROR_UNEXPECTED_TOKEN = 1; 036 public static final int ERROR_UNEXPECTED_EXCEPTION = 2; 037 038 private int errorType; 039 private Object unexpectedObject; 040 private int position; 041 042 public ParseException(int errorType){ 043 this(-1, errorType, null); 044 } 045 046 public ParseException(int errorType, Object unexpectedObject){ 047 this(-1, errorType, unexpectedObject); 048 } 049 050 public ParseException(int position, int errorType, 051 Object unexpectedObject) { 052 this.position = position; 053 this.errorType = errorType; 054 this.unexpectedObject = unexpectedObject; 055 } 056 057 public int getErrorType() { 058 return errorType; 059 } 060 061 public void setErrorType(int errorType) { 062 this.errorType = errorType; 063 } 064 065 /** 066 * @see com.orsoncharts.util.json.parser.JSONParser#getPosition() 067 * 068 * @return The character position (starting with 0) of the input where the 069 * error occurs. 070 */ 071 public int getPosition() { 072 return position; 073 } 074 075 public void setPosition(int position) { 076 this.position = position; 077 } 078 079 /** 080 * @see com.orsoncharts.util.json.parser.Yytoken 081 * 082 * @return One of the following base on the value of errorType: 083 * ERROR_UNEXPECTED_CHAR java.lang.Character 084 * ERROR_UNEXPECTED_TOKEN com.orsoncharts.util.json.simple.parser.Yytoken 085 * ERROR_UNEXPECTED_EXCEPTION java.lang.Exception 086 */ 087 public Object getUnexpectedObject() { 088 return unexpectedObject; 089 } 090 091 public void setUnexpectedObject(Object unexpectedObject) { 092 this.unexpectedObject = unexpectedObject; 093 } 094 095 @Override 096 public String toString(){ 097 StringBuilder sb = new StringBuilder(); 098 099 switch(errorType) { 100 case ERROR_UNEXPECTED_CHAR: 101 sb.append("Unexpected character (").append(unexpectedObject); 102 sb.append(") at position ").append(position).append("."); 103 break; 104 case ERROR_UNEXPECTED_TOKEN: 105 sb.append("Unexpected token ").append(unexpectedObject); 106 sb.append(" at position ").append(position).append("."); 107 break; 108 case ERROR_UNEXPECTED_EXCEPTION: 109 sb.append("Unexpected exception at position ").append(position); 110 sb.append(": ").append(unexpectedObject); 111 break; 112 default: 113 sb.append("Unkown error at position ").append(position).append("."); 114 break; 115 } 116 return sb.toString(); 117 } 118} 119