IBM Version 5 Universal Remote User Manual


 
Chapter 3. Processing XML 67
Studying Example 3-6, notice that JAXP can read directly from a File object, so
three files objects are created for the source XML, the XSLT stylesheet, and the
target HTML file. The Source interface is used to read both the XML file and the
XSLT file. The Source interface can have many implementations. In this example,
we are using StreamSource, which has the ability to read a File object, an
InputStream, a Reader, or a system ID. Later we will examine additional Source
implementations that use SAX and DOM as input. Just like Source, Result is an
interface that can have several implementations. In this example, a StreamResult
sends the output of the transformations to a target file.
Next an instance of TransformerFactory is created. The TransformerFactory is
responsible for creating Transformer and Template objects. In our example, we
create a Transformer object. Transformer objects are not thread-safe, although
they can be used multiple times. For a simple example like this, we will not
encounter any problems. In a threaded servlet environment, however, multiple
users cannot concurrently access the same TransformerFactory instance. JAXP
also provides a templates interface, which represents a stylesheet that can be
accessed by many concurrent threads.
The transformer instance is then used to perform the actual transformation. It
applies the XSLT stylesheet to the XML data, sending the result to the target file.
XSLT support packages in JAXP
JAXP support for XSLT is broken down into the packages listed below:
javax.xml.transform: defines a general purpose API for XML
transformations without any dependencies on SAX or DOM. The Transformer
class is obtained from the TransformerFactory class. The Transformer
transforms from a source to a result.
javax.xml.transform.dom: defines how transformations can be performed
using DOM. It also provides implementations of Source and Result:
DOMSource and DOMResult.
javax.xml.transform.sax: supports SAX2 transformations. It defines SAX
versions of Source and Result: SAXSource and SAXResult. It also defines a
subclass of TransformerFactory that allows SAX2 events to be fed into an
XSLT processor.
javax.xml.transform.stream: defines I/O stream implementations of Source
and Result: StreamSource and StreamResult.
The heart of JAXP XSLT support lies in the javax.xml.transform package,
which lays out the mechanics and overall process for any transformation that is
performed.