1 /* 2 * Copyright (c) 2000, 2006, 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 56 * 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 * 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 Services API (as detailed in the JAR specification), if 79 * available, to determine the classname. The Services API will look 80 * for a classname in the file 81 * <code>META-INF/services/javax.xml.transform.TransformerFactory</code> 82 * in jars available to the runtime. 83 * </li> 84 * <li> 85 * Platform default <code>TransformerFactory</code> instance. 86 * </li> 87 * </ul> 88 * 89 * <p>Once an application has obtained a reference to a <code> 90 * TransformerFactory</code> it can use the factory to configure 91 * and obtain transformer instances.</p> 92 * 93 * @return new TransformerFactory instance, never null. 94 * 95 * @throws TransformerFactoryConfigurationError Thrown if the implementation 96 * is not available or cannot be instantiated. 97 */ 98 public static TransformerFactory newInstance() 99 throws TransformerFactoryConfigurationError { 100 try { 101 return (TransformerFactory) FactoryFinder.find( 102 /* The default property name according to the JAXP spec */ 103 "javax.xml.transform.TransformerFactory", 104 /* The fallback implementation class name, XSLTC */ 105 "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); 106 } catch (FactoryFinder.ConfigurationError e) { 107 throw new TransformerFactoryConfigurationError( 108 e.getException(), 109 e.getMessage()); 110 } 111 } 112 113 /** 114 * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name. 115 * This function is useful when there are multiple providers in the classpath. 116 * It gives more control to the application as it can specify which provider 117 * should be loaded.</p> 118 * 119 * <p>Once an application has obtained a reference to a <code> 120 * TransformerFactory</code> it can use the factory to configure 121 * and obtain transformer instances.</p> 122 * 123 * <h2>Tip for Trouble-shooting</h2> 124 * <p>Setting the <code>jaxp.debug</code> system property will cause 125 * this method to print a lot of debug messages 126 * to <code>System.err</code> about what it is doing and where it is looking at.</p> 127 * 128 * <p> If you have problems try:</p> 129 * <pre> 130 * java -Djaxp.debug=1 YourProgram .... 131 * </pre> 132 * 133 * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>. 134 * 135 * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code> 136 * current <code>Thread</code>'s context classLoader is used to load the factory class. 137 * 138 * @return new TransformerFactory instance, never null. 139 * 140 * @throws TransformerFactoryConfigurationError 141 * if <code>factoryClassName</code> is <code>null</code>, or 142 * the factory class cannot be loaded, instantiated. 143 * 144 * @see #newInstance() 145 * 146 * @since 1.6 147 */ 148 public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader) 149 throws TransformerFactoryConfigurationError{ 150 try { 151 //do not fallback if given classloader can't find the class, throw exception 152 return (TransformerFactory) FactoryFinder.newInstance(factoryClassName, classLoader, false); 153 } catch (FactoryFinder.ConfigurationError e) { 154 throw new TransformerFactoryConfigurationError( 155 e.getException(), 156 e.getMessage()); 157 } 158 } 159 /** 160 * <p>Process the <code>Source</code> into a <code>Transformer</code> 161 * <code>Object</code>. The <code>Source</code> is an XSLT document that 162 * conforms to <a href="http://www.w3.org/TR/xslt"> 163 * XSL Transformations (XSLT) Version 1.0</a>. Care must 164 * be taken not to use this <code>Transformer</code> in multiple 165 * <code>Thread</code>s running concurrently. 166 * Different <code>TransformerFactories</code> can be used concurrently by 167 * different <code>Thread</code>s.</p> 168 * 169 * @param source <code>Source </code> of XSLT document used to create 170 * <code>Transformer</code>. 171 * Examples of XML <code>Source</code>s include 172 * {@link javax.xml.transform.dom.DOMSource DOMSource}, 173 * {@link javax.xml.transform.sax.SAXSource SAXSource}, and 174 * {@link javax.xml.transform.stream.StreamSource StreamSource}. 175 * 176 * @return A <code>Transformer</code> object that may be used to perform 177 * a transformation in a single <code>Thread</code>, never 178 * <code>null</code>. 179 * 180 * @throws TransformerConfigurationException Thrown if there are errors when 181 * parsing the <code>Source</code> or it is not possible to create a 182 * <code>Transformer</code> instance. 183 * 184 * @see <a href="http://www.w3.org/TR/xslt"> 185 * XSL Transformations (XSLT) Version 1.0</a> 186 */ 187 public abstract Transformer newTransformer(Source source) 188 throws TransformerConfigurationException; 189 190 /** 191 * <p>Create a new <code>Transformer</code> that performs a copy 192 * of the <code>Source</code> to the <code>Result</code>. 193 * i.e. the "<em>identity transform</em>".</p> 194 * 195 * @return A Transformer object that may be used to perform a transformation 196 * in a single thread, never null. 197 * 198 * @throws TransformerConfigurationException When it is not 199 * possible to create a <code>Transformer</code> instance. 200 */ 201 public abstract Transformer newTransformer() 202 throws TransformerConfigurationException; 203 204 /** 205 * Process the Source into a Templates object, which is a 206 * a compiled representation of the source. This Templates object 207 * may then be used concurrently across multiple threads. Creating 208 * a Templates object allows the TransformerFactory to do detailed 209 * performance optimization of transformation instructions, without 210 * penalizing runtime transformation. 211 * 212 * @param source An object that holds a URL, input stream, etc. 213 * 214 * @return A Templates object capable of being used for transformation 215 * purposes, never <code>null</code>. 216 * 217 * @throws TransformerConfigurationException When parsing to 218 * construct the Templates object fails. 219 */ 220 public abstract Templates newTemplates(Source source) 221 throws TransformerConfigurationException; 222 223 /** 224 * <p>Get the stylesheet specification(s) associated with the 225 * XML <code>Source</code> document via the 226 * <a href="http://www.w3.org/TR/xml-stylesheet/"> 227 * xml-stylesheet processing instruction</a> that match the given criteria. 228 * Note that it is possible to return several stylesheets, in which case 229 * they are applied as if they were a list of imports or cascades in a 230 * single stylesheet.</p> 231 * 232 * @param source The XML source document. 233 * @param media The media attribute to be matched. May be null, in which 234 * case the prefered templates will be used (i.e. alternate = no). 235 * @param title The value of the title attribute to match. May be null. 236 * @param charset The value of the charset attribute to match. May be null. 237 * 238 * @return A <code>Source</code> <code>Object</code> suitable for passing 239 * to the <code>TransformerFactory</code>. 240 * 241 * @throws TransformerConfigurationException An <code>Exception</code> 242 * is thrown if an error occurings during parsing of the 243 * <code>source</code>. 244 * 245 * @see <a href="http://www.w3.org/TR/xml-stylesheet/"> 246 * Associating Style Sheets with XML documents Version 1.0</a> 247 */ 248 public abstract Source getAssociatedStylesheet( 249 Source source, 250 String media, 251 String title, 252 String charset) 253 throws TransformerConfigurationException; 254 255 /** 256 * Set an object that is used by default during the transformation 257 * to resolve URIs used in document(), xsl:import, or xsl:include. 258 * 259 * @param resolver An object that implements the URIResolver interface, 260 * or null. 261 */ 262 public abstract void setURIResolver(URIResolver resolver); 263 264 /** 265 * Get the object that is used by default during the transformation 266 * to resolve URIs used in document(), xsl:import, or xsl:include. 267 * 268 * @return The URIResolver that was set with setURIResolver. 269 */ 270 public abstract URIResolver getURIResolver(); 271 272 //======= CONFIGURATION METHODS ======= 273 274 /** 275 * <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s 276 * or <code>Template</code>s created by this factory.</p> 277 * 278 * <p> 279 * Feature names are fully qualified {@link java.net.URI}s. 280 * Implementations may define their own features. 281 * An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the 282 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature. 283 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state. 284 * </p> 285 * 286 * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature. 287 * When the feature is:</p> 288 * <ul> 289 * <li> 290 * <code>true</code>: the implementation will limit XML processing to conform to implementation limits 291 * and behave in a secure fashion as defined by the implementation. 292 * Examples include resolving user defined style sheets and functions. 293 * If XML processing is limited for security reasons, it will be reported via a call to the registered 294 * {@link ErrorListener#fatalError(TransformerException exception)}. 295 * See {@link #setErrorListener(ErrorListener listener)}. 296 * </li> 297 * <li> 298 * <code>false</code>: the implementation will processing XML according to the XML specifications without 299 * regard to possible implementation limits. 300 * </li> 301 * </ul> 302 * 303 * @param name Feature name. 304 * @param value Is feature state <code>true</code> or <code>false</code>. 305 * 306 * @throws TransformerConfigurationException if this <code>TransformerFactory</code> 307 * or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature. 308 * @throws NullPointerException If the <code>name</code> parameter is null. 309 */ 310 public abstract void setFeature(String name, boolean value) 311 throws TransformerConfigurationException; 312 313 /** 314 * Look up the value of a feature. 315 * 316 * <p> 317 * Feature names are fully qualified {@link java.net.URI}s. 318 * Implementations may define their own features. 319 * <code>false</code> is returned if this <code>TransformerFactory</code> or the 320 * <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature. 321 * It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state. 322 * </p> 323 * 324 * @param name Feature name. 325 * 326 * @return The current state of the feature, <code>true</code> or <code>false</code>. 327 * 328 * @throws NullPointerException If the <code>name</code> parameter is null. 329 */ 330 public abstract boolean getFeature(String name); 331 332 /** 333 * Allows the user to set specific attributes on the underlying 334 * implementation. An attribute in this context is defined to 335 * be an option that the implementation provides. 336 * An <code>IllegalArgumentException</code> is thrown if the underlying 337 * implementation doesn't recognize the attribute. 338 * 339 * @param name The name of the attribute. 340 * @param value The value of the attribute. 341 * 342 * @throws IllegalArgumentException When implementation does not 343 * recognize the attribute. 344 */ 345 public abstract void setAttribute(String name, Object value); 346 347 /** 348 * Allows the user to retrieve specific attributes on the underlying 349 * implementation. 350 * An <code>IllegalArgumentException</code> is thrown if the underlying 351 * implementation doesn't recognize the attribute. 352 * 353 * @param name The name of the attribute. 354 * 355 * @return value The value of the attribute. 356 * 357 * @throws IllegalArgumentException When implementation does not 358 * recognize the attribute. 359 */ 360 public abstract Object getAttribute(String name); 361 362 /** 363 * Set the error event listener for the TransformerFactory, which 364 * is used for the processing of transformation instructions, 365 * and not for the transformation itself. 366 * An <code>IllegalArgumentException</code> is thrown if the 367 * <code>ErrorListener</code> listener is <code>null</code>. 368 * 369 * @param listener The new error listener. 370 * 371 * @throws IllegalArgumentException When <code>listener</code> is 372 * <code>null</code> 373 */ 374 public abstract void setErrorListener(ErrorListener listener); 375 376 /** 377 * Get the error event handler for the TransformerFactory. 378 * 379 * @return The current error handler, which should never be null. 380 */ 381 public abstract ErrorListener getErrorListener(); 382 383 }