OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[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  * <p>必要に応じて、パースに失敗した位置を保持する。
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      * エラー文字列に追加出力される。
62      * @return {@inheritDoc}
63      */
64     @Override
65     public String getMessage(){
66         StringBuilder result = new StringBuilder();
67
68         String msg = super.getMessage();
69         if(msg != null) result.append(msg);
70
71         if(hasPosition()){
72             result.append('(')
73                   .append("position:")
74                   .append(this.position)
75                   .append(')');
76         }
77
78         if(result.length() <= 0) return null;
79
80         return result.toString();
81     }
82
83     /**
84      * エラー位置を取得する。
85      * @return 入力ソース先頭からのバイト数で表されるエラー位置。
86      * 負なら無効なエラー位置。
87      */
88     public long getPosition(){
89         return this.position;
90     }
91
92     /**
93      * 有効なエラー位置が設定されているか判定する。
94      * @return エラー位置が有効(非負)ならtrue
95      */
96     public boolean hasPosition(){
97         if(this.position < 0L) return false;
98         return true;
99     }
100
101 }