October 8, 2001
Brought to you by:
Check It Out!
|
Package org.apache.xalan.xsltc
There are no Javadocs for this package, but let's hope Apache changes this soon. The package contains the interfaces and classes used and implemented by translets. Remember that translets are compiled XSL stylesheets. The primary interfaces and classes in this package are:
- Translet
- TransletOutputHandler
- TransletException
We will discuss each of these in this section.
Interface Translet
org.apache.xalan.xsltc
|
public interface Translet |
A class that implements interface Translet must be able to transform XML input into the output specified by the mapping in the original XSL stylesheet. The XSLTC library creates classes that implement this interface; you shouldn't ever need to write code that implements interface Translet. You will, however, need to call the transform() method to tell the implementing class when to begin the transformation process.
The transform() Method
|
public void transform(DOM Document,
TransletOutputHandler handler)
throws TransletException |
A transformation requires two items: a org.apache.xalan.xsltc.dom.DOMImpl object (which unfortunately carries no documentation!) and an object implementing the org.apache.xalan.xsltc.TransletOutputHandler interface. DOMImpl implements interface org.apache.xalan.xsltc.DOM. These two items are created in your application code and given to the translet.
Arguments
|
Arguments |
Type |
Effect |
|
Document |
org.apache.
xalan.xsltc.DOM |
The parsed XML input document to be transformed. It is a DOM tree implementing the DOM interface, and so is usually an instance of the DOM implementation class org.apache.xalan.xsltc.dom.DOMImpl |
|
handler |
org.apache.
xalan.xsltc.
Translet
OutputHandler |
The callback handler, which the translet uses to notify your application of transformed elements, attributes, and data. Conceptually very similar to SAX's org.xml.sax.DocumentHandler. |
Usage and Examples
To create an instance of a class which implements org.apache.xalan.xsltc.Translet, we use the Java reflection API:
Class cls = Class.forName("MyClass");
Translet xlet = (Translet)cls.newInstance();
xlet.tranform(dom, handler);
"MyClass" is the name of the class generated by XSLTC during compilation (we go over how to compile an XSL stylesheet in the section Example: Compiling and Using a Translet). xlet.transform() method can now be called to perform the transformation.
Interface TransletOutputHandler
org.apache.xalan.xsltc
|
public interface TransletOutputHandler |
This interface contains the callback methods which a translet calls as it transforms XML input to some output. Conceptually, a translet behaves just like a SAX parsing engine, calling back into interface org.apache.xalan.xsltc.TransletOutputHandler instead of interface org.xml.sax.DocumentHandler or org.xml.sax.ContentHandler. However, the designers of XSLTC have chosen natively to support TransletOutputHandler rather than SAX 1.0's DocumentHandler and SAX 2.0's ContentHandler.
Conceptually, a translet behaves just like a SAX parsing engine, calling back into interface org.apache.xalan.xsltc.TransletOutputHandler instead of interface org.xml.sax.DocumentHandler or org.xml.sax.ContentHandler.
SAX 1.0 is supported by wrapping a org.apache.xalan.xsltc.runtime.TextOutput object around an object implementing TransletOutputHandler. Since SAX is the de facto push parser standard, we'll focus on how to use it with translets rather than the proprietary TransletOutputHandler. However, let's briefly examine some of TransletOutputHandler to further understand how translets work.
Callback Methods
|
public void startDocument()
throws TransletException |
|
public void endDocument()
throws TransletException |
|
public void characters(char[] characters, int offset, int length)
throws TransletException |
|
public void startElement(String elementName)
throws TransletException |
|
public void endElement(String elementName)
throws TransletException |
|
public void attribute(String attributeName, String attributeValue)
throws TransletException |
|
public void comment(String comment)
throws TransletException |
|
public void processingInstruction(String target, String data)
throws TransletException |
Although this isn't complete, you should immediately see the similarities between this interface and SAX 1.0's org.xml.sax.DocumentHandler and SAX 2.0's org.xml.sax.ContentHandler.
A helper class is given to us to enable SAX 1.0 support. org.apache.xalan.xsltc.runtime
TextOutput not only implements interface TransletOutputHandler, but it also maps TransletOutputHandler methods to corresponding org.xml.sax.DocumentHandler methods.