001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.0 */
031: package de.intarsys.pdf.postscript;
032:
033: /**
034: * An implementation of interface CharStream, where the stream is assumed to
035: * contain only ASCII characters (with java-like unicode escape processing).
036: */
037: public class JavaCharStream {
038: public static final boolean staticFlag = false;
039:
040: static final int hexval(char c) throws java.io.IOException {
041: switch (c) {
042: case '0':
043: return 0;
044: case '1':
045: return 1;
046: case '2':
047: return 2;
048: case '3':
049: return 3;
050: case '4':
051: return 4;
052: case '5':
053: return 5;
054: case '6':
055: return 6;
056: case '7':
057: return 7;
058: case '8':
059: return 8;
060: case '9':
061: return 9;
062: case 'a':
063: case 'A':
064: return 10;
065: case 'b':
066: case 'B':
067: return 11;
068: case 'c':
069: case 'C':
070: return 12;
071: case 'd':
072: case 'D':
073: return 13;
074: case 'e':
075: case 'E':
076: return 14;
077: case 'f':
078: case 'F':
079: return 15;
080: }
081:
082: throw new java.io.IOException(); // Should never come here
083: }
084:
085: public int bufpos = -1;
086:
087: int bufsize;
088:
089: int available;
090:
091: int tokenBegin;
092:
093: protected int[] bufline;
094:
095: protected int[] bufcolumn;
096:
097: protected int column = 0;
098:
099: protected int line = 1;
100:
101: protected boolean prevCharIsCR = false;
102:
103: protected boolean prevCharIsLF = false;
104:
105: protected java.io.Reader inputStream;
106:
107: protected char[] nextCharBuf;
108:
109: protected char[] buffer;
110:
111: protected int maxNextCharInd = 0;
112:
113: protected int nextCharInd = -1;
114:
115: protected int inBuf = 0;
116:
117: protected int tabSize = 8;
118:
119: protected void setTabSize(int i) {
120: tabSize = i;
121: }
122:
123: protected int getTabSize(int i) {
124: return tabSize;
125: }
126:
127: protected void ExpandBuff(boolean wrapAround) {
128: char[] newbuffer = new char[bufsize + 2048];
129: int[] newbufline = new int[bufsize + 2048];
130: int[] newbufcolumn = new int[bufsize + 2048];
131:
132: try {
133: if (wrapAround) {
134: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
135: bufsize - tokenBegin);
136: System.arraycopy(buffer, 0, newbuffer, bufsize
137: - tokenBegin, bufpos);
138: buffer = newbuffer;
139:
140: System.arraycopy(bufline, tokenBegin, newbufline, 0,
141: bufsize - tokenBegin);
142: System.arraycopy(bufline, 0, newbufline, bufsize
143: - tokenBegin, bufpos);
144: bufline = newbufline;
145:
146: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
147: 0, bufsize - tokenBegin);
148: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
149: - tokenBegin, bufpos);
150: bufcolumn = newbufcolumn;
151:
152: bufpos += (bufsize - tokenBegin);
153: } else {
154: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
155: bufsize - tokenBegin);
156: buffer = newbuffer;
157:
158: System.arraycopy(bufline, tokenBegin, newbufline, 0,
159: bufsize - tokenBegin);
160: bufline = newbufline;
161:
162: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
163: 0, bufsize - tokenBegin);
164: bufcolumn = newbufcolumn;
165:
166: bufpos -= tokenBegin;
167: }
168: } catch (Throwable t) {
169: throw new Error(t.getMessage());
170: }
171:
172: available = (bufsize += 2048);
173: tokenBegin = 0;
174: }
175:
176: protected void FillBuff() throws java.io.IOException {
177: int i;
178: if (maxNextCharInd == 4096) {
179: maxNextCharInd = nextCharInd = 0;
180: }
181:
182: try {
183: if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
184: 4096 - maxNextCharInd)) == -1) {
185: inputStream.close();
186: throw new java.io.IOException();
187: } else {
188: maxNextCharInd += i;
189: }
190: return;
191: } catch (java.io.IOException e) {
192: if (bufpos != 0) {
193: --bufpos;
194: backup(0);
195: } else {
196: bufline[bufpos] = line;
197: bufcolumn[bufpos] = column;
198: }
199: throw e;
200: }
201: }
202:
203: protected char ReadByte() throws java.io.IOException {
204: if (++nextCharInd >= maxNextCharInd) {
205: FillBuff();
206: }
207:
208: return nextCharBuf[nextCharInd];
209: }
210:
211: public char BeginToken() throws java.io.IOException {
212: if (inBuf > 0) {
213: --inBuf;
214:
215: if (++bufpos == bufsize) {
216: bufpos = 0;
217: }
218:
219: tokenBegin = bufpos;
220: return buffer[bufpos];
221: }
222:
223: tokenBegin = 0;
224: bufpos = -1;
225:
226: return readChar();
227: }
228:
229: protected void AdjustBuffSize() {
230: if (available == bufsize) {
231: if (tokenBegin > 2048) {
232: bufpos = 0;
233: available = tokenBegin;
234: } else {
235: ExpandBuff(false);
236: }
237: } else if (available > tokenBegin) {
238: available = bufsize;
239: } else if ((tokenBegin - available) < 2048) {
240: ExpandBuff(true);
241: } else {
242: available = tokenBegin;
243: }
244: }
245:
246: protected void UpdateLineColumn(char c) {
247: column++;
248:
249: if (prevCharIsLF) {
250: prevCharIsLF = false;
251: line += (column = 1);
252: } else if (prevCharIsCR) {
253: prevCharIsCR = false;
254: if (c == '\n') {
255: prevCharIsLF = true;
256: } else {
257: line += (column = 1);
258: }
259: }
260:
261: switch (c) {
262: case '\r':
263: prevCharIsCR = true;
264: break;
265: case '\n':
266: prevCharIsLF = true;
267: break;
268: case '\t':
269: column--;
270: column += (tabSize - (column % tabSize));
271: break;
272: default:
273: break;
274: }
275:
276: bufline[bufpos] = line;
277: bufcolumn[bufpos] = column;
278: }
279:
280: public char readChar() throws java.io.IOException {
281: if (inBuf > 0) {
282: --inBuf;
283:
284: if (++bufpos == bufsize) {
285: bufpos = 0;
286: }
287:
288: return buffer[bufpos];
289: }
290:
291: char c;
292:
293: if (++bufpos == available) {
294: AdjustBuffSize();
295: }
296:
297: if ((buffer[bufpos] = c = ReadByte()) == '\\') {
298: UpdateLineColumn(c);
299:
300: int backSlashCnt = 1;
301:
302: for (;;) // Read all the backslashes
303: {
304: if (++bufpos == available) {
305: AdjustBuffSize();
306: }
307:
308: try {
309: if ((buffer[bufpos] = c = ReadByte()) != '\\') {
310: UpdateLineColumn(c);
311: // found a non-backslash char.
312: if ((c == 'u') && ((backSlashCnt & 1) == 1)) {
313: if (--bufpos < 0) {
314: bufpos = bufsize - 1;
315: }
316:
317: break;
318: }
319:
320: backup(backSlashCnt);
321: return '\\';
322: }
323: } catch (java.io.IOException e) {
324: if (backSlashCnt > 1) {
325: backup(backSlashCnt);
326: }
327:
328: return '\\';
329: }
330:
331: UpdateLineColumn(c);
332: backSlashCnt++;
333: }
334:
335: // Here, we have seen an odd number of backslash's followed by a 'u'
336: try {
337: while ((c = ReadByte()) == 'u')
338: ++column;
339:
340: buffer[bufpos] = c = (char) ((hexval(c) << 12)
341: | (hexval(ReadByte()) << 8)
342: | (hexval(ReadByte()) << 4) | hexval(ReadByte()));
343:
344: column += 4;
345: } catch (java.io.IOException e) {
346: throw new Error("Invalid escape character at line "
347: + line + " column " + column + ".");
348: }
349:
350: if (backSlashCnt == 1) {
351: return c;
352: } else {
353: backup(backSlashCnt - 1);
354: return '\\';
355: }
356: } else {
357: UpdateLineColumn(c);
358: return (c);
359: }
360: }
361:
362: /**
363: * @deprecated
364: * @see #getEndColumn
365: */
366: public int getColumn() {
367: return bufcolumn[bufpos];
368: }
369:
370: /**
371: * @deprecated
372: * @see #getEndLine
373: */
374: public int getLine() {
375: return bufline[bufpos];
376: }
377:
378: public int getEndColumn() {
379: return bufcolumn[bufpos];
380: }
381:
382: public int getEndLine() {
383: return bufline[bufpos];
384: }
385:
386: public int getBeginColumn() {
387: return bufcolumn[tokenBegin];
388: }
389:
390: public int getBeginLine() {
391: return bufline[tokenBegin];
392: }
393:
394: public void backup(int amount) {
395: inBuf += amount;
396: if ((bufpos -= amount) < 0) {
397: bufpos += bufsize;
398: }
399: }
400:
401: public JavaCharStream(java.io.Reader dstream, int startline,
402: int startcolumn, int buffersize) {
403: inputStream = dstream;
404: line = startline;
405: column = startcolumn - 1;
406:
407: available = bufsize = buffersize;
408: buffer = new char[buffersize];
409: bufline = new int[buffersize];
410: bufcolumn = new int[buffersize];
411: nextCharBuf = new char[4096];
412: }
413:
414: public JavaCharStream(java.io.Reader dstream, int startline,
415: int startcolumn) {
416: this (dstream, startline, startcolumn, 4096);
417: }
418:
419: public JavaCharStream(java.io.Reader dstream) {
420: this (dstream, 1, 1, 4096);
421: }
422:
423: public void ReInit(java.io.Reader dstream, int startline,
424: int startcolumn, int buffersize) {
425: inputStream = dstream;
426: line = startline;
427: column = startcolumn - 1;
428:
429: if ((buffer == null) || (buffersize != buffer.length)) {
430: available = bufsize = buffersize;
431: buffer = new char[buffersize];
432: bufline = new int[buffersize];
433: bufcolumn = new int[buffersize];
434: nextCharBuf = new char[4096];
435: }
436: prevCharIsLF = prevCharIsCR = false;
437: tokenBegin = inBuf = maxNextCharInd = 0;
438: nextCharInd = bufpos = -1;
439: }
440:
441: public void ReInit(java.io.Reader dstream, int startline,
442: int startcolumn) {
443: ReInit(dstream, startline, startcolumn, 4096);
444: }
445:
446: public void ReInit(java.io.Reader dstream) {
447: ReInit(dstream, 1, 1, 4096);
448: }
449:
450: public JavaCharStream(java.io.InputStream dstream, String encoding,
451: int startline, int startcolumn, int buffersize)
452: throws java.io.UnsupportedEncodingException {
453: this (
454: (encoding == null) ? new java.io.InputStreamReader(
455: dstream) : new java.io.InputStreamReader(
456: dstream, encoding), startline, startcolumn,
457: buffersize);
458: }
459:
460: public JavaCharStream(java.io.InputStream dstream, int startline,
461: int startcolumn, int buffersize) {
462: this (new java.io.InputStreamReader(dstream), startline,
463: startcolumn, 4096);
464: }
465:
466: public JavaCharStream(java.io.InputStream dstream, String encoding,
467: int startline, int startcolumn)
468: throws java.io.UnsupportedEncodingException {
469: this (dstream, encoding, startline, startcolumn, 4096);
470: }
471:
472: public JavaCharStream(java.io.InputStream dstream, int startline,
473: int startcolumn) {
474: this (dstream, startline, startcolumn, 4096);
475: }
476:
477: public JavaCharStream(java.io.InputStream dstream, String encoding)
478: throws java.io.UnsupportedEncodingException {
479: this (dstream, encoding, 1, 1, 4096);
480: }
481:
482: public JavaCharStream(java.io.InputStream dstream) {
483: this (dstream, 1, 1, 4096);
484: }
485:
486: public void ReInit(java.io.InputStream dstream, String encoding,
487: int startline, int startcolumn, int buffersize)
488: throws java.io.UnsupportedEncodingException {
489: ReInit((encoding == null) ? new java.io.InputStreamReader(
490: dstream) : new java.io.InputStreamReader(dstream,
491: encoding), startline, startcolumn, buffersize);
492: }
493:
494: public void ReInit(java.io.InputStream dstream, int startline,
495: int startcolumn, int buffersize) {
496: ReInit(new java.io.InputStreamReader(dstream), startline,
497: startcolumn, buffersize);
498: }
499:
500: public void ReInit(java.io.InputStream dstream, String encoding,
501: int startline, int startcolumn)
502: throws java.io.UnsupportedEncodingException {
503: ReInit(dstream, encoding, startline, startcolumn, 4096);
504: }
505:
506: public void ReInit(java.io.InputStream dstream, int startline,
507: int startcolumn) {
508: ReInit(dstream, startline, startcolumn, 4096);
509: }
510:
511: public void ReInit(java.io.InputStream dstream, String encoding)
512: throws java.io.UnsupportedEncodingException {
513: ReInit(dstream, encoding, 1, 1, 4096);
514: }
515:
516: public void ReInit(java.io.InputStream dstream) {
517: ReInit(dstream, 1, 1, 4096);
518: }
519:
520: public String GetImage() {
521: if (bufpos >= tokenBegin) {
522: return new String(buffer, tokenBegin, bufpos - tokenBegin
523: + 1);
524: } else {
525: return new String(buffer, tokenBegin, bufsize - tokenBegin)
526: + new String(buffer, 0, bufpos + 1);
527: }
528: }
529:
530: public char[] GetSuffix(int len) {
531: char[] ret = new char[len];
532:
533: if ((bufpos + 1) >= len) {
534: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
535: } else {
536: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
537: 0, len - bufpos - 1);
538: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
539: bufpos + 1);
540: }
541:
542: return ret;
543: }
544:
545: public void Done() {
546: nextCharBuf = null;
547: buffer = null;
548: bufline = null;
549: bufcolumn = null;
550: }
551:
552: /**
553: * Method to adjust line and column numbers for the start of a token.
554: */
555: public void adjustBeginLineColumn(int newLine, int newCol) {
556: int start = tokenBegin;
557: int len;
558:
559: if (bufpos >= tokenBegin) {
560: len = bufpos - tokenBegin + inBuf + 1;
561: } else {
562: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
563: }
564:
565: int i = 0;
566: int j = 0;
567: int k = 0;
568: int nextColDiff = 0;
569: int columnDiff = 0;
570:
571: while ((i < len)
572: && (bufline[j = start % bufsize] == bufline[k = ++start
573: % bufsize])) {
574: bufline[j] = newLine;
575: nextColDiff = (columnDiff + bufcolumn[k]) - bufcolumn[j];
576: bufcolumn[j] = newCol + columnDiff;
577: columnDiff = nextColDiff;
578: i++;
579: }
580:
581: if (i < len) {
582: bufline[j] = newLine++;
583: bufcolumn[j] = newCol + columnDiff;
584:
585: while (i++ < len) {
586: if (bufline[j = start % bufsize] != bufline[++start
587: % bufsize]) {
588: bufline[j] = newLine++;
589: } else {
590: bufline[j] = newLine;
591: }
592: }
593: }
594:
595: line = bufline[j];
596: column = bufcolumn[j];
597: }
598: }
|