OSDN Git Service

start 1.101.105-SNAPSHOT
[jovsonz/Jovsonz.git] / src / main / java / jp / sourceforge / jovsonz / JsParseException.java
1 /*
2  * JSON parse error information
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jovsonz;
9
10 /**
11  * 入力文字列パース中断例外。
12  * JSON文字列ソースへのパース処理の中断時に投げられる。
13  */
14 @SuppressWarnings("serial")
15 public class JsParseException extends Exception {
16
17     static final String ERRMSG_INVALIDTOKEN =
18             "invalid JSON token";
19     static final String ERRMSG_INVALIDROOT =
20             "top root JSON value must be OBJECT or ARRAY";
21     static final String ERRMSG_NODATA =
22             "We need but no more JSON data";
23
24     private static final int LINE_UNKNOWN = 0;
25
26     private final int lineNumber;
27
28     /**
29      * コンストラクタ。
30      */
31     public JsParseException(){
32         this(null, LINE_UNKNOWN);
33         return;
34     }
35
36     /**
37      * コンストラクタ。
38      * @param message 詳細メッセージ。不明な場合はnull
39      * @param lineNumber 行番号。不明な場合は0以下の値
40      */
41     public JsParseException(String message, int lineNumber){
42         this(message, (Throwable) null, lineNumber);
43         return;
44     }
45
46     /**
47      * コンストラクタ。
48      * @param message 詳細メッセージ。不明な場合はnull
49      * @param cause 原因となった例外。不明な場合はnull
50      * @param lineNumber 行番号。不明な場合は0以下の値
51      */
52     public JsParseException(String message, Throwable cause, int lineNumber){
53         super(message, cause);
54         this.lineNumber = lineNumber;
55         return;
56     }
57
58     /**
59      * パースエラーの起きた行番号を返す。
60      * @return 行番号。不明な場合は0以下の値。
61      */
62     public int getLineNumber(){
63         return this.lineNumber;
64     }
65
66     /**
67      * 有効な行番号を保持しているか判定する。
68      * @return 有効な行番号(1以上)を保持していればtrue
69      */
70     public boolean hasValidLineNumber(){
71         if(this.lineNumber > 0) return true;
72         return false;
73     }
74
75     /**
76      * {@inheritDoc}
77      * 有効な行番号があれば一緒に出力される。
78      * @return {@inheritDoc}
79      */
80     @Override
81     public String getMessage(){
82         StringBuilder message = new StringBuilder();
83
84         String superMessage = super.getMessage();
85         if(superMessage != null){
86             message.append(superMessage);
87         }
88
89         if(hasValidLineNumber()){
90             if(message.length() > 0) message.append(' ');
91             message.append("[line:").append(this.lineNumber).append(']');
92         }
93
94         if(message.length() <= 0) return null;
95         return message.toString();
96     }
97
98 }