001: /*
002: $Id: ZephyrParserFactory.java,v 1.4 2006/11/28 19:22:49 spericas Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream;
030:
031: import java.io.BufferedInputStream;
032: import java.io.BufferedReader;
033: import java.io.ByteArrayInputStream;
034: import java.io.CharArrayReader;
035: import java.io.InputStream;
036: import java.io.Reader;
037: import java.io.StringReader;
038:
039: import javax.xml.stream.*;
040: import javax.xml.stream.util.XMLEventAllocator;
041: import javax.xml.transform.Source;
042: import javax.xml.transform.stream.StreamSource;
043: import com.sun.xml.stream.xerces.xni.parser.XMLInputSource;
044:
045: /** Factory Implementation for XMLInputFactory.
046: * @author Neeraj Bajaj Sun Microsystems
047: * @author K.Venugopal Sun Microsystems
048: */
049:
050: //xxx: Should we be reusing the XMLInputSource object
051: public class ZephyrParserFactory extends
052: javax.xml.stream.XMLInputFactory {
053:
054: //List of supported properties and default values.
055: private PropertyManager fPropertyManager = new PropertyManager(
056: PropertyManager.CONTEXT_READER);
057: private static final boolean DEBUG = false;
058:
059: //Maintain a reference to last reader instantiated.
060: private XMLReaderImpl fTempReader = null;
061:
062: boolean fPropertyChanged = false;
063: //default value is true
064: boolean fReuseInstance = true;
065:
066: /** Creates a new instance of ZephryParserFactory */
067: public ZephyrParserFactory() {
068: //fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER) ;
069: }
070:
071: void initEventReader() {
072: fPropertyChanged = true;
073: }
074:
075: /**
076: * @param inputstream
077: * @throws XMLStreamException
078: * @return
079: */
080: public XMLEventReader createXMLEventReader(InputStream inputstream)
081: throws XMLStreamException {
082: initEventReader();
083: //delegate everything to XMLStreamReader
084: return new XMLEventReaderImpl(
085: createXMLStreamReader(inputstream));
086: }
087:
088: public XMLEventReader createXMLEventReader(Reader reader)
089: throws XMLStreamException {
090: initEventReader();
091: //delegate everything to XMLStreamReader
092: return new XMLEventReaderImpl(createXMLStreamReader(reader));
093: }
094:
095: public XMLEventReader createXMLEventReader(Source source)
096: throws XMLStreamException {
097: initEventReader();
098: //delegate everything to XMLStreamReader
099: return new XMLEventReaderImpl(createXMLStreamReader(source));
100: }
101:
102: public XMLEventReader createXMLEventReader(String systemId,
103: InputStream inputstream) throws XMLStreamException {
104: initEventReader();
105: //delegate everything to XMLStreamReader
106: return new XMLEventReaderImpl(createXMLStreamReader(systemId,
107: inputstream));
108: }
109:
110: public XMLEventReader createXMLEventReader(
111: java.io.InputStream stream, String encoding)
112: throws XMLStreamException {
113: initEventReader();
114: //delegate everything to XMLStreamReader
115: return new XMLEventReaderImpl(createXMLStreamReader(stream,
116: encoding));
117: }
118:
119: public XMLEventReader createXMLEventReader(String systemId,
120: Reader reader) throws XMLStreamException {
121: initEventReader();
122: //delegate everything to XMLStreamReader
123: return new XMLEventReaderImpl(createXMLStreamReader(systemId,
124: reader));
125: }
126:
127: /** Create a new XMLEventReader from an XMLStreamReader. After being used
128: * to construct the XMLEventReader instance returned from this method
129: * the XMLStreamReader must not be used.
130: * @param reader the XMLStreamReader to read from (may not be modified)
131: * @return a new XMLEventReader
132: * @throws XMLStreamException
133: */
134: public XMLEventReader createXMLEventReader(XMLStreamReader reader)
135: throws XMLStreamException {
136: initEventReader();
137: //xxx: what do we do now -- instance is passed from the application
138: //probably we should check if the state is at the start document,
139: //eventreader call to next() should return START_DOCUMENT and
140: //then delegate every call to underlying streamReader
141: return new XMLEventReaderImpl(reader);
142: }
143:
144: public XMLStreamReader createXMLStreamReader(Reader reader)
145: throws XMLStreamException {
146: return createXMLStreamReader(null, reader);
147: }
148:
149: public XMLStreamReader createXMLStreamReader(String systemId,
150: Reader reader) throws XMLStreamException {
151: XMLInputSource inputSource = new XMLInputSource(null, systemId,
152: null, reader, null);
153: return getXMLStreamReaderImpl(inputSource);
154: }
155:
156: public XMLStreamReader createXMLStreamReader(Source source)
157: throws XMLStreamException {
158: return new XMLReaderImpl(jaxpSourcetoXMLInputSource(source),
159: new PropertyManager(fPropertyManager));
160: }
161:
162: public XMLStreamReader createXMLStreamReader(InputStream inputStream)
163: throws XMLStreamException {
164: return createXMLStreamReader(null, inputStream, null);
165: }
166:
167: public XMLStreamReader createXMLStreamReader(String systemId,
168: InputStream inputStream) throws XMLStreamException {
169: return createXMLStreamReader(systemId, inputStream, null);
170: }
171:
172: public XMLStreamReader createXMLStreamReader(
173: InputStream inputStream, String encoding)
174: throws XMLStreamException {
175: return createXMLStreamReader(null, inputStream, encoding);
176: }
177:
178: //API doesn't allow to set systemId and encoding simultaneously. I think that is a
179: //mistake - Neeraj Bajaj
180: //Making this function public in case any one wants to use.
181: public XMLStreamReader createXMLStreamReader(String systemId,
182: InputStream inputStream, String encoding)
183: throws XMLStreamException {
184: XMLInputSource inputSource = new XMLInputSource(null, systemId,
185: null, inputStream, encoding);
186: return getXMLStreamReaderImpl(inputSource);
187: }
188:
189: public XMLEventAllocator getEventAllocator() {
190: return (XMLEventAllocator) getProperty(XMLInputFactory.ALLOCATOR);
191: }
192:
193: public XMLReporter getXMLReporter() {
194: return (XMLReporter) fPropertyManager
195: .getProperty(XMLInputFactory.REPORTER);
196: }
197:
198: public XMLResolver getXMLResolver() {
199: Object object = fPropertyManager
200: .getProperty(XMLInputFactory.RESOLVER);
201: return (XMLResolver) object;
202: //return (XMLResolver)fPropertyManager.getProperty(XMLInputFactory.RESOLVER);
203: }
204:
205: public void setXMLReporter(XMLReporter xmlreporter) {
206: fPropertyManager.setProperty(XMLInputFactory.REPORTER,
207: xmlreporter);
208: }
209:
210: public void setXMLResolver(XMLResolver xmlresolver) {
211: fPropertyManager.setProperty(XMLInputFactory.RESOLVER,
212: xmlresolver);
213: }
214:
215: /** Create a filtered event reader that wraps the filter around the event reader
216: * @param reader the event reader to wrap
217: * @param filter the filter to apply to the event reader
218: * @throws XMLStreamException
219: */
220: public XMLEventReader createFilteredReader(XMLEventReader reader,
221: EventFilter filter) throws XMLStreamException {
222: return new EventFilterSupport(reader, filter);
223: }
224:
225: /** Create a filtered reader that wraps the filter around the reader
226: * @param reader the reader to filter
227: * @param filter the filter to apply to the reader
228: * @throws XMLStreamException
229: */
230: public XMLStreamReader createFilteredReader(XMLStreamReader reader,
231: StreamFilter filter) throws XMLStreamException {
232: if (reader != null && filter != null)
233: return new XMLStreamFilterImpl(reader, filter);
234:
235: return null;
236: }
237:
238: /** Get the value of a feature/property from the underlying implementation
239: * @param name The name of the property (may not be null)
240: * @return The value of the property
241: * @throws IllegalArgumentException if the property is not supported
242: */
243: public Object getProperty(java.lang.String name)
244: throws java.lang.IllegalArgumentException {
245: if (name == null) {
246: throw new IllegalArgumentException("Property not supported");
247: }
248: if (fPropertyManager.containsProperty(name))
249: return fPropertyManager.getProperty(name);
250: throw new IllegalArgumentException("Property not supported");
251: }
252:
253: /** Query the set of fProperties that this factory supports.
254: *
255: * @param name The name of the property (may not be null)
256: * @return true if the property is supported and false otherwise
257: */
258: public boolean isPropertySupported(String name) {
259: if (name == null)
260: return false;
261: else
262: return fPropertyManager.containsProperty(name);
263: }
264:
265: /** Set a user defined event allocator for events
266: * @param allocator the user defined allocator
267: */
268: public void setEventAllocator(XMLEventAllocator allocator) {
269: fPropertyManager.setProperty(XMLInputFactory.ALLOCATOR,
270: allocator);
271: }
272:
273: /** Allows the user to set specific feature/property on the underlying implementation. The underlying implementation
274: * is not required to support every setting of every property in the specification and may use IllegalArgumentException
275: * to signal that an unsupported property may not be set with the specified value.
276: * @param name The name of the property (may not be null)
277: * @param value The value of the property
278: * @throws java.lang.IllegalArgumentException if the property is not supported
279: */
280: public void setProperty(java.lang.String name, Object value)
281: throws java.lang.IllegalArgumentException {
282:
283: if (name == null || value == null
284: || !fPropertyManager.containsProperty(name)) {
285: throw new IllegalArgumentException("Property " + name
286: + " is not supported");
287: }
288: if (name == Constants.REUSE_INSTANCE
289: || name.equals(Constants.REUSE_INSTANCE)) {
290: fReuseInstance = ((Boolean) value).booleanValue();
291: if (DEBUG)
292: System.out.println("fReuseInstance is set to "
293: + fReuseInstance);
294: } else {//for any other property set the flag
295: //REVISIT: Even in this case instance can be reused, by passing PropertyManager
296: fPropertyChanged = true;
297: }
298:
299: fPropertyManager.setProperty(name, value);
300: }
301:
302: XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource)
303: throws javax.xml.stream.XMLStreamException {
304: //1. if the temp reader is null -- create the instance and return
305: if (fTempReader == null) {
306: fPropertyChanged = false;
307: if (DEBUG)
308: System.out.println("New Instance is being returned");
309: return fTempReader = new XMLReaderImpl(inputSource,
310: new PropertyManager(fPropertyManager));
311: }
312: //if factory is configured to reuse the instance & this instance can be reused
313: //& the setProperty() hasn't been called
314: if (fReuseInstance && fTempReader.canReuse()
315: && !fPropertyChanged) {
316: if (DEBUG)
317: System.out.println("Reusing the instance");
318: //we can make setInputSource() call reset() and this way there wont be two function calls
319: fTempReader.reset();
320: fTempReader.setInputSource(inputSource);
321: fPropertyChanged = false;
322: return fTempReader;
323: } else {
324: fPropertyChanged = false;
325: if (DEBUG)
326: System.out.println("New Instance is being returned");
327: //just return the new instance.. note that we are not setting fTempReader to the newly created instance
328: return fTempReader = new XMLReaderImpl(inputSource,
329: new PropertyManager(fPropertyManager));
330: }
331: }
332:
333: XMLInputSource jaxpSourcetoXMLInputSource(Source source) {
334:
335: if (source instanceof StreamSource) {
336: StreamSource stSource = (StreamSource) source;
337: String systemId = stSource.getSystemId();
338: String publicId = stSource.getPublicId();
339: InputStream istream = stSource.getInputStream();
340: Reader reader = stSource.getReader();
341:
342: if (istream != null) {
343: return new XMLInputSource(publicId, systemId, null,
344: istream, null);
345: } else if (reader != null) {
346: return new XMLInputSource(publicId, systemId, null,
347: reader, null);
348: } else {
349: return new XMLInputSource(publicId, systemId, null);
350: }
351: } else if (source instanceof Source) {
352: return new XMLInputSource(null, source.getSystemId(), null);
353: } else {
354: throw new java.lang.UnsupportedOperationException(source
355: .getClass().getName()
356: + " type is not supported");
357: }
358: }
359: }//ZephyrParserFactory
|