Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLOutputFactory.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.XMLOutputFactoryImpl;28import javax.xml.transform.Result;2930/**31* Defines an abstract implementation of a factory for32* getting XMLEventWriters and XMLStreamWriters.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.isRepairingNamespaces</th><td>defaults prefixes51* on the output side</td><td>Boolean</td><td>False</td><td>Yes</td></tr>52* </tbody>53* </table>54*55* <p>The following paragraphs describe the namespace and prefix repair algorithm:56*57* <p>The property can be set with the following code line:58* {@code setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true|false));}59*60* <p>This property specifies that the writer default namespace prefix declarations.61* The default value is false.62*63* <p>If a writer isRepairingNamespaces it will create a namespace declaration64* on the current StartElement for65* any attribute that does not66* currently have a namespace declaration in scope. If the StartElement67* has a uri but no prefix specified a prefix will be assigned, if the prefix68* has not been declared in a parent of the current StartElement it will be declared69* on the current StartElement. If the defaultNamespace is bound and in scope70* and the default namespace matches the URI of the attribute or StartElement71* QName no prefix will be assigned.72*73* <p>If an element or attribute name has a prefix, but is not74* bound to any namespace URI, then the prefix will be removed75* during serialization.76*77* <p>If element and/or attribute names in the same start or78* empty-element tag are bound to different namespace URIs and79* are using the same prefix then the element or the first80* occurring attribute retains the original prefix and the81* following attributes have their prefixes replaced with a82* new prefix that is bound to the namespace URIs of those83* attributes.84*85* <p>If an element or attribute name uses a prefix that is86* bound to a different URI than that inherited from the87* namespace context of the parent of that element and there88* is no namespace declaration in the context of the current89* element then such a namespace declaration is added.90*91* <p>If an element or attribute name is bound to a prefix and92* there is a namespace declaration that binds that prefix93* to a different URI then that namespace declaration is94* either removed if the correct mapping is inherited from95* the parent context of that element, or changed to the96* namespace URI of the element or attribute using that prefix.97*98* @version 1.299* @author Copyright (c) 2009, 2015 by Oracle Corporation. All Rights Reserved.100* @see XMLInputFactory101* @see XMLEventWriter102* @see XMLStreamWriter103* @since 1.6104*/105public abstract class XMLOutputFactory {106/**107* Property used to set prefix defaulting on the output side108*/109public static final String IS_REPAIRING_NAMESPACES=110"javax.xml.stream.isRepairingNamespaces";111112static final String DEFAULIMPL = "com.sun.xml.internal.stream.XMLOutputFactoryImpl";113114/**115* Protected constructor to prevent instantiation.116* Use {@link #newFactory()} instead.117*/118protected XMLOutputFactory(){}119120/**121* Creates a new instance of the {@code XMLOutputFactory} builtin122* system-default implementation.123*124* @return A new instance of the {@code XMLOutputFactory} builtin125* system-default implementation.126*127* @since 9128*/129public static XMLOutputFactory newDefaultFactory() {130return new XMLOutputFactoryImpl();131}132133/**134* Creates a new instance of the factory in exactly the same manner as the135* {@link #newFactory()} method.136* @return an instance of the {@code XMLOutputFactory}137* @throws FactoryConfigurationError if an instance of this factory cannot be loaded138*/139public static XMLOutputFactory newInstance()140throws FactoryConfigurationError141{142return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);143}144145/**146* Creates a new instance of the factory. This method uses the147* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>148* to determine the {@code XMLOutputFactory} implementation class to load.149* <p>150* Once an application has obtained a reference to a {@code XMLOutputFactory}, it151* can use the factory to configure and obtain stream instances.152*153* @return an instance of the {@code XMLOutputFactory}154* @throws FactoryConfigurationError in case of {@linkplain155* java.util.ServiceConfigurationError service configuration error} or if156* the implementation is not available or cannot be instantiated.157*/158public static XMLOutputFactory newFactory()159throws FactoryConfigurationError160{161return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);162}163164/**165* Create a new instance of the factory.166*167* @param factoryId Name of the factory to find, same as168* a property name169* @param classLoader classLoader to use170* @return the factory implementation171* @throws FactoryConfigurationError if an instance of this factory cannot be loaded172*173* @deprecated This method has been deprecated because it returns an174* instance of XMLInputFactory, which is of the wrong class.175* Use the new method {@link #newFactory(java.lang.String,176* java.lang.ClassLoader)} instead.177*/178@Deprecated(since="1.7")179public static XMLInputFactory newInstance(String factoryId,180ClassLoader classLoader)181throws FactoryConfigurationError {182//do not fallback if given classloader can't find the class, throw exception183return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null);184}185186/**187* Create a new instance of the factory.188* If the classLoader argument is null, then the ContextClassLoader is used.189* <p>190* This method uses the following ordered lookup procedure to determine191* the XMLOutputFactory implementation class to load:192* <ul>193* <li>194* Use the value of the system property identified by {@code factoryId}.195* </li>196* <li>197* <p>198* Use the configuration file "stax.properties". The file is in standard199* {@link java.util.Properties} format and typically located in the200* {@code conf} directory of the Java installation. It contains the fully qualified201* name of the implementation class with the key being the system property202* defined above.203*204* <p>205* The stax.properties file is read only once by the implementation206* and its values are then cached for future use. If the file does not exist207* when the first attempt is made to read from it, no further attempts are208* made to check for its existence. It is not possible to change the value209* of any property in stax.properties after it has been read for the first time.210*211* <p>212* Use the jaxp configuration file "jaxp.properties". The file is in the same213* format as stax.properties and will only be read if stax.properties does214* not exist.215* </li>216* <li>217* <p>218* If {@code factoryId} is "javax.xml.stream.XMLOutputFactory",219* use the service-provider loading facility, defined by the220* {@link java.util.ServiceLoader} class, to attempt to {@linkplain221* java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load}222* an implementation of the service using the specified {@code ClassLoader}.223* If {@code classLoader} is null, the {@linkplain224* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:225* That is, the service-provider loading facility will use the {@linkplain226* java.lang.Thread#getContextClassLoader() current thread's context class loader}227* to attempt to load the service. If the context class228* loader is null, the {@linkplain229* ClassLoader#getSystemClassLoader() system class loader} will be used.230* </li>231* <li>232* <p>233* Otherwise, throws a {@link FactoryConfigurationError}.234* </li>235* </ul>236*237* @apiNote The parameter factoryId defined here is inconsistent with that238* of other JAXP factories where the first parameter is fully qualified239* factory class name that provides implementation of the factory.240*241* <p>242* Note that this is a new method that replaces the deprecated243* {@link #newInstance(java.lang.String, java.lang.ClassLoader)244* newInstance(String factoryId, ClassLoader classLoader)} method.245* The original method was incorrectly defined to return XMLInputFactory.246*247*248* @param factoryId Name of the factory to find, same as249* a property name250* @param classLoader classLoader to use251* @return the factory implementation252* @throws FactoryConfigurationError in case of {@linkplain253* java.util.ServiceConfigurationError service configuration error} or if254* the implementation is not available or cannot be instantiated.255*/256public static XMLOutputFactory newFactory(String factoryId,257ClassLoader classLoader)258throws FactoryConfigurationError {259//do not fallback if given classloader can't find the class, throw exception260return FactoryFinder.find(XMLOutputFactory.class, factoryId, classLoader, null);261}262263/**264* Create a new XMLStreamWriter that writes to a writer265* @param stream the writer to write to266* @return instance of the {@code XMLStreamWriter}267* @throws XMLStreamException if an error occurs268*/269public abstract XMLStreamWriter createXMLStreamWriter(java.io.Writer stream) throws XMLStreamException;270271/**272* Create a new XMLStreamWriter that writes to a stream273* @param stream the stream to write to274* @return instance of the {@code XMLStreamWriter}275* @throws XMLStreamException if an error occurs276*/277public abstract XMLStreamWriter createXMLStreamWriter(java.io.OutputStream stream) throws XMLStreamException;278279/**280* Create a new XMLStreamWriter that writes to a stream281* @param stream the stream to write to282* @param encoding the encoding to use283* @return instance of the {@code XMLStreamWriter}284* @throws XMLStreamException if an error occurs285*/286public abstract XMLStreamWriter createXMLStreamWriter(java.io.OutputStream stream,287String encoding) throws XMLStreamException;288289/**290* Create a new XMLStreamWriter that writes to a JAXP result. This method is optional.291* @param result the result to write to292* @return instance of the {@code XMLStreamWriter}293* @throws UnsupportedOperationException if this method is not294* supported by this XMLOutputFactory295* @throws XMLStreamException if an error occurs296*/297public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;298299300/**301* Create a new XMLEventWriter that writes to a JAXP result. This method is optional.302* @param result the result to write to303* @return instance of the {@code XMLEventWriter}304* @throws UnsupportedOperationException if this method is not305* supported by this XMLOutputFactory306* @throws XMLStreamException if an error occurs307*/308public abstract XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException;309310/**311* Create a new XMLEventWriter that writes to a stream312* @param stream the stream to write to313* @return instance of the {@code XMLEventWriter}314* @throws XMLStreamException if an error occurs315*/316public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream) throws XMLStreamException;317318319320/**321* Create a new XMLEventWriter that writes to a stream322* @param stream the stream to write to323* @param encoding the encoding to use324* @return instance of the {@code XMLEventWriter}325* @throws XMLStreamException if an error occurs326*/327public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream,328String encoding) throws XMLStreamException;329330/**331* Create a new XMLEventWriter that writes to a writer332* @param stream the stream to write to333* @return instance of the {@code XMLEventWriter}334* @throws XMLStreamException if an error occurs335*/336public abstract XMLEventWriter createXMLEventWriter(java.io.Writer stream) throws XMLStreamException;337338/**339* Allows the user to set specific features/properties on the underlying implementation.340* @param name The name of the property341* @param value The value of the property342* @throws java.lang.IllegalArgumentException if the property is not supported343*/344public abstract void setProperty(java.lang.String name,345Object value)346throws IllegalArgumentException;347348/**349* Get a feature/property on the underlying implementation350* @param name The name of the property351* @return The value of the property352* @throws java.lang.IllegalArgumentException if the property is not supported353*/354public abstract Object getProperty(java.lang.String name)355throws IllegalArgumentException;356357/**358* Query the set of properties that this factory supports.359*360* @param name The name of the property (may not be null)361* @return true if the property is supported and false otherwise362*/363public abstract boolean isPropertySupported(String name);364}365366367