View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream;
17  
18  import java.io.Reader;
19  import java.util.Enumeration;
20  import javax.xml.stream.EventFilter;
21  import javax.xml.stream.XMLInputFactory;
22  import javax.xml.stream.util.XMLEventAllocator;
23  import javax.xml.stream.XMLEventReader;
24  import javax.xml.stream.XMLStreamReader;
25  import javax.xml.stream.XMLReporter;
26  import javax.xml.stream.StreamFilter;
27  import javax.xml.stream.XMLResolver;
28  import javax.xml.stream.XMLStreamException;
29  
30  import javax.xml.transform.Source;
31  
32  public class MXParserFactory extends XMLInputFactory {
33  
34    ConfigurationContextBase config = 
35      new ConfigurationContextBase();
36  
37    public static XMLInputFactory newInstance() {
38      return XMLInputFactory.newInstance();
39    }
40  
41    public XMLStreamReader createXMLStreamReader(Source source) throws XMLStreamException {
42      return null;
43    }
44  
45    /***
46     * Create a new XMLStreamReader from a java.io.stream
47     * @param stream the InputStream to read from
48     */
49    public XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException {
50      try {
51        return createXMLStreamReader(com.bea.xml.stream.reader.XmlReader.createReader(stream));
52      } catch (java.io.IOException ioe) {
53        throw new XMLStreamException("Unable to instantiate a reader",ioe);
54      }
55    }
56  
57    /***
58     * Create a new XMLStreamReader from a java.io.stream
59     * @param stream the InputStream to read from
60     * @param encoding the character encoding of the stream
61     */
62    public XMLStreamReader createXMLStreamReader(java.io.InputStream stream, String encoding) throws XMLStreamException {
63      try {
64        return createXMLStreamReader(new java.io.InputStreamReader(stream,encoding));
65      } catch (java.io.UnsupportedEncodingException uee) {
66        throw new XMLStreamException("The encoding "+encoding+" is not supported.",uee);
67      }
68  
69    }
70  
71    public XMLStreamReader createXMLStreamReader(String systemId, java.io.InputStream stream) throws XMLStreamException {
72      return createXMLStreamReader(stream);
73    }
74  
75    public XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader) throws XMLStreamException {
76      return createXMLStreamReader(reader);
77    }
78  
79    public XMLEventReader createXMLEventReader(String systemId, java.io.Reader reader) throws XMLStreamException {
80      return createXMLEventReader(reader);
81    }
82  
83    public XMLEventReader createXMLEventReader(String systemId, java.io.InputStream stream) throws XMLStreamException {
84      return createXMLEventReader(stream);
85    }
86  
87    /***
88     * Create a new XMLEventReader from a reader
89     * @param reader the XML data to read from
90     */
91    public XMLEventReader createXMLEventReader(java.io.Reader reader) throws XMLStreamException {
92      return createXMLEventReader(createXMLStreamReader(reader));
93    }
94  
95    /***
96     * Create a new XMLEventReader from an XMLStreamReader
97     * @param reader the XMLEventReader to read from
98     */
99    public XMLEventReader createXMLEventReader(XMLStreamReader reader) throws XMLStreamException {
100 
101 
102     XMLEventReaderBase base;
103     if (config.getEventAllocator() == null) {
104       base = new XMLEventReaderBase(reader);
105     } else {
106       base = new XMLEventReaderBase(reader,
107                                     (config.getEventAllocator()).newInstance());
108     }
109     return base;
110   }
111   
112   /***
113    * Create a new XMLEventReader from a JAXP source
114    * @param source the source to read from
115    */
116   public XMLEventReader createXMLEventReader(Source source) throws XMLStreamException {
117     return createXMLEventReader(createXMLStreamReader(source));
118   }
119 
120   /***
121    * Create a new XMLEventReader from a java.io.stream
122    * @param stream the InputStream to read from
123    */
124   public XMLEventReader createXMLEventReader(java.io.InputStream stream) throws XMLStreamException {
125     return createXMLEventReader(createXMLStreamReader(stream));
126   }
127 
128   /***
129    * Create a new XMLEventReader from a java.io.stream
130    * @param stream the InputStream to read from
131    * @param encoding the character encoding of the stream
132    */
133   public XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding) throws XMLStreamException {
134     return createXMLEventReader(createXMLStreamReader(stream,encoding));
135   }
136 
137 
138   /***
139    * The resolver that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.
140    */
141   public XMLResolver getXMLResolver() {
142     return config.getXMLResolver();
143   }
144 
145   /***
146    * The resolver that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.
147    * @param resolver the resolver to use to resolve references
148    */
149   public void  setXMLResolver(XMLResolver resolver) {
150     config.setXMLResolver(resolver);
151   }
152 
153   /***
154    * Create a filtered reader that wraps the filter around the reader
155    * @param reader the reader to filter
156    * @param filter the filter to apply to the reader
157    */
158   public XMLStreamReader createFilteredReader(XMLStreamReader reader, 
159                                               StreamFilter filter) 
160     throws XMLStreamException 
161   {
162     return new StreamReaderFilter(reader,filter);
163   }
164 
165   /***
166    * Create a filtered event reader that wraps the filter around the event reader
167    * @param reader the event reader to wrap
168    * @param filter the filter to apply to the event reader
169    */
170   public XMLEventReader createFilteredReader(XMLEventReader reader, 
171                                              EventFilter filter) 
172     throws XMLStreamException
173   {
174     return new EventReaderFilter(reader,filter);
175   }
176   
177   /***
178    * The reporter that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.
179    */
180   public XMLReporter getXMLReporter() {
181     return config.getXMLReporter();
182   }
183 
184   /***
185    * The reporter that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.
186    * @param reporter the resolver to use to report non fatal errors
187    */
188   public void setXMLReporter(XMLReporter reporter) {
189     config.setXMLReporter(reporter);
190   }
191 
192 
193   /***
194    * Set a user defined event allocator for events
195    * @param allocator the user defined allocator
196    */
197   public void setEventAllocator(XMLEventAllocator allocator) { 
198     config.setEventAllocator(allocator);
199   }
200 
201   /***
202    * Gets the allocator used by streams created with this factory
203    */
204   public XMLEventAllocator getEventAllocator() {
205     return config.getEventAllocator();
206   }
207 
208   /***
209    * Specifies that the stream produced by this code will append all adjacent text nodes. 
210    */  
211   public void setCoalescing(boolean coalescing){
212     config.setCoalescing(coalescing);
213   }
214 
215   /***
216    * Indicates whether or not the factory is configured to produced streams that coalesce adjacent text nodes.
217    */
218   public boolean isCoalescing(){
219     return config.isCoalescing();
220   }
221 
222   public void setProperty(String name, Object value) throws IllegalArgumentException {
223     // TODO - cwitt : check against supported feature list
224     config.setProperty(name,value);
225   }
226 
227   public Object getProperty(String name) throws IllegalArgumentException {
228     return config.getProperty(name);
229   }
230 
231   public XMLStreamReader createXMLStreamReader(Reader in) 
232     throws XMLStreamException
233   {
234     MXParser pp = new MXParser();
235     pp.setInput(in);
236     pp.setConfigurationContext(config);
237     return pp;
238   }
239 
240   public boolean isPropertySupported(String name) {
241     return config.isPropertySupported(name);
242   }
243 
244 }
245 
246