KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > spi > conf > util > SimpleCharStream


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22
23 package org.enhydra.spi.conf.util;
24
25 /**
26  * An implementation of interface CharStream, where the stream is assumed to
27  * contain only ASCII characters (without unicode processing).
28  */

29
30 public class SimpleCharStream
31 {
32   public static final boolean staticFlag = false;
33   int bufsize;
34   int available;
35   int tokenBegin;
36   public int bufpos = -1;
37   protected int bufline[];
38   protected int bufcolumn[];
39
40   protected int column = 0;
41   protected int line = 1;
42
43   protected boolean prevCharIsCR = false;
44   protected boolean prevCharIsLF = false;
45
46   protected java.io.Reader JavaDoc inputStream;
47
48   protected char[] buffer;
49   protected int maxNextCharInd = 0;
50   protected int inBuf = 0;
51
52   protected void ExpandBuff(boolean wrapAround)
53   {
54      char[] newbuffer = new char[bufsize + 2048];
55      int newbufline[] = new int[bufsize + 2048];
56      int newbufcolumn[] = new int[bufsize + 2048];
57
58      try
59      {
60         if (wrapAround)
61         {
62            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
63            System.arraycopy(buffer, 0, newbuffer,
64                                              bufsize - tokenBegin, bufpos);
65            buffer = newbuffer;
66
67            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
68            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
69            bufline = newbufline;
70
71            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
72            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
73            bufcolumn = newbufcolumn;
74
75            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
76         }
77         else
78         {
79            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
80            buffer = newbuffer;
81
82            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
83            bufline = newbufline;
84
85            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
86            bufcolumn = newbufcolumn;
87
88            maxNextCharInd = (bufpos -= tokenBegin);
89         }
90      }
91      catch (Throwable JavaDoc t)
92      {
93         throw new Error JavaDoc(t.getMessage());
94      }
95
96
97      bufsize += 2048;
98      available = bufsize;
99      tokenBegin = 0;
100   }
101
102   protected void FillBuff() throws java.io.IOException JavaDoc
103   {
104      if (maxNextCharInd == available)
105      {
106         if (available == bufsize)
107         {
108            if (tokenBegin > 2048)
109            {
110               bufpos = maxNextCharInd = 0;
111               available = tokenBegin;
112            }
113            else if (tokenBegin < 0)
114               bufpos = maxNextCharInd = 0;
115            else
116               ExpandBuff(false);
117         }
118         else if (available > tokenBegin)
119            available = bufsize;
120         else if ((tokenBegin - available) < 2048)
121            ExpandBuff(true);
122         else
123            available = tokenBegin;
124      }
125
126      int i;
127      try {
128         if ((i = inputStream.read(buffer, maxNextCharInd,
129                                     available - maxNextCharInd)) == -1)
130         {
131            inputStream.close();
132            throw new java.io.IOException JavaDoc();
133         }
134         else
135            maxNextCharInd += i;
136         return;
137      }
138      catch(java.io.IOException JavaDoc e) {
139         --bufpos;
140         backup(0);
141         if (tokenBegin == -1)
142            tokenBegin = bufpos;
143         throw e;
144      }
145   }
146
147   public char BeginToken() throws java.io.IOException JavaDoc
148   {
149      tokenBegin = -1;
150      char c = readChar();
151      tokenBegin = bufpos;
152
153      return c;
154   }
155
156   protected void UpdateLineColumn(char c)
157   {
158      column++;
159
160      if (prevCharIsLF)
161      {
162         prevCharIsLF = false;
163         line += (column = 1);
164      }
165      else if (prevCharIsCR)
166      {
167         prevCharIsCR = false;
168         if (c == '\n')
169         {
170            prevCharIsLF = true;
171         }
172         else
173            line += (column = 1);
174      }
175
176      switch (c)
177      {
178         case '\r' :
179            prevCharIsCR = true;
180            break;
181         case '\n' :
182            prevCharIsLF = true;
183            break;
184         case '\t' :
185            column--;
186            column += (8 - (column & 07));
187            break;
188         default :
189            break;
190      }
191
192      bufline[bufpos] = line;
193      bufcolumn[bufpos] = column;
194   }
195
196   public char readChar() throws java.io.IOException JavaDoc
197   {
198      if (inBuf > 0)
199      {
200         --inBuf;
201
202         if (++bufpos == bufsize)
203            bufpos = 0;
204
205         return buffer[bufpos];
206      }
207
208      if (++bufpos >= maxNextCharInd)
209         FillBuff();
210
211      char c = buffer[bufpos];
212
213      UpdateLineColumn(c);
214      return (c);
215   }
216
217   /**
218    * @deprecated
219    * @see #getEndColumn
220    * @return Column.
221    */

222
223   public int getColumn() {
224      return bufcolumn[bufpos];
225   }
226
227   /**
228    * @deprecated
229    * @see #getEndLine
230    * @return Line.
231    */

232
233   public int getLine() {
234      return bufline[bufpos];
235   }
236
237   public int getEndColumn() {
238      return bufcolumn[bufpos];
239   }
240
241   public int getEndLine() {
242      return bufline[bufpos];
243   }
244
245   public int getBeginColumn() {
246      return bufcolumn[tokenBegin];
247   }
248
249   public int getBeginLine() {
250      return bufline[tokenBegin];
251   }
252
253   public void backup(int amount) {
254
255     inBuf += amount;
256     if ((bufpos -= amount) < 0)
257        bufpos += bufsize;
258   }
259
260   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
261   int startcolumn, int buffersize)
262   {
263     inputStream = dstream;
264     line = startline;
265     column = startcolumn - 1;
266
267     available = bufsize = buffersize;
268     buffer = new char[buffersize];
269     bufline = new int[buffersize];
270     bufcolumn = new int[buffersize];
271   }
272
273   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
274                                                            int startcolumn)
275   {
276      this(dstream, startline, startcolumn, 4096);
277   }
278
279   public SimpleCharStream(java.io.Reader JavaDoc dstream)
280   {
281      this(dstream, 1, 1, 4096);
282   }
283   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
284   int startcolumn, int buffersize)
285   {
286     inputStream = dstream;
287     line = startline;
288     column = startcolumn - 1;
289
290     if (buffer == null || buffersize != buffer.length)
291     {
292       available = bufsize = buffersize;
293       buffer = new char[buffersize];
294       bufline = new int[buffersize];
295       bufcolumn = new int[buffersize];
296     }
297     prevCharIsLF = prevCharIsCR = false;
298     tokenBegin = inBuf = maxNextCharInd = 0;
299     bufpos = -1;
300   }
301
302   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
303                                                            int startcolumn)
304   {
305      ReInit(dstream, startline, startcolumn, 4096);
306   }
307
308   public void ReInit(java.io.Reader JavaDoc dstream)
309   {
310      ReInit(dstream, 1, 1, 4096);
311   }
312   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
313   int startcolumn, int buffersize)
314   {
315      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
316   }
317
318   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
319                                                            int startcolumn)
320   {
321      this(dstream, startline, startcolumn, 4096);
322   }
323
324   public SimpleCharStream(java.io.InputStream JavaDoc dstream)
325   {
326      this(dstream, 1, 1, 4096);
327   }
328
329   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
330                           int startcolumn, int buffersize)
331   {
332      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
333   }
334
335   public void ReInit(java.io.InputStream JavaDoc dstream)
336   {
337      ReInit(dstream, 1, 1, 4096);
338   }
339   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
340                                                            int startcolumn)
341   {
342      ReInit(dstream, startline, startcolumn, 4096);
343   }
344   public String JavaDoc GetImage()
345   {
346      if (bufpos >= tokenBegin)
347         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
348      else
349         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
350                               new String JavaDoc(buffer, 0, bufpos + 1);
351   }
352
353   public char[] GetSuffix(int len)
354   {
355      char[] ret = new char[len];
356
357      if ((bufpos + 1) >= len)
358         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
359      else
360      {
361         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
362                                                           len - bufpos - 1);
363         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
364      }
365
366      return ret;
367   }
368
369   public void Done()
370   {
371      buffer = null;
372      bufline = null;
373      bufcolumn = null;
374   }
375
376   /**
377    * Method to adjust line and column numbers for the start of a token.
378    * @param newLine New line.
379    * @param newCol New column.
380    */

381   public void adjustBeginLineColumn(int newLine, int newCol)
382   {
383      int start = tokenBegin;
384      int len;
385
386      if (bufpos >= tokenBegin)
387      {
388         len = bufpos - tokenBegin + inBuf + 1;
389      }
390      else
391      {
392         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
393      }
394
395      int i = 0, j = 0, k = 0;
396      int nextColDiff = 0, columnDiff = 0;
397
398      while (i < len &&
399             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
400      {
401         bufline[j] = newLine;
402         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
403         bufcolumn[j] = newCol + columnDiff;
404         columnDiff = nextColDiff;
405         i++;
406      }
407
408      if (i < len)
409      {
410         bufline[j] = newLine++;
411         bufcolumn[j] = newCol + columnDiff;
412
413         while (i++ < len)
414         {
415            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
416               bufline[j] = newLine++;
417            else
418               bufline[j] = newLine;
419         }
420      }
421
422      line = bufline[j];
423      column = bufcolumn[j];
424   }
425
426 }
427
Popular Tags