1 /* XMLInputFactory.java --
2 Copyright (C) 2005,2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.xml.stream;
40 import java.io.BufferedReader;
42 import java.io.FileInputStream;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.io.IOException;
46 import java.io.Reader;
47 import java.util.Properties;
48 import javax.xml.stream.util.XMLEventAllocator;
49 import javax.xml.transform.Source;
52 * Factory for creating stream and event readers from various kinds of input
58 * <th>Description</th>
64 * <td>javax.xml.stream.isValidating</td>
65 * <td>Controls DTD validation</td>
67 * <td>Boolean.FALSE</td>
71 * <td>javax.xml.stream.isNamespaceAware</td>
72 * <td>Controls namespace processing for XML 1.0</td>
74 * <td>Boolean.TRUE</td>
75 * <td>true is required, false is optional</td>
78 * <td>javax.xml.stream.isCoalescing</td>
79 * <td>Controls coalescing (normalization of adjacent character data)</td>
81 * <td>Boolean.FALSE</td>
85 * <td>javax.xml.stream.isReplacingEntityReferences</td>
86 * <td>Controls replacement of entity references with their replacement
89 * <td>Boolean.TRUE</td>
93 * <td>javax.xml.stream.isSupportingExternalEntities</td>
94 * <td>Controls whether to resolve external entities</td>
96 * <td>not specified</td>
100 * <td>javax.xml.stream.supportDTD</td>
101 * <td>Controls whether to support DTDs</td>
103 * <td>Boolean.TRUE</td>
107 * <td>javax.xml.stream.reporter</td>
109 * <td>javax.xml.stream.XMLReporter</td>
114 * <td>javax.xml.stream.resolver</td>
116 * <td>javax.xml.stream.XMLResolver</td>
121 * <td>javax.xml.stream.allocator</td>
123 * <td>javax.xml.stream.util.XMLEventAllocator</td>
129 public abstract class XMLInputFactory
133 * Property used to control namespace support.
135 public static final String IS_NAMESPACE_AWARE =
136 "javax.xml.stream.isNamespaceAware";
139 * Property used to control DTD validation.
141 public static final String IS_VALIDATING = "javax.xml.stream.isValidating";
144 * Property used to control whether to coalesce adjacent text events.
146 public static final String IS_COALESCING = "javax.xml.stream.isCoalescing";
149 * Property used to control whether to replace entity references with
150 * their replacement text.
152 public static final String IS_REPLACING_ENTITY_REFERENCES =
153 "javax.xml.stream.isReplacingEntityReferences";
156 * Property used to control whether to resolve external entities.
158 public static final String IS_SUPPORTING_EXTERNAL_ENTITIES =
159 "javax.xml.stream.isSupportingExternalEntities";
162 * Property used to indicate whether to support DTDs.
164 public static final String SUPPORT_DTD = "javax.xml.stream.supportDTD";
167 * Property used to control the error reporter implementation.
169 public static final String REPORTER = "javax.xml.stream.reporter";
172 * Property used to control the entity resolver implementation.
174 public static final String RESOLVER = "javax.xml.stream.resolver";
177 * Property used to control the event allocator implementation.
179 public static final String ALLOCATOR = "javax.xml.stream.allocator";
181 protected XMLInputFactory()
186 * Creates a new factory instance.
187 * @see #newInstance(String,ClassLoader)
189 public static XMLInputFactory newInstance()
190 throws FactoryConfigurationError
192 return newInstance(null, null);
196 * Creates a new factory instance.
197 * The implementation class to load is the first found in the following
200 * <li>the <code>javax.xml.stream.XMLInputFactory</code> system
202 * <li>the above named property value in the
203 * <code><i>$JAVA_HOME</i>/lib/stax.properties</code> file</li>
204 * <li>the class name specified in the
205 * <code>META-INF/services/javax.xml.stream.XMLInputFactory</code>
206 * system resource</li>
207 * <li>the default factory class</li>
210 public static XMLInputFactory newInstance(String factoryId,
211 ClassLoader classLoader)
212 throws FactoryConfigurationError
214 ClassLoader loader = classLoader;
217 loader = Thread.currentThread().getContextClassLoader();
221 loader = XMLInputFactory.class.getClassLoader();
223 String className = null;
227 className = getFactoryClassName(loader, count++);
228 if (className != null)
232 Class<?> t = (loader != null) ? loader.loadClass(className) :
233 Class.forName(className);
234 return (XMLInputFactory) t.newInstance();
236 catch (ClassNotFoundException e)
242 throw new FactoryConfigurationError(e,
243 "error instantiating class " + className);
247 while (className == null && count < 3);
248 return new gnu.xml.stream.XMLInputFactoryImpl();
251 private static String getFactoryClassName(ClassLoader loader, int attempt)
253 final String propertyName = "javax.xml.stream.XMLInputFactory";
257 return System.getProperty(propertyName);
261 File file = new File(System.getProperty("java.home"));
262 file = new File(file, "lib");
263 file = new File(file, "stax.properties");
264 InputStream in = new FileInputStream(file);
265 Properties props = new Properties();
268 return props.getProperty(propertyName);
270 catch (IOException e)
277 String serviceKey = "/META-INF/services/" + propertyName;
278 InputStream in = (loader != null) ?
279 loader.getResourceAsStream(serviceKey) :
280 XMLInputFactory.class.getResourceAsStream(serviceKey);
284 new BufferedReader(new InputStreamReader(in));
285 String ret = r.readLine();
290 catch (IOException e)
300 * Creates a new stream reader.
302 public abstract XMLStreamReader createXMLStreamReader(Reader reader)
303 throws XMLStreamException;
306 * Creates a new stream reader.
308 public abstract XMLStreamReader createXMLStreamReader(Source source)
309 throws XMLStreamException;
312 * Creates a new stream reader.
314 public abstract XMLStreamReader createXMLStreamReader(InputStream stream)
315 throws XMLStreamException;
318 * Creates a new stream reader.
320 public abstract XMLStreamReader createXMLStreamReader(InputStream stream,
322 throws XMLStreamException;
325 * Creates a new stream reader.
327 public abstract XMLStreamReader createXMLStreamReader(String systemId,
329 throws XMLStreamException;
332 * Creates a new stream reader.
334 public abstract XMLStreamReader createXMLStreamReader(String systemId,
336 throws XMLStreamException;
339 * Creates a new event reader.
341 public abstract XMLEventReader createXMLEventReader(Reader reader)
342 throws XMLStreamException;
345 * Creates a new event reader.
347 public abstract XMLEventReader createXMLEventReader(String systemId,
349 throws XMLStreamException;
352 * Creates a new event reader.
354 public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader)
355 throws XMLStreamException;
358 * Creates a new event reader.
360 public abstract XMLEventReader createXMLEventReader(Source source)
361 throws XMLStreamException;
364 * Creates a new event reader.
366 public abstract XMLEventReader createXMLEventReader(InputStream stream)
367 throws XMLStreamException;
370 * Creates a new event reader.
372 public abstract XMLEventReader createXMLEventReader(InputStream stream,
374 throws XMLStreamException;
377 * Creates a new event reader.
379 public abstract XMLEventReader createXMLEventReader(String systemId,
381 throws XMLStreamException;
384 * Create a new filtered reader.
386 public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader,
388 throws XMLStreamException;
391 * Create a new filtered reader.
393 public abstract XMLEventReader createFilteredReader(XMLEventReader reader,
395 throws XMLStreamException;
398 * Returns the entity resolver.
400 public abstract XMLResolver getXMLResolver();
403 * Sets the entity resolver.
405 public abstract void setXMLResolver(XMLResolver resolver);
408 * Returns the error reporter.
410 public abstract XMLReporter getXMLReporter();
413 * Sets the error reporter.
415 public abstract void setXMLReporter(XMLReporter reporter);
418 * Sets the implementation-specific property of the given name.
419 * @exception IllegalArgumentException if the property is not supported
421 public abstract void setProperty(String name, Object value)
422 throws IllegalArgumentException;
425 * Returns the implementation-specific property of the given name.
426 * @exception IllegalArgumentException if the property is not supported
428 public abstract Object getProperty(String name)
429 throws IllegalArgumentException;
432 * Indicates whether the specified property is supported.
434 public abstract boolean isPropertySupported(String name);
437 * Sets the event allocator.
439 public abstract void setEventAllocator(XMLEventAllocator allocator);
442 * Returns the event allocator.
444 public abstract XMLEventAllocator getEventAllocator();