Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLInputFactory.java
40948 views
/*1* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.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 it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.stream;2627import com.sun.xml.internal.stream.XMLInputFactoryImpl;28import javax.xml.stream.util.XMLEventAllocator;29import javax.xml.transform.Source;3031/**32* Defines an abstract implementation of a factory for getting streams.33*34* The following table defines the standard properties of this specification.35* Each property varies in the level of support required by each implementation.36* The level of support required is described in the 'Required' column.37*38* <table class="striped">39* <caption>Configuration Parameters</caption>40* <thead>41* <tr>42* <th scope="col">Property Name</th>43* <th scope="col">Behavior</th>44* <th scope="col">Return type</th>45* <th scope="col">Default Value</th>46* <th scope="col">Required</th>47* </tr>48* </thead>49* <tbody>50* <tr><th scope="row">javax.xml.stream.isValidating</th><td>Turns on/off implementation specific DTD validation</td><td>Boolean</td><td>False</td><td>No</td></tr>51* <tr><th scope="row">javax.xml.stream.isNamespaceAware</th><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>52* <tr><th scope="row">javax.xml.stream.isCoalescing</th><td>Requires the processor to coalesce adjacent character data</td><td>Boolean</td><td>False</td><td>Yes</td></tr>53* <tr><th scope="row">javax.xml.stream.isReplacingEntityReferences</th><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>54*<tr><th scope="row">javax.xml.stream.isSupportingExternalEntities</th><td>Resolve external parsed entities</td><td>Boolean</td><td>Unspecified</td><td>Yes</td></tr>55*<tr><th scope="row">javax.xml.stream.supportDTD</th><td>Use this property to request processors that do not support DTDs</td><td>Boolean</td><td>True</td><td>Yes</td></tr>56*<tr><th scope="row">javax.xml.stream.reporter</th><td>sets/gets the impl of the XMLReporter </td><td>javax.xml.stream.XMLReporter</td><td>Null</td><td>Yes</td></tr>57*<tr><th scope="row">javax.xml.stream.resolver</th><td>sets/gets the impl of the XMLResolver interface</td><td>javax.xml.stream.XMLResolver</td><td>Null</td><td>Yes</td></tr>58*<tr><th scope="row">javax.xml.stream.allocator</th><td>sets/gets the impl of the XMLEventAllocator interface</td><td>javax.xml.stream.util.XMLEventAllocator</td><td>Null</td><td>Yes</td></tr>59* </tbody>60* </table>61*62*63* @version 1.264* @author Copyright (c) 2009, 2015 by Oracle Corporation. All Rights Reserved.65* @see XMLOutputFactory66* @see XMLEventReader67* @see XMLStreamReader68* @see EventFilter69* @see XMLReporter70* @see XMLResolver71* @see javax.xml.stream.util.XMLEventAllocator72* @since 1.673*/7475public abstract class XMLInputFactory {76/**77* The property used to turn on/off namespace support,78* this is to support XML 1.0 documents,79* only the true setting must be supported80*/81public static final String IS_NAMESPACE_AWARE=82"javax.xml.stream.isNamespaceAware";8384/**85* The property used to turn on/off implementation specific validation86*/87public static final String IS_VALIDATING=88"javax.xml.stream.isValidating";8990/**91* The property that requires the parser to coalesce adjacent character data sections92*/93public static final String IS_COALESCING=94"javax.xml.stream.isCoalescing";9596/**97* Requires the parser to replace internal98* entity references with their replacement99* text and report them as characters100*/101public static final String IS_REPLACING_ENTITY_REFERENCES=102"javax.xml.stream.isReplacingEntityReferences";103104/**105* The property that requires the parser to resolve external parsed entities106*/107public static final String IS_SUPPORTING_EXTERNAL_ENTITIES=108"javax.xml.stream.isSupportingExternalEntities";109110/**111* The property that requires the parser to support DTDs112*/113public static final String SUPPORT_DTD=114"javax.xml.stream.supportDTD";115116/**117* The property used to118* set/get the implementation of the XMLReporter interface119*/120public static final String REPORTER=121"javax.xml.stream.reporter";122123/**124* The property used to set/get the implementation of the XMLResolver125*/126public static final String RESOLVER=127"javax.xml.stream.resolver";128129/**130* The property used to set/get the implementation of the allocator131*/132public static final String ALLOCATOR=133"javax.xml.stream.allocator";134135static final String DEFAULIMPL = "com.sun.xml.internal.stream.XMLInputFactoryImpl";136137/**138* Protected constructor to prevent instantiation.139* Use {@link #newFactory()} instead.140*/141protected XMLInputFactory(){}142143/**144* Creates a new instance of the {@code XMLInputFactory} builtin145* system-default implementation.146*147* @return A new instance of the {@code XMLInputFactory} builtin148* system-default implementation.149*150* @since 9151*/152public static XMLInputFactory newDefaultFactory() {153return new XMLInputFactoryImpl();154}155156/**157* Creates a new instance of the factory in exactly the same manner as the158* {@link #newFactory()} method.159* @return an instance of the {@code XMLInputFactory}160* @throws FactoryConfigurationError if an instance of this factory cannot be loaded161*/162public static XMLInputFactory newInstance()163throws FactoryConfigurationError164{165return FactoryFinder.find(XMLInputFactory.class, DEFAULIMPL);166}167168/**169* Creates a new instance of the factory. This method uses the170* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>171* to determine the {@code XMLInputFactory} implementation class to load.172* <p>173* Once an application has obtained a reference to a {@code XMLInputFactory}, it174* can use the factory to configure and obtain stream instances.175*176* @return an instance of the {@code XMLInputFactory}177* @throws FactoryConfigurationError in case of {@linkplain178* java.util.ServiceConfigurationError service configuration error} or if179* the implementation is not available or cannot be instantiated.180*/181public static XMLInputFactory newFactory()182throws FactoryConfigurationError183{184return FactoryFinder.find(XMLInputFactory.class, DEFAULIMPL);185}186187/**188* Create a new instance of the factory.189*190* @param factoryId Name of the factory to find, same as191* a property name192* @param classLoader classLoader to use193* @return the factory implementation194* @throws FactoryConfigurationError if an instance of this factory cannot be loaded195*196* @deprecated This method has been deprecated to maintain API consistency.197* All newInstance methods have been replaced with corresponding198* newFactory methods. The replacement {@link199* #newFactory(java.lang.String, java.lang.ClassLoader)} method200* defines no changes in behavior.201*/202@Deprecated(since="1.7")203public static XMLInputFactory newInstance(String factoryId,204ClassLoader classLoader)205throws FactoryConfigurationError {206//do not fallback if given classloader can't find the class, throw exception207return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null);208}209210/**211* Create a new instance of the factory.212* If the classLoader argument is null, then the ContextClassLoader is used.213* <p>214* This method uses the following ordered lookup procedure to determine215* the XMLInputFactory implementation class to load:216* <ul>217* <li>218* <p>219* Use the value of the system property identified by {@code factoryId}.220* </li>221* <li>222* <p>223* Use the configuration file "stax.properties". The file is in standard224* {@link java.util.Properties} format and typically located in the225* {@code conf} directory of the Java installation. It contains the fully qualified226* name of the implementation class with the key being the system property227* defined above.228*229* <p>230* The stax.properties file is read only once by the implementation231* and its values are then cached for future use. If the file does not exist232* when the first attempt is made to read from it, no further attempts are233* made to check for its existence. It is not possible to change the value234* of any property in stax.properties after it has been read for the first time.235*236* <p>237* Use the jaxp configuration file "jaxp.properties". The file is in the same238* format as stax.properties and will only be read if stax.properties does239* not exist.240* </li>241* <li>242* <p>243* If {@code factoryId} is "javax.xml.stream.XMLInputFactory",244* use the service-provider loading facility, defined by the245* {@link java.util.ServiceLoader} class, to attempt to {@linkplain246* java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load}247* an implementation of the service using the specified {@code ClassLoader}.248* If {@code classLoader} is null, the {@linkplain249* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:250* That is, the service-provider loading facility will use the {@linkplain251* java.lang.Thread#getContextClassLoader() current thread's context class loader}252* to attempt to load the service. If the context class253* loader is null, the {@linkplain254* ClassLoader#getSystemClassLoader() system class loader} will be used.255* </li>256* <li>257* <p>258* Otherwise, throws a {@link FactoryConfigurationError}.259* </li>260* </ul>261*262* <p>263* Note that this is a new method that replaces the deprecated264* {@link #newInstance(java.lang.String, java.lang.ClassLoader)265* newInstance(String factoryId, ClassLoader classLoader)} method.266* No changes in behavior are defined by this replacement method relative267* to the deprecated method.268*269*270* @apiNote The parameter factoryId defined here is inconsistent with that271* of other JAXP factories where the first parameter is fully qualified272* factory class name that provides implementation of the factory.273*274* @param factoryId Name of the factory to find, same as275* a property name276* @param classLoader classLoader to use277* @return the factory implementation278* @throws FactoryConfigurationError in case of {@linkplain279* java.util.ServiceConfigurationError service configuration error} or if280* the implementation is not available or cannot be instantiated.281* @throws FactoryConfigurationError if an instance of this factory cannot be loaded282*/283public static XMLInputFactory newFactory(String factoryId,284ClassLoader classLoader)285throws FactoryConfigurationError {286//do not fallback if given classloader can't find the class, throw exception287return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null);288}289290/**291* Create a new XMLStreamReader from a reader.292* @param reader the XML data to read from293* @return an instance of the {@code XMLStreamReader}294* @throws XMLStreamException if an error occurs295*/296public abstract XMLStreamReader createXMLStreamReader(java.io.Reader reader)297throws XMLStreamException;298299/**300* Create a new XMLStreamReader from a JAXP source. This method is optional.301* @param source the source to read from302* @return an instance of the {@code XMLStreamReader}303* @throws UnsupportedOperationException if this method is not304* supported by this XMLInputFactory305* @throws XMLStreamException if an error occurs306*/307public abstract XMLStreamReader createXMLStreamReader(Source source)308throws XMLStreamException;309310/**311* Create a new XMLStreamReader from a java.io.InputStream.312* @param stream the InputStream to read from313* @return an instance of the {@code XMLStreamReader}314* @throws XMLStreamException if an error occurs315*/316public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream)317throws XMLStreamException;318319/**320* Create a new XMLStreamReader from a java.io.InputStream.321* @param stream the InputStream to read from322* @param encoding the character encoding of the stream323* @return an instance of the {@code XMLStreamReader}324* @throws XMLStreamException if an error occurs325*/326public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream, String encoding)327throws XMLStreamException;328329/**330* Create a new XMLStreamReader from a java.io.InputStream.331* @param systemId the system ID of the stream332* @param stream the InputStream to read from333* @return an instance of the {@code XMLStreamReader}334* @throws XMLStreamException if an error occurs335*/336public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.InputStream stream)337throws XMLStreamException;338339/**340* Create a new XMLStreamReader from a java.io.InputStream.341* @param systemId the system ID of the stream342* @param reader the InputStream to read from343* @return an instance of the {@code XMLStreamReader}344* @throws XMLStreamException if an error occurs345*/346public abstract XMLStreamReader createXMLStreamReader(String systemId, java.io.Reader reader)347throws XMLStreamException;348349/**350* Create a new XMLEventReader from a reader.351* @param reader the XML data to read from352* @return an instance of the {@code XMLEventReader}353* @throws XMLStreamException if an error occurs354*/355public abstract XMLEventReader createXMLEventReader(java.io.Reader reader)356throws XMLStreamException;357358/**359* Create a new XMLEventReader from a reader.360* @param systemId the system ID of the input361* @param reader the XML data to read from362* @return an instance of the {@code XMLEventReader}363* @throws XMLStreamException if an error occurs364*/365public abstract XMLEventReader createXMLEventReader(String systemId, java.io.Reader reader)366throws XMLStreamException;367368/**369* Create a new XMLEventReader from an XMLStreamReader. After being used370* to construct the XMLEventReader instance returned from this method371* the XMLStreamReader must not be used.372* @param reader the XMLStreamReader to read from (may not be modified)373* @return a new XMLEventReader374* @throws XMLStreamException if an error occurs375*/376public abstract XMLEventReader createXMLEventReader(XMLStreamReader reader)377throws XMLStreamException;378379/**380* Create a new XMLEventReader from a JAXP source.381* Support of this method is optional.382* @param source the source to read from383* @return an instance of the {@code XMLEventReader}384* @throws XMLStreamException if an error occurs385* @throws UnsupportedOperationException if this method is not386* supported by this XMLInputFactory387*/388public abstract XMLEventReader createXMLEventReader(Source source)389throws XMLStreamException;390391/**392* Create a new XMLEventReader from a java.io.InputStream393* @param stream the InputStream to read from394* @return an instance of the {@code XMLEventReader}395* @throws XMLStreamException if an error occurs396*/397public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream)398throws XMLStreamException;399400/**401* Create a new XMLEventReader from a java.io.InputStream402* @param stream the InputStream to read from403* @param encoding the character encoding of the stream404* @return an instance of the {@code XMLEventReader}405* @throws XMLStreamException if an error occurs406*/407public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream, String encoding)408throws XMLStreamException;409410/**411* Create a new XMLEventReader from a java.io.InputStream412* @param systemId the system ID of the stream413* @param stream the InputStream to read from414* @return an instance of the {@code XMLEventReader}415* @throws XMLStreamException if an error occurs416*/417public abstract XMLEventReader createXMLEventReader(String systemId, java.io.InputStream stream)418throws XMLStreamException;419420/**421* Create a filtered reader that wraps the filter around the reader422* @param reader the reader to filter423* @param filter the filter to apply to the reader424* @return an instance of the {@code XMLEventReader}425* @throws XMLStreamException if an error occurs426*/427public abstract XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter)428throws XMLStreamException;429430/**431* Create a filtered event reader that wraps the filter around the event reader432* @param reader the event reader to wrap433* @param filter the filter to apply to the event reader434* @return an instance of the {@code XMLEventReader}435* @throws XMLStreamException if an error occurs436*/437public abstract XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter)438throws XMLStreamException;439440/**441* The resolver that will be set on any XMLStreamReader or XMLEventReader created442* by this factory instance.443* @return an instance of the {@code XMLResolver}444*/445public abstract XMLResolver getXMLResolver();446447/**448* The resolver that will be set on any XMLStreamReader or XMLEventReader created449* by this factory instance.450* @param resolver the resolver to use to resolve references451*/452public abstract void setXMLResolver(XMLResolver resolver);453454/**455* The reporter that will be set on any XMLStreamReader or XMLEventReader created456* by this factory instance.457* @return an instance of the {@code XMLReporter}458*/459public abstract XMLReporter getXMLReporter();460461/**462* The reporter that will be set on any XMLStreamReader or XMLEventReader created463* by this factory instance.464* @param reporter the resolver to use to report non fatal errors465*/466public abstract void setXMLReporter(XMLReporter reporter);467468/**469* Allows the user to set specific feature/property on the underlying470* implementation. The underlying implementation is not required to support471* every setting of every property in the specification and may use472* IllegalArgumentException to signal that an unsupported property may not be473* set with the specified value.474* <p>475* All implementations that implement JAXP 1.5 or newer are required to476* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.477* <ul>478* <li>479* <p>480* Access to external DTDs, external Entity References is restricted to the481* protocols specified by the property. If access is denied during parsing482* due to the restriction of this property, {@link javax.xml.stream.XMLStreamException}483* will be thrown by the {@link javax.xml.stream.XMLStreamReader#next()} or484* {@link javax.xml.stream.XMLEventReader#nextEvent()} method.485*486* </li>487* </ul>488* @param name The name of the property (may not be null)489* @param value The value of the property490* @throws java.lang.IllegalArgumentException if the property is not supported491*/492public abstract void setProperty(java.lang.String name, Object value)493throws java.lang.IllegalArgumentException;494495/**496* Get the value of a feature/property from the underlying implementation497* @param name The name of the property (may not be null)498* @return The value of the property499* @throws IllegalArgumentException if the property is not supported500*/501public abstract Object getProperty(java.lang.String name)502throws java.lang.IllegalArgumentException;503504505/**506* Query the set of properties that this factory supports.507*508* @param name The name of the property (may not be null)509* @return true if the property is supported and false otherwise510*/511public abstract boolean isPropertySupported(String name);512513/**514* Set a user defined event allocator for events515* @param allocator the user defined allocator516*/517public abstract void setEventAllocator(XMLEventAllocator allocator);518519/**520* Gets the allocator used by streams created with this factory521* @return an instance of the {@code XMLEventAllocator}522*/523public abstract XMLEventAllocator getEventAllocator();524525}526527528