OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / parser / MmdFormatException.java
1 /*
2  * unexpected file format founded exception
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.parser;
9
10 /**
11  * MMD関連ファイルのパース異常系。
12  * 必要に応じて、パースに失敗した位置を保持する。
13  */
14 @SuppressWarnings("serial")
15 public class MmdFormatException extends Exception {
16
17     private final long position;
18
19     /**
20      * コンストラクタ。
21      */
22     public MmdFormatException(){
23         this(null);
24         return;
25     }
26
27     /**
28      * コンストラクタ。
29      * @param message エラーメッセージ
30      */
31     public MmdFormatException(String message){
32         this(message, -1L);
33         return;
34     }
35
36     /**
37      * コンストラクタ。
38      * @param position 入力ソース先頭から数えたエラー位置。(バイト単位)
39      * 負の値を与えると、エラー位置は無効と解釈される。
40      */
41     public MmdFormatException(long position){
42         this(null, position);
43         return;
44     }
45
46     /**
47      * コンストラクタ。
48      * @param message エラーメッセージ
49      * @param position 入力ソース先頭から数えたエラー位置。(バイト単位)
50      * 負の値を与えると、エラー位置は無効と解釈される。
51      */
52     public MmdFormatException(String message, long position){
53         super(message);
54         this.position = position;
55         return;
56     }
57
58     /**
59      * {@inheritDoc}
60      * 有効なエラー発生位置を保持している場合、追加出力される。
61      * @return {@inheritDoc}
62      */
63     @Override
64     public String getMessage(){
65         StringBuilder result = new StringBuilder();
66
67         String msg = super.getMessage();
68         if(msg != null) result.append(msg);
69
70         if(hasPosition()){
71             result.append('(')
72                   .append("position:")
73                   .append(this.position)
74                   .append(')');
75         }
76
77         if(result.length() <= 0) return null;
78
79         return result.toString();
80     }
81
82     /**
83      * エラー位置を取得する。
84      * @return 入力ソース先頭からのバイト数で表されるエラー位置。
85      * 負なら無効なエラー位置。
86      */
87     public long getPosition(){
88         return this.position;
89     }
90
91     /**
92      * 有効なエラー位置が設定されているか判定する。
93      * @return エラー位置が有効(非負)ならtrue
94      */
95     public boolean hasPosition(){
96         if(this.position < 0L) return false;
97         return true;
98     }
99
100 }