001/* Copyright 2006 FangYidong 002 003 Licensed under the Apache License, Version 2.0 (the "License"); 004 you may not use this file except in compliance with the License. 005 You may obtain a copy of the License at 006 007 http://www.apache.org/licenses/LICENSE-2.0 008 009 Unless required by applicable law or agreed to in writing, software 010 distributed under the License is distributed on an "AS IS" BASIS, 011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 See the License for the specific language governing permissions and 013 limitations under the License. */ 014package org.json.simple.parser; 015 016/** 017 * ParseException explains why and where the error occurs in source JSON text. 018 * 019 * @author FangYidong<fangyidong@yahoo.com.cn> 020 * 021 * @deprecated since 2.0.0, copied to a new package. 022 */ 023@Deprecated 024public class ParseException extends Exception { 025 private static final long serialVersionUID = -7880698968187728547L; 026 027 /** 028 * description omitted. 029 */ 030 public static final int ERROR_UNEXPECTED_CHAR = 0; 031 /** 032 * description omitted. 033 */ 034 public static final int ERROR_UNEXPECTED_TOKEN = 1; 035 /** 036 * description omitted. 037 */ 038 public static final int ERROR_UNEXPECTED_EXCEPTION = 2; 039 040 private int errorType; 041 private Object unexpectedObject; 042 private int position; 043 044 /** 045 * @param errorType description omitted. 046 */ 047 public ParseException(int errorType){ 048 this(-1, errorType, null); 049 } 050 051 /** 052 * @param errorType description omitted. 053 * @param unexpectedObject description omitted. 054 */ 055 public ParseException(int errorType, Object unexpectedObject){ 056 this(-1, errorType, unexpectedObject); 057 } 058 059 /** 060 * @param position description omitted. 061 * @param errorType description omitted. 062 * @param unexpectedObject description omitted. 063 */ 064 public ParseException(int position, int errorType, Object unexpectedObject){ 065 this.position = position; 066 this.errorType = errorType; 067 this.unexpectedObject = unexpectedObject; 068 } 069 070 /** 071 * Action on data for a result. 072 * 073 * @return description omitted. 074 */ 075 public int getErrorType() { 076 return errorType; 077 } 078 079 /** 080 * Action on data for a result. 081 * 082 * @param errorType description omitted. 083 */ 084 public void setErrorType(int errorType) { 085 this.errorType = errorType; 086 } 087 088 /** 089 * @see org.json.simple.parser.JSONParser#getPosition() 090 * 091 * @return The character position (starting with 0) of the input where the error occurs. 092 */ 093 public int getPosition() { 094 return position; 095 } 096 097 /** 098 * Action on data for a result. 099 * 100 * @param position description omitted. 101 */ 102 public void setPosition(int position) { 103 this.position = position; 104 } 105 106 /** 107 * @see org.json.simple.parser.Yytoken 108 * 109 * @return One of the following base on the value of errorType: 110 * ERROR_UNEXPECTED_CHAR java.lang.Character 111 * ERROR_UNEXPECTED_TOKEN org.json.simple.parser.Yytoken 112 * ERROR_UNEXPECTED_EXCEPTION java.lang.Exception 113 */ 114 public Object getUnexpectedObject() { 115 return unexpectedObject; 116 } 117 118 /** 119 * Action on data for a result. 120 * 121 * @param unexpectedObject description omitted. 122 */ 123 public void setUnexpectedObject(Object unexpectedObject) { 124 this.unexpectedObject = unexpectedObject; 125 } 126 127 public String getMessage() { 128 StringBuffer sb = new StringBuffer(); 129 130 switch(errorType){ 131 case ERROR_UNEXPECTED_CHAR: 132 sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position).append("."); 133 break; 134 case ERROR_UNEXPECTED_TOKEN: 135 sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position).append("."); 136 break; 137 case ERROR_UNEXPECTED_EXCEPTION: 138 sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject); 139 break; 140 default: 141 sb.append("Unkown error at position ").append(position).append("."); 142 break; 143 } 144 return sb.toString(); 145 } 146}