OSDN Git Service

25fa5aa76b87003503bc9076c098b7848c22873e
[jindolf/JinParser.git] / src / main / java / jp / sourceforge / jindolf / parser / ContentBuilderSJ.java
1 /*\r
2  * content builder for Shift_JIS\r
3  *\r
4  * License : The MIT License\r
5  * Copyright(c) 2009 olyutorskii\r
6  */\r
7 \r
8 package jp.sourceforge.jindolf.parser;\r
9 \r
10 /**\r
11  * "Shift_JIS"エンコーディング用デコードハンドラ。\r
12  * {@link SjisDecoder}からの通知に従い、\r
13  * {@link DecodedContent}へとデコードする。\r
14  */\r
15 public class ContentBuilderSJ extends ContentBuilder{\r
16 \r
17     private boolean hasByte1st;\r
18     private byte byte1st;\r
19 \r
20     /**\r
21      * コンストラクタ。\r
22      * 長さ0で空の{@link DecodedContent}がセットされる。\r
23      */\r
24     public ContentBuilderSJ(){\r
25         this(128);\r
26         return;\r
27     }\r
28 \r
29     /**\r
30      * コンストラクタ。\r
31      * 長さ0で空の{@link DecodedContent}がセットされる。\r
32      * @param capacity 初期容量\r
33      * @throws NegativeArraySizeException 容量指定が負。\r
34      */\r
35     public ContentBuilderSJ(int capacity) throws NegativeArraySizeException{\r
36         super(capacity);\r
37         initImpl();\r
38         return;\r
39     }\r
40 \r
41     /**\r
42      * デコード処理の初期化下請。\r
43      */\r
44     private void initImpl(){\r
45         this.content.init();\r
46         this.hasByte1st = false;\r
47         this.byte1st = 0x00;\r
48         return;\r
49     }\r
50 \r
51     /**\r
52      * デコード処理の初期化。\r
53      */\r
54     @Override\r
55     protected void init(){\r
56         initImpl();\r
57         return;\r
58     }\r
59 \r
60     /**\r
61      * エラー情報をフラッシュする。\r
62      */\r
63     @Override\r
64     protected void flushError(){\r
65         if(this.hasByte1st){\r
66             this.content.addDecodeError(this.byte1st);\r
67             this.hasByte1st = false;\r
68         }\r
69         return;\r
70     }\r
71 \r
72     /**\r
73      * {@inheritDoc}\r
74      * @param seq {@inheritDoc}\r
75      * @throws DecodeException {@inheritDoc}\r
76      */\r
77     public void charContent(CharSequence seq)\r
78             throws DecodeException{\r
79         flushError();\r
80         this.content.append(seq);\r
81         return;\r
82     }\r
83 \r
84     /**\r
85      * {@inheritDoc}\r
86      * @param errorArray {@inheritDoc}\r
87      * @param offset {@inheritDoc}\r
88      * @param length {@inheritDoc}\r
89      * @throws DecodeException {@inheritDoc}\r
90      */\r
91     public void decodingError(byte[] errorArray, int offset, int length)\r
92             throws DecodeException{\r
93         int limit = offset + length;\r
94         for(int bpos = offset; bpos < limit; bpos++){\r
95             byte bval = errorArray[bpos];\r
96             if( ! this.hasByte1st){\r
97                 if(ShiftJis.isShiftJIS1stByte(bval)){\r
98                     this.byte1st = bval;\r
99                     this.hasByte1st = true;\r
100                 }else{\r
101                     this.content.addDecodeError(bval);\r
102                 }\r
103             }else{\r
104                 if(ShiftJis.isShiftJIS2ndByte(bval)){   // 文字集合エラー\r
105                     this.content.addDecodeError(this.byte1st, bval);\r
106                     this.hasByte1st = false;\r
107                 }else if(ShiftJis.isShiftJIS1stByte(bval)){\r
108                     this.content.addDecodeError(this.byte1st);\r
109                     this.byte1st = bval;\r
110                     this.hasByte1st = true;\r
111                 }else{\r
112                     this.content.addDecodeError(this.byte1st);\r
113                     this.content.addDecodeError(bval);\r
114                     this.hasByte1st = false;\r
115                 }\r
116             }\r
117         }\r
118 \r
119         return;\r
120     }\r
121 \r
122 }\r