KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXInputFactory


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.stax;
41
42 import com.sun.xml.fastinfoset.tools.XML_SAX_FI;
43
44 import java.io.InputStream JavaDoc;
45 import java.io.Reader JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.BufferedInputStream JavaDoc;
49 import java.io.BufferedOutputStream JavaDoc;
50
51 import javax.xml.stream.*;
52 import javax.xml.stream.XMLStreamException;
53 import javax.xml.stream.util.XMLEventAllocator ;
54 import javax.xml.transform.Source JavaDoc;
55 import javax.xml.transform.stream.StreamSource JavaDoc;
56 import com.sun.xml.fastinfoset.CommonResourceBundle;
57
58 public class StAXInputFactory extends XMLInputFactory {
59     //List of supported properties and default values.
60
private StAXManager _manager = new StAXManager(StAXManager.CONTEXT_READER) ;
61     
62     public StAXInputFactory() {
63     }
64     
65     public static XMLInputFactory newInstance() {
66         return XMLInputFactory.newInstance();
67     }
68
69   /**
70    * Create a new XMLStreamReader from a reader
71    * @param reader the XML data to read from
72    * @throws XMLStreamException
73    */

74     public XMLStreamReader createXMLStreamReader(Reader JavaDoc xmlfile) throws XMLStreamException {
75         return getXMLStreamReader(xmlfile);
76     }
77     
78     public XMLStreamReader createXMLStreamReader(InputStream JavaDoc s) throws XMLStreamException {
79         return new StAXDocumentParser(s, _manager);
80     }
81     
82     public XMLStreamReader createXMLStreamReader(String JavaDoc systemId, Reader JavaDoc xmlfile) throws XMLStreamException {
83         return getXMLStreamReader(xmlfile);
84     }
85     
86     public XMLStreamReader createXMLStreamReader(Source JavaDoc source) throws XMLStreamException {
87         return null;
88     }
89     
90     public XMLStreamReader createXMLStreamReader(String JavaDoc systemId, InputStream JavaDoc inputstream) throws XMLStreamException {
91         return createXMLStreamReader(inputstream);
92     }
93     
94     
95     public XMLStreamReader createXMLStreamReader(InputStream JavaDoc inputstream, String JavaDoc encoding) throws XMLStreamException {
96         return createXMLStreamReader(inputstream);
97     }
98
99     
100     XMLStreamReader getXMLStreamReader(String JavaDoc systemId, InputStream JavaDoc inputstream, String JavaDoc encoding)
101         throws XMLStreamException{
102         return createXMLStreamReader(inputstream);
103
104     }
105
106     /**
107      * @param inputstream
108      * @throws XMLStreamException
109      * @return
110      */

111     XMLStreamReader getXMLStreamReader(Reader JavaDoc xmlfile)
112         throws XMLStreamException{
113         
114         ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
115         BufferedOutputStream JavaDoc bufferedStream = new BufferedOutputStream JavaDoc(byteStream);
116         StAXDocumentParser sr = null;
117         try {
118             XML_SAX_FI convertor = new XML_SAX_FI();
119             convertor.convert(xmlfile, bufferedStream);
120
121             ByteArrayInputStream JavaDoc byteInputStream = new ByteArrayInputStream JavaDoc(byteStream.toByteArray());
122             InputStream JavaDoc document = new BufferedInputStream JavaDoc(byteInputStream);
123             sr = new StAXDocumentParser();
124             sr.setInputStream(document);
125             sr.setManager(_manager);
126             return sr;
127             //return new StAXDocumentParser(document, _manager);
128
} catch (Exception JavaDoc e) {
129             return null;
130         }
131
132     }
133            
134     
135     /**
136      * @param inputstream
137      * @throws XMLStreamException
138      * @return
139      */

140     public XMLEventReader createXMLEventReader(InputStream JavaDoc inputstream) throws XMLStreamException {
141         return new StAXEventReader(createXMLStreamReader(inputstream));
142     }
143     
144     public XMLEventReader createXMLEventReader(Reader JavaDoc reader) throws XMLStreamException {
145         return new StAXEventReader(createXMLStreamReader(reader));
146     }
147     
148     public XMLEventReader createXMLEventReader(Source JavaDoc source) throws XMLStreamException {
149         return new StAXEventReader(createXMLStreamReader(source));
150     }
151     
152     public XMLEventReader createXMLEventReader(String JavaDoc systemId, InputStream JavaDoc inputstream) throws XMLStreamException {
153         return new StAXEventReader(createXMLStreamReader(systemId, inputstream));
154     }
155     
156     public XMLEventReader createXMLEventReader(java.io.InputStream JavaDoc stream, String JavaDoc encoding) throws XMLStreamException {
157         return new StAXEventReader(createXMLStreamReader(stream, encoding));
158     }
159     
160     public XMLEventReader createXMLEventReader(String JavaDoc systemId, Reader JavaDoc reader) throws XMLStreamException {
161         return new StAXEventReader(createXMLStreamReader(systemId, reader));
162     }
163     
164     /** Create a new XMLEventReader from an XMLStreamReader. After being used
165      * to construct the XMLEventReader instance returned from this method
166      * the XMLStreamReader must not be used.
167      * @param reader the XMLStreamReader to read from (may not be modified)
168      * @return a new XMLEventReader
169      * @throws XMLStreamException
170      */

171     public XMLEventReader createXMLEventReader(XMLStreamReader streamReader) throws XMLStreamException {
172         return new StAXEventReader(streamReader);
173     }
174
175     public XMLEventAllocator getEventAllocator() {
176         return (XMLEventAllocator)getProperty(XMLInputFactory.ALLOCATOR);
177     }
178     
179     public XMLReporter getXMLReporter() {
180         return (XMLReporter)_manager.getProperty(XMLInputFactory.REPORTER);
181     }
182     
183     public XMLResolver getXMLResolver() {
184         Object JavaDoc object = _manager.getProperty(XMLInputFactory.RESOLVER);
185         return (XMLResolver)object;
186         //return (XMLResolver)_manager.getProperty(XMLInputFactory.RESOLVER);
187
}
188     
189     public void setXMLReporter(XMLReporter xmlreporter) {
190         _manager.setProperty(XMLInputFactory.REPORTER, xmlreporter);
191     }
192     
193     public void setXMLResolver(XMLResolver xmlresolver) {
194         _manager.setProperty(XMLInputFactory.RESOLVER, xmlresolver);
195     }
196     
197     /** Create a filtered event reader that wraps the filter around the event reader
198      * @param reader the event reader to wrap
199      * @param filter the filter to apply to the event reader
200      * @throws XMLStreamException
201      */

202     public XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter) throws XMLStreamException {
203         return new StAXFilteredEvent(reader, filter);
204     }
205     
206     /** Create a filtered reader that wraps the filter around the reader
207      * @param reader the reader to filter
208      * @param filter the filter to apply to the reader
209      * @throws XMLStreamException
210      */

211     public XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) throws XMLStreamException {
212         
213         if( reader != null && filter != null )
214             return new StAXFilteredParser(reader,filter);
215         
216         return null;
217     }
218     
219     
220     
221     /** Get the value of a feature/property from the underlying implementation
222      * @param name The name of the property (may not be null)
223      * @return The value of the property
224      * @throws IllegalArgumentException if the property is not supported
225      */

226     public Object JavaDoc getProperty(String JavaDoc name) throws IllegalArgumentException JavaDoc {
227         if(name == null){
228             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.nullPropertyName"));
229         }
230         if(_manager.containsProperty(name))
231             return _manager.getProperty(name);
232         throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object JavaDoc[]{name}));
233     }
234     
235     /** Query the set of Properties that this factory supports.
236      *
237      * @param name The name of the property (may not be null)
238      * @return true if the property is supported and false otherwise
239      */

240     public boolean isPropertySupported(String JavaDoc name) {
241         if(name == null)
242             return false ;
243         else
244             return _manager.containsProperty(name);
245     }
246     
247     /** Set a user defined event allocator for events
248      * @param allocator the user defined allocator
249      */

250     public void setEventAllocator(XMLEventAllocator allocator) {
251         _manager.setProperty(XMLInputFactory.ALLOCATOR, allocator);
252     }
253     
254     /** Allows the user to set specific feature/property on the underlying implementation. The underlying implementation
255      * is not required to support every setting of every property in the specification and may use IllegalArgumentException
256      * to signal that an unsupported property may not be set with the specified value.
257      * @param name The name of the property (may not be null)
258      * @param value The value of the property
259      * @throws IllegalArgumentException if the property is not supported
260      */

261     public void setProperty(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc {
262         _manager.setProperty(name,value);
263     }
264     
265 }
266
Popular Tags