OSDN Git Service

rename package
[jindolf/JinParser.git] / src / test / java / jp / osdn / jindolf / parser / content / TestListener.java
1 /*
2  * License : The MIT License
3  * Copyright(c) 2018 olyutorskii
4  */
5
6 package jp.osdn.jindolf.parser.content;
7
8 import io.bitbucket.olyutorskii.jiocema.CharDecodeListener;
9 import io.bitbucket.olyutorskii.jiocema.DecodeBreakException;
10 import java.nio.charset.CharsetDecoder;
11
12 /**
13  * Test listener for {@link CharDecodeListener}
14  */
15 class TestListener implements CharDecodeListener{
16
17     private final StringBuilder text = new StringBuilder();
18
19
20     @Override
21     public void startDecoding(CharsetDecoder decoder)
22             throws DecodeBreakException {
23         append("[ST]");
24         return;
25     }
26
27     @Override
28     public void endDecoding() throws DecodeBreakException {
29         append("[EN]");
30         return;
31     }
32
33     @Override
34     public void charContent(char[] charArray, int offset, int length)
35             throws DecodeBreakException {
36         append("[CH]");
37         this.text.append(charArray, offset, length);
38         return;
39     }
40
41     @Override
42     public void rawBytes(byte[] byteArray, int offset, int length)
43             throws DecodeBreakException {
44         // NOTHING
45         return;
46     }
47
48     @Override
49     public void malformedError(byte[] errorArray, int offset, int length)
50             throws DecodeBreakException {
51         append("[ME]");
52         dumpHex(errorArray, offset, length);
53         return;
54     }
55
56     @Override
57     public void unmapError(byte[] errorArray, int offset, int length)
58             throws DecodeBreakException {
59         append("[UE]");
60         dumpHex(errorArray, offset, length);
61         return;
62     }
63
64     protected void append(CharSequence seq){
65         this.text.append(seq);
66         return;
67     }
68
69     protected void dumpHex(byte[] errorArray, int offset, int length){
70         for(int ct = 0; ct < length; ct++){
71             dumpHex(errorArray[offset + ct]);
72         }
73         return;
74     }
75
76     private void dumpHex(byte bVal){
77         int val = bVal & 0xff;
78         if(val <= 0xf) this.text.append('0');
79         append(Integer.toHexString(val));
80         return;
81     }
82
83     public void clear(){
84         text.setLength(0);
85         return;
86     }
87
88     @Override
89     public String toString(){
90         return text.toString();
91     }
92
93 }