json-simple/tags/1.1.1-1/src/main/java/org/json/simple/parser/ParseException.java

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