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