Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/stream/XMLOutputFactory.java
48534 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* Copyright (c) 2009, 2013, by Oracle Corporation. All Rights Reserved.26*/2728package javax.xml.stream;2930import javax.xml.transform.Result;3132/**33* Defines an abstract implementation of a factory for34* getting XMLEventWriters and XMLStreamWriters.35*36* The following table defines the standard properties of this specification.37* Each property varies in the level of support required by each implementation.38* The level of support required is described in the 'Required' column.39*40* <table border="2" rules="all" cellpadding="4">41* <thead>42* <tr>43* <th align="center" colspan="2">44* Configuration parameters45* </th>46* </tr>47* </thead>48* <tbody>49* <tr>50* <th>Property Name</th>51* <th>Behavior</th>52* <th>Return type</th>53* <th>Default Value</th>54* <th>Required</th>55* </tr>56* <tr><td>javax.xml.stream.isRepairingNamespaces</td><td>defaults prefixes on the output side</td><td>Boolean</td><td>False</td><td>Yes</td></tr>57* </tbody>58* </table>59*60* <p>The following paragraphs describe the namespace and prefix repair algorithm:</p>61*62* <p>The property can be set with the following code line:63* <code>setProperty("javax.xml.stream.isRepairingNamespaces",new Boolean(true|false));</code></p>64*65* <p>This property specifies that the writer default namespace prefix declarations.66* The default value is false. </p>67*68* <p>If a writer isRepairingNamespaces it will create a namespace declaration69* on the current StartElement for70* any attribute that does not71* currently have a namespace declaration in scope. If the StartElement72* has a uri but no prefix specified a prefix will be assigned, if the prefix73* has not been declared in a parent of the current StartElement it will be declared74* on the current StartElement. If the defaultNamespace is bound and in scope75* and the default namespace matches the URI of the attribute or StartElement76* QName no prefix will be assigned.</p>77*78* <p>If an element or attribute name has a prefix, but is not79* bound to any namespace URI, then the prefix will be removed80* during serialization.</p>81*82* <p>If element and/or attribute names in the same start or83* empty-element tag are bound to different namespace URIs and84* are using the same prefix then the element or the first85* occurring attribute retains the original prefix and the86* following attributes have their prefixes replaced with a87* new prefix that is bound to the namespace URIs of those88* attributes. </p>89*90* <p>If an element or attribute name uses a prefix that is91* bound to a different URI than that inherited from the92* namespace context of the parent of that element and there93* is no namespace declaration in the context of the current94* element then such a namespace declaration is added. </p>95*96* <p>If an element or attribute name is bound to a prefix and97* there is a namespace declaration that binds that prefix98* to a different URI then that namespace declaration is99* either removed if the correct mapping is inherited from100* the parent context of that element, or changed to the101* namespace URI of the element or attribute using that prefix.</p>102*103* @version 1.2104* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.105* @see XMLInputFactory106* @see XMLEventWriter107* @see XMLStreamWriter108* @since 1.6109*/110public abstract class XMLOutputFactory {111/**112* Property used to set prefix defaulting on the output side113*/114public static final String IS_REPAIRING_NAMESPACES=115"javax.xml.stream.isRepairingNamespaces";116117static final String DEFAULIMPL = "com.sun.xml.internal.stream.XMLOutputFactoryImpl";118119protected XMLOutputFactory(){}120121/**122* Creates a new instance of the factory in exactly the same manner as the123* {@link #newFactory()} method.124* @throws FactoryConfigurationError if an instance of this factory cannot be loaded125*/126public static XMLOutputFactory newInstance()127throws FactoryConfigurationError128{129return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);130}131132/**133* Create a new instance of the factory.134* <p>135* This static method creates a new factory instance. This method uses the136* following ordered lookup procedure to determine the XMLOutputFactory137* implementation class to load:138* </p>139* <ul>140* <li>141* Use the javax.xml.stream.XMLOutputFactory system property.142* </li>143* <li>144* Use the properties file "lib/stax.properties" in the JRE directory.145* This configuration file is in standard java.util.Properties format146* and contains the fully qualified name of the implementation class147* with the key being the system property defined above.148* </li>149* <li>150* Use the service-provider loading facilities, defined by the151* {@link java.util.ServiceLoader} class, to attempt to locate and load an152* implementation of the service using the {@linkplain153* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:154* the service-provider loading facility will use the {@linkplain155* java.lang.Thread#getContextClassLoader() current thread's context class loader}156* to attempt to load the service. If the context class157* loader is null, the {@linkplain158* ClassLoader#getSystemClassLoader() system class loader} will be used.159* </li>160* <li>161* Otherwise, the system-default implementation is returned.162* </li>163* <p>164* Once an application has obtained a reference to a XMLOutputFactory it165* can use the factory to configure and obtain stream instances.166* </p>167* <p>168* Note that this is a new method that replaces the deprecated newInstance() method.169* No changes in behavior are defined by this replacement method relative to the170* deprecated method.171* </p>172* @throws FactoryConfigurationError in case of {@linkplain173* java.util.ServiceConfigurationError service configuration error} or if174* the implementation is not available or cannot be instantiated.175*/176public static XMLOutputFactory newFactory()177throws FactoryConfigurationError178{179return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);180}181182/**183* Create a new instance of the factory.184*185* @param factoryId Name of the factory to find, same as186* a property name187* @param classLoader classLoader to use188* @return the factory implementation189* @throws FactoryConfigurationError if an instance of this factory cannot be loaded190*191* @deprecated This method has been deprecated because it returns an192* instance of XMLInputFactory, which is of the wrong class.193* Use the new method {@link #newFactory(java.lang.String,194* java.lang.ClassLoader)} instead.195*/196public static XMLInputFactory newInstance(String factoryId,197ClassLoader classLoader)198throws FactoryConfigurationError {199//do not fallback if given classloader can't find the class, throw exception200return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null);201}202203/**204* Create a new instance of the factory.205* If the classLoader argument is null, then the ContextClassLoader is used.206* <p>207* This method uses the following ordered lookup procedure to determine208* the XMLOutputFactory implementation class to load:209* </p>210* <ul>211* <li>212* Use the value of the system property identified by {@code factoryId}.213* </li>214* <li>215* Use the properties file "lib/stax.properties" in the JRE directory.216* This configuration file is in standard java.util.Properties format217* and contains the fully qualified name of the implementation class218* with the key being the given {@code factoryId}.219* </li>220* <li>221* If {@code factoryId} is "javax.xml.stream.XMLOutputFactory",222* use the service-provider loading facilities, defined by the223* {@link java.util.ServiceLoader} class, to attempt to {@linkplain224* java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load}225* an implementation of the service using the specified {@code ClassLoader}.226* If {@code classLoader} is null, the {@linkplain227* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:228* That is, the service-provider loading facility will use the {@linkplain229* java.lang.Thread#getContextClassLoader() current thread's context class loader}230* to attempt to load the service. If the context class231* loader is null, the {@linkplain232* ClassLoader#getSystemClassLoader() system class loader} will be used.233* </li>234* <li>235* Otherwise, throws a {@link FactoryConfigurationError}.236* </li>237* </ul>238*239* @apiNote The parameter factoryId defined here is inconsistent with that240* of other JAXP factories where the first parameter is fully qualified241* factory class name that provides implementation of the factory.242*243* <p>244* Note that this is a new method that replaces the deprecated245* {@link #newInstance(java.lang.String, java.lang.ClassLoader)246* newInstance(String factoryId, ClassLoader classLoader)} method.247* The original method was incorrectly defined to return XMLInputFactory.248* </p>249*250* @param factoryId Name of the factory to find, same as251* a property name252* @param classLoader classLoader to use253* @return the factory implementation254* @throws FactoryConfigurationError in case of {@linkplain255* java.util.ServiceConfigurationError service configuration error} or if256* the implementation is not available or cannot be instantiated.257*/258public static XMLOutputFactory newFactory(String factoryId,259ClassLoader classLoader)260throws FactoryConfigurationError {261//do not fallback if given classloader can't find the class, throw exception262return FactoryFinder.find(XMLOutputFactory.class, factoryId, classLoader, null);263}264265/**266* Create a new XMLStreamWriter that writes to a writer267* @param stream the writer to write to268* @throws XMLStreamException269*/270public abstract XMLStreamWriter createXMLStreamWriter(java.io.Writer stream) throws XMLStreamException;271272/**273* Create a new XMLStreamWriter that writes to a stream274* @param stream the stream to write to275* @throws XMLStreamException276*/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* @throws XMLStreamException284*/285public abstract XMLStreamWriter createXMLStreamWriter(java.io.OutputStream stream,286String encoding) throws XMLStreamException;287288/**289* Create a new XMLStreamWriter that writes to a JAXP result. This method is optional.290* @param result the result to write to291* @throws UnsupportedOperationException if this method is not292* supported by this XMLOutputFactory293* @throws XMLStreamException294*/295public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;296297298/**299* Create a new XMLEventWriter that writes to a JAXP result. This method is optional.300* @param result the result to write to301* @throws UnsupportedOperationException if this method is not302* supported by this XMLOutputFactory303* @throws XMLStreamException304*/305public abstract XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException;306307/**308* Create a new XMLEventWriter that writes to a stream309* @param stream the stream to write to310* @throws XMLStreamException311*/312public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream) throws XMLStreamException;313314315316/**317* Create a new XMLEventWriter that writes to a stream318* @param stream the stream to write to319* @param encoding the encoding to use320* @throws XMLStreamException321*/322public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream,323String encoding) throws XMLStreamException;324325/**326* Create a new XMLEventWriter that writes to a writer327* @param stream the stream to write to328* @throws XMLStreamException329*/330public abstract XMLEventWriter createXMLEventWriter(java.io.Writer stream) throws XMLStreamException;331332/**333* Allows the user to set specific features/properties on the underlying implementation.334* @param name The name of the property335* @param value The value of the property336* @throws java.lang.IllegalArgumentException if the property is not supported337*/338public abstract void setProperty(java.lang.String name,339Object value)340throws IllegalArgumentException;341342/**343* Get a feature/property on the underlying implementation344* @param name The name of the property345* @return The value of the property346* @throws java.lang.IllegalArgumentException if the property is not supported347*/348public abstract Object getProperty(java.lang.String name)349throws IllegalArgumentException;350351/**352* Query the set of properties that this factory supports.353*354* @param name The name of the property (may not be null)355* @return true if the property is supported and false otherwise356*/357public abstract boolean isPropertySupported(String name);358}359360361