1 /* 2 * Copyright (c) 2000, 2013, 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.transform; 27 28 /** 29 * <p>A TransformerFactory instance can be used to create 30 * {@link javax.xml.transform.Transformer} and 31 * {@link javax.xml.transform.Templates} objects.</p> 32 * 33 * <p>The system property that determines which Factory implementation 34 * to create is named <code>"javax.xml.transform.TransformerFactory"</code>. 35 * This property names a concrete subclass of the 36 * <code>TransformerFactory</code> abstract class. If the property is not 37 * defined, a platform default is be used.</p> 38 * 39 * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a> 40 * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a> 41 * 42 * @since 1.5 43 */ 44 public abstract class TransformerFactory { 45 46 /** 47 * Default constructor is protected on purpose. 48 */ 49 protected TransformerFactory() { } 50 51 52 53 /** 54 * <p>Obtain a new instance of a <code>TransformerFactory</code>. 55 * This static method creates a new factory instance.</p> 56 * <p>This method uses the following ordered lookup procedure to determine 57 * the <code>TransformerFactory</code> implementation class to 58 * load:</p> 59 * <ul> 60 * <li> 61 * Use the <code>javax.xml.transform.TransformerFactory</code> system 62 * property. 63 * </li> 64 * <li> 65 * Use the properties file "lib/jaxp.properties" in the JRE directory. 66 * This configuration file is in standard <code>java.util.Properties 67 * </code> format and contains the fully qualified name of the 68 * implementation class with the key being the system property defined 69 * above. 70 * <br> 71 * The jaxp.properties file is read only once by the JAXP implementation 72 * and it's values are then cached for future use. If the file does not exist 73 * when the first attempt is made to read from it, no further attempts are 74 * made to check for its existence. It is not possible to change the value 75 * of any property in jaxp.properties after it has been read for the first time. 76 * </li> 77 * <li> 78 * Use the service-provider loading facilities, defined by the 79 * {@link java.util.ServiceLoader} class, to attempt to locate and load an 80 * implementation of the service. 81 * </li> 82 * <li> 83 * Otherwise, the system-default implementation is returned. 84 * </li> 85 * </ul> 86 * 87 * <p>Once an application has obtained a reference to a <code> 88 * TransformerFactory</code> it can use the factory to configure 89 * and obtain transformer instances.</p> 90 * 91 * @return new TransformerFactory instance, never null. 92 * 93 * @throws TransformerFactoryConfigurationError Thrown in case of {@linkplain 94 * java.util.ServiceConfigurationError service configuration error} or if 95 * the implementation is not available or cannot be instantiated. 96 */ 97 public static TransformerFactory newInstance() 98 throws TransformerFactoryConfigurationError { 99 100 return FactoryFinder.find( 101 /* The default property name according to the JAXP spec */ 102 TransformerFactory.class, 103 /* The fallback implementation class name, XSLTC */ 104 "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); 105 } 106 107 /** 108 * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name. 109 * This function is useful when there are multiple providers in the classpath. 110 * It gives more control to the application as it can specify which provider 111 * should be loaded.</p> 112 * 113 * <p>Once an application has obtained a reference to a <code> 114 * TransformerFactory</code> it can use the factory to configure 115 * and obtain transformer instances.</p> 116 * 117 * <h2>Tip for Trouble-shooting</h2> 118 * <p>Setting the <code>jaxp.debug</code> system property will cause 119 * this method to print a lot of debug messages 120 * to <code>System.err</code> about what it is doing and where it is looking at.</p> 121 * 122 * <p> If you have problems try:</p> 123 * <pre> 124 * java -Djaxp.debug=1 YourProgram .... 125 * </pre> 126 * 127 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>. 128 * 129 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code> 130 * current <code>Thread</code>'s context classLoader is used to load the factory class. 131 * 132 * @return new TransformerFactory instance, never null. 133 * 134 * @throws TransformerFactoryConfigurationError 135 * if <code>factoryClassName</code> is <code>null</code>, or 136 * the factory class cannot be loaded, instantiated. 137 * 138 * @see #newInstance() 139 * 140 * @since 1.6 141 */ 142 public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader) 143 throws TransformerFactoryConfigurationError{ 144 145 //do not fallback if given classloader can't find the class, throw exception 146 return FactoryFinder.newInstance(TransformerFactory.class, 147 factoryClassName, classLoader, false, false); 148 } 149 /** 150 * <p>Process the <code>Source</code> into a <code>Transformer</code> 151 * <code>Object</code>. The <code>Source</code> is an XSLT document that 152 * conforms to <a href="http://www.w3.org/TR/xslt"> 153 * XSL Transformations (XSLT) Version 1.0</a>. Care must 154 * be taken not to use this <code>Transformer</code> in multiple 155 * <code>Thread</code>s running concurrently. 156 * Different <code>TransformerFactories</code> can be used concurrently by 157 * different <code>Thread</code>s.</p> 158 * 159 * @param source <code>Source </code> of XSLT document used to create 160 * <code>Transformer</code>. 161 * Examples of XML <code>Source</code>s include 162 * {@link javax.xml.transform.dom.DOMSource DOMSource}, 163 * {@link javax.xml.transform.sax.SAXSource SAXSource}, and 164 * {@link javax.xml.transform.stream.StreamSource StreamSource}. 165 * 166 * @return A <code>Transformer</code> object that may be used to perform 167 * a transformation in a single <code>Thread</code>, never 168 * <code>null</code>. 169 * 170 * @throws TransformerConfigurationException Thrown if there are errors when 171 * parsing the <code>Source</code> or it is not possible to create a 172 * <code>Transformer</code> instance. 173 * 174 * @see <a href="http://www.w3.org/TR/xslt"> 175 * XSL Transformations (XSLT) Version 1.0</a> 176 */ 177 public abstract Transformer newTransformer(Source source) 178 throws TransformerConfigurationException; 179 180 /** 181 * <p>Create a new <code>Transformer</code> that performs a copy 182 * of the <code>Source</code> to the <code>Result</code>. 183 * i.e. the "<em>identity transform</em>".</p> 184 * 185 * @return A Transformer object that may be used to perform a transformation 186 * in a single thread, never null. 187 * 188 * @throws TransformerConfigurationException When it is not 189 * possible to create a <code>Transformer</code> instance. 190 */ 191 public abstract Transformer newTransformer() 192 throws TransformerConfigurationException; 193 194 /** 195 * Process the Source into a Templates object, which is a 196 * a compiled representation of the source. This Templates object 197 * may then be used concurrently across multiple threads. Creating 198 * a Templates object allows the TransformerFactory to do detailed 199 * performance optimization of transformation instructions, without 200 * penalizing runtime transformation. 201 * 202 * @param source An object that holds a URL, input stream, etc. 203 * 204 * @return A Templates object capable of being used for transformation 205 * purposes, never <code>null</code>. 206 * 207 * @throws TransformerConfigurationException When parsing to 208 * construct the Templates object fails. 209 */ 210 public abstract Templates newTemplates(Source source) 211 throws TransformerConfigurationException; 212 213 /** 214 * <p>Get the stylesheet specification(s) associated with the 215 * XML <code>Source</code> document via the 216 * <a href="http://www.w3.org/TR/xml-stylesheet/"> 217 * xml-stylesheet processing instruction</a> that match the given criteria. 218 * Note that it is possible to return several stylesheets, in which case 219 * they are applied as if they were a list of imports or cascades in a 220 * single stylesheet.</p> 221 * 222 * @param source The XML source document. 223 * @param media The media attribute to be matched. May be null, in which 224 * case the prefered templates will be used (i.e. alternate = no). 225 * @param title The value of the title attribute to match. May be null. 226 * @param charset The value of the charset attribute to match. May be null. 227 * 228 * @return A <code>Source</code> <code>Object</code> suitable for passing 229 * to the <code>TransformerFactory</code>. 230 * 231 * @throws TransformerConfigurationException An <code>Exception</code> 232 * is thrown if an error occurings during parsing of the 233 * <code>source</code>. 234 * 235 * @see <a href="http://www.w3.org/TR/xml-stylesheet/"> 236 * Associating Style Sheets with XML documents Version 1.0</a> 237 */ 238 public abstract Source getAssociatedStylesheet( 239 Source source, 240 String media, 241 String title, 242 String charset) 243 throws TransformerConfigurationException; 244 245 /** 246 * Set an object that is used by default during the transformation 247 * to resolve URIs used in document(), xsl:import, or xsl:include. 248 * 249 * @param resolver An object that implements the URIResolver interface, 250 * or null. 251 */ 252 public abstract void setURIResolver(URIResolver resolver); 253 254 /** 255 * Get the object that is used by default during the transformation 256 * to resolve URIs used in document(), xsl:import, or xsl:include. 257 * 258 * @return The URIResolver that was set with setURIResolver. 259 */ 260 public abstract URIResolver getURIResolver(); 261 262 //======= CONFIGURATION METHODS ======= 263 264 /** 265 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s 266 * or <code>Template</code>s created by this factory.</p> 267 * 268 * <p> 269 * Feature names are fully qualified {@link java.net.URI}s. 270 * Implementations may define their own features. 271 * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the 272 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature. 273 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state. 274 * </p> 275 * 276 * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature. 277 * When the feature is:</p> 278 * <ul> 279 * <li> 280 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits 281 * and behave in a secure fashion as defined by the implementation. 282 * Examples include resolving user defined style sheets and functions. 283 * If XML processing is limited for security reasons, it will be reported via a call to the registered 284 * {@link ErrorListener#fatalError(TransformerException exception)}. 285 * See {@link #setErrorListener(ErrorListener listener)}. 286 * </li> 287 * <li> 288 * <code>false</code>: the implementation will processing XML according to the XML specifications without 289 * regard to possible implementation limits. 290 * </li> 291 * </ul> 292 * 293 * @param name Feature name. 294 * @param value Is feature state <code>true</code> or <code>false</code>. 295 * 296 * @throws TransformerConfigurationException if this <code>TransformerFactory</code> 297 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature. 298 * @throws NullPointerException If the <code>name</code> parameter is null. 299 */ 300 public abstract void setFeature(String name, boolean value) 301 throws TransformerConfigurationException; 302 303 /** 304 * Look up the value of a feature. 305 * 306 * <p> 307 * Feature names are fully qualified {@link java.net.URI}s. 308 * Implementations may define their own features. 309 * <code>false</code> is returned if this <code>TransformerFactory</code> or the 310 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature. 311 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state. 312 * </p> 313 * 314 * @param name Feature name. 315 * 316 * @return The current state of the feature, <code>true</code> or <code>false</code>. 317 * 318 * @throws NullPointerException If the <code>name</code> parameter is null. 319 */ 320 public abstract boolean getFeature(String name); 321 322 /** 323 * Allows the user to set specific attributes on the underlying 324 * implementation. An attribute in this context is defined to 325 * be an option that the implementation provides. 326 * An <code>IllegalArgumentException</code> is thrown if the underlying 327 * implementation doesn't recognize the attribute. 328 * <p> 329 * All implementations that implement JAXP 1.5 or newer are required to 330 * support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and 331 * {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} properties. 332 * </p> 333 * <ul> 334 * <li> 335 * <p> 336 * Access to external DTDs in the source file is restricted to the protocols 337 * specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property. 338 * If access is denied during transformation due to the restriction of this property, 339 * {@link javax.xml.transform.TransformerException} will be thrown by 340 * {@link javax.xml.transform.Transformer#transform(Source, Result)}. 341 * </p> 342 * <p> 343 * Access to external DTDs in the stylesheet is restricted to the protocols 344 * specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property. 345 * If access is denied during the creation of a new transformer due to the 346 * restriction of this property, 347 * {@link javax.xml.transform.TransformerConfigurationException} will be thrown 348 * by the {@link #newTransformer(Source)} method. 349 * </p> 350 * <p> 351 * Access to external reference set by the stylesheet processing instruction, 352 * Import and Include element is restricted to the protocols specified by the 353 * {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} property. 354 * If access is denied during the creation of a new transformer due to the 355 * restriction of this property, 356 * {@link javax.xml.transform.TransformerConfigurationException} will be thrown 357 * by the {@link #newTransformer(Source)} method. 358 * </p> 359 * <p> 360 * Access to external document through XSLT document function is restricted 361 * to the protocols specified by the property. If access is denied during 362 * the transformation due to the restriction of this property, 363 * {@link javax.xml.transform.TransformerException} will be thrown by the 364 * {@link javax.xml.transform.Transformer#transform(Source, Result)} method. 365 * </p> 366 * </li> 367 * </ul> 368 * 369 * @param name The name of the attribute. 370 * @param value The value of the attribute. 371 * 372 * @throws IllegalArgumentException When implementation does not 373 * recognize the attribute. 374 */ 375 public abstract void setAttribute(String name, Object value); 376 377 /** 378 * Allows the user to retrieve specific attributes on the underlying 379 * implementation. 380 * An <code>IllegalArgumentException</code> is thrown if the underlying 381 * implementation doesn't recognize the attribute. 382 * 383 * @param name The name of the attribute. 384 * 385 * @return value The value of the attribute. 386 * 387 * @throws IllegalArgumentException When implementation does not 388 * recognize the attribute. 389 */ 390 public abstract Object getAttribute(String name); 391 392 /** 393 * Set the error event listener for the TransformerFactory, which 394 * is used for the processing of transformation instructions, 395 * and not for the transformation itself. 396 * An <code>IllegalArgumentException</code> is thrown if the 397 * <code>ErrorListener</code> listener is <code>null</code>. 398 * 399 * @param listener The new error listener. 400 * 401 * @throws IllegalArgumentException When <code>listener</code> is 402 * <code>null</code> 403 */ 404 public abstract void setErrorListener(ErrorListener listener); 405 406 /** 407 * Get the error event handler for the TransformerFactory. 408 * 409 * @return The current error handler, which should never be null. 410 */ 411 public abstract ErrorListener getErrorListener(); 412 413 }