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      * <p>Uses the service-provider loading facilities, defined by the {@link java.util.ServiceLoader} class, to attempt 
  79      * to locate and load an implementation of the service. In case of multiple providers, the first non-default implementation
  80      * shall be instantiated and returned.  The default implementation is returned if it is the only one
  81      * found by the service loader.</p>
  82      * <p>
  83      * If a misconfigured provider is encountered and {@link java.util.ServiceConfigurationError} is thrown, the error will be wrapped 
  84      * in a {@link javax.xml.transform.TransformerFactoryConfigurationError}.</p>
  85      * </li>
  86      * <li>
  87      * Platform default <code>TransformerFactory</code> instance.
  88      * </li>
  89      * </ul>
  90      *
  91      * <p>Once an application has obtained a reference to a <code>
  92      * TransformerFactory</code> it can use the factory to configure
  93      * and obtain transformer instances.</p>
  94      *
  95      * @return new TransformerFactory instance, never null.
  96      *
  97      * @throws TransformerFactoryConfigurationError Thrown if the implementation
  98      *    is not available or cannot be instantiated.
  99      */
 100     public static TransformerFactory newInstance()
 101         throws TransformerFactoryConfigurationError {
 102         return (TransformerFactory) FactoryFinder.find(TransformerFactory.class,
 103             /* The default property name according to the JAXP spec */
 104             "javax.xml.transform.TransformerFactory",
 105             /* The fallback implementation class name, XSLTC */
 106             "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
 107         }
 108 
 109     /**
 110      * <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.
 111      * This function is useful when there are multiple providers in the classpath.
 112      * It gives more control to the application as it can specify which provider
 113      * should be loaded.</p>
 114      *
 115      * <p>Once an application has obtained a reference to a <code>
 116      * TransformerFactory</code> it can use the factory to configure
 117      * and obtain transformer instances.</p>
 118      *
 119      * <h2>Tip for Trouble-shooting</h2>
 120      * <p>Setting the <code>jaxp.debug</code> system property will cause
 121      * this method to print a lot of debug messages
 122      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 123      *
 124      * <p> If you have problems try:</p>
 125      * <pre>
 126      * java -Djaxp.debug=1 YourProgram ....
 127      * </pre>
 128      *
 129      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.
 130      *
 131      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 132      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 133      *
 134      * @return new TransformerFactory instance, never null.
 135      *
 136      * @throws TransformerFactoryConfigurationError
 137      *                    if <code>factoryClassName</code> is <code>null</code>, or
 138      *                   the factory class cannot be loaded, instantiated.
 139      *
 140      * @see #newInstance()
 141      *
 142      * @since 1.6
 143      */
 144     public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader)
 145         throws TransformerFactoryConfigurationError{
 146             //do not fallback if given classloader can't find the class, throw exception
 147             return (TransformerFactory) FactoryFinder.newInstance(factoryClassName, classLoader, 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      *
 329      * @param name The name of the attribute.
 330      * @param value The value of the attribute.
 331      *
 332      * @throws IllegalArgumentException When implementation does not
 333      *   recognize the attribute.
 334      */
 335     public abstract void setAttribute(String name, Object value);
 336 
 337     /**
 338      * Allows the user to retrieve specific attributes on the underlying
 339      * implementation.
 340      * An <code>IllegalArgumentException</code> is thrown if the underlying
 341      * implementation doesn't recognize the attribute.
 342      *
 343      * @param name The name of the attribute.
 344      *
 345      * @return value The value of the attribute.
 346      *
 347      * @throws IllegalArgumentException When implementation does not
 348      *   recognize the attribute.
 349      */
 350     public abstract Object getAttribute(String name);
 351 
 352     /**
 353      * Set the error event listener for the TransformerFactory, which
 354      * is used for the processing of transformation instructions,
 355      * and not for the transformation itself.
 356      * An <code>IllegalArgumentException</code> is thrown if the
 357      * <code>ErrorListener</code> listener is <code>null</code>.
 358      *
 359      * @param listener The new error listener.
 360      *
 361      * @throws IllegalArgumentException When <code>listener</code> is
 362      *   <code>null</code>
 363      */
 364     public abstract void setErrorListener(ErrorListener listener);
 365 
 366     /**
 367      * Get the error event handler for the TransformerFactory.
 368      *
 369      * @return The current error handler, which should never be null.
 370      */
 371     public abstract ErrorListener getErrorListener();
 372 
 373 }