Xalan 1 Implementation
The complete code for the Xalan implementation is listed in
Example 5-1. As comments
in the code indicate, this code was developed and tested using
Xalan 1.2.2, which is not the most recent XSLT processor from Apache.
Fully qualified Java class names, such as
org.apache.xalan.xslt.XSLTProcessor, are used for all
Xalan-specific code.
TIP:
A Xalan 2 example is not shown here because Xalan 2 is compatible
with Sun's JAXP. The JAXP version of this program works with Xalan 2,
as well as any other JAXP compatible processor.
Example 5-1:
SimpleXalan1.java
package chap5;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import org.xml.sax.SAXException;
/**
* A simple demo of Xalan 1. This code was originally written
* using Xalan 1.2.2. It will not work with Xalan 2.
*/
public class SimpleXalan1 {
/**
* Accept two command line arguments: the name of an XML file,
* and the name of an XSLT stylesheet. The result of the
* transformation is written to stdout.
*/
public static void main(String[] args)
throws MalformedURLException, SAXException {
if (args.length != 2) {
System.err.println("Usage:");
System.err.println(" java " + SimpleXalan1.class.getName( )
+ " xmlFileName xsltFileName");
System.exit(1);
}
String xmlFileName = args[0];
String xsltFileName = args[1];
String xmlSystemId =
new File(xmlFileName).toURL().toExternalForm( );
String xsltSystemId =
new File(xsltFileName).toURL().toExternalForm( );
org.apache.xalan.xslt.XSLTProcessor processor =
org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor( );
org.apache.xalan.xslt.XSLTInputSource xmlInputSource =
new org.apache.xalan.xslt.XSLTInputSource(xmlSystemId);
org.apache.xalan.xslt.XSLTInputSource xsltInputSource =
new org.apache.xalan.xslt.XSLTInputSource(xsltSystemId);
org.apache.xalan.xslt.XSLTResultTarget resultTree =
new org.apache.xalan.xslt.XSLTResultTarget(System.out);
processor.process(xmlInputSource, xsltInputSource, resultTree);
}
}
Note: Color coded lines were broken for display purposes only. In
use they should appear on one line.
The code begins with the usual list of imports and the class
declaration, followed by a simple check to ensure that two
command line arguments are provided. If all is OK, then the
XML file name and XSLT file name are converted into system
identifier values:
String xmlSystemId =
new File(xmlFileName).toURL().toExternalForm( );
String xsltSystemId =
new File(xsltFileName).toURL().toExternalForm( );
Note: Color coded lines were broken for display purposes only. In
use they should appear on one line.
System identifiers are part of the XML specification and really mean
the same thing as a Uniform Resource Identifier (URI). A Uniform
Resource Locator (URL) is a specific type of URI and can be used
for methods that require system identifiers as parameters. From a
Java programming perspective, this means that a platform-specific
filename such as C:/data/simple.xml needs to be converted
to file:///C:/data/simple.xml before it can be used by most
XML APIs. The code shown here does the conversion and will work on
Unix, Windows, and other platforms supported by Java. Although you
could try to manually prepend the filename with the literal string
file:///, that may not result in portable code. The
documentation for java.io.File clearly states that its
toURL( ) method generates a system-dependent URL, so
the results will vary when the same code is executed on a non-Windows
platform. In fact, on Windows the code actually produces a nonstandard
URL (with a single slash), although it does work within Java programs:
file:/C:/data/simple.xml.
Now that we have system identifiers for our two input files, an
instance of the XSLT processor is created:
org.apache.xalan.xslt.XSLTProcessor processor =
org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor( );
XSLTProcessor is an interface, and
XSLTProcessorFactory is a factory for creating new
instances of classes that implement it. Because Xalan is open source
software, it is easy enough to determine that XSLTEngineImpl
is the class that implements the XSLTProcessor interface,
although you should try to avoid code that depends on the specific
implementation.
The next few lines of code create XSLTInputSource
objects, one for the XML file and another for the XSLT file:
org.apache.xalan.xslt.XSLTInputSource xmlInputSource =
new org.apache.xalan.xslt.XSLTInputSource(xmlSystemId);
org.apache.xalan.xslt.XSLTInputSource xsltInputSource =
new org.apache.xalan.xslt.XSLTInputSource(xsltSystemId);
XSLTInputSource is a subclass of
org.xml.sax.InputSource, adding the ability to read
directly from a DOM Node. XSLTInputSource
has the ability to read XML or XSLT data from a system ID,
java.io.InputStream, java.io.Reader,
org.w3c.dom.Node, or an existing InputSource.
As shown in the code, the source of the data is specified in the
constructor. XSLTInputSource also has a
no-arg constructor, along with get/set methods for
each of the supported data source types.
An instance of XSLTResultTarget is created next, sending
the result of the transformation to System.out:
org.apache.xalan.xslt.XSLTResultTarget resultTree =
new org.apache.xalan.xslt.XSLTResultTarget(System.out);
In a manner similar to XSLTInputSource, the
XSLTResultTarget can also be wrapped around an instance
of org.w3c.dom.Node, an OutputStream or
Writer, a filename (not a system ID!), or an instance of
org.xml.sax.DocumentHandler.
The final line of code simply instructs the processor to perform the
transformation:
processor.process(xmlInputSource, xsltInputSource, resultTree);
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.