KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > readers > XmlReader


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.xml.readers;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.xml.XmlChar;
36 import com.caucho.xml.XmlParser;
37
38 import org.xml.sax.SAXException JavaDoc;
39
40 import java.io.IOException JavaDoc;
41
42 /**
43  * A fast reader to convert bytes to characters for parsing XML.
44  */

45 public class XmlReader {
46   static final L10N L = new L10N(XmlReader.class);
47   
48   protected static boolean []isAsciiNameChar;
49   
50   protected XmlParser _parser;
51   protected XmlReader _next;
52
53   protected Path _searchPath;
54   protected ReadStream _is;
55   protected String JavaDoc _filename;
56   protected int _line;
57
58   protected String JavaDoc _systemId;
59   protected String JavaDoc _publicId;
60
61   /**
62    * Create a new reader.
63    */

64   public XmlReader()
65   {
66   }
67
68   /**
69    * Create a new reader with the given read stream.
70    */

71   public XmlReader(XmlParser parser, ReadStream is)
72   {
73     init(parser, is);
74   }
75
76   /**
77    * Initialize a reader at the start of parsing.
78    */

79   public void init(XmlParser parser, ReadStream is)
80   {
81     _parser = parser;
82     _is = is;
83     _filename = is.getUserPath();
84     _line = 1;
85   }
86
87   /**
88    * Sets the filename.
89    */

90   public void setFilename(String JavaDoc filename)
91   {
92     _filename = filename;
93   }
94
95   /**
96    * Gets the filename.
97    */

98   public String JavaDoc getFilename()
99   {
100     return _filename;
101   }
102
103   /**
104    * Sets the current line number.
105    */

106   public void setLine(int line)
107   {
108     _line = line;
109   }
110
111   /**
112    * Gets the current line number.
113    */

114   public int getLine()
115   {
116     return _line;
117   }
118
119   /**
120    * Sets the systemId.
121    */

122   public void setSystemId(String JavaDoc systemId)
123   {
124     _systemId = systemId;
125   }
126
127   /**
128    * Gets the systemId.
129    */

130   public String JavaDoc getSystemId()
131   {
132     return _systemId;
133   }
134
135   /**
136    * Sets the publicId.
137    */

138   public void setPublicId(String JavaDoc publicId)
139   {
140     _publicId = publicId;
141   }
142
143   /**
144    * Gets the publicId.
145    */

146   public String JavaDoc getPublicId()
147   {
148     return _publicId;
149   }
150
151   /**
152    * Sets the current search path.
153    */

154   public void setSearchPath(Path searchPath)
155   {
156     _searchPath = searchPath;
157   }
158
159   /**
160    * Gets the current search path.
161    */

162   public Path getSearchPath()
163   {
164     return _searchPath;
165   }
166
167   /**
168    * Sets the next reader.
169    */

170   public void setNext(XmlReader next)
171   {
172     _next = next;
173   }
174
175   /**
176    * Sets the next reader.
177    */

178   public XmlReader getNext()
179   {
180     return _next;
181   }
182
183   /**
184    * Returns the read stream.
185    */

186   public ReadStream getReadStream()
187   {
188     return _is;
189   }
190   
191   /**
192    * Read the next character, returning -1 on end of file..
193    */

194   public int read()
195     throws IOException JavaDoc
196   {
197     int ch = _is.readChar();
198
199     if (ch == '\n')
200       _parser.setLine(++_line);
201     
202     return ch;
203   }
204
205   /**
206    * Parses a name.
207    */

208   public int parseName(CharBuffer name, int ch)
209     throws IOException JavaDoc, SAXException JavaDoc
210   {
211     char []buffer = name.getBuffer();
212     int capacity = buffer.length;
213     int offset = 0;
214
215     buffer[offset++] = (char) ch;
216
217     for (ch = read();
218          ch > 0 && ch < 128 && isAsciiNameChar[ch] || XmlChar.isNameChar(ch);
219          ch = read()) {
220       if (offset >= capacity) {
221         name.setLength(offset);
222         name.append((char) ch);
223         offset++;
224         buffer = name.getBuffer();
225         capacity = buffer.length;
226       }
227       else
228         buffer[offset++] = (char) ch;
229     }
230
231     name.setLength(offset);
232
233     return ch;
234   }
235
236   /**
237    * Finish reading.
238    */

239   public void finish()
240   {
241     _is = null;
242   }
243
244   static {
245     isAsciiNameChar = XmlChar.getAsciiNameCharArray();
246   }
247 }
248
249
Popular Tags