001: /*
002: * $Id: BaseXMLInputFactory.java,v 1.2 2004/07/15 02:51:32 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, unit12.net
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils;
035:
036: import java.io.InputStream;
037: import java.io.InputStreamReader;
038: import java.io.Reader;
039: import java.io.UnsupportedEncodingException;
040:
041: import javanet.staxutils.XMLStreamEventReader;
042:
043: import javax.xml.stream.EventFilter;
044: import javax.xml.stream.StreamFilter;
045: import javax.xml.stream.XMLEventReader;
046: import javax.xml.stream.XMLInputFactory;
047: import javax.xml.stream.XMLReporter;
048: import javax.xml.stream.XMLResolver;
049: import javax.xml.stream.XMLStreamException;
050: import javax.xml.stream.XMLStreamReader;
051: import javax.xml.stream.util.XMLEventAllocator;
052: import javax.xml.transform.Source;
053:
054: /**
055: *
056: * @author Christian Niles
057: * @version $Revision: 1.2 $
058: */
059: public abstract class BaseXMLInputFactory extends XMLInputFactory {
060:
061: protected XMLEventAllocator eventAllocator;
062:
063: protected XMLReporter xmlReporter;
064:
065: protected XMLResolver xmlResolver;
066:
067: public Object getProperty(String name)
068: throws IllegalArgumentException {
069:
070: // TODO provide base support for well-known properties?
071: throw new IllegalArgumentException(name
072: + " property not supported");
073:
074: }
075:
076: public boolean isPropertySupported(String name) {
077:
078: return false;
079:
080: }
081:
082: public void setProperty(String name, Object value)
083: throws IllegalArgumentException {
084:
085: throw new IllegalArgumentException(name
086: + " property not supported");
087:
088: }
089:
090: public XMLEventAllocator getEventAllocator() {
091:
092: return this .eventAllocator;
093:
094: }
095:
096: public void setEventAllocator(XMLEventAllocator eventAllocator) {
097:
098: this .eventAllocator = eventAllocator;
099:
100: }
101:
102: public XMLReporter getXMLReporter() {
103:
104: return this .xmlReporter;
105:
106: }
107:
108: public void setXMLReporter(XMLReporter xmlReporter) {
109:
110: this .xmlReporter = xmlReporter;
111:
112: }
113:
114: public XMLResolver getXMLResolver() {
115:
116: return this .xmlResolver;
117:
118: }
119:
120: public void setXMLResolver(XMLResolver xmlResolver) {
121:
122: this .xmlResolver = xmlResolver;
123:
124: }
125:
126: public XMLEventReader createXMLEventReader(InputStream stream,
127: String encoding) throws XMLStreamException {
128:
129: try {
130:
131: if (encoding != null) {
132:
133: return createXMLEventReader(new InputStreamReader(
134: stream, encoding), encoding);
135:
136: } else {
137:
138: return createXMLEventReader(new InputStreamReader(
139: stream));
140:
141: }
142:
143: } catch (UnsupportedEncodingException e) {
144:
145: throw new XMLStreamException(e);
146:
147: }
148:
149: }
150:
151: public XMLEventReader createXMLEventReader(InputStream stream)
152: throws XMLStreamException {
153:
154: return createXMLEventReader(new InputStreamReader(stream));
155:
156: }
157:
158: public XMLEventReader createXMLEventReader(String systemId,
159: InputStream stream) throws XMLStreamException {
160:
161: return createXMLEventReader(systemId, new InputStreamReader(
162: stream));
163:
164: }
165:
166: public XMLEventReader createXMLEventReader(XMLStreamReader reader)
167: throws XMLStreamException {
168:
169: return new XMLStreamEventReader(reader);
170:
171: }
172:
173: public XMLStreamReader createXMLStreamReader(InputStream stream,
174: String encoding) throws XMLStreamException {
175:
176: try {
177:
178: if (encoding != null) {
179:
180: return createXMLStreamReader(new InputStreamReader(
181: stream, encoding), encoding);
182:
183: } else {
184:
185: return createXMLStreamReader(new InputStreamReader(
186: stream));
187:
188: }
189:
190: } catch (UnsupportedEncodingException e) {
191:
192: throw new XMLStreamException(e);
193:
194: }
195:
196: }
197:
198: public XMLStreamReader createXMLStreamReader(InputStream stream)
199: throws XMLStreamException {
200:
201: return createXMLStreamReader(new InputStreamReader(stream));
202:
203: }
204:
205: public XMLStreamReader createXMLStreamReader(String systemId,
206: InputStream stream) throws XMLStreamException {
207:
208: return createXMLStreamReader(systemId, new InputStreamReader(
209: stream));
210:
211: }
212:
213: public XMLEventReader createXMLEventReader(Reader reader)
214: throws XMLStreamException {
215:
216: return createXMLEventReader(createXMLStreamReader(reader));
217:
218: }
219:
220: public XMLEventReader createXMLEventReader(Reader reader,
221: String encoding) throws XMLStreamException {
222:
223: return createXMLEventReader(createXMLStreamReader(reader,
224: encoding));
225:
226: }
227:
228: public XMLEventReader createXMLEventReader(Source source)
229: throws XMLStreamException {
230:
231: return createXMLEventReader(createXMLStreamReader(source));
232:
233: }
234:
235: public XMLEventReader createXMLEventReader(String systemId,
236: Reader reader) throws XMLStreamException {
237:
238: return createXMLEventReader(createXMLStreamReader(systemId,
239: reader));
240:
241: }
242:
243: public XMLEventReader createXMLEventReader(String systemId,
244: Reader reader, String encoding) throws XMLStreamException {
245:
246: return createXMLEventReader(createXMLStreamReader(systemId,
247: reader, encoding));
248:
249: }
250:
251: public XMLStreamReader createXMLStreamReader(Source source)
252: throws XMLStreamException {
253:
254: // TODO implement TrAX support
255: throw new UnsupportedOperationException();
256:
257: }
258:
259: public XMLStreamReader createXMLStreamReader(Reader reader)
260: throws XMLStreamException {
261:
262: return createXMLStreamReader(null, reader, null);
263:
264: }
265:
266: public XMLStreamReader createXMLStreamReader(Reader reader,
267: String encoding) throws XMLStreamException {
268:
269: return createXMLStreamReader(null, reader, encoding);
270:
271: }
272:
273: public XMLStreamReader createXMLStreamReader(String systemId,
274: Reader reader) throws XMLStreamException {
275:
276: String encoding = null;
277: if (reader instanceof InputStreamReader) {
278:
279: encoding = ((InputStreamReader) reader).getEncoding();
280:
281: }
282:
283: return createXMLStreamReader(systemId, reader, encoding);
284:
285: }
286:
287: public XMLEventReader createFilteredReader(XMLEventReader reader,
288: EventFilter filter) throws XMLStreamException {
289:
290: // TODO implement filter support
291: throw new UnsupportedOperationException();
292:
293: }
294:
295: public XMLStreamReader createFilteredReader(XMLStreamReader reader,
296: StreamFilter filter) throws XMLStreamException {
297:
298: // TODO implement filter support
299: throw new UnsupportedOperationException();
300:
301: }
302:
303: /**
304: * Called by all other methods to construct an {@link XMLStreamReader}.
305: *
306: * @param systemId The system ID of the provided reader, or <code>null</code>
307: * @param reader The character stream from which to construct the StAX stream.
308: * @param encoding The underlying encoding of the reader, or <code>null</code>.
309: * @return The newly constructed {@link XMLStreamReader}.
310: * @throws XMLStreamException If an error occurs constructing the reader.
311: */
312: public abstract XMLStreamReader createXMLStreamReader(
313: String systemId, Reader reader, String encoding)
314: throws XMLStreamException;
315:
316: }
|