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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Adam Megacz
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import javax.xml.stream.*;
033: import javax.xml.stream.util.XMLEventAllocator;
034: import javax.xml.transform.Source;
035: import javax.xml.transform.dom.DOMSource;
036: import javax.xml.transform.sax.SAXSource;
037: import javax.xml.transform.stream.StreamSource;
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.io.InputStreamReader;
041: import java.io.Reader;
042: import java.net.MalformedURLException;
043: import java.net.URL;
044:
045: public class XMLInputFactoryImpl extends XMLInputFactory {
046:
047: private XMLEventAllocator _allocator = new XMLEventAllocatorImpl();
048: private XMLReporter _reporter;
049: private XMLResolver _resolver;
050:
051: public XMLInputFactoryImpl() {
052: }
053:
054: //
055: // Filtered
056: //
057:
058: public XMLEventReader createFilteredReader(XMLEventReader reader,
059: EventFilter filter) throws XMLStreamException {
060: return new FilteredEventReader(reader, filter);
061: }
062:
063: public XMLStreamReader createFilteredReader(XMLStreamReader reader,
064: StreamFilter filter) throws XMLStreamException {
065: return new FilteredStreamReader(reader, filter);
066: }
067:
068: //
069: // Event reader
070: //
071:
072: public XMLEventReader createXMLEventReader(InputStream stream)
073: throws XMLStreamException {
074: return new XMLEventReaderImpl(getEventAllocator(),
075: createXMLStreamReader(stream));
076: }
077:
078: public XMLEventReader createXMLEventReader(InputStream stream,
079: String encoding) throws XMLStreamException {
080: return new XMLEventReaderImpl(getEventAllocator(),
081: createXMLStreamReader(stream, encoding));
082: }
083:
084: public XMLEventReader createXMLEventReader(Reader reader)
085: throws XMLStreamException {
086: return new XMLEventReaderImpl(getEventAllocator(),
087: createXMLStreamReader(reader));
088: }
089:
090: /**
091: * "Support of this method is optional."
092: */
093: public XMLEventReader createXMLEventReader(Source source)
094: throws XMLStreamException {
095: if (source instanceof SAXSource)
096: return new SAXSourceXMLEventReaderImpl((SAXSource) source);
097:
098: return new XMLEventReaderImpl(getEventAllocator(),
099: createXMLStreamReader(source));
100: }
101:
102: public XMLEventReader createXMLEventReader(String systemId,
103: InputStream stream) throws XMLStreamException {
104: return new XMLEventReaderImpl(getEventAllocator(),
105: createXMLStreamReader(systemId, stream));
106: }
107:
108: public XMLEventReader createXMLEventReader(String systemId,
109: Reader reader) throws XMLStreamException {
110: return new XMLEventReaderImpl(getEventAllocator(),
111: createXMLStreamReader(systemId, reader));
112: }
113:
114: public XMLEventReader createXMLEventReader(XMLStreamReader reader)
115: throws XMLStreamException {
116: return new XMLEventReaderImpl(getEventAllocator(), reader);
117: }
118:
119: //
120: // Stream reader
121: //
122:
123: public XMLStreamReader createXMLStreamReader(InputStream stream)
124: throws XMLStreamException {
125: return new XMLStreamReaderImpl(stream);
126: }
127:
128: public XMLStreamReader createXMLStreamReader(InputStream stream,
129: String encoding) throws XMLStreamException {
130: if (encoding == null)
131: encoding = "iso-8859-1";
132:
133: try {
134: InputStreamReader isr = new InputStreamReader(stream,
135: encoding);
136: return new XMLStreamReaderImpl(isr);
137: } catch (IOException e) {
138: throw new XMLStreamException(e);
139: }
140: }
141:
142: public XMLStreamReader createXMLStreamReader(Reader reader)
143: throws XMLStreamException {
144: return new XMLStreamReaderImpl(reader);
145: }
146:
147: /**
148: * "Support of this method is optional."
149: */
150: public XMLStreamReader createXMLStreamReader(Source source)
151: throws XMLStreamException {
152: if (source instanceof StreamSource) {
153: StreamSource streamSource = (StreamSource) source;
154:
155: InputStream is = streamSource.getInputStream();
156:
157: if (is != null)
158: return new XMLStreamReaderImpl(is);
159:
160: Reader r = streamSource.getReader();
161:
162: if (r != null)
163: return new XMLStreamReaderImpl(r);
164:
165: if (streamSource.getSystemId() != null) {
166: try {
167: URL url = new URL(streamSource.getSystemId());
168:
169: return new XMLStreamReaderImpl(url.openStream());
170: } catch (MalformedURLException e) {
171: throw new XMLStreamException(e);
172: } catch (IOException e) {
173: throw new XMLStreamException(e);
174: }
175: } else
176: throw new XMLStreamException(
177: "StreamSource contains no stream information");
178: } else if (source instanceof DOMSource)
179: return new DOMSourceXMLStreamReaderImpl((DOMSource) source);
180: else if (source instanceof SAXSource)
181: throw new JAXPNotSupportedInStAXException();
182:
183: throw new JAXPNotSupportedInStAXException();
184: }
185:
186: public XMLStreamReader createXMLStreamReader(String systemId,
187: InputStream stream) throws XMLStreamException {
188: return new XMLStreamReaderImpl(stream, systemId);
189: }
190:
191: public XMLStreamReader createXMLStreamReader(String systemId,
192: Reader reader) throws XMLStreamException {
193: return new XMLStreamReaderImpl(reader, systemId);
194: }
195:
196: public XMLEventAllocator getEventAllocator() {
197: return _allocator;
198: }
199:
200: public Object getProperty(String name)
201: throws IllegalArgumentException {
202: throw new IllegalArgumentException("property \"" + name
203: + "\" not supported");
204: }
205:
206: public XMLReporter getXMLReporter() {
207: return _reporter;
208: }
209:
210: public XMLResolver getXMLResolver() {
211: return _resolver;
212: }
213:
214: public boolean isPropertySupported(String name) {
215: return false;
216: }
217:
218: public void setEventAllocator(XMLEventAllocator allocator) {
219: _allocator = allocator;
220: }
221:
222: public void setProperty(String name, Object value)
223: throws IllegalArgumentException {
224: if ("javax.xml.stream.allocator".equals(name)) {
225: setEventAllocator((XMLEventAllocator) value);
226: return;
227: } else if ("javax.xml.stream.isNamespaceAware".equals(name)) {
228: // XXX?
229: return;
230: }
231:
232: throw new IllegalArgumentException("property \"" + name
233: + "\" not supported");
234: }
235:
236: public void setXMLReporter(XMLReporter reporter) {
237: _reporter = reporter;
238: }
239:
240: public void setXMLResolver(XMLResolver resolver) {
241: _resolver = resolver;
242: }
243: }
|