OSDN Git Service

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