001: /*
002: * $RCSfile: VrmlCharStream.java,v $
003: *
004: * @(#)VrmlCharStream.java 1.5 98/11/05 20:35:31
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.3 $
033: * $Date: 2006/03/30 08:19:28 $
034: * $State: Exp $
035: */
036: /*
037: * Generated by JavaCC, renamed from ASCII_CharStream to VrmlCharStream to
038: * avoid name conflict. Had to put into SCCS since JavaCC doesn't geneate
039: * the file when using a user token manager
040: */
041: // Search for _Node to see start of nodes
042: package org.jdesktop.j3d.loaders.vrml97.impl;
043:
044: /**
045: * An implementation of interface CharStream, where the stream is assumed to
046: * contain only ASCII characters (without unicode processing).
047: */
048:
049: public final class VrmlCharStream {
050: /** Description of the Field */
051: public final static boolean staticFlag = false;
052: int bufsize;
053: int available;
054: int tokenBegin;
055: /** Description of the Field */
056: public int bufpos = -1;
057: private int bufline[];
058: private int bufcolumn[];
059:
060: private int column = 0;
061: private int line = 1;
062:
063: private boolean prevCharIsCR = false;
064: private boolean prevCharIsLF = false;
065:
066: private java.io.Reader inputStream;
067:
068: private char[] buffer;
069: private int maxNextCharInd = 0;
070: private int inBuf = 0;
071:
072: /**
073: * Description of the Method
074: *
075: *@param wrapAround Description of the Parameter
076: */
077: private final void ExpandBuff(boolean wrapAround) {
078: char[] newbuffer = new char[bufsize + 2048];
079: int newbufline[] = new int[bufsize + 2048];
080: int newbufcolumn[] = new int[bufsize + 2048];
081:
082: try {
083: if (wrapAround) {
084: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
085: bufsize - tokenBegin);
086: System.arraycopy(buffer, 0, newbuffer, bufsize
087: - tokenBegin, bufpos);
088: buffer = newbuffer;
089:
090: System.arraycopy(bufline, tokenBegin, newbufline, 0,
091: bufsize - tokenBegin);
092: System.arraycopy(bufline, 0, newbufline, bufsize
093: - tokenBegin, bufpos);
094: bufline = newbufline;
095:
096: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
097: 0, bufsize - tokenBegin);
098: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
099: - tokenBegin, bufpos);
100: bufcolumn = newbufcolumn;
101:
102: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
103: } else {
104: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
105: bufsize - tokenBegin);
106: buffer = newbuffer;
107:
108: System.arraycopy(bufline, tokenBegin, newbufline, 0,
109: bufsize - tokenBegin);
110: bufline = newbufline;
111:
112: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
113: 0, bufsize - tokenBegin);
114: bufcolumn = newbufcolumn;
115:
116: maxNextCharInd = (bufpos -= tokenBegin);
117: }
118: } catch (Throwable t) {
119: throw new Error(t);
120: }
121:
122: bufsize += 2048;
123: available = bufsize;
124: tokenBegin = 0;
125: }
126:
127: /**
128: * Description of the Method
129: *
130: *@exception java.io.IOException Description of the Exception
131: */
132: private final void FillBuff() throws java.io.IOException {
133: if (maxNextCharInd == available) {
134: if (available == bufsize) {
135: if (tokenBegin > 2048) {
136: bufpos = maxNextCharInd = 0;
137: available = tokenBegin;
138: } else if (tokenBegin < 0) {
139: bufpos = maxNextCharInd = 0;
140: } else {
141: ExpandBuff(false);
142: }
143: } else if (available > tokenBegin) {
144: available = bufsize;
145: } else if ((tokenBegin - available) < 2048) {
146: ExpandBuff(true);
147: } else {
148: available = tokenBegin;
149: }
150: }
151:
152: int i;
153: try {
154: if ((i = inputStream.read(buffer, maxNextCharInd, available
155: - maxNextCharInd)) == -1) {
156: inputStream.close();
157: throw new java.io.IOException();
158: } else {
159: maxNextCharInd += i;
160: }
161: return;
162: } catch (java.io.IOException e) {
163: --bufpos;
164: backup(0);
165: if (tokenBegin == -1) {
166: tokenBegin = bufpos;
167: }
168: throw e;
169: }
170: }
171:
172: /**
173: * Description of the Method
174: *
175: *@return Description of the Return Value
176: *@exception java.io.IOException Description of the Exception
177: */
178: public final char BeginToken() throws java.io.IOException {
179: tokenBegin = -1;
180: char c = readChar();
181: tokenBegin = bufpos;
182:
183: return c;
184: }
185:
186: /**
187: * Description of the Method
188: *
189: *@param c Description of the Parameter
190: */
191: private final void UpdateLineColumn(char c) {
192: column++;
193:
194: if (prevCharIsLF) {
195: prevCharIsLF = false;
196: line += (column = 1);
197: } else if (prevCharIsCR) {
198: prevCharIsCR = false;
199: if (c == '\n') {
200: prevCharIsLF = true;
201: } else {
202: line += (column = 1);
203: }
204: }
205:
206: switch (c) {
207: case '\r':
208: prevCharIsCR = true;
209: break;
210: case '\n':
211: prevCharIsLF = true;
212: break;
213: case '\t':
214: column--;
215: column += (8 - (column & 07));
216: break;
217: default:
218: break;
219: }
220:
221: bufline[bufpos] = line;
222: bufcolumn[bufpos] = column;
223: }
224:
225: /**
226: * Description of the Method
227: *
228: *@return Description of the Return Value
229: *@exception java.io.IOException Description of the Exception
230: */
231: public final char readChar() throws java.io.IOException {
232: if (inBuf > 0) {
233: --inBuf;
234: return (char) ((char) 0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0)
235: : ++bufpos]);
236: }
237:
238: if (++bufpos >= maxNextCharInd) {
239: FillBuff();
240: }
241:
242: char c = (char) ((char) 0xff & buffer[bufpos]);
243:
244: UpdateLineColumn(c);
245: return (c);
246: }
247:
248: /**
249: *@return The column value
250: *@deprecated
251: *@see #getEndColumn
252: */
253:
254: public final int getColumn() {
255: return bufcolumn[bufpos];
256: }
257:
258: /**
259: *@return The line value
260: *@deprecated
261: *@see #getEndLine
262: */
263:
264: public final int getLine() {
265: return bufline[bufpos];
266: }
267:
268: /**
269: * Gets the endColumn attribute of the VrmlCharStream object
270: *
271: *@return The endColumn value
272: */
273: public final int getEndColumn() {
274: return bufcolumn[bufpos];
275: }
276:
277: /**
278: * Gets the endLine attribute of the VrmlCharStream object
279: *
280: *@return The endLine value
281: */
282: public final int getEndLine() {
283: return bufline[bufpos];
284: }
285:
286: /**
287: * Gets the beginColumn attribute of the VrmlCharStream object
288: *
289: *@return The beginColumn value
290: */
291: public final int getBeginColumn() {
292: return bufcolumn[tokenBegin];
293: }
294:
295: /**
296: * Gets the beginLine attribute of the VrmlCharStream object
297: *
298: *@return The beginLine value
299: */
300: public final int getBeginLine() {
301: return bufline[tokenBegin];
302: }
303:
304: /**
305: * Description of the Method
306: *
307: *@param amount Description of the Parameter
308: */
309: public final void backup(int amount) {
310:
311: inBuf += amount;
312: if ((bufpos -= amount) < 0) {
313: bufpos += bufsize;
314: }
315: }
316:
317: /**
318: *Constructor for the VrmlCharStream object
319: *
320: *@param dstream Description of the Parameter
321: *@param startline Description of the Parameter
322: *@param startcolumn Description of the Parameter
323: *@param buffersize Description of the Parameter
324: */
325: public VrmlCharStream(java.io.Reader dstream, int startline,
326: int startcolumn, int buffersize) {
327: inputStream = dstream;
328: line = startline;
329: column = startcolumn - 1;
330:
331: available = bufsize = buffersize;
332: buffer = new char[buffersize];
333: bufline = new int[buffersize];
334: bufcolumn = new int[buffersize];
335: }
336:
337: /**
338: *Constructor for the VrmlCharStream object
339: *
340: *@param dstream Description of the Parameter
341: *@param startline Description of the Parameter
342: *@param startcolumn Description of the Parameter
343: */
344: public VrmlCharStream(java.io.Reader dstream, int startline,
345: int startcolumn) {
346: this (dstream, startline, startcolumn, 4096);
347: }
348:
349: /**
350: * Description of the Method
351: *
352: *@param dstream Description of the Parameter
353: *@param startline Description of the Parameter
354: *@param startcolumn Description of the Parameter
355: *@param buffersize Description of the Parameter
356: */
357: public void ReInit(java.io.Reader dstream, int startline,
358: int startcolumn, int buffersize) {
359: inputStream = dstream;
360: line = startline;
361: column = startcolumn - 1;
362:
363: if (buffer == null || buffersize != buffer.length) {
364: available = bufsize = buffersize;
365: buffer = new char[buffersize];
366: bufline = new int[buffersize];
367: bufcolumn = new int[buffersize];
368: }
369: prevCharIsLF = prevCharIsCR = false;
370: tokenBegin = inBuf = maxNextCharInd = 0;
371: bufpos = -1;
372: }
373:
374: /**
375: * Description of the Method
376: *
377: *@param dstream Description of the Parameter
378: *@param startline Description of the Parameter
379: *@param startcolumn Description of the Parameter
380: */
381: public void ReInit(java.io.Reader dstream, int startline,
382: int startcolumn) {
383: ReInit(dstream, startline, startcolumn, 4096);
384: }
385:
386: /**
387: *Constructor for the VrmlCharStream object
388: *
389: *@param dstream Description of the Parameter
390: *@param startline Description of the Parameter
391: *@param startcolumn Description of the Parameter
392: *@param buffersize Description of the Parameter
393: */
394: public VrmlCharStream(java.io.InputStream dstream, int startline,
395: int startcolumn, int buffersize) {
396: this (new java.io.InputStreamReader(dstream), startline,
397: startcolumn, 4096);
398: }
399:
400: /**
401: *Constructor for the VrmlCharStream object
402: *
403: *@param dstream Description of the Parameter
404: *@param startline Description of the Parameter
405: *@param startcolumn Description of the Parameter
406: */
407: public VrmlCharStream(java.io.InputStream dstream, int startline,
408: int startcolumn) {
409: this (dstream, startline, startcolumn, 4096);
410: }
411:
412: /**
413: * Description of the Method
414: *
415: *@param dstream Description of the Parameter
416: *@param startline Description of the Parameter
417: *@param startcolumn Description of the Parameter
418: *@param buffersize Description of the Parameter
419: */
420: public void ReInit(java.io.InputStream dstream, int startline,
421: int startcolumn, int buffersize) {
422: ReInit(new java.io.InputStreamReader(dstream), startline,
423: startcolumn, 4096);
424: }
425:
426: /**
427: * Description of the Method
428: *
429: *@param dstream Description of the Parameter
430: *@param startline Description of the Parameter
431: *@param startcolumn Description of the Parameter
432: */
433: public void ReInit(java.io.InputStream dstream, int startline,
434: int startcolumn) {
435: ReInit(dstream, startline, startcolumn, 4096);
436: }
437:
438: /**
439: * Description of the Method
440: *
441: *@return Description of the Return Value
442: */
443: public final String GetImage() {
444: if (bufpos >= tokenBegin) {
445: return new String(buffer, tokenBegin, bufpos - tokenBegin
446: + 1);
447: } else {
448: return new String(buffer, tokenBegin, bufsize - tokenBegin)
449: + new String(buffer, 0, bufpos + 1);
450: }
451: }
452:
453: /**
454: * Description of the Method
455: *
456: *@param len Description of the Parameter
457: *@return Description of the Return Value
458: */
459: public final char[] GetSuffix(int len) {
460: char[] ret = new char[len];
461:
462: if ((bufpos + 1) >= len) {
463: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
464: } else {
465: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
466: 0, len - bufpos - 1);
467: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
468: bufpos + 1);
469: }
470:
471: return ret;
472: }
473:
474: /** Description of the Method */
475: public void Done() {
476: buffer = null;
477: bufline = null;
478: bufcolumn = null;
479: }
480:
481: /**
482: * Method to adjust line and column numbers for the start of a token.<BR>
483: *
484: *@param newLine Description of the Parameter
485: *@param newCol Description of the Parameter
486: */
487: public void adjustBeginLineColumn(int newLine, int newCol) {
488: int start = tokenBegin;
489: int len;
490:
491: if (bufpos >= tokenBegin) {
492: len = bufpos - tokenBegin + inBuf + 1;
493: } else {
494: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
495: }
496:
497: int i = 0;
498:
499: int j = 0;
500:
501: int k = 0;
502: int nextColDiff = 0;
503: int columnDiff = 0;
504:
505: while (i < len
506: && bufline[j = start % bufsize] == bufline[k = ++start
507: % bufsize]) {
508: bufline[j] = newLine;
509: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
510: bufcolumn[j] = newCol + columnDiff;
511: columnDiff = nextColDiff;
512: i++;
513: }
514:
515: if (i < len) {
516: bufline[j] = newLine++;
517: bufcolumn[j] = newCol + columnDiff;
518:
519: while (i++ < len) {
520: if (bufline[j = start % bufsize] != bufline[++start
521: % bufsize]) {
522: bufline[j] = newLine++;
523: } else {
524: bufline[j] = newLine;
525: }
526: }
527: }
528:
529: line = bufline[j];
530: column = bufcolumn[j];
531: }
532:
533: }
|