View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2000 (C) Intalio Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  package org.exolab.castor.xml.dtd.parser;
37  
38  /**
39   * An implementation of interface {@link org.exolab.castor.xml.dtd.parser.CharStream CharStream}.
40   * Implements input character stream that maintains line and column number positions of the
41   * characters. It also has the capability to backup the stream to some extent.<br>
42   * The object of this class is constructed using {@link java.io.Reader java.io.Reader}
43   * <tt>reader</tt> and it is left to constructor of the <tt>reader</tt> to set up character encoding
44   * correctly. This means that method <u><font color="blue">read</font></u> of the <tt>reader</tt> is
45   * used to get next characters, assuming it returns appropriate values. It is recommended to use
46   * class {@link java.io.InputStreamReader java.io.InputStreamReader} as a <tt>reader</tt>, which
47   * allows to set desired character encoding. This class is an intermediate component between input
48   * character reader and the parser.<br>
49   * The code of this class is based on the class <b>ASCII_CharStream</b> - implementation of
50   * interface {@link org.exolab.castor.xml.dtd.parser.CharStream CharStream}, that JavaCC would have
51   * generated with the following options set in a JavaCC grammar file:
52   * 
53   * <pre>
54   * JAVA_UNICODE_ESCAPE = false;
55   * UNICODE_INPUT = false;
56   * USER_CHAR_STREAM = false;
57   * </pre>
58   * 
59   * Note that this class is not fully JavaCC generated.<br>
60   * 
61   * @author <b>JavaCC</b>, <a href="mailto:totok@intalio.com">Alexander Totok</a>
62   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
63   */
64  public final class InputCharStream implements CharStream {
65  
66    public static final boolean staticFlag = false;
67    int bufsize;
68    int available;
69    int tokenBegin;
70    public int bufpos = -1;
71    private int bufline[];
72    private int bufcolumn[];
73  
74    private int column = 0;
75    private int line = 1;
76  
77    private boolean prevCharIsCR = false;
78    private boolean prevCharIsLF = false;
79  
80    private java.io.Reader inputStream;
81  
82    private char[] buffer;
83    private int maxNextCharInd = 0;
84    private int inBuf = 0;
85  
86    private final void ExpandBuff(boolean wrapAround) {
87  
88      char[] newbuffer = new char[bufsize + 2048];
89      int newbufline[] = new int[bufsize + 2048];
90      int newbufcolumn[] = new int[bufsize + 2048];
91  
92      try {
93        if (wrapAround) {
94          System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
95          System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
96          buffer = newbuffer;
97  
98          System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
99          System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
100         bufline = newbufline;
101 
102         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
103         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
104         bufcolumn = newbufcolumn;
105 
106         maxNextCharInd = (bufpos += (bufsize - tokenBegin));
107 
108       } else {
109 
110         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
111         buffer = newbuffer;
112 
113         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
114         bufline = newbufline;
115 
116         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
117         bufcolumn = newbufcolumn;
118 
119         maxNextCharInd = (bufpos -= tokenBegin);
120       }
121     } catch (Throwable t) {
122       throw new Error(t.getMessage());
123     }
124 
125     bufsize += 2048;
126     available = bufsize;
127     tokenBegin = 0;
128   }
129 
130   private final void FillBuff() throws java.io.IOException {
131     if (maxNextCharInd == available) {
132       if (available == bufsize) {
133         if (tokenBegin > 2048) {
134           bufpos = maxNextCharInd = 0;
135           available = tokenBegin;
136         } else if (tokenBegin < 0)
137           bufpos = maxNextCharInd = 0;
138         else
139           ExpandBuff(false);
140       } else if (available > tokenBegin)
141         available = bufsize;
142       else if ((tokenBegin - available) < 2048)
143         ExpandBuff(true);
144       else
145         available = tokenBegin;
146     }
147 
148     int i;
149 
150     try {
151       if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
152         inputStream.close();
153         throw new java.io.IOException();
154       }
155       maxNextCharInd += i;
156       return;
157     } catch (java.io.IOException e) {
158       --bufpos;
159       backup(0);
160       if (tokenBegin == -1)
161         tokenBegin = bufpos;
162       throw e;
163     }
164   }
165 
166   public final char BeginToken() throws java.io.IOException {
167 
168     tokenBegin = -1;
169     char c = readChar();
170     tokenBegin = bufpos;
171     return c;
172   }
173 
174   private final void UpdateLineColumn(char c) {
175 
176     column++;
177     if (prevCharIsLF) {
178       prevCharIsLF = false;
179       line += (column = 1);
180     } else if (prevCharIsCR) {
181       prevCharIsCR = false;
182       if (c == '\n')
183         prevCharIsLF = true;
184       else
185         line += (column = 1);
186     }
187 
188     switch (c) {
189       case '\r':
190         prevCharIsCR = true;
191         break;
192       case '\n':
193         prevCharIsLF = true;
194         break;
195       case '\t':
196         column--;
197         column += (8 - (column & 07));
198         break;
199       default:
200         break;
201     }
202 
203     bufline[bufpos] = line;
204     bufcolumn[bufpos] = column;
205   }
206 
207   /**
208    * Returns the next character from the input stream. The only method whose implementation is
209    * different from its original in the ASCII_CharStream class.
210    */
211   public final char readChar() throws java.io.IOException {
212 
213     if (inBuf > 0) {
214       --inBuf;
215 
216       // ----------------------------------------------------------------------
217       // the next line was changed, the original line generated by JavaCC was:
218       // return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
219       // ----------------------------------------------------------------------
220       return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
221     }
222     if (++bufpos >= maxNextCharInd)
223       FillBuff();
224 
225     // ----------------------------------------------------------------------
226     // the next line was changed, the original line generated by JavaCC was:
227     // char c = (char)((char)0xff & buffer[bufpos]);
228     // ----------------------------------------------------------------------
229     char c = buffer[bufpos];
230 
231     UpdateLineColumn(c);
232     return (c);
233   }
234 
235   /**
236    * @deprecated
237    * @see #getEndColumn
238    */
239   public final int getColumn() {
240     return bufcolumn[bufpos];
241   }
242 
243   /**
244    * @deprecated
245    * @see #getEndLine
246    */
247   public final int getLine() {
248     return bufline[bufpos];
249   }
250 
251   public final int getEndColumn() {
252     return bufcolumn[bufpos];
253   }
254 
255   public final int getEndLine() {
256     return bufline[bufpos];
257   }
258 
259   public final int getBeginColumn() {
260     return bufcolumn[tokenBegin];
261   }
262 
263   public final int getBeginLine() {
264     return bufline[tokenBegin];
265   }
266 
267   public final void backup(int amount) {
268     inBuf += amount;
269     if ((bufpos -= amount) < 0)
270       bufpos += bufsize;
271   }
272 
273   /**
274    * Constructor, allowing to specify start line and start column of the char stream, and buffer
275    * size as well.
276    */
277   public InputCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
278     inputStream = dstream;
279     line = startline;
280     column = startcolumn - 1;
281     available = bufsize = buffersize;
282     buffer = new char[buffersize];
283     bufline = new int[buffersize];
284     bufcolumn = new int[buffersize];
285   }
286 
287   /**
288    * Constructor, allowing to specify start line and start column of the char stream.
289    */
290   public InputCharStream(java.io.Reader dstream, int startline, int startcolumn) {
291     this(dstream, startline, startcolumn, 4096);
292   }
293 
294   /**
295    * Constructor, instantiating the char stream to begin at 1-st line and 1-st column of
296    * <tt>dstream</tt>.
297    */
298   public InputCharStream(java.io.Reader dstream) {
299     this(dstream, 1, 1);
300   }
301 
302   /**
303    * Reinitialization of the char stream, allowing to specify start line and start column of the
304    * char stream, and buffer size as well.
305    */
306   public void ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
307     inputStream = dstream;
308     line = startline;
309     column = startcolumn - 1;
310 
311     if (buffer == null || buffersize != buffer.length) {
312       available = bufsize = buffersize;
313       buffer = new char[buffersize];
314       bufline = new int[buffersize];
315       bufcolumn = new int[buffersize];
316     }
317     prevCharIsLF = prevCharIsCR = false;
318     tokenBegin = inBuf = maxNextCharInd = 0;
319     bufpos = -1;
320   }
321 
322   /**
323    * Reinitialization of the char stream, allowing to specify start line and start column of the
324    * char stream.
325    */
326   public void ReInit(java.io.Reader dstream, int startline, int startcolumn) {
327     ReInit(dstream, startline, startcolumn, 4096);
328   }
329 
330   /**
331    * Reinitialization of the char stream, instantiating the char stream to begin at 1-st line and
332    * 1-st column of <tt>dstream</tt>.
333    */
334   public void ReInit(java.io.Reader dstream) {
335     ReInit(dstream, 1, 1);
336   }
337 
338   public final String GetImage() {
339     if (bufpos >= tokenBegin) {
340       return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
341     }
342     return new String(buffer, tokenBegin, bufsize - tokenBegin) + new String(buffer, 0, bufpos + 1);
343   }
344 
345   public final char[] GetSuffix(int len) {
346     char[] ret = new char[len];
347 
348     if ((bufpos + 1) >= len)
349       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
350     else {
351       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
352       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
353     }
354     return ret;
355   }
356 
357   public void Done() {
358     buffer = null;
359     bufline = null;
360     bufcolumn = null;
361   }
362 
363   /**
364    * Method to adjust line and column numbers for the start of a token.
365    */
366   public void adjustBeginLineColumn(int newLine, int newCol) {
367     int start = tokenBegin;
368     int len;
369 
370     if (bufpos >= tokenBegin) {
371       len = bufpos - tokenBegin + inBuf + 1;
372     } else {
373       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
374     }
375 
376     int i = 0, j = 0, k = 0;
377     int nextColDiff = 0, columnDiff = 0;
378 
379     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
380       bufline[j] = newLine;
381       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
382       bufcolumn[j] = newCol + columnDiff;
383       columnDiff = nextColDiff;
384       i++;
385     }
386 
387     if (i < len) {
388       bufline[j] = newLine++;
389       bufcolumn[j] = newCol + columnDiff;
390 
391       while (i++ < len) {
392         if (bufline[j = start % bufsize] != bufline[++start % bufsize])
393           bufline[j] = newLine++;
394         else
395           bufline[j] = newLine;
396       }
397     }
398 
399     line = bufline[j];
400     column = bufcolumn[j];
401   }
402 }