TransformerFactory


TransformerFactory javax.xml.transform

Java 1.4

An instance of this abstract class represents a document "transformation engine" such as an XSLT processor. A transformerFactory is used to create TRansformer objects that perform document transformations, and can also be used to process transformation instructions (such as XSLT stylesheets) into compiled Templates objects.

Obtain a transformerFactory instance by calling the static newInstance( ) method. newInstance( ) returns an instance of the default implementation for your Java installation, or, if the system property javax.xml.transform.TransformerFactory is set, then it returns an instance of the implementation class named by that property. The default TRansformerFactory implementation provided with the Java distribution transforms XML documents using XSL stylesheets.

You can configure a transformerFactory instance by calling setErrorListener( ) and setURIResolver( ) to specify an ErrorListener object and a URIResolver object to be used by the factory when reading and parsing XSL stylesheets. The setAttribute( ) and getAttribute( ) methods can be used to set and query implementation-dependent attributes of the transformation engine. The default engine supplied by Sun does not define any attributes. The getFeature( ) method is used to test whether the factory supports a given feature. For uniqueness, feature names are expressed as URIs, and each of the Source and Result implementations defined in the three subpackages of this package define a FEATURE constant that specifies a URL that you can use to test whether a transformerFactory supports that particular Source or Result type.

Once you have obtained and configured your TRansformerFactory object, you can use it in several ways. If you call the newtransformer( ) method that takes no arguments, you'll obtain a transformer object that transforms the format or representation of an XML document without transforming its content. For example, you could use a transformer created in this way to transform a DOM tree (represented by a javax.xml.transform.dom.DOMSource object) to a stream of XML text stored in a file named by a javax.xml.transform.stream.StreamResult .

Another way to use a transformerFactory is to call the newTemplates( ) method, passing in a Source object that represents an XSL stylesheet. This produces a Templates object, which you can use to obtain a transformer object that applies the stylesheet to transform document content. Alternatively, if you do not plan to create more than one transformer object from the Templates object, you can combine the two steps and simply pass the Source object representing the stylesheet to the one-argument version of newtransformer( ) .

XML documents may include references to XSL stylesheets in the form of an xml-stylesheet processing instruction. The getAssociatedStylesheet( ) method reads the XML document represented by a Source object and returns a new Source object that represents the stylesheet (or the concatenation of all the stylesheets) contained in that document that match the media, title, and charset constraints defined by the other three parameters (which may be null). If you want to process an XML document using the stylesheet that it defines itself, use this method to obtain a Source object that you can pass to newtransformer( ) to create the transformer object that you can use to transform the document.

transformerFactory implementations are not typically threadsafe.

 public abstract class  TransformerFactory  {  // Protected Constructors  protected  TransformerFactory  ( );  // Public Class Methods  public static TransformerFactory  newInstance  ( )          throws TransformerFactoryConfigurationError;  // Public Instance Methods  public abstract Source  getAssociatedStylesheet  (Source  source  , String  media  ,          String  title  , String  charset  )          throws TransformerConfigurationException;        public abstract Object  getAttribute  (String  name  );        public abstract ErrorListener  getErrorListener  ( );        public abstract boolean  getFeature  (String  name  );        public abstract URIResolver  getURIResolver  ( );        public abstract Templates  newTemplates  (Source  source  )          throws TransformerConfigurationException;        public abstract Transformer  newTransformer  ( ) throws TransformerConfigurationException;        public abstract Transformer  newTransformer  (Source  source  )          throws TransformerConfigurationException;        public abstract void  setAttribute  (String  name  , Object  value  );        public abstract void  setErrorListener  (ErrorListener  listener  );  5.0  public abstract void  setFeature  (String  name  , boolean  value  )          throws TransformerConfigurationException;        public abstract void  setURIResolver  (URIResolver  resolver  );   } 

Subclasses

javax.xml.transform.sax.SAXTransformerFactory



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net