2 * invalid Shift_JIS decoding information
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2009 olyutorskii
\r
8 package jp.sourceforge.jindolf.parser;
\r
10 import java.util.Comparator;
\r
13 * 不正な Shift_JIS デコードの情報。
\r
14 * 1バイトもしくは2バイトで構成される。
\r
15 * 1バイトの場合はおそらくエンコーディングに関するエラー。
\r
16 * 2バイトの場合はおそらく文字集合に関するエラー。
\r
18 public class DecodeErrorInfo{
\r
20 /** 出現位置順Comparator。 */
\r
21 public static final Comparator<DecodeErrorInfo> POS_COMPARATOR =
\r
22 new PosComparator();
\r
24 private final int charPos;
\r
25 private final boolean has2ndFlag;
\r
26 private final byte rawByte1st;
\r
27 private final byte rawByte2nd;
\r
31 * @param charPos デコードエラーで置き換えられた文字列の開始位置
\r
32 * @param has2ndFlag 2バイト目が有効ならtrueを渡す。
\r
33 * @param rawByte1st デコードエラーを引き起こした最初のバイト値
\r
34 * @param rawByte2nd デコードエラーを引き起こした2番目のバイト値
\r
35 * @throws IndexOutOfBoundsException charPosが負
\r
37 private DecodeErrorInfo(int charPos,
\r
41 throws IndexOutOfBoundsException{
\r
42 if(charPos < 0) throw new IndexOutOfBoundsException();
\r
44 this.charPos = charPos;
\r
45 this.has2ndFlag = has2ndFlag;
\r
46 this.rawByte1st = rawByte1st;
\r
47 this.rawByte2nd = rawByte2nd;
\r
54 * @param charPos デコードエラーで置き換えられた文字列の開始位置
\r
55 * @param rawByte1st デコードエラーを引き起こした最初のバイト値
\r
56 * @param rawByte2nd デコードエラーを引き起こした2番目のバイト値
\r
57 * @throws IndexOutOfBoundsException charPosが負
\r
59 public DecodeErrorInfo(int charPos,
\r
62 throws IndexOutOfBoundsException{
\r
63 this(charPos, true, rawByte1st, rawByte2nd);
\r
69 * @param charPos デコードエラーで置き換えられた文字列の開始位置
\r
70 * @param rawByte1st デコードエラーを引き起こしたバイト値
\r
71 * @throws IndexOutOfBoundsException charPosが負
\r
73 public DecodeErrorInfo(int charPos,
\r
75 throws IndexOutOfBoundsException{
\r
76 this(charPos, false, rawByte1st, (byte)0x00);
\r
81 * デコードエラーで置き換えられた文字列の開始位置を返す。
\r
82 * @return デコードエラーで置き換えられた文字列の開始位置
\r
84 public int getCharPosition(){
\r
85 return this.charPos;
\r
90 * @return 2バイト目の情報を持つならtrue
\r
92 public boolean has2nd(){
\r
93 return this.has2ndFlag;
\r
100 public byte getRawByte1st(){
\r
101 return this.rawByte1st;
\r
107 * @throws IllegalStateException 2バイト目の情報を把持していないとき
\r
109 public byte getRawByte2nd() throws IllegalStateException{
\r
110 if( ! this.has2ndFlag ) throw new IllegalStateException();
\r
111 return this.rawByte2nd;
\r
115 * 出現位置のみが違う複製オブジェクトを生成する。
\r
116 * @param gap 出現位置から引きたい値。正の値なら文字開始位置に向かう。
\r
118 * @throws IndexOutOfBoundsException 再計算された出現位置が負
\r
120 public DecodeErrorInfo createGappedClone(int gap)
\r
121 throws IndexOutOfBoundsException{
\r
122 DecodeErrorInfo result;
\r
124 int newPos = this.charPos - gap;
\r
125 if(this.has2ndFlag){
\r
126 result = new DecodeErrorInfo(newPos,
\r
127 this.rawByte1st, this.rawByte2nd);
\r
129 result = new DecodeErrorInfo(newPos, this.rawByte1st);
\r
137 * @return {@inheritDoc}
\r
140 public String toString(){
\r
141 StringBuilder result = new StringBuilder();
\r
143 result.append("start:").append(this.charPos).append(' ');
\r
146 hex = Integer.toHexString(this.rawByte1st & 0xff);
\r
147 if(hex.length() <= 1) result.append('0');
\r
148 result.append(hex);
\r
150 if(this.has2ndFlag){
\r
151 hex = Integer.toHexString(this.rawByte2nd & 0xff);
\r
152 result.append(':');
\r
153 if(hex.length() <= 1) result.append('0');
\r
154 result.append(hex);
\r
157 return result.toString();
\r
163 private static final class PosComparator
\r
164 implements Comparator<DecodeErrorInfo> {
\r
169 private PosComparator(){
\r
176 * @param info1 {@inheritDoc}
\r
177 * @param info2 {@inheritDoc}
\r
178 * @return {@inheritDoc}
\r
181 public int compare(DecodeErrorInfo info1, DecodeErrorInfo info2){
\r
185 if(info1 == null) pos1 = -1;
\r
186 else pos1 = info1.charPos;
\r
188 if(info2 == null) pos2 = -1;
\r
189 else pos2 = info2.charPos;
\r
191 return pos1 - pos2;
\r