KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > readers > MacroReader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2.readers;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.vfs.ReadStream;
33 import com.caucho.xml2.XmlParser;
34
35 import java.io.IOException JavaDoc;
36
37 /**
38  * A fast reader to convert bytes to characters for parsing XML.
39  */

40 public class MacroReader extends XmlReader {
41   char []_buffer = new char[256];
42   int _offset;
43   int _length;
44   
45   /**
46    * Create a new reader.
47    */

48   public MacroReader()
49   {
50   }
51
52   public void init(XmlParser parser, XmlReader next)
53   {
54     _parser = parser;
55     _next = next;
56     _offset = 0;
57     _length = 0;
58   }
59
60   public ReadStream getReadStream()
61   {
62     return _next.getReadStream();
63   }
64
65   public String JavaDoc getSystemId()
66   {
67     if (_next != null)
68       return _next.getSystemId();
69     else
70       return super.getSystemId();
71   }
72
73   public String JavaDoc getPublicId()
74   {
75     if (_next != null)
76       return _next.getPublicId();
77     else
78       return super.getPublicId();
79   }
80
81   public String JavaDoc getFilename()
82   {
83     if (_next != null)
84       return _next.getFilename();
85     else
86       return super.getFilename();
87   }
88
89   public int getLine()
90   {
91     if (_next != null)
92       return _next.getLine();
93     else
94       return super.getLine();
95   }
96
97   /**
98    * Adds a string to the macro.
99    */

100   public void add(String JavaDoc s)
101   {
102     int len = s.length();
103
104     for (int i = 0; i < len; i++)
105       add(s.charAt(i));
106   }
107
108   /**
109    * Adds a char buffer to the macro.
110    */

111   public void add(CharBuffer cb)
112   {
113     int len = cb.length();
114
115     for (int i = 0; i < len; i++)
116       add(cb.charAt(i));
117   }
118
119   /**
120    * Adds a new character to the buffer.
121    */

122   public void add(char ch)
123   {
124     if (_offset == _length) {
125       _offset = 0;
126       _length = 0;
127     }
128
129     if (_buffer.length == _length) {
130       char []newBuffer = new char[2 * _buffer.length];
131       System.arraycopy(_buffer, 0, newBuffer, 0, _length);
132       _buffer = newBuffer;
133     }
134
135     _buffer[_length++] = ch;
136   }
137
138   public void prepend(char ch)
139   {
140     if (_offset == _length) {
141       _offset = 0;
142       _length = 0;
143     }
144
145     if (_buffer.length == _length) {
146       char []newBuffer = new char[2 * _buffer.length];
147       System.arraycopy(_buffer, 0, newBuffer, 0, _length);
148       _buffer = newBuffer;
149     }
150
151     for (int i = _length; i >= 0; i--)
152       _buffer[i + 1] = _buffer[i];
153
154     _length++;
155     _buffer[0] = ch;
156   }
157   
158   /**
159    * Read the next character, returning -1 on end of file..
160    */

161   public int read()
162     throws IOException JavaDoc
163   {
164     if (_offset < _length)
165       return _buffer[_offset++];
166     else
167       return _next.read();
168   }
169 }
170
171
Popular Tags