JAXP XSLT Design
Now that we have seen some example code and have begun our exploration
of the Transformer class, let's step back and look at
the overall design of the XSLT plugability layer. JAXP support for
XSLT is broken down into the packages listed in
Table 5-2.
Table 5-2:
JAXP transformation packages
|
Package
|
Description
|
|
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. Provides
implementations of Source and Result:
DOMSource and DOMResult.
|
|
javax.xml.transform.sax
|
Supports SAX2 transformations. Defines SAX versions of
Source and Result: SAXSource
and SAXResult. 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. This
package mostly consists of interfaces and abstract classes, except
for OutputKeys and a few exception and error classes.
Figure 5-2 presents a UML
class diagram that shows all of the pieces in this important package.
Figure 5-2.
javax.xml.transform class diagram
|
|
As you can see, this is a small package, indicative of the fact that
JAXP is merely a wrapper around the tools that actually perform
transformations. The entry point is TransformerFactory,
which creates instances of Transformer, as we have
already seen, as well as instances of the Templates
abstract class. A Templates object represents a compiled
stylesheet and will be covered in detail later in this chapter.
[3]
The advantage of compilation is performance: the same
Templates object can be used over and over by many
threads without reparsing the XSLT file.
The URIResolver is responsible for resolving URIs found
within stylesheets and is generally something you will not need to
deal with directly. It is used when a stylesheet imports or includes
another document, and the processor needs to figure out where to look
for that document. For example:
<xsl:import href="commonFooter.xslt"/>
ErrorListener, as you may guess, is an interface that
allows your code to register as a listener for error conditions. This
interface defines the following three methods:
void error(TransformerException ex)
void fatalError(TransformerException ex)
void warning(TransformerException ex)
The TransformerException has the ability to wrap around
another Exception or Throwable object and
may return an instance of the SourceLocator class. If the
underlying XSLT implementation does not provide a
SourceLocator, null is returned. The
SourceLocator interface defines methods to locate
where a TransformerException originated. In the case
of error() and warning(), the XSLT
processor is required to continue processing the document until
the end. For fatalError(), on the other hand, the
XSLT processor is not required to continue. If you do not register
an ErrorListener object, then all errors, fatal errors,
and warnings are normally written to System.err.
TransformerFactoryConfigurationError and
TransformerConfigurationException round out the
error-handling APIs for JAXP, indicating problems configuring
the underlying XSLT processor implementation. The
TransformerFactoryConfigurationError class is
generally used when the implementation class cannot be found
on the CLASSPATH or cannot be instantiated at all.
TransformerConfigurationException simply
indicates a "serious configuration error" according to
its documentation.
Footnote:
3.
The exact definition of a "compiled" stylesheet is vague. XSLT processors are free to optimize cached stylesheets however they see fit.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.