001: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
002: // package com.tchibo.misc.lucene;
003: package vqwiki.utils.lucene;
004:
005: /**
006: * An implementation of interface CharStream, where the stream is assumed to
007: * contain only ASCII characters (without unicode processing).
008: *
009: * @version $Id: SimpleCharStream.java 365 2003-10-05 05:07:32Z garethc $
010: */
011:
012: public final class SimpleCharStream {
013: /**
014: * TODO: Document this field.
015: */
016: int available;
017: /**
018: * TODO: Document this field.
019: */
020: private int bufcolumn[];
021:
022: /**
023: * TODO: Document this field.
024: */
025: private char[] buffer;
026: /**
027: * TODO: Document this field.
028: */
029: private int bufline[];
030: /**
031: * TODO: Document this field.
032: */
033: public int bufpos = -1;
034: /**
035: * TODO: Document this field.
036: */
037: int bufsize;
038:
039: /**
040: * TODO: Document this field.
041: */
042: private int column = 0;
043: /**
044: * TODO: Document this field.
045: */
046: private int inBuf = 0;
047:
048: /**
049: * TODO: Document this field.
050: */
051: private java.io.Reader inputStream;
052: /**
053: * TODO: Document this field.
054: */
055: private int line = 1;
056: /**
057: * TODO: Document this field.
058: */
059: private int maxNextCharInd = 0;
060:
061: /**
062: * TODO: Document this field.
063: */
064: private boolean prevCharIsCR = false;
065: /**
066: * TODO: Document this field.
067: */
068: private boolean prevCharIsLF = false;
069: /**
070: * TODO: Document this field.
071: */
072: public final static boolean staticFlag = false;
073: /**
074: * TODO: Document this field.
075: */
076: int tokenBegin;
077:
078: /**
079: *Creates a new SimpleCharStream.
080: *
081: * @param dstream TODO: Document this parameter.
082: * @param startline TODO: Document this parameter.
083: * @param startcolumn TODO: Document this parameter.
084: * @param buffersize TODO: Document this parameter.
085: */
086: public SimpleCharStream(java.io.Reader dstream, int startline,
087: int startcolumn, int buffersize) {
088: inputStream = dstream;
089: line = startline;
090: column = startcolumn - 1;
091:
092: available = bufsize = buffersize;
093: buffer = new char[buffersize];
094: bufline = new int[buffersize];
095: bufcolumn = new int[buffersize];
096: }
097:
098: /**
099: *Creates a new SimpleCharStream.
100: *
101: * @param dstream TODO: Document this parameter.
102: * @param startline TODO: Document this parameter.
103: * @param startcolumn TODO: Document this parameter.
104: */
105: public SimpleCharStream(java.io.Reader dstream, int startline,
106: int startcolumn) {
107: this (dstream, startline, startcolumn, 4096);
108: }
109:
110: /**
111: *Creates a new SimpleCharStream.
112: *
113: * @param dstream TODO: Document this parameter.
114: */
115: public SimpleCharStream(java.io.Reader dstream) {
116: this (dstream, 1, 1, 4096);
117: }
118:
119: /**
120: *Creates a new SimpleCharStream.
121: *
122: * @param dstream TODO: Document this parameter.
123: * @param startline TODO: Document this parameter.
124: * @param startcolumn TODO: Document this parameter.
125: * @param buffersize TODO: Document this parameter.
126: */
127: public SimpleCharStream(java.io.InputStream dstream, int startline,
128: int startcolumn, int buffersize) {
129: this (new java.io.InputStreamReader(dstream), startline,
130: startcolumn, 4096);
131: }
132:
133: /**
134: *Creates a new SimpleCharStream.
135: *
136: * @param dstream TODO: Document this parameter.
137: * @param startline TODO: Document this parameter.
138: * @param startcolumn TODO: Document this parameter.
139: */
140: public SimpleCharStream(java.io.InputStream dstream, int startline,
141: int startcolumn) {
142: this (dstream, startline, startcolumn, 4096);
143: }
144:
145: /**
146: *Creates a new SimpleCharStream.
147: *
148: * @param dstream TODO: Document this parameter.
149: */
150: public SimpleCharStream(java.io.InputStream dstream) {
151: this (dstream, 1, 1, 4096);
152: }
153:
154: /**
155: * TODO: Document this method.
156: *
157: * @return TODO: Document the result.
158: * @exception java.io.IOException TODO: Document this exception.
159: */
160: public final char BeginToken() throws java.io.IOException {
161: tokenBegin = -1;
162: char c = readChar();
163: tokenBegin = bufpos;
164:
165: return c;
166: }
167:
168: /**
169: * TODO: Document this method.
170: */
171: public void Done() {
172: buffer = null;
173: bufline = null;
174: bufcolumn = null;
175: }
176:
177: /**
178: * TODO: Document this method.
179: *
180: * @param wrapAround TODO: Document this parameter.
181: */
182: private final void ExpandBuff(boolean wrapAround) {
183: char[] newbuffer = new char[bufsize + 2048];
184: int newbufline[] = new int[bufsize + 2048];
185: int newbufcolumn[] = new int[bufsize + 2048];
186:
187: try {
188: if (wrapAround) {
189: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
190: bufsize - tokenBegin);
191: System.arraycopy(buffer, 0, newbuffer, bufsize
192: - tokenBegin, bufpos);
193: buffer = newbuffer;
194:
195: System.arraycopy(bufline, tokenBegin, newbufline, 0,
196: bufsize - tokenBegin);
197: System.arraycopy(bufline, 0, newbufline, bufsize
198: - tokenBegin, bufpos);
199: bufline = newbufline;
200:
201: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
202: 0, bufsize - tokenBegin);
203: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
204: - tokenBegin, bufpos);
205: bufcolumn = newbufcolumn;
206:
207: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
208: } else {
209: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
210: bufsize - tokenBegin);
211: buffer = newbuffer;
212:
213: System.arraycopy(bufline, tokenBegin, newbufline, 0,
214: bufsize - tokenBegin);
215: bufline = newbufline;
216:
217: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
218: 0, bufsize - tokenBegin);
219: bufcolumn = newbufcolumn;
220:
221: maxNextCharInd = (bufpos -= tokenBegin);
222: }
223: } catch (Throwable t) {
224: throw new Error(t.getMessage());
225: }
226:
227: bufsize += 2048;
228: available = bufsize;
229: tokenBegin = 0;
230: }
231:
232: /**
233: * TODO: Document this method.
234: *
235: * @exception java.io.IOException TODO: Document this exception.
236: */
237: private final void FillBuff() throws java.io.IOException {
238: if (maxNextCharInd == available) {
239: if (available == bufsize) {
240: if (tokenBegin > 2048) {
241: bufpos = maxNextCharInd = 0;
242: available = tokenBegin;
243: } else if (tokenBegin < 0) {
244: bufpos = maxNextCharInd = 0;
245: } else {
246: ExpandBuff(false);
247: }
248: } else if (available > tokenBegin) {
249: available = bufsize;
250: } else if ((tokenBegin - available) < 2048) {
251: ExpandBuff(true);
252: } else {
253: available = tokenBegin;
254: }
255: }
256:
257: int i;
258: try {
259: if ((i = inputStream.read(buffer, maxNextCharInd, available
260: - maxNextCharInd)) == -1) {
261: inputStream.close();
262: throw new java.io.IOException();
263: } else {
264: maxNextCharInd += i;
265: }
266: return;
267: } catch (java.io.IOException e) {
268: --bufpos;
269: backup(0);
270: if (tokenBegin == -1) {
271: tokenBegin = bufpos;
272: }
273: throw e;
274: }
275: }
276:
277: /**
278: * TODO: Document this method.
279: *
280: * @return TODO: Document the result.
281: */
282: public final String GetImage() {
283: if (bufpos >= tokenBegin) {
284: return new String(buffer, tokenBegin, bufpos - tokenBegin
285: + 1);
286: } else {
287: return new String(buffer, tokenBegin, bufsize - tokenBegin)
288: + new String(buffer, 0, bufpos + 1);
289: }
290: }
291:
292: /**
293: * TODO: Document this method.
294: *
295: * @param len TODO: Document this parameter.
296: * @return TODO: Document the result.
297: */
298: public final char[] GetSuffix(int len) {
299: char[] ret = new char[len];
300:
301: if ((bufpos + 1) >= len) {
302: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
303: } else {
304: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
305: 0, len - bufpos - 1);
306: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
307: bufpos + 1);
308: }
309:
310: return ret;
311: }
312:
313: /**
314: * TODO: Document this method.
315: *
316: * @param dstream TODO: Document this parameter.
317: * @param startline TODO: Document this parameter.
318: * @param startcolumn TODO: Document this parameter.
319: * @param buffersize TODO: Document this parameter.
320: */
321: public void ReInit(java.io.Reader dstream, int startline,
322: int startcolumn, int buffersize) {
323: inputStream = dstream;
324: line = startline;
325: column = startcolumn - 1;
326:
327: if (buffer == null || buffersize != buffer.length) {
328: available = bufsize = buffersize;
329: buffer = new char[buffersize];
330: bufline = new int[buffersize];
331: bufcolumn = new int[buffersize];
332: }
333: prevCharIsLF = prevCharIsCR = false;
334: tokenBegin = inBuf = maxNextCharInd = 0;
335: bufpos = -1;
336: }
337:
338: /**
339: * TODO: Document this method.
340: *
341: * @param dstream TODO: Document this parameter.
342: * @param startline TODO: Document this parameter.
343: * @param startcolumn TODO: Document this parameter.
344: */
345: public void ReInit(java.io.Reader dstream, int startline,
346: int startcolumn) {
347: ReInit(dstream, startline, startcolumn, 4096);
348: }
349:
350: /**
351: * TODO: Document this method.
352: *
353: * @param dstream TODO: Document this parameter.
354: */
355: public void ReInit(java.io.Reader dstream) {
356: ReInit(dstream, 1, 1, 4096);
357: }
358:
359: /**
360: * TODO: Document this method.
361: *
362: * @param dstream TODO: Document this parameter.
363: * @param startline TODO: Document this parameter.
364: * @param startcolumn TODO: Document this parameter.
365: * @param buffersize TODO: Document this parameter.
366: */
367: public void ReInit(java.io.InputStream dstream, int startline,
368: int startcolumn, int buffersize) {
369: ReInit(new java.io.InputStreamReader(dstream), startline,
370: startcolumn, 4096);
371: }
372:
373: /**
374: * TODO: Document this method.
375: *
376: * @param dstream TODO: Document this parameter.
377: */
378: public void ReInit(java.io.InputStream dstream) {
379: ReInit(dstream, 1, 1, 4096);
380: }
381:
382: /**
383: * TODO: Document this method.
384: *
385: * @param dstream TODO: Document this parameter.
386: * @param startline TODO: Document this parameter.
387: * @param startcolumn TODO: Document this parameter.
388: */
389: public void ReInit(java.io.InputStream dstream, int startline,
390: int startcolumn) {
391: ReInit(dstream, startline, startcolumn, 4096);
392: }
393:
394: /**
395: * TODO: Document this method.
396: *
397: * @param c TODO: Document this parameter.
398: */
399: private final void UpdateLineColumn(char c) {
400: column++;
401:
402: if (prevCharIsLF) {
403: prevCharIsLF = false;
404: line += (column = 1);
405: } else if (prevCharIsCR) {
406: prevCharIsCR = false;
407: if (c == '\n') {
408: prevCharIsLF = true;
409: } else {
410: line += (column = 1);
411: }
412: }
413:
414: switch (c) {
415: case '\r':
416: prevCharIsCR = true;
417: break;
418: case '\n':
419: prevCharIsLF = true;
420: break;
421: case '\t':
422: column--;
423: column += (8 - (column & 07));
424: break;
425: default:
426: break;
427: }
428:
429: bufline[bufpos] = line;
430: bufcolumn[bufpos] = column;
431: }
432:
433: /**
434: * Method to adjust line and column numbers for the start of a token.<BR>
435: *
436: * @param newLine TODO: Document this parameter.
437: * @param newCol TODO: Document this parameter.
438: */
439: public void adjustBeginLineColumn(int newLine, int newCol) {
440: int start = tokenBegin;
441: int len;
442:
443: if (bufpos >= tokenBegin) {
444: len = bufpos - tokenBegin + inBuf + 1;
445: } else {
446: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
447: }
448:
449: int i = 0;
450:
451: int j = 0;
452:
453: int k = 0;
454: int nextColDiff = 0;
455: int columnDiff = 0;
456:
457: while (i < len
458: && bufline[j = start % bufsize] == bufline[k = ++start
459: % bufsize]) {
460: bufline[j] = newLine;
461: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
462: bufcolumn[j] = newCol + columnDiff;
463: columnDiff = nextColDiff;
464: i++;
465: }
466:
467: if (i < len) {
468: bufline[j] = newLine++;
469: bufcolumn[j] = newCol + columnDiff;
470:
471: while (i++ < len) {
472: if (bufline[j = start % bufsize] != bufline[++start
473: % bufsize]) {
474: bufline[j] = newLine++;
475: } else {
476: bufline[j] = newLine;
477: }
478: }
479: }
480:
481: line = bufline[j];
482: column = bufcolumn[j];
483: }
484:
485: /**
486: * TODO: Document this method.
487: *
488: * @param amount TODO: Document this parameter.
489: */
490: public final void backup(int amount) {
491:
492: inBuf += amount;
493: if ((bufpos -= amount) < 0) {
494: bufpos += bufsize;
495: }
496: }
497:
498: /**
499: * Returns the BeginColumn.
500: *
501: * @return The BeginColumn.
502: */
503: public final int getBeginColumn() {
504: return bufcolumn[tokenBegin];
505: }
506:
507: /**
508: * Returns the BeginLine.
509: *
510: * @return The BeginLine.
511: */
512: public final int getBeginLine() {
513: return bufline[tokenBegin];
514: }
515:
516: /**
517: * @return The Column.
518: * @see #getEndColumn
519: * @deprecated
520: */
521:
522: public final int getColumn() {
523: return bufcolumn[bufpos];
524: }
525:
526: /**
527: * Returns the EndColumn.
528: *
529: * @return The EndColumn.
530: */
531: public final int getEndColumn() {
532: return bufcolumn[bufpos];
533: }
534:
535: /**
536: * Returns the EndLine.
537: *
538: * @return The EndLine.
539: */
540: public final int getEndLine() {
541: return bufline[bufpos];
542: }
543:
544: /**
545: * @return The Line.
546: * @see #getEndLine
547: * @deprecated
548: */
549:
550: public final int getLine() {
551: return bufline[bufpos];
552: }
553:
554: /**
555: * TODO: Document this method.
556: *
557: * @return TODO: Document the result.
558: * @exception java.io.IOException TODO: Document this exception.
559: */
560: public final char readChar() throws java.io.IOException {
561: if (inBuf > 0) {
562: --inBuf;
563:
564: if (++bufpos == bufsize) {
565: bufpos = 0;
566: }
567:
568: return buffer[bufpos];
569: }
570:
571: if (++bufpos >= maxNextCharInd) {
572: FillBuff();
573: }
574:
575: char c = buffer[bufpos];
576:
577: UpdateLineColumn(c);
578: return (c);
579: }
580:
581: }
|