View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
18  
19  package org.apache.commons.jxpath.ri.parser;
20  
21  /**
22   * An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without unicode processing).
23   */
24  public class SimpleCharStream {
25  
26      public static final boolean staticFlag = false;
27      int bufsize;
28      int available;
29      int tokenBegin;
30      public int bufpos = -1;
31      protected int bufline[];
32      protected int bufcolumn[];
33      protected int column = 0;
34      protected int line = 1;
35      protected boolean prevCharIsCR = false;
36      protected boolean prevCharIsLF = false;
37      protected java.io.Reader inputStream;
38      protected char[] buffer;
39      protected int maxNextCharInd = 0;
40      protected int inBuf = 0;
41  
42      public SimpleCharStream(final java.io.InputStream dstream) {
43          this(dstream, 1, 1, 4096);
44      }
45  
46      public SimpleCharStream(final java.io.InputStream dstream, final int startLine, final int startColumn) {
47          this(dstream, startLine, startColumn, 4096);
48      }
49  
50      public SimpleCharStream(final java.io.InputStream dstream, final int startLine, final int startColumn, final int buffersize) {
51          this(new java.io.InputStreamReader(dstream), startLine, startColumn, 4096);
52      }
53  
54      public SimpleCharStream(final java.io.Reader dstream) {
55          this(dstream, 1, 1, 4096);
56      }
57  
58      public SimpleCharStream(final java.io.Reader dstream, final int startLine, final int startColumn) {
59          this(dstream, startLine, startColumn, 4096);
60      }
61  
62      public SimpleCharStream(final java.io.Reader dstream, final int startLine, final int startColumn, final int bufferSize) {
63          inputStream = dstream;
64          line = startLine;
65          column = startColumn - 1;
66          available = bufsize = bufferSize;
67          buffer = new char[bufferSize];
68          bufline = new int[bufferSize];
69          bufcolumn = new int[bufferSize];
70      }
71  
72      /**
73       * Method to adjust line and column numbers for the start of a token.<BR>
74       *
75       * @param newLine TODO
76       * @param newCol  TODO
77       */
78      public void adjustBeginLineColumn(int newLine, final int newCol) {
79          int start = tokenBegin;
80          int len;
81          if (bufpos >= tokenBegin) {
82              len = bufpos - tokenBegin + inBuf + 1;
83          } else {
84              len = bufsize - tokenBegin + bufpos + 1 + inBuf;
85          }
86          int i = 0, j = 0, k = 0;
87          int nextColDiff = 0, columnDiff = 0;
88          while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
89              bufline[j] = newLine;
90              nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
91              bufcolumn[j] = newCol + columnDiff;
92              columnDiff = nextColDiff;
93              i++;
94          }
95          if (i < len) {
96              bufline[j] = newLine++;
97              bufcolumn[j] = newCol + columnDiff;
98              while (i++ < len) {
99                  if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
100                     bufline[j] = newLine++;
101                 } else {
102                     bufline[j] = newLine;
103                 }
104             }
105         }
106         line = bufline[j];
107         column = bufcolumn[j];
108     }
109 
110     public void backup(final int amount) {
111         inBuf += amount;
112         if ((bufpos -= amount) < 0) {
113             bufpos += bufsize;
114         }
115     }
116 
117     public char BeginToken() throws java.io.IOException {
118         tokenBegin = -1;
119         final char c = readChar();
120         tokenBegin = bufpos;
121         return c;
122     }
123 
124     public void Done() {
125         buffer = null;
126         bufline = null;
127         bufcolumn = null;
128     }
129 
130     protected void ExpandBuff(final boolean wrapAround) {
131         final char[] newbuffer = new char[bufsize + 2048];
132         final int newbufline[] = new int[bufsize + 2048];
133         final int newbufcolumn[] = new int[bufsize + 2048];
134         try {
135             if (wrapAround) {
136                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
137                 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
138                 buffer = newbuffer;
139                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
140                 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
141                 bufline = newbufline;
142                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
143                 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
144                 bufcolumn = newbufcolumn;
145                 maxNextCharInd = bufpos += bufsize - tokenBegin;
146             } else {
147                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
148                 buffer = newbuffer;
149                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
150                 bufline = newbufline;
151                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
152                 bufcolumn = newbufcolumn;
153                 maxNextCharInd = bufpos -= tokenBegin;
154             }
155         } catch (final Throwable t) {
156             throw new Error(t.getMessage());
157         }
158         bufsize += 2048;
159         available = bufsize;
160         tokenBegin = 0;
161     }
162 
163     protected void FillBuff() throws java.io.IOException {
164         if (maxNextCharInd == available) {
165             if (available == bufsize) {
166                 if (tokenBegin > 2048) {
167                     bufpos = maxNextCharInd = 0;
168                     available = tokenBegin;
169                 } else if (tokenBegin < 0) {
170                     bufpos = maxNextCharInd = 0;
171                 } else {
172                     ExpandBuff(false);
173                 }
174             } else if (available > tokenBegin) {
175                 available = bufsize;
176             } else if (tokenBegin - available < 2048) {
177                 ExpandBuff(true);
178             } else {
179                 available = tokenBegin;
180             }
181         }
182         int i;
183         try {
184             if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
185                 inputStream.close();
186                 throw new java.io.IOException();
187             }
188             maxNextCharInd += i;
189         } catch (final java.io.IOException e) {
190             --bufpos;
191             backup(0);
192             if (tokenBegin == -1) {
193                 tokenBegin = bufpos;
194             }
195             throw e;
196         }
197     }
198 
199     public int getBeginColumn() {
200         return bufcolumn[tokenBegin];
201     }
202 
203     public int getBeginLine() {
204         return bufline[tokenBegin];
205     }
206 
207     /**
208      * @deprecated
209      * @return the end column.
210      * @see #getEndColumn
211      */
212     @Deprecated
213     public int getColumn() {
214         return bufcolumn[bufpos];
215     }
216 
217     public int getEndColumn() {
218         return bufcolumn[bufpos];
219     }
220 
221     public int getEndLine() {
222         return bufline[bufpos];
223     }
224 
225     public String GetImage() {
226         if (bufpos >= tokenBegin) {
227             return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
228         }
229         return new String(buffer, tokenBegin, bufsize - tokenBegin) + new String(buffer, 0, bufpos + 1);
230     }
231 
232     /**
233      * @deprecated
234      * @return the line number.
235      * @see #getEndLine
236      */
237     @Deprecated
238     public int getLine() {
239         return bufline[bufpos];
240     }
241 
242     public char[] GetSuffix(final int len) {
243         final char[] ret = new char[len];
244         if (bufpos + 1 >= len) {
245             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
246         } else {
247             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
248             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
249         }
250         return ret;
251     }
252 
253     public char readChar() throws java.io.IOException {
254         if (inBuf > 0) {
255             --inBuf;
256             if (++bufpos == bufsize) {
257                 bufpos = 0;
258             }
259             return buffer[bufpos];
260         }
261         if (++bufpos >= maxNextCharInd) {
262             FillBuff();
263         }
264         final char c = buffer[bufpos];
265         UpdateLineColumn(c);
266         return c;
267     }
268 
269     public void ReInit(final java.io.InputStream dstream) {
270         ReInit(dstream, 1, 1, 4096);
271     }
272 
273     public void ReInit(final java.io.InputStream dstream, final int startLine, final int startColumn) {
274         ReInit(dstream, startLine, startColumn, 4096);
275     }
276 
277     public void ReInit(final java.io.InputStream dstream, final int startLine, final int startColumn, final int buffersize) {
278         ReInit(new java.io.InputStreamReader(dstream), startLine, startColumn, 4096);
279     }
280 
281     public void ReInit(final java.io.Reader dstream) {
282         ReInit(dstream, 1, 1, 4096);
283     }
284 
285     public void ReInit(final java.io.Reader dstream, final int startLine, final int startColumn) {
286         ReInit(dstream, startLine, startColumn, 4096);
287     }
288 
289     public void ReInit(final java.io.Reader dstream, final int startLine, final int startColumn, final int bufferSize) {
290         inputStream = dstream;
291         line = startLine;
292         column = startColumn - 1;
293         if (buffer == null || bufferSize != buffer.length) {
294             available = bufsize = bufferSize;
295             buffer = new char[bufferSize];
296             bufline = new int[bufferSize];
297             bufcolumn = new int[bufferSize];
298         }
299         prevCharIsLF = prevCharIsCR = false;
300         tokenBegin = inBuf = maxNextCharInd = 0;
301         bufpos = -1;
302     }
303 
304     protected void UpdateLineColumn(final char c) {
305         column++;
306         if (prevCharIsLF) {
307             prevCharIsLF = false;
308             line += column = 1;
309         } else if (prevCharIsCR) {
310             prevCharIsCR = false;
311             if (c == '\n') {
312                 prevCharIsLF = true;
313             } else {
314                 line += column = 1;
315             }
316         }
317         switch (c) {
318         case '\r':
319             prevCharIsCR = true;
320             break;
321         case '\n':
322             prevCharIsLF = true;
323             break;
324         case '\t':
325             column--;
326             column += 8 - (column & 07);
327             break;
328         default:
329             break;
330         }
331         bufline[bufpos] = line;
332         bufcolumn[bufpos] = column;
333     }
334 }