OSDN Git Service

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