001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml.readers;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.util.L10N;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.ReadStream;
035: import com.caucho.xml.XmlChar;
036: import com.caucho.xml.XmlParser;
037:
038: import org.xml.sax.SAXException;
039:
040: import java.io.IOException;
041:
042: /**
043: * A fast reader to convert bytes to characters for parsing XML.
044: */
045: public class XmlReader {
046: static final L10N L = new L10N(XmlReader.class);
047:
048: protected static boolean[] isAsciiNameChar;
049:
050: protected XmlParser _parser;
051: protected XmlReader _next;
052:
053: protected Path _searchPath;
054: protected ReadStream _is;
055: protected String _filename;
056: protected int _line;
057:
058: protected String _systemId;
059: protected String _publicId;
060:
061: /**
062: * Create a new reader.
063: */
064: public XmlReader() {
065: }
066:
067: /**
068: * Create a new reader with the given read stream.
069: */
070: public XmlReader(XmlParser parser, ReadStream is) {
071: init(parser, is);
072: }
073:
074: /**
075: * Initialize a reader at the start of parsing.
076: */
077: public void init(XmlParser parser, ReadStream is) {
078: _parser = parser;
079: _is = is;
080: _filename = is.getUserPath();
081: _line = 1;
082: }
083:
084: /**
085: * Sets the filename.
086: */
087: public void setFilename(String filename) {
088: _filename = filename;
089: }
090:
091: /**
092: * Gets the filename.
093: */
094: public String getFilename() {
095: return _filename;
096: }
097:
098: /**
099: * Sets the current line number.
100: */
101: public void setLine(int line) {
102: _line = line;
103: }
104:
105: /**
106: * Gets the current line number.
107: */
108: public int getLine() {
109: return _line;
110: }
111:
112: /**
113: * Sets the systemId.
114: */
115: public void setSystemId(String systemId) {
116: _systemId = systemId;
117: }
118:
119: /**
120: * Gets the systemId.
121: */
122: public String getSystemId() {
123: return _systemId;
124: }
125:
126: /**
127: * Sets the publicId.
128: */
129: public void setPublicId(String publicId) {
130: _publicId = publicId;
131: }
132:
133: /**
134: * Gets the publicId.
135: */
136: public String getPublicId() {
137: return _publicId;
138: }
139:
140: /**
141: * Sets the current search path.
142: */
143: public void setSearchPath(Path searchPath) {
144: _searchPath = searchPath;
145: }
146:
147: /**
148: * Gets the current search path.
149: */
150: public Path getSearchPath() {
151: return _searchPath;
152: }
153:
154: /**
155: * Sets the next reader.
156: */
157: public void setNext(XmlReader next) {
158: _next = next;
159: }
160:
161: /**
162: * Sets the next reader.
163: */
164: public XmlReader getNext() {
165: return _next;
166: }
167:
168: /**
169: * Returns the read stream.
170: */
171: public ReadStream getReadStream() {
172: return _is;
173: }
174:
175: /**
176: * Read the next character, returning -1 on end of file..
177: */
178: public int read() throws IOException {
179: int ch = _is.readChar();
180:
181: if (ch == '\n')
182: _parser.setLine(++_line);
183:
184: return ch;
185: }
186:
187: /**
188: * Parses a name.
189: */
190: public int parseName(CharBuffer name, int ch) throws IOException,
191: SAXException {
192: char[] buffer = name.getBuffer();
193: int capacity = buffer.length;
194: int offset = 0;
195:
196: buffer[offset++] = (char) ch;
197:
198: for (ch = read(); ch > 0 && ch < 128 && isAsciiNameChar[ch]
199: || XmlChar.isNameChar(ch); ch = read()) {
200: if (offset >= capacity) {
201: name.setLength(offset);
202: name.append((char) ch);
203: offset++;
204: buffer = name.getBuffer();
205: capacity = buffer.length;
206: } else
207: buffer[offset++] = (char) ch;
208: }
209:
210: name.setLength(offset);
211:
212: return ch;
213: }
214:
215: /**
216: * Finish reading.
217: */
218: public void finish() {
219: _is = null;
220: }
221:
222: static {
223: isAsciiNameChar = XmlChar.getAsciiNameCharArray();
224: }
225: }
|