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 * StreamIn Implementation using java writers.
65 * @author Fabrizio Giustina
66 * @version $Revision: 1.5 $ ($Author: fgiust $)
67 */
68 public class StreamInJavaImpl implements StreamIn
69 {
70
71 /***
72 * number of characters kept in buffer.
73 */
74 private static final int CHARBUF_SIZE = 5;
75
76 /***
77 * character buffer.
78 */
79 private int[] charbuf = new int[CHARBUF_SIZE];
80
81 /***
82 * actual position in buffer.
83 */
84 private int bufpos;
85
86 /***
87 * Java input stream reader.
88 */
89 private Reader reader;
90
91 /***
92 * has end of stream been reached?
93 */
94 private boolean endOfStream;
95
96 /***
97 * Is char pushed?
98 */
99 private boolean pushed;
100
101 /***
102 * current column number.
103 */
104 private int curcol;
105
106 /***
107 * last column.
108 */
109 private int lastcol;
110
111 /***
112 * current line number.
113 */
114 private int curline;
115
116 /***
117 * tab size in chars.
118 */
119 private int tabsize;
120
121 private int tabs;
122
123 /***
124 * Instantiates a new StreamInJavaImpl.
125 * @param stream
126 * @param encoding
127 * @param tabsize
128 * @throws UnsupportedEncodingException
129 */
130 public StreamInJavaImpl(InputStream stream, String encoding, int tabsize) throws UnsupportedEncodingException
131 {
132 reader = new InputStreamReader(stream, encoding);
133 this.pushed = false;
134 this.tabsize = tabsize;
135 this.curline = 1;
136 this.curcol = 1;
137 this.endOfStream = false;
138 }
139
140 /***
141 * @see org.w3c.tidy.StreamIn#readCharFromStream()
142 */
143 public int readCharFromStream()
144 {
145 int c;
146 try
147 {
148 c = reader.read();
149 if (c < 0)
150 {
151 endOfStream = true;
152 }
153
154 }
155 catch (IOException e)
156 {
157
158 endOfStream = true;
159 return END_OF_STREAM;
160 }
161
162 return c;
163 }
164
165 /***
166 * @see org.w3c.tidy.StreamIn#readChar()
167 */
168 public int readChar()
169 {
170 int c;
171
172 if (this.pushed)
173 {
174 c = this.charbuf[--(this.bufpos)];
175 if ((this.bufpos) == 0)
176 {
177 this.pushed = false;
178 }
179
180 if (c == '\n')
181 {
182 this.curcol = 1;
183 this.curline++;
184 return c;
185 }
186
187 this.curcol++;
188 return c;
189 }
190
191 this.lastcol = this.curcol;
192
193 if (this.tabs > 0)
194 {
195 this.curcol++;
196 this.tabs--;
197 return ' ';
198 }
199
200 c = readCharFromStream();
201
202 if (c < 0)
203 {
204 endOfStream = true;
205 return END_OF_STREAM;
206 }
207
208 if (c == '\n')
209 {
210 this.curcol = 1;
211 this.curline++;
212 return c;
213 }
214 else if (c == '\r')
215 {
216 c = readCharFromStream();
217 if (c != '\n')
218 {
219 if (c != END_OF_STREAM)
220 {
221 ungetChar(c);
222 }
223 c = '\n';
224 }
225 this.curcol = 1;
226 this.curline++;
227 return c;
228 }
229
230 if (c == '\t')
231 {
232 this.tabs = this.tabsize - ((this.curcol - 1) % this.tabsize) - 1;
233 this.curcol++;
234 c = ' ';
235 return c;
236 }
237
238 this.curcol++;
239
240 return c;
241 }
242
243 /***
244 * @see org.w3c.tidy.StreamIn#ungetChar(int)
245 */
246 public void ungetChar(int c)
247 {
248 this.pushed = true;
249 if (this.bufpos >= CHARBUF_SIZE)
250 {
251
252 System.arraycopy(this.charbuf, 0, this.charbuf, 1, CHARBUF_SIZE - 1);
253 this.bufpos--;
254 }
255 this.charbuf[(this.bufpos)++] = c;
256
257 if (c == '\n')
258 {
259 --this.curline;
260 }
261
262 this.curcol = this.lastcol;
263 }
264
265 /***
266 * @see org.w3c.tidy.StreamIn#isEndOfStream()
267 */
268 public boolean isEndOfStream()
269 {
270 return endOfStream;
271 }
272
273 /***
274 * Getter for <code>curcol</code>.
275 * @return Returns the curcol.
276 */
277 public int getCurcol()
278 {
279 return this.curcol;
280 }
281
282 /***
283 * Getter for <code>curline</code>.
284 * @return Returns the curline.
285 */
286 public int getCurline()
287 {
288 return this.curline;
289 }
290
291 /***
292 * @see org.w3c.tidy.StreamIn#setLexer(org.w3c.tidy.Lexer)
293 */
294 public void setLexer(Lexer lexer)
295 {
296
297 }
298
299 }