1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25 /*
26 * Copyright (c) 2003 by BEA Systems, Inc. All Rights Reserved.
27 */
28
29 package javax.xml.stream;
30
31 import javax.xml.transform.Source;
32 import javax.xml.stream.util.XMLEventAllocator;
33
34 /**
35 * Defines an abstract implementation of a factory for getting streams.
36 *
37 * The following table defines the standard properties of this specification.
38 * Each property varies in the level of support required by each implementation.
39 * The level of support required is described in the 'Required' column.
40 *
41 * <table border="2" rules="all" cellpadding="4">
42 * <thead>
43 * <tr>
44 * <th align="center" colspan="5">
45 * Configuration parameters
46 * </th>
47 * </tr>
48 * </thead>
49 * <tbody>
50 * <tr>
51 * <th>Property Name</th>
52 * <th>Behavior</th>
53 * <th>Return type</th>
54 * <th>Default Value</th>
55 * <th>Required</th>
56 * </tr>
57 * <tr><td>javax.xml.stream.isValidating</td><td>Turns on/off implementation specific DTD validation</td><td>Boolean</td><td>False</td><td>No</td></tr>
58 * <tr><td>javax.xml.stream.isNamespaceAware</td><td>Turns on/off namespace processing for XML 1.0 support</td><td>Boolean</td><td>True</td><td>True (required) / False (optional)</td></tr>
59 * <tr><td>javax.xml.stream.isCoalescing</td><td>Requires the processor to coalesce adjacent character data</td><td>Boolean</td><td>False</td><td>Yes</td></tr>
60 * <tr><td>javax.xml.stream.isReplacingEntityReferences</td><td>replace internal entity references with their replacement text and report them as characters</td><td>Boolean</td><td>True</td><td>Yes</td></tr>
61 *<tr><td>javax.xml.stream.isSupportingExternalEntities</td><td>Resolve external parsed entities</td><td>Boolean</td><td>Unspecified</td><td>Yes</td></tr>
62 *<tr><td>javax.xml.stream.supportDTD</td><td>Use this property to request processors that do not support DTDs</td><td>Boolean</td><td>True</td><td>Yes</td></tr>
63 *<tr><td>javax.xml.stream.reporter</td><td>sets/gets the impl of the XMLReporter </td><td>javax.xml.stream.XMLReporter</td><td>Null</td><td>Yes</td></tr>
64 *<tr><td>javax.xml.stream.resolver</td><td>sets/gets the impl of the XMLResolver interface</td><td>javax.xml.stream.XMLResolver</td><td>Null</td><td>Yes</td></tr>
65 *<tr><td>javax.xml.stream.allocator</td><td>sets/gets the impl of the XMLEventAllocator interface</td><td>javax.xml.stream.util.XMLEventAllocator</td><td>Null</td><td>Yes</td></tr>
66 * </tbody>
67 * </table>
68 *
69 *
70 * @author Copyright (c) 2003 by BEA Systems. All Rights Reserved.
71 * @see XMLOutputFactory
72 * @see XMLEventReader
73 * @see XMLStreamReader
74 * @see EventFilter
75 * @see XMLReporter
76 * @see XMLResolver
77 * @see javax.xml.stream.util.XMLEventAllocator
78 * @since 1.6
79 */
80
81 public abstract class XMLInputFactory {
82 /**
83 * The property used to turn on/off namespace support,
84 * this is to support XML 1.0 documents,
85 * only the true setting must be supported
86 */
87 public static final String IS_NAMESPACE_AWARE=
88 "javax.xml.stream.isNamespaceAware";
89
90 /**
91 * The property used to turn on/off implementation specific validation
92 */
93 public static final String IS_VALIDATING=
94 "javax.xml.stream.isValidating";
95
96 /**
97 * The property that requires the parser to coalesce adjacent character data sections
98 */
99 public static final String IS_COALESCING=
100 "javax.xml.stream.isCoalescing";
101
102 /**
103 * Requires the parser to replace internal
104 * entity references with their replacement
105 * text and report them as characters
106 */
107 public static final String IS_REPLACING_ENTITY_REFERENCES=
108 "javax.xml.stream.isReplacingEntityReferences";
109
110 /**
111 * The property that requires the parser to resolve external parsed entities
112 */
113 public static final String IS_SUPPORTING_EXTERNAL_ENTITIES=
114 "javax.xml.stream.isSupportingExternalEntities";
115
116 /**
117 * The property that requires the parser to support DTDs
118 */
119 public static final String SUPPORT_DTD=
120 "javax.xml.stream.supportDTD";
121
122 /**
123 * The property used to
124 * set/get the implementation of the XMLReporter interface
125 */
126 public static final String REPORTER=
127 "javax.xml.stream.reporter";
128
129 /**
130 * The property used to set/get the implementation of the XMLResolver
131 */
132 public static final String RESOLVER=
133 "javax.xml.stream.resolver";
134
135 /**
136 * The property used to set/get the implementation of the allocator
137 */
138 public static final String ALLOCATOR=
139 "javax.xml.stream.allocator";
140
141 protected XMLInputFactory(){}
142
143 /**
144 * Create a new instance of the factory.
145 * This static method creates a new factory instance.
146 * This method uses the following ordered lookup procedure to determine
147 * the XMLInputFactory implementation class to load:
148 * Use the javax.xml.stream.XMLInputFactory system property.
149 * Use the properties file "lib/stax.properties" in the JRE directory.
150 * This configuration file is in standard java.util.Properties format and contains
151 * the fully qualified name of the implementation class with the key being the system property defined above.
152 * Use the Services API (as detailed in the JAR specification), if available, to determine the classname.
153 * The Services API will look for a classname in the file META-INF/services/javax.xml.stream.XMLInputFactory
154 * in jars available to the runtime.
155 * Platform default XMLInputFactory instance.
156 * Once an application has obtained a reference to a XMLInputFactory
157 * it can use the factory to configure and obtain stream instances.
158 *
159 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded
160 */
161 public static XMLInputFactory newInstance()
162 throws FactoryConfigurationError
163 {
164 return (XMLInputFactory) FactoryFinder.find(
165 "javax.xml.stream.XMLInputFactory",
166 "com.sun.xml.internal.stream.XMLInputFactoryImpl");
167 }
168
169 /**
170 * Create a new instance of the factory
171 *
172 * @param factoryId Name of the factory to find, same as
173 * a property name
174 * @param classLoader classLoader to use
175 * @return the factory implementation
176 * @throws FactoryConfigurationError if an instance of this factory cannot be loaded
177 */
178 public static XMLInputFactory newInstance(String factoryId,
179 ClassLoader classLoader)
180 throws FactoryConfigurationError {
181 try {
182 //do not fallback if given classloader can't find the class, throw exception
183 return (XMLInputFactory) FactoryFinder.newInstance(factoryId, classLoader, false);
184 } catch (FactoryFinder.ConfigurationError e) {
185 throw new FactoryConfigurationError(e.getException(),
186 e.getMessage());
187 }
188 }
189
190 /**
191 * Create a new XMLStreamReader from a reader
192 * @param reader the XML data to read from
193 * @throws XMLStreamException
194 */
195 public abstract XMLStreamReader createXMLStreamReader(java.io.Reader reader)
196 throws XMLStreamException;
197
198 /**
199 * Create a new XMLStreamReader from a JAXP source. This method is optional.
200 * @param source the source to read from
201 * @throws UnsupportedOperationException if this method is not
202 * supported by this XMLInputFactory
203 * @throws XMLStreamException
204 */
205 public abstract XMLStreamReader createXMLStreamReader(Source source)
206 throws XMLStreamException;
207
208 /**
209 * Create a new XMLStreamReader from a java.io.InputStream
210 * @param stream the InputStream to read from
211 * @throws XMLStreamException
212 */
213 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream)
214 throws XMLStreamException;
215
216 /**
217 * Create a new XMLStreamReader from a java.io.InputStream
218 * @param stream the InputStream to read from
219 * @param encoding the character encoding of the stream
220 * @throws XMLStreamException
221 */
222 public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream, String encoding)
223 throws XMLStreamException;
224
225 /**
226 * Create a new XMLStreamReader from a java.io.InputStream
227 * @param systemId the system ID of the stream
228 * @param stream the InputStream to read from
229 */
230 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.InputStream stream)
231 throws XMLStreamException;
232
233 /**
234 * Create a new XMLStreamReader from a java.io.InputStream
235 * @param systemId the system ID of the stream
236 * @param reader the InputStream to read from
237 */
238 public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader)
239 throws XMLStreamException;
240
241 /**
242 * Create a new XMLEventReader from a reader
243 * @param reader the XML data to read from
244 * @throws XMLStreamException
245 */
246 public abstract XMLEventReader createXMLEventReader(java.io.Reader reader)
247 throws XMLStreamException;
248
249 /**
250 * Create a new XMLEventReader from a reader
251 * @param systemId the system ID of the input
252 * @param reader the XML data to read from
253 * @throws XMLStreamException
254 */
255 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.Reader reader)
256 throws XMLStreamException;
257
258 /**
259 * Create a new XMLEventReader from an XMLStreamReader. After being used
260 * to construct the XMLEventReader instance returned from this method
261 * the XMLStreamReader must not be used.
262 * @param reader the XMLStreamReader to read from (may not be modified)
263 * @return a new XMLEventReader
264 * @throws XMLStreamException
265 */
266 public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader)
267 throws XMLStreamException;
268
269 /**
270 * Create a new XMLEventReader from a JAXP source.
271 * Support of this method is optional.
272 * @param source the source to read from
273 * @throws UnsupportedOperationException if this method is not
274 * supported by this XMLInputFactory
275 */
276 public abstract XMLEventReader createXMLEventReader(Source source)
277 throws XMLStreamException;
278
279 /**
280 * Create a new XMLEventReader from a java.io.InputStream
281 * @param stream the InputStream to read from
282 * @throws XMLStreamException
283 */
284 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream)
285 throws XMLStreamException;
286
287 /**
288 * Create a new XMLEventReader from a java.io.InputStream
289 * @param stream the InputStream to read from
290 * @param encoding the character encoding of the stream
291 * @throws XMLStreamException
292 */
293 public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding)
294 throws XMLStreamException;
295
296 /**
297 * Create a new XMLEventReader from a java.io.InputStream
298 * @param systemId the system ID of the stream
299 * @param stream the InputStream to read from
300 * @throws XMLStreamException
301 */
302 public abstract XMLEventReader createXMLEventReader(String systemId, java.io.InputStream stream)
303 throws XMLStreamException;
304
305 /**
306 * Create a filtered reader that wraps the filter around the reader
307 * @param reader the reader to filter
308 * @param filter the filter to apply to the reader
309 * @throws XMLStreamException
310 */
311 public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter)
312 throws XMLStreamException;
313
314 /**
315 * Create a filtered event reader that wraps the filter around the event reader
316 * @param reader the event reader to wrap
317 * @param filter the filter to apply to the event reader
318 * @throws XMLStreamException
319 */
320 public abstract XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter)
321 throws XMLStreamException;
322
323 /**
324 * The resolver that will be set on any XMLStreamReader or XMLEventReader created
325 * by this factory instance.
326 */
327 public abstract XMLResolver getXMLResolver();
328
329 /**
330 * The resolver that will be set on any XMLStreamReader or XMLEventReader created
331 * by this factory instance.
332 * @param resolver the resolver to use to resolve references
333 */
334 public abstract void setXMLResolver(XMLResolver resolver);
335
336 /**
337 * The reporter that will be set on any XMLStreamReader or XMLEventReader created
338 * by this factory instance.
339 */
340 public abstract XMLReporter getXMLReporter();
341
342 /**
343 * The reporter that will be set on any XMLStreamReader or XMLEventReader created
344 * by this factory instance.
345 * @param reporter the resolver to use to report non fatal errors
346 */
347 public abstract void setXMLReporter(XMLReporter reporter);
348
349 /**
350 * Allows the user to set specific feature/property on the underlying implementation. The underlying implementation
351 * is not required to support every setting of every property in the specification and may use IllegalArgumentException
352 * to signal that an unsupported property may not be set with the specified value.
353 * @param name The name of the property (may not be null)
354 * @param value The value of the property
355 * @throws java.lang.IllegalArgumentException if the property is not supported
356 */
357 public abstract void setProperty(java.lang.String name, Object value)
358 throws java.lang.IllegalArgumentException;
359
360 /**
361 * Get the value of a feature/property from the underlying implementation
362 * @param name The name of the property (may not be null)
363 * @return The value of the property
364 * @throws IllegalArgumentException if the property is not supported
365 */
366 public abstract Object getProperty(java.lang.String name)
367 throws java.lang.IllegalArgumentException;
368
369
370 /**
371 * Query the set of properties that this factory supports.
372 *
373 * @param name The name of the property (may not be null)
374 * @return true if the property is supported and false otherwise
375 */
376 public abstract boolean isPropertySupported(String name);
377
378 /**
379 * Set a user defined event allocator for events
380 * @param allocator the user defined allocator
381 */
382 public abstract void setEventAllocator(XMLEventAllocator allocator);
383
384 /**
385 * Gets the allocator used by streams created with this factory
386 */
387 public abstract XMLEventAllocator getEventAllocator();
388
389 }