OSDN Git Service

改行コード指定
[jindolf/JinParser.git] / src / main / java / jp / sourceforge / jindolf / parser / ContentBuilderUCS2.java
1 /*
2  * content builder for UTF-8 (UCS2 only)
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.parser;
9
10 /**
11  * "UTF-8"エンコーディング用デコードハンドラ。
12  * {@link StreamDecoder}からの通知に従い、
13  * {@link DecodedContent}へとデコードする。
14  * UCS-4はUTF-16エラー扱い。
15  */
16 public class ContentBuilderUCS2 extends ContentBuilder{
17
18     private static final int DEF_BUF_SZ = 128;
19
20
21     /**
22      * コンストラクタ。
23      * 長さ0で空の{@link DecodedContent}がセットされる。
24      */
25     public ContentBuilderUCS2(){
26         this(DEF_BUF_SZ);
27         return;
28     }
29
30     /**
31      * コンストラクタ。
32      * 長さ0で空の{@link DecodedContent}がセットされる。
33      * @param capacity 初期容量
34      * @throws NegativeArraySizeException 容量指定が負。
35      */
36     public ContentBuilderUCS2(int capacity)
37             throws NegativeArraySizeException{
38         super(capacity);
39         initImpl();
40         return;
41     }
42
43
44     /**
45      * サロゲートペア文字(上位,下位)をUTF-16BEバイト列に変換する。
46      * @param ch 文字
47      * @return UTF-8バイト列
48      */
49     public static byte[] charToUTF16(char ch){
50         byte[] result = new byte[2];
51         result[0] = (byte)(ch >> 8);
52         result[1] = (byte)(ch & 0xff);
53
54         return result;
55     }
56
57
58     /**
59      * デコード処理の初期化下請。
60      */
61     private void initImpl(){
62         this.getContent().init();
63         return;
64     }
65
66     /**
67      * デコード処理の初期化。
68      */
69     @Override
70     protected void init(){
71         initImpl();
72         return;
73     }
74
75     /**
76      * {@inheritDoc}
77      * @param seq {@inheritDoc}
78      * @throws DecodeException {@inheritDoc}
79      */
80     @Override
81     public void charContent(CharSequence seq)
82             throws DecodeException{
83         flushError();
84
85         int length = seq.length();
86         int startPos = 0;
87
88         for(int pos = 0; pos < length; pos++){
89             char ch = seq.charAt(pos);
90
91             if(   ! Character.isHighSurrogate(ch)
92                && ! Character.isLowSurrogate (ch) ){
93                 continue;
94             }
95
96             if(startPos < pos){
97                 CharSequence chopped = seq.subSequence(startPos, pos);
98                 getContent().append(chopped);
99                 startPos = pos + 1;
100             }
101
102             byte[] barr = charToUTF16(ch);
103             for(byte bval : barr){
104                 getContent().addDecodeError(bval);
105             }
106         }
107
108         if(startPos < length){
109             CharSequence chopped = seq.subSequence(startPos, length);
110             getContent().append(chopped);
111         }
112
113         return;
114     }
115
116     /**
117      * {@inheritDoc}
118      * @param errorArray {@inheritDoc}
119      * @param offset {@inheritDoc}
120      * @param length {@inheritDoc}
121      * @throws DecodeException {@inheritDoc}
122      */
123     @Override
124     public void decodingError(byte[] errorArray, int offset, int length)
125             throws DecodeException{
126         int limit = offset + length;
127
128         for(int bpos = offset; bpos < limit; bpos++){
129             byte bval = errorArray[bpos];
130             getContent().addDecodeError(bval);
131         }
132
133         return;
134     }
135
136 }