1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| package org.w3c.tidy; |
55 |
| |
56 |
| import java.io.IOException; |
57 |
| import java.io.InputStream; |
58 |
| import java.io.InputStreamReader; |
59 |
| import java.io.Reader; |
60 |
| import java.io.UnsupportedEncodingException; |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| public class StreamInJavaImpl implements StreamIn |
69 |
| { |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| private static final int CHARBUF_SIZE = 5; |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| private int[] charbuf = new int[CHARBUF_SIZE]; |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| private int bufpos; |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| private Reader reader; |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| private boolean endOfStream; |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| private boolean pushed; |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| private int curcol; |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| private int lastcol; |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| private int curline; |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| private int tabsize; |
120 |
| |
121 |
| private int tabs; |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
238
| protected StreamInJavaImpl(InputStream stream, String encoding, int tabsize) throws UnsupportedEncodingException
|
131 |
| { |
132 |
238
| reader = new InputStreamReader(stream, encoding);
|
133 |
238
| this.pushed = false;
|
134 |
238
| this.tabsize = tabsize;
|
135 |
238
| this.curline = 1;
|
136 |
238
| this.curcol = 1;
|
137 |
238
| this.endOfStream = false;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
0
| protected StreamInJavaImpl(Reader reader, int tabsize)
|
147 |
| { |
148 |
0
| this.reader = reader;
|
149 |
0
| this.pushed = false;
|
150 |
0
| this.tabsize = tabsize;
|
151 |
0
| this.curline = 1;
|
152 |
0
| this.curcol = 1;
|
153 |
0
| this.endOfStream = false;
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
350839
| public int readCharFromStream()
|
160 |
| { |
161 |
350839
| int c;
|
162 |
350839
| try
|
163 |
| { |
164 |
350839
| c = reader.read();
|
165 |
350839
| if (c < 0)
|
166 |
| { |
167 |
280
| endOfStream = true;
|
168 |
| } |
169 |
| |
170 |
| } |
171 |
| catch (IOException e) |
172 |
| { |
173 |
| |
174 |
0
| endOfStream = true;
|
175 |
0
| return END_OF_STREAM;
|
176 |
| } |
177 |
| |
178 |
350839
| return c;
|
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
368247
| public int readChar()
|
185 |
| { |
186 |
368247
| int c;
|
187 |
| |
188 |
368247
| if (this.pushed)
|
189 |
| { |
190 |
25261
| c = this.charbuf[--(this.bufpos)];
|
191 |
25261
| if ((this.bufpos) == 0)
|
192 |
| { |
193 |
25254
| this.pushed = false;
|
194 |
| } |
195 |
| |
196 |
25261
| if (c == '\n')
|
197 |
| { |
198 |
2
| this.curcol = 1;
|
199 |
2
| this.curline++;
|
200 |
2
| return c;
|
201 |
| } |
202 |
| |
203 |
25259
| this.curcol++;
|
204 |
25259
| return c;
|
205 |
| } |
206 |
| |
207 |
342986
| this.lastcol = this.curcol;
|
208 |
| |
209 |
342986
| if (this.tabs > 0)
|
210 |
| { |
211 |
1799
| this.curcol++;
|
212 |
1799
| this.tabs--;
|
213 |
1799
| return ' ';
|
214 |
| } |
215 |
| |
216 |
341187
| c = readCharFromStream();
|
217 |
| |
218 |
341187
| if (c < 0)
|
219 |
| { |
220 |
279
| endOfStream = true;
|
221 |
279
| return END_OF_STREAM;
|
222 |
| } |
223 |
| |
224 |
340908
| if (c == '\n')
|
225 |
| { |
226 |
7
| this.curcol = 1;
|
227 |
7
| this.curline++;
|
228 |
7
| return c;
|
229 |
| } |
230 |
340901
| else if (c == '\r')
|
231 |
| { |
232 |
9652
| c = readCharFromStream();
|
233 |
9652
| if (c != '\n')
|
234 |
| { |
235 |
17
| if (c != END_OF_STREAM)
|
236 |
| { |
237 |
16
| ungetChar(c);
|
238 |
| } |
239 |
17
| c = '\n';
|
240 |
| } |
241 |
9652
| this.curcol = 1;
|
242 |
9652
| this.curline++;
|
243 |
9652
| return c;
|
244 |
| } |
245 |
| |
246 |
331249
| if (c == '\t')
|
247 |
| { |
248 |
258
| this.tabs = this.tabsize - ((this.curcol - 1) % this.tabsize) - 1;
|
249 |
258
| this.curcol++;
|
250 |
258
| c = ' ';
|
251 |
258
| return c;
|
252 |
| } |
253 |
| |
254 |
330991
| this.curcol++;
|
255 |
| |
256 |
330991
| return c;
|
257 |
| } |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
25261
| public void ungetChar(int c)
|
263 |
| { |
264 |
25261
| this.pushed = true;
|
265 |
25261
| if (this.bufpos >= CHARBUF_SIZE)
|
266 |
| { |
267 |
| |
268 |
0
| System.arraycopy(this.charbuf, 0, this.charbuf, 1, CHARBUF_SIZE - 1);
|
269 |
0
| this.bufpos--;
|
270 |
| } |
271 |
25261
| this.charbuf[(this.bufpos)++] = c;
|
272 |
| |
273 |
25261
| if (c == '\n')
|
274 |
| { |
275 |
2
| --this.curline;
|
276 |
| } |
277 |
| |
278 |
25261
| this.curcol = this.lastcol;
|
279 |
| } |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
7927
| public boolean isEndOfStream()
|
285 |
| { |
286 |
7927
| return endOfStream;
|
287 |
| } |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
47385
| public int getCurcol()
|
294 |
| { |
295 |
47385
| return this.curcol;
|
296 |
| } |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
46172
| public int getCurline()
|
303 |
| { |
304 |
46172
| return this.curline;
|
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
238
| public void setLexer(Lexer lexer)
|
311 |
| { |
312 |
| |
313 |
| } |
314 |
| |
315 |
| } |