ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

XML Processing with TRaX
Pages: 1, 2


Java and XML

Java and XML
By Brett McLaughlin
1st Edition June 2000
0-596-00016-2, Order Number: 0162
498 pages, $39.95

Basic TRaX Pieces The Interfaces

One of the main reasons the TRaX API is so clean and simple is the Interface-driven approach to design. The highest-level interfaces define the essential entities that are being modeled, and the interactions are left to the implementations. The interfaces themselves aren't very interesting. They are essentially marker interfaces.



public interface Source {

    public void setSystemId(String systemId);
    public String getSystemId();
}

Source

Implementations of the Source interface provide access to the XML document to be processed. TRaX defines Source implementations for DOM trees (DOMSource); SAX 2.0 InputSources (SAXSource); and Java InputStreams, Readers and any of their derived classes (StreamSource).

public interface Result {

    public void setSystemId(String systemId);
    public String getSystemId();
}

Result

Implementations of the Result interface provide access to the transformed XML document. TRaX defines Result implementations for DOM trees (DOMResult); SAX 2.0 ContentHandlers (SAXResult); and Java OutputStreams, Writers and any of their derived classes (StreamResult).

public interface Templates {
    Transformer newTransformer() throws 
TransformerConfigurationException;
    Properties getOutputProperties();
}

Templates

A template implementation is the optimized, in-memory representation of an XML transformation that is processed and ready to be executed. Templates objects are safe to use in concurrent threads. To reuse a single Template instance in multiple concurrent threads, multiple Transformer instances would have to be created via the Templates.newTransformer() factory method. Each Transformer instance may be used completely independently in concurrent threads, and both the Templates and the Transformer instances can be reused for subsequent transformations.

Related article:

JDOM and TRaX -- Two innovative technologies have recently been announced to the XML developer community: JDOM, a Java-specific DOM; and TRaX, an API for XML transformations. [from XML.com]

Comment on this articleWhat are your thoughts on TRaX?
Post your comments

Basic TRaX Pieces - The Abstract Classes

Transformer

Resources

Xalan-J 2 Design Document, Scott Boag, included in Xalan2 distribution

Xalan-j TRaX Fundamentals, Apache Software Foundation

JSR 63, Java Specifcication Request JavaTM API for XML Processing 1.1

DOM level 2 Core Specification, W3C

SAX 2.0: The Simple API for XML, David Megginson

A Transformer is the object that actually applies the transformation to the source document and creates the result document. However, it is not responsible for outputting, or serializing, the result of the transformation. This is the responsibility of the transformation engine's serializer and this behavior can be modified via the setOutputProperty(java.lang.String name, java.lang.String value) method. The configurable OutputProperties are defined in the OutputKeys class, and are described in the XSLT 1.0 Specification. Transformers are immutable, they cannot change which Templates instance gets applied to the Source.

TransformerFactory

The TransformerFactory is primarily responsible for creating new Transformers and Templates objects. New instances of Transformer are created via the static newTransformer() method. Processing Source instances into Templates objects is handled by the newTemplates(Source source) method.

Xalan History

Xalan 1 started off as the LotusXSL project at Lotus Corporation. Lotus contributed the code to the Apache Jakarta Project to create and maintain Xalan, Apache's XSLT engine. Lotus employees are still heavily involved in the Xalan project and currently, they are the primary developers. This is consistent with their parent company's (IBM) commitment to open source software through IBM's donations of their XML4J XML Parser (better known as Xerces Java) and their LOG4J logging package to the Apache Project.

Xalan 2 is complete refactoring of Xalan 1. The goals of the refactoring are to create an more easily understandable and maintainable code base through a more modular design approach. Consequently, the API for executing transformations is completely different in Xalan 2 from Xalan 1. Xalan 2 does provide a compatibility package to allow existing applications to move to Xalan 2 with no code changes.

Conclusion

JAXP 1.0 unified Java-based XML development efforts by providing a single, powerful, interface to all participating vendors' XML parsers. The addition of TRaX to JAXP 1.1 is a natural and necessary addition to ensure the success of XML on the Java platform.