KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > AttributeStream


1 /* ****************************************************************************
2  * AttributeStream.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 /*
11  * This is a ripoff of JavaCC's SimpleCharStream, which I would have
12  * subclassed if it were not declared final, or I would have used
13  * directly, if it admitted that it implemented CharStream. Perhaps a
14  * future version of JavaCC will make this unnecessary.
15  */

16
17 package org.openlaszlo.compiler;
18
19 import org.openlaszlo.css.CharStream;
20 import java.io.*;
21 import org.jdom.Element;
22
23 /* START JavaCC SimpleCharStream */
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 class SimpleCharStream implements CharStream
31 {
32   public static final boolean staticFlag = false;
33   int bufsize;
34   int available;
35   int tokenBegin;
36   public int bufpos = -1;
37   private int bufline[];
38   private int bufcolumn[];
39
40   private int column = 0;
41   private int line = 1;
42
43   private boolean prevCharIsCR = false;
44   private boolean prevCharIsLF = false;
45
46   private java.io.Reader JavaDoc inputStream;
47
48   private char[] buffer;
49   private int maxNextCharInd = 0;
50   private int inBuf = 0;
51
52   private final 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   private final 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 final 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   private final 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 final 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    */

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

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

377   public void adjustBeginLineColumn(int newLine, int newCol)
378   {
379      int start = tokenBegin;
380      int len;
381
382      if (bufpos >= tokenBegin)
383      {
384         len = bufpos - tokenBegin + inBuf + 1;
385      }
386      else
387      {
388         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
389      }
390
391      int i = 0, j = 0, k = 0;
392      int nextColDiff = 0, columnDiff = 0;
393
394      while (i < len &&
395             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
396      {
397         bufline[j] = newLine;
398         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
399         bufcolumn[j] = newCol + columnDiff;
400         columnDiff = nextColDiff;
401         i++;
402      }
403
404      if (i < len)
405      {
406         bufline[j] = newLine++;
407         bufcolumn[j] = newCol + columnDiff;
408
409         while (i++ < len)
410         {
411            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
412               bufline[j] = newLine++;
413            else
414               bufline[j] = newLine;
415         }
416      }
417
418      line = bufline[j];
419      column = bufcolumn[j];
420   }
421
422 }
423
424 /* END JavaCC SimpleCharStream */
425
426 /**
427  * Takes and element and an attribute and makes a CharStream that can
428  * be used to parse that attribute's value.
429  */

430
431 public class AttributeStream
432   extends SimpleCharStream
433 {
434
435   public AttributeStream(Element elt, String JavaDoc attr) {
436     this(elt, attr, elt.getAttributeValue(attr));
437   }
438
439   public AttributeStream(Element elt, String JavaDoc attr, String JavaDoc value) {
440     super
441       (new StringReader(value),
442        // TODO: [2003-04-24 ptw] Get attribute location within
443
// element
444
Parser.getSourceLocation(elt, Parser.LINENO, true).intValue(),
445        Parser.getSourceLocation(elt, Parser.COLNO, true).intValue()
446        );
447   }
448 }
449
Popular Tags