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