OSDN Git Service

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