KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > html > SimpleCharStream


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: SimpleCharStream.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene.html;
21
22
23 /**
24  * An implementation of interface CharStream, where the stream is assumed to contain only ASCII
25  * characters (without unicode processing).
26  */

27 public final class SimpleCharStream {
28     public static final boolean staticFlag = false;
29     int bufsize;
30     int available;
31     int tokenBegin;
32     public int bufpos = -1;
33     private int[] bufline;
34     private int[] bufcolumn;
35     private int column = 0;
36     private int line = 1;
37     private boolean prevCharIsCR = false;
38     private boolean prevCharIsLF = false;
39     private java.io.Reader JavaDoc inputStream;
40     private char[] buffer;
41     private int maxNextCharInd = 0;
42     private int inBuf = 0;
43
44     /**
45      * Creates a new SimpleCharStream object.
46      *
47      * @param dstream DOCUMENT ME!
48      * @param startline DOCUMENT ME!
49      * @param startcolumn DOCUMENT ME!
50      * @param buffersize DOCUMENT ME!
51      */

52     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
53         inputStream = dstream;
54         line = startline;
55         column = startcolumn - 1;
56
57         available = bufsize = buffersize;
58         buffer = new char[buffersize];
59         bufline = new int[buffersize];
60         bufcolumn = new int[buffersize];
61     }
62
63     /**
64      * Creates a new SimpleCharStream object.
65      *
66      * @param dstream DOCUMENT ME!
67      * @param startline DOCUMENT ME!
68      * @param startcolumn DOCUMENT ME!
69      */

70     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
71         this(dstream, startline, startcolumn, 4096);
72     }
73
74     /**
75      * Creates a new SimpleCharStream object.
76      *
77      * @param dstream DOCUMENT ME!
78      */

79     public SimpleCharStream(java.io.Reader JavaDoc dstream) {
80         this(dstream, 1, 1, 4096);
81     }
82
83     /**
84      * Creates a new SimpleCharStream object.
85      *
86      * @param dstream DOCUMENT ME!
87      * @param startline DOCUMENT ME!
88      * @param startcolumn DOCUMENT ME!
89      * @param buffersize DOCUMENT ME!
90      */

91     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn,
92         int buffersize) {
93         this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
94     }
95
96     /**
97      * Creates a new SimpleCharStream object.
98      *
99      * @param dstream DOCUMENT ME!
100      * @param startline DOCUMENT ME!
101      * @param startcolumn DOCUMENT ME!
102      */

103     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
104         this(dstream, startline, startcolumn, 4096);
105     }
106
107     /**
108      * Creates a new SimpleCharStream object.
109      *
110      * @param dstream DOCUMENT ME!
111      */

112     public SimpleCharStream(java.io.InputStream JavaDoc dstream) {
113         this(dstream, 1, 1, 4096);
114     }
115
116     private final void ExpandBuff(boolean wrapAround) {
117         char[] newbuffer = new char[bufsize + 2048];
118         int[] newbufline = new int[bufsize + 2048];
119         int[] newbufcolumn = new int[bufsize + 2048];
120
121         try {
122             if (wrapAround) {
123                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
124                 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
125                 buffer = newbuffer;
126
127                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
128                 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
129                 bufline = newbufline;
130
131                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
132                 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
133                 bufcolumn = newbufcolumn;
134
135                 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
136             } else {
137                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
138                 buffer = newbuffer;
139
140                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
141                 bufline = newbufline;
142
143                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
144                 bufcolumn = newbufcolumn;
145
146                 maxNextCharInd = (bufpos -= tokenBegin);
147             }
148         } catch (Throwable JavaDoc t) {
149             throw new Error JavaDoc(t.getMessage());
150         }
151
152         bufsize += 2048;
153         available = bufsize;
154         tokenBegin = 0;
155     }
156
157     private final void FillBuff() throws java.io.IOException JavaDoc {
158         if (maxNextCharInd == available) {
159             if (available == bufsize) {
160                 if (tokenBegin > 2048) {
161                     bufpos = maxNextCharInd = 0;
162                     available = tokenBegin;
163                 } else if (tokenBegin < 0) {
164                     bufpos = maxNextCharInd = 0;
165                 } else {
166                     ExpandBuff(false);
167                 }
168             } else if (available > tokenBegin) {
169                 available = bufsize;
170             } else if ((tokenBegin - available) < 2048) {
171                 ExpandBuff(true);
172             } else {
173                 available = tokenBegin;
174             }
175         }
176
177         int i;
178
179         try {
180             if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
181                 inputStream.close();
182                 throw new java.io.IOException JavaDoc();
183             } else {
184                 maxNextCharInd += i;
185             }
186
187             return;
188         } catch (java.io.IOException JavaDoc e) {
189             --bufpos;
190             backup(0);
191
192             if (tokenBegin == -1) {
193                 tokenBegin = bufpos;
194             }
195
196             throw e;
197         }
198     }
199
200     /**
201      * DOCUMENT ME!
202      *
203      * @return DOCUMENT ME!
204      *
205      * @throws java.io.IOException DOCUMENT ME!
206      */

207     public final char BeginToken() throws java.io.IOException JavaDoc {
208         tokenBegin = -1;
209
210         char c = readChar();
211         tokenBegin = bufpos;
212
213         return c;
214     }
215
216     private final void UpdateLineColumn(char c) {
217         column++;
218
219         if (prevCharIsLF) {
220             prevCharIsLF = false;
221             line += (column = 1);
222         } else if (prevCharIsCR) {
223             prevCharIsCR = false;
224
225             if (c == '\n') {
226                 prevCharIsLF = true;
227             } else {
228                 line += (column = 1);
229             }
230         }
231
232         switch (c) {
233         case '\r':
234             prevCharIsCR = true;
235
236             break;
237
238         case '\n':
239             prevCharIsLF = true;
240
241             break;
242
243         case '\t':
244             column--;
245             column += (8 - (column & 07));
246
247             break;
248
249         default:
250             break;
251         }
252
253         bufline[bufpos] = line;
254         bufcolumn[bufpos] = column;
255     }
256
257     /**
258      * DOCUMENT ME!
259      *
260      * @return DOCUMENT ME!
261      *
262      * @throws java.io.IOException DOCUMENT ME!
263      */

264     public final char readChar() throws java.io.IOException JavaDoc {
265         if (inBuf > 0) {
266             --inBuf;
267
268             if (++bufpos == bufsize) {
269                 bufpos = 0;
270             }
271
272             return buffer[bufpos];
273         }
274
275         if (++bufpos >= maxNextCharInd) {
276             FillBuff();
277         }
278
279         char c = buffer[bufpos];
280
281         UpdateLineColumn(c);
282
283         return (c);
284     }
285
286     /**
287      * DOCUMENT ME!
288      *
289      * @return DOCUMENT ME!
290      *
291      * @see #getEndColumn
292      * @deprecated
293      */

294     public final int getColumn() {
295         return bufcolumn[bufpos];
296     }
297
298     /**
299      * DOCUMENT ME!
300      *
301      * @return DOCUMENT ME!
302      *
303      * @see #getEndLine
304      * @deprecated
305      */

306     public final int getLine() {
307         return bufline[bufpos];
308     }
309
310     /**
311      * DOCUMENT ME!
312      *
313      * @return DOCUMENT ME!
314      */

315     public final int getEndColumn() {
316         return bufcolumn[bufpos];
317     }
318
319     /**
320      * DOCUMENT ME!
321      *
322      * @return DOCUMENT ME!
323      */

324     public final int getEndLine() {
325         return bufline[bufpos];
326     }
327
328     /**
329      * DOCUMENT ME!
330      *
331      * @return DOCUMENT ME!
332      */

333     public final int getBeginColumn() {
334         return bufcolumn[tokenBegin];
335     }
336
337     /**
338      * DOCUMENT ME!
339      *
340      * @return DOCUMENT ME!
341      */

342     public final int getBeginLine() {
343         return bufline[tokenBegin];
344     }
345
346     /**
347      * DOCUMENT ME!
348      *
349      * @param amount DOCUMENT ME!
350      */

351     public final void backup(int amount) {
352         inBuf += amount;
353
354         if ((bufpos -= amount) < 0) {
355             bufpos += bufsize;
356         }
357     }
358
359     /**
360      * DOCUMENT ME!
361      *
362      * @param dstream DOCUMENT ME!
363      * @param startline DOCUMENT ME!
364      * @param startcolumn DOCUMENT ME!
365      * @param buffersize DOCUMENT ME!
366      */

367     public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn, int buffersize) {
368         inputStream = dstream;
369         line = startline;
370         column = startcolumn - 1;
371
372         if ((buffer == null) || (buffersize != buffer.length)) {
373             available = bufsize = buffersize;
374             buffer = new char[buffersize];
375             bufline = new int[buffersize];
376             bufcolumn = new int[buffersize];
377         }
378
379         prevCharIsLF = prevCharIsCR = false;
380         tokenBegin = inBuf = maxNextCharInd = 0;
381         bufpos = -1;
382     }
383
384     /**
385      * DOCUMENT ME!
386      *
387      * @param dstream DOCUMENT ME!
388      * @param startline DOCUMENT ME!
389      * @param startcolumn DOCUMENT ME!
390      */

391     public void ReInit(java.io.Reader JavaDoc dstream, int startline, int startcolumn) {
392         ReInit(dstream, startline, startcolumn, 4096);
393     }
394
395     /**
396      * DOCUMENT ME!
397      *
398      * @param dstream DOCUMENT ME!
399      */

400     public void ReInit(java.io.Reader JavaDoc dstream) {
401         ReInit(dstream, 1, 1, 4096);
402     }
403
404     /**
405      * DOCUMENT ME!
406      *
407      * @param dstream DOCUMENT ME!
408      * @param startline DOCUMENT ME!
409      * @param startcolumn DOCUMENT ME!
410      * @param buffersize DOCUMENT ME!
411      */

412     public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn, int buffersize) {
413         ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
414     }
415
416     /**
417      * DOCUMENT ME!
418      *
419      * @param dstream DOCUMENT ME!
420      */

421     public void ReInit(java.io.InputStream JavaDoc dstream) {
422         ReInit(dstream, 1, 1, 4096);
423     }
424
425     /**
426      * DOCUMENT ME!
427      *
428      * @param dstream DOCUMENT ME!
429      * @param startline DOCUMENT ME!
430      * @param startcolumn DOCUMENT ME!
431      */

432     public void ReInit(java.io.InputStream JavaDoc dstream, int startline, int startcolumn) {
433         ReInit(dstream, startline, startcolumn, 4096);
434     }
435
436     /**
437      * DOCUMENT ME!
438      *
439      * @return DOCUMENT ME!
440      */

441     public final String JavaDoc GetImage() {
442         if (bufpos >= tokenBegin) {
443             return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
444         } else {
445             return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
446             new String JavaDoc(buffer, 0, bufpos + 1);
447         }
448     }
449
450     /**
451      * DOCUMENT ME!
452      *
453      * @param len DOCUMENT ME!
454      *
455      * @return DOCUMENT ME!
456      */

457     public final char[] GetSuffix(int len) {
458         char[] ret = new char[len];
459
460         if ((bufpos + 1) >= len) {
461             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
462         } else {
463             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
464             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
465         }
466
467         return ret;
468     }
469
470     /**
471      * DOCUMENT ME!
472      */

473     public void Done() {
474         buffer = null;
475         bufline = null;
476         bufcolumn = null;
477     }
478
479     /**
480      * Method to adjust line and column numbers for the start of a token.<BR>
481      *
482      * @param newLine DOCUMENT ME!
483      * @param newCol DOCUMENT ME!
484      */

485     public void adjustBeginLineColumn(int newLine, int newCol) {
486         int start = tokenBegin;
487         int len;
488
489         if (bufpos >= tokenBegin) {
490             len = bufpos - tokenBegin + inBuf + 1;
491         } else {
492             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
493         }
494
495         int i = 0;
496         int j = 0;
497         int k = 0;
498         int nextColDiff = 0;
499         int columnDiff = 0;
500
501         while ((i < len) && (bufline[j = start % bufsize] == bufline[k = ++start % bufsize])) {
502             bufline[j] = newLine;
503             nextColDiff = (columnDiff + bufcolumn[k]) - bufcolumn[j];
504             bufcolumn[j] = newCol + columnDiff;
505             columnDiff = nextColDiff;
506             i++;
507         }
508
509         if (i < len) {
510             bufline[j] = newLine++;
511             bufcolumn[j] = newCol + columnDiff;
512
513             while (i++ < len) {
514                 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
515                     bufline[j] = newLine++;
516                 } else {
517                     bufline[j] = newLine;
518                 }
519             }
520         }
521
522         line = bufline[j];
523         column = bufcolumn[j];
524     }
525 }
526
Popular Tags