This project has retired. For details please refer to its Attic page.
JSONParseException xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl.json.parser;
20  
21  /**
22   * ParseException explains why and where the error occurs in source JSON text.
23   * 
24   * (Taken from JSON.simple <http://code.google.com/p/json-simple/> and modified
25   * for OpenCMIS.)
26   * 
27   * @author FangYidong<fangyidong@yahoo.com.cn>
28   */
29  public class JSONParseException extends Exception {
30      private static final long serialVersionUID = -7880698968187728548L;
31  
32      public static final int ERROR_UNEXPECTED_CHAR = 0;
33      public static final int ERROR_UNEXPECTED_TOKEN = 1;
34      public static final int ERROR_UNEXPECTED_EXCEPTION = 2;
35  
36      private int errorType;
37      private Object unexpectedObject;
38      private int position;
39  
40      public JSONParseException(int errorType) {
41          this(-1, errorType, null);
42      }
43  
44      public JSONParseException(int errorType, Object unexpectedObject) {
45          this(-1, errorType, unexpectedObject);
46      }
47  
48      public JSONParseException(int position, int errorType, Object unexpectedObject) {
49          this.position = position;
50          this.errorType = errorType;
51          this.unexpectedObject = unexpectedObject;
52      }
53  
54      public int getErrorType() {
55          return errorType;
56      }
57  
58      public void setErrorType(int errorType) {
59          this.errorType = errorType;
60      }
61  
62      /**
63       * @see org.json.simple.parser.JSONParser#getPosition()
64       * 
65       * @return The character position (starting with 0) of the input where the
66       *         error occurs.
67       */
68      public int getPosition() {
69          return position;
70      }
71  
72      public void setPosition(int position) {
73          this.position = position;
74      }
75  
76      /**
77       * @see org.json.simple.parser.Yytoken
78       * 
79       * @return One of the following base on the value of errorType:
80       *         ERROR_UNEXPECTED_CHAR java.lang.Character ERROR_UNEXPECTED_TOKEN
81       *         org.json.simple.parser.Yytoken ERROR_UNEXPECTED_EXCEPTION
82       *         java.lang.Exception
83       */
84      public Object getUnexpectedObject() {
85          return unexpectedObject;
86      }
87  
88      public void setUnexpectedObject(Object unexpectedObject) {
89          this.unexpectedObject = unexpectedObject;
90      }
91  
92      public String toString() {
93          StringBuilder sb = new StringBuilder();
94  
95          switch (errorType) {
96          case ERROR_UNEXPECTED_CHAR:
97              sb.append("Unexpected character (").append(unexpectedObject).append(") at position ").append(position)
98                      .append(".");
99              break;
100         case ERROR_UNEXPECTED_TOKEN:
101             sb.append("Unexpected token ").append(unexpectedObject).append(" at position ").append(position)
102                     .append(".");
103             break;
104         case ERROR_UNEXPECTED_EXCEPTION:
105             sb.append("Unexpected exception at position ").append(position).append(": ").append(unexpectedObject);
106             break;
107         default:
108             sb.append("Unkown error at position ").append(position).append(".");
109             break;
110         }
111         return sb.toString();
112     }
113 }