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
.io
.Writer
;
48 import java
.util
.Properties
;
49 import javax
.xml
.stream
.util
.XMLEventAllocator
;
50 import javax
.xml
.transform
.Source
;
53 * Factory for creating stream and event readers from various kinds of input
59 * <th>Description</th>
65 * <td>javax.xml.stream.isValidating</td>
66 * <td>Controls DTD validation</td>
68 * <td>Boolean.FALSE</td>
72 * <td>javax.xml.stream.isNamespaceAware</td>
73 * <td>Controls namespace processing for XML 1.0</td>
75 * <td>Boolean.TRUE</td>
76 * <td>true is required, false is optional</td>
79 * <td>javax.xml.stream.isCoalescing</td>
80 * <td>Controls coalescing (normalization of adjacent character data)</td>
82 * <td>Boolean.FALSE</td>
86 * <td>javax.xml.stream.isReplacingEntityReferences</td>
87 * <td>Controls replacement of entity references with their replacement
90 * <td>Boolean.TRUE</td>
94 * <td>javax.xml.stream.isSupportingExternalEntities</td>
95 * <td>Controls whether to resolve external entities</td>
97 * <td>not specified</td>
101 * <td>javax.xml.stream.supportDTD</td>
102 * <td>Controls whether to support DTDs</td>
104 * <td>Boolean.TRUE</td>
108 * <td>javax.xml.stream.reporter</td>
110 * <td>javax.xml.stream.XMLReporter</td>
115 * <td>javax.xml.stream.resolver</td>
117 * <td>javax.xml.stream.XMLResolver</td>
122 * <td>javax.xml.stream.allocator</td>
124 * <td>javax.xml.stream.util.XMLEventAllocator</td>
130 public abstract class XMLInputFactory
134 * Property used to control namespace support.
136 public static final String IS_NAMESPACE_AWARE
=
137 "javax.xml.stream.isNamespaceAware";
140 * Property used to control DTD validation.
142 public static final String IS_VALIDATING
= "javax.xml.stream.isValidating";
145 * Property used to control whether to coalesce adjacent text events.
147 public static final String IS_COALESCING
= "javax.xml.stream.isCoalescing";
150 * Property used to control whether to replace entity references with
151 * their replacement text.
153 public static final String IS_REPLACING_ENTITY_REFERENCES
=
154 "javax.xml.stream.isReplacingEntityReferences";
157 * Property used to control whether to resolve external entities.
159 public static final String IS_SUPPORTING_EXTERNAL_ENTITIES
=
160 "javax.xml.stream.isSupportingExternalEntities";
163 * Property used to indicate whether to support DTDs.
165 public static final String SUPPORT_DTD
= "javax.xml.stream.supportDTD";
168 * Property used to control the error reporter implementation.
170 public static final String REPORTER
= "javax.xml.stream.reporter";
173 * Property used to control the entity resolver implementation.
175 public static final String RESOLVER
= "javax.xml.stream.resolver";
178 * Property used to control the event allocator implementation.
180 public static final String ALLOCATOR
= "javax.xml.stream.allocator";
182 protected XMLInputFactory()
187 * Creates a new factory instance.
188 * @see #newInstance(String,ClassLoader)
190 public static XMLInputFactory
newInstance()
191 throws FactoryConfigurationError
193 return newInstance(null, null);
197 * Creates a new factory instance.
198 * The implementation class to load is the first found in the following
201 * <li>the <code>javax.xml.stream.XMLInputFactory</code> system
203 * <li>the above named property value in the
204 * <code><i>$JAVA_HOME</i>/lib/stax.properties</code> file</li>
205 * <li>the class name specified in the
206 * <code>META-INF/services/javax.xml.stream.XMLInputFactory</code>
207 * system resource</li>
208 * <li>the default factory class</li>
211 public static XMLInputFactory
newInstance(String factoryId
,
212 ClassLoader classLoader
)
213 throws FactoryConfigurationError
215 ClassLoader loader
= classLoader
;
218 loader
= Thread
.currentThread().getContextClassLoader();
222 loader
= XMLInputFactory
.class.getClassLoader();
224 String className
= null;
228 className
= getFactoryClassName(loader
, count
++);
229 if (className
!= null)
233 Class t
= (loader
!= null) ? loader
.loadClass(className
) :
234 Class
.forName(className
);
235 return (XMLInputFactory
) t
.newInstance();
237 catch (ClassNotFoundException e
)
243 throw new FactoryConfigurationError(e
,
244 "error instantiating class " + className
);
248 while (className
== null && count
< 3);
249 return new gnu
.xml
.stream
.XMLInputFactoryImpl();
252 private static String
getFactoryClassName(ClassLoader loader
, int attempt
)
254 final String propertyName
= "javax.xml.stream.XMLInputFactory";
258 return System
.getProperty(propertyName
);
262 File file
= new File(System
.getProperty("java.home"));
263 file
= new File(file
, "lib");
264 file
= new File(file
, "stax.properties");
265 InputStream in
= new FileInputStream(file
);
266 Properties props
= new Properties();
269 return props
.getProperty(propertyName
);
271 catch (IOException e
)
278 String serviceKey
= "/META-INF/services/" + propertyName
;
279 InputStream in
= (loader
!= null) ?
280 loader
.getResourceAsStream(serviceKey
) :
281 XMLInputFactory
.class.getResourceAsStream(serviceKey
);
285 new BufferedReader(new InputStreamReader(in
));
286 String ret
= r
.readLine();
291 catch (IOException e
)
301 * Creates a new stream reader.
303 public abstract XMLStreamReader
createXMLStreamReader(Reader reader
)
304 throws XMLStreamException
;
307 * Creates a new stream reader.
309 public abstract XMLStreamReader
createXMLStreamReader(Source source
)
310 throws XMLStreamException
;
313 * Creates a new stream reader.
315 public abstract XMLStreamReader
createXMLStreamReader(InputStream stream
)
316 throws XMLStreamException
;
319 * Creates a new stream reader.
321 public abstract XMLStreamReader
createXMLStreamReader(InputStream stream
,
323 throws XMLStreamException
;
326 * Creates a new stream reader.
328 public abstract XMLStreamReader
createXMLStreamReader(String systemId
,
330 throws XMLStreamException
;
333 * Creates a new stream reader.
335 public abstract XMLStreamReader
createXMLStreamReader(String systemId
,
337 throws XMLStreamException
;
340 * Creates a new event reader.
342 public abstract XMLEventReader
createXMLEventReader(Reader reader
)
343 throws XMLStreamException
;
346 * Creates a new event reader.
348 public abstract XMLEventReader
createXMLEventReader(String systemId
,
350 throws XMLStreamException
;
353 * Creates a new event reader.
355 public abstract XMLEventReader
createXMLEventReader(XMLStreamReader reader
)
356 throws XMLStreamException
;
359 * Creates a new event reader.
361 public abstract XMLEventReader
createXMLEventReader(Source source
)
362 throws XMLStreamException
;
365 * Creates a new event reader.
367 public abstract XMLEventReader
createXMLEventReader(InputStream stream
)
368 throws XMLStreamException
;
371 * Creates a new event reader.
373 public abstract XMLEventReader
createXMLEventReader(InputStream stream
,
375 throws XMLStreamException
;
378 * Creates a new event reader.
380 public abstract XMLEventReader
createXMLEventReader(String systemId
,
382 throws XMLStreamException
;
385 * Create a new filtered reader.
387 public abstract XMLStreamReader
createFilteredReader(XMLStreamReader reader
,
389 throws XMLStreamException
;
392 * Create a new filtered reader.
394 public abstract XMLEventReader
createFilteredReader(XMLEventReader reader
,
396 throws XMLStreamException
;
399 * Returns the entity resolver.
401 public abstract XMLResolver
getXMLResolver();
404 * Sets the entity resolver.
406 public abstract void setXMLResolver(XMLResolver resolver
);
409 * Returns the error reporter.
411 public abstract XMLReporter
getXMLReporter();
414 * Sets the error reporter.
416 public abstract void setXMLReporter(XMLReporter reporter
);
419 * Sets the implementation-specific property of the given name.
420 * @exception IllegalArgumentException if the property is not supported
422 public abstract void setProperty(String name
, Object value
)
423 throws IllegalArgumentException
;
426 * Returns the implementation-specific property of the given name.
427 * @exception IllegalArgumentException if the property is not supported
429 public abstract Object
getProperty(String name
)
430 throws IllegalArgumentException
;
433 * Indicates whether the specified property is supported.
435 public abstract boolean isPropertySupported(String name
);
438 * Sets the event allocator.
440 public abstract void setEventAllocator(XMLEventAllocator allocator
);
443 * Returns the event allocator.
445 public abstract XMLEventAllocator
getEventAllocator();