1 /*
   2  * Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.stream;
  27 
  28 import com.sun.xml.internal.stream.XMLOutputFactoryImpl;
  29 import javax.xml.transform.Result;
  30 
  31 /**
  32  * Defines an abstract implementation of a factory for
  33  * getting XMLEventWriters and XMLStreamWriters.
  34  *
  35  * The following table defines the standard properties of this specification.
  36  * Each property varies in the level of support required by each implementation.
  37  * The level of support required is described in the 'Required' column.
  38  *
  39  *     <table class="striped">
  40  *     <caption>Configuration Parameters</caption>
  41  *     <thead>
  42  *      <tr>
  43  *        <th scope="col">Property Name</th>
  44  *        <th scope="col">Behavior</th>
  45  *        <th scope="col">Return type</th>
  46  *        <th scope="col">Default Value</th>
  47  *        <th scope="col">Required</th>
  48  *              </tr>
  49  *    </thead>
  50  *    <tbody>
  51  *         <tr><th scope="row">javax.xml.stream.isRepairingNamespaces</th><td>defaults prefixes
  52  *                 on the output side</td><td>Boolean</td><td>False</td><td>Yes</td></tr>
  53  *      </tbody>
  54  *   </table>
  55  *
  56  * <p>The following paragraphs describe the namespace and prefix repair algorithm:
  57  *
  58  * <p>The property can be set with the following code line:
  59  * {@code setProperty("javax.xml.stream.isRepairingNamespaces", new Boolean(true|false));}
  60  *
  61  * <p>This property specifies that the writer default namespace prefix declarations.
  62  * The default value is false.
  63  *
  64  * <p>If a writer isRepairingNamespaces it will create a namespace declaration
  65  * on the current StartElement for
  66  * any attribute that does not
  67  * currently have a namespace declaration in scope.  If the StartElement
  68  * has a uri but no prefix specified a prefix will be assigned, if the prefix
  69  * has not been declared in a parent of the current StartElement it will be declared
  70  * on the current StartElement.  If the defaultNamespace is bound and in scope
  71  * and the default namespace matches the URI of the attribute or StartElement
  72  * QName no prefix will be assigned.
  73  *
  74  * <p>If an element or attribute name has a prefix, but is not
  75  * bound to any namespace URI, then the prefix will be removed
  76  * during serialization.
  77  *
  78  * <p>If element and/or attribute names in the same start or
  79  * empty-element tag are bound to different namespace URIs and
  80  * are using the same prefix then the element or the first
  81  * occurring attribute retains the original prefix and the
  82  * following attributes have their prefixes replaced with a
  83  * new prefix that is bound to the namespace URIs of those
  84  * attributes.
  85  *
  86  * <p>If an element or attribute name uses a prefix that is
  87  * bound to a different URI than that inherited from the
  88  * namespace context of the parent of that element and there
  89  * is no namespace declaration in the context of the current
  90  * element then such a namespace declaration is added.
  91  *
  92  * <p>If an element or attribute name is bound to a prefix and
  93  * there is a namespace declaration that binds that prefix
  94  * to a different URI then that namespace declaration is
  95  * either removed if the correct mapping is inherited from
  96  * the parent context of that element, or changed to the
  97  * namespace URI of the element or attribute using that prefix.
  98  *
  99  * @version 1.2
 100  * @author Copyright (c) 2009, 2015 by Oracle Corporation. All Rights Reserved.
 101  * @see XMLInputFactory
 102  * @see XMLEventWriter
 103  * @see XMLStreamWriter
 104  * @since 1.6
 105  */
 106 public abstract class XMLOutputFactory {
 107   /**
 108    * Property used to set prefix defaulting on the output side
 109    */
 110   public static final String IS_REPAIRING_NAMESPACES=
 111     "javax.xml.stream.isRepairingNamespaces";
 112 
 113   static final String DEFAULIMPL = "com.sun.xml.internal.stream.XMLOutputFactoryImpl";
 114 
 115   /**
 116    * Protected constructor to prevent instantiation.
 117    * Use {@link #newFactory()} instead.
 118    */
 119   protected XMLOutputFactory(){}
 120 
 121    /**
 122    * Creates a new instance of the {@code XMLOutputFactory} builtin
 123    * system-default implementation.
 124    *
 125    * @return A new instance of the {@code XMLOutputFactory} builtin
 126    *         system-default implementation.
 127    *
 128    * @since 9
 129    */
 130   public static XMLOutputFactory newDefaultFactory() {
 131       return new XMLOutputFactoryImpl();
 132   }
 133 
 134   /**
 135    * Creates a new instance of the factory in exactly the same manner as the
 136    * {@link #newFactory()} method.
 137    * @return an instance of the {@code XMLOutputFactory}
 138    * @throws FactoryConfigurationError if an instance of this factory cannot be loaded
 139    */
 140   public static XMLOutputFactory newInstance()
 141     throws FactoryConfigurationError
 142   {
 143     return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);
 144   }
 145 
 146   /**
 147    * Create a new instance of the factory.
 148    * <p>
 149    * This static method creates a new factory instance. This method uses the
 150    * following ordered lookup procedure to determine the XMLOutputFactory
 151    * implementation class to load:
 152    * <ul>
 153    * <li>
 154    *   Use the javax.xml.stream.XMLOutputFactory system property.
 155    * </li>
 156    * <li>
 157    *   <p>
 158    *   Use the configuration file "stax.properties". The file is in standard
 159    *   {@link java.util.Properties} format and typically located in the
 160    *   {@code conf} directory of the Java installation. It contains the fully qualified
 161    *   name of the implementation class with the key being the system property
 162    *   defined above.
 163    *
 164    *   <p>
 165    *   The stax.properties file is read only once by the implementation
 166    *   and its values are then cached for future use.  If the file does not exist
 167    *   when the first attempt is made to read from it, no further attempts are
 168    *   made to check for its existence.  It is not possible to change the value
 169    *   of any property in stax.properties after it has been read for the first time.
 170    *
 171    *   <p>
 172    *   Use the jaxp configuration file "jaxp.properties". The file is in the same
 173    *   format as stax.properties and will only be read if stax.properties does
 174    *   not exist.
 175    * </li>
 176    * <li>
 177    *   <p>
 178    *   Use the service-provider loading facility, defined by the
 179    *   {@link java.util.ServiceLoader} class, to attempt to locate and load an
 180    *   implementation of the service using the {@linkplain
 181    *   java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
 182    *   the service-provider loading facility will use the {@linkplain
 183    *   java.lang.Thread#getContextClassLoader() current thread's context class loader}
 184    *   to attempt to load the service. If the context class
 185    *   loader is null, the {@linkplain
 186    *   ClassLoader#getSystemClassLoader() system class loader} will be used.
 187    * </li>
 188    * <li>
 189    *   <p>
 190    *   Otherwise, the {@linkplain #newDefaultFactory() system-default}
 191    *   implementation is returned.
 192    * </li>
 193    * </ul>
 194    * <p>
 195    * Once an application has obtained a reference to a XMLOutputFactory it
 196    * can use the factory to configure and obtain stream instances.
 197    *
 198    * @return an instance of the {@code XMLOutputFactory}
 199    * @throws FactoryConfigurationError in case of {@linkplain
 200    *   java.util.ServiceConfigurationError service configuration error} or if
 201    *   the implementation is not available or cannot be instantiated.
 202    */
 203   public static XMLOutputFactory newFactory()
 204     throws FactoryConfigurationError
 205   {
 206     return FactoryFinder.find(XMLOutputFactory.class, DEFAULIMPL);
 207   }
 208 
 209   /**
 210    * Create a new instance of the factory.
 211    *
 212    * @param factoryId             Name of the factory to find, same as
 213    *                              a property name
 214    * @param classLoader           classLoader to use
 215    * @return the factory implementation
 216    * @throws FactoryConfigurationError if an instance of this factory cannot be loaded
 217    *
 218    * @deprecated  This method has been deprecated because it returns an
 219    *              instance of XMLInputFactory, which is of the wrong class.
 220    *              Use the new method {@link #newFactory(java.lang.String,
 221    *              java.lang.ClassLoader)} instead.
 222    */
 223   @Deprecated(since="1.7")
 224   public static XMLInputFactory newInstance(String factoryId,
 225           ClassLoader classLoader)
 226           throws FactoryConfigurationError {
 227       //do not fallback if given classloader can't find the class, throw exception
 228       return FactoryFinder.find(XMLInputFactory.class, factoryId, classLoader, null);
 229   }
 230 
 231   /**
 232    * Create a new instance of the factory.
 233    * If the classLoader argument is null, then the ContextClassLoader is used.
 234    * <p>
 235    * This method uses the following ordered lookup procedure to determine
 236    * the XMLOutputFactory implementation class to load:
 237    * <ul>
 238    * <li>
 239    *   Use the value of the system property identified by {@code factoryId}.
 240    * </li>
 241    * <li>
 242    *   <p>
 243    *   Use the configuration file "stax.properties". The file is in standard
 244    *   {@link java.util.Properties} format and typically located in the
 245    *   {@code conf} directory of the Java installation. It contains the fully qualified
 246    *   name of the implementation class with the key being the system property
 247    *   defined above.
 248    *
 249    *   <p>
 250    *   The stax.properties file is read only once by the implementation
 251    *   and its values are then cached for future use.  If the file does not exist
 252    *   when the first attempt is made to read from it, no further attempts are
 253    *   made to check for its existence.  It is not possible to change the value
 254    *   of any property in stax.properties after it has been read for the first time.
 255    *
 256    *   <p>
 257    *   Use the jaxp configuration file "jaxp.properties". The file is in the same
 258    *   format as stax.properties and will only be read if stax.properties does
 259    *   not exist.
 260    * </li>
 261    * <li>
 262    *   <p>
 263    *   If {@code factoryId} is "javax.xml.stream.XMLOutputFactory",
 264    *   use the service-provider loading facility, defined by the
 265    *   {@link java.util.ServiceLoader} class, to attempt to {@linkplain
 266    *   java.util.ServiceLoader#load(java.lang.Class, java.lang.ClassLoader) locate and load}
 267    *   an implementation of the service using the specified {@code ClassLoader}.
 268    *   If {@code classLoader} is null, the {@linkplain
 269    *   java.util.ServiceLoader#load(java.lang.Class) default loading mechanism} will apply:
 270    *   That is, the service-provider loading facility will use the {@linkplain
 271    *   java.lang.Thread#getContextClassLoader() current thread's context class loader}
 272    *   to attempt to load the service. If the context class
 273    *   loader is null, the {@linkplain
 274    *   ClassLoader#getSystemClassLoader() system class loader} will be used.
 275    * </li>
 276    * <li>
 277    *   <p>
 278    *   Otherwise, throws a {@link FactoryConfigurationError}.
 279    * </li>
 280    * </ul>
 281    *
 282    * @apiNote The parameter factoryId defined here is inconsistent with that
 283    * of other JAXP factories where the first parameter is fully qualified
 284    * factory class name that provides implementation of the factory.
 285    *
 286    * <p>
 287    *   Note that this is a new method that replaces the deprecated
 288    *   {@link #newInstance(java.lang.String, java.lang.ClassLoader)
 289    *   newInstance(String factoryId, ClassLoader classLoader)} method.
 290    *   The original method was incorrectly defined to return XMLInputFactory.
 291    *
 292    *
 293    * @param factoryId             Name of the factory to find, same as
 294    *                              a property name
 295    * @param classLoader           classLoader to use
 296    * @return the factory implementation
 297    * @throws FactoryConfigurationError in case of {@linkplain
 298    *   java.util.ServiceConfigurationError service configuration error} or if
 299    *   the implementation is not available or cannot be instantiated.
 300    */
 301   public static XMLOutputFactory newFactory(String factoryId,
 302           ClassLoader classLoader)
 303           throws FactoryConfigurationError {
 304       //do not fallback if given classloader can't find the class, throw exception
 305       return FactoryFinder.find(XMLOutputFactory.class, factoryId, classLoader, null);
 306   }
 307 
 308   /**
 309    * Create a new XMLStreamWriter that writes to a writer
 310    * @param stream the writer to write to
 311    * @return instance of the {@code XMLStreamWriter}
 312    * @throws XMLStreamException if an error occurs
 313    */
 314   public abstract XMLStreamWriter createXMLStreamWriter(java.io.Writer stream) throws XMLStreamException;
 315 
 316   /**
 317    * Create a new XMLStreamWriter that writes to a stream
 318    * @param stream the stream to write to
 319    * @return instance of the {@code XMLStreamWriter}
 320    * @throws XMLStreamException if an error occurs
 321    */
 322   public abstract XMLStreamWriter createXMLStreamWriter(java.io.OutputStream stream) throws XMLStreamException;
 323 
 324   /**
 325    * Create a new XMLStreamWriter that writes to a stream
 326    * @param stream the stream to write to
 327    * @param encoding the encoding to use
 328    * @return instance of the {@code XMLStreamWriter}
 329    * @throws XMLStreamException if an error occurs
 330    */
 331   public abstract XMLStreamWriter createXMLStreamWriter(java.io.OutputStream stream,
 332                                          String encoding) throws XMLStreamException;
 333 
 334   /**
 335    * Create a new XMLStreamWriter that writes to a JAXP result.  This method is optional.
 336    * @param result the result to write to
 337    * @return instance of the {@code XMLStreamWriter}
 338    * @throws UnsupportedOperationException if this method is not
 339    * supported by this XMLOutputFactory
 340    * @throws XMLStreamException if an error occurs
 341    */
 342   public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;
 343 
 344 
 345   /**
 346    * Create a new XMLEventWriter that writes to a JAXP result.  This method is optional.
 347    * @param result the result to write to
 348    * @return instance of the {@code XMLEventWriter}
 349    * @throws UnsupportedOperationException if this method is not
 350    * supported by this XMLOutputFactory
 351    * @throws XMLStreamException if an error occurs
 352    */
 353   public abstract XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException;
 354 
 355   /**
 356    * Create a new XMLEventWriter that writes to a stream
 357    * @param stream the stream to write to
 358    * @return instance of the {@code XMLEventWriter}
 359    * @throws XMLStreamException if an error occurs
 360    */
 361   public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream) throws XMLStreamException;
 362 
 363 
 364 
 365   /**
 366    * Create a new XMLEventWriter that writes to a stream
 367    * @param stream the stream to write to
 368    * @param encoding the encoding to use
 369    * @return instance of the {@code XMLEventWriter}
 370    * @throws XMLStreamException if an error occurs
 371    */
 372   public abstract XMLEventWriter createXMLEventWriter(java.io.OutputStream stream,
 373                                                      String encoding) throws XMLStreamException;
 374 
 375   /**
 376    * Create a new XMLEventWriter that writes to a writer
 377    * @param stream the stream to write to
 378    * @return instance of the {@code XMLEventWriter}
 379    * @throws XMLStreamException if an error occurs
 380    */
 381   public abstract XMLEventWriter createXMLEventWriter(java.io.Writer stream) throws XMLStreamException;
 382 
 383   /**
 384    * Allows the user to set specific features/properties on the underlying implementation.
 385    * @param name The name of the property
 386    * @param value The value of the property
 387    * @throws java.lang.IllegalArgumentException if the property is not supported
 388    */
 389   public abstract void setProperty(java.lang.String name,
 390                                     Object value)
 391     throws IllegalArgumentException;
 392 
 393   /**
 394    * Get a feature/property on the underlying implementation
 395    * @param name The name of the property
 396    * @return The value of the property
 397    * @throws java.lang.IllegalArgumentException if the property is not supported
 398    */
 399   public abstract Object getProperty(java.lang.String name)
 400     throws IllegalArgumentException;
 401 
 402   /**
 403    * Query the set of properties that this factory supports.
 404    *
 405    * @param name The name of the property (may not be null)
 406    * @return true if the property is supported and false otherwise
 407    */
 408   public abstract boolean isPropertySupported(String name);
 409 }