TemplatesHandlerImpl.javaAPI DocJava SE 5 API10516Fri Aug 26 14:55:40 BST


public class TemplatesHandlerImpl extends Object implements TemplatesHandler, ContentHandler, SourceLoader
Implementation of a JAXP1.1 TemplatesHandler
Morten Jorgensen
Santiago Pericas-Geertsen

Fields Summary
private String
System ID for this stylesheet.
private int
Number of spaces to add for output indentation.
private URIResolver
This URIResolver is passed to all Transformers.
private TransformerFactoryImpl
A reference to the transformer factory that this templates object belongs to.
private Parser
A reference to XSLTC's parser object.
private TemplatesImpl
The created Templates object.
Constructors Summary
protected TemplatesHandlerImpl(int indentNumber, TransformerFactoryImpl tfactory)
Default constructor

	_indentNumber = indentNumber;
	_tfactory = tfactory;

        // Instantiate XSLTC and get reference to parser object
        _parser = new XSLTC().getParser();
Methods Summary
public voidcharacters(char[] ch, int start, int length)
Just forward SAX2 event to parser object.

        _parser.characters(ch, start, length);
public voidendDocument()
Just forward SAX2 event to parser object.


        // create the templates
        try {
            XSLTC xsltc = _parser.getXSLTC();

            // Set the translet class name if not already set
            String transletName = null;
            if (_systemId != null) {
                transletName = Util.baseName(_systemId);
            else {
                transletName = (String)_tfactory.getAttribute("translet-name");

            // Get java-legal class name from XSLTC module
            transletName = xsltc.getClassName();

            Stylesheet stylesheet = null;
            SyntaxTreeNode root = _parser.getDocumentRoot();

            // Compile the translet - this is where the work is done!
            if (!_parser.errorsFound() && root != null) {
                // Create a Stylesheet element from the root node
                stylesheet = _parser.makeStylesheet(root);

                // Set a document loader (for xsl:include/import) if defined
                if (_uriResolver != null) {


                // Set it as top-level in the XSLTC object

                // Create AST under the Stylesheet element

            // Generate the bytecodes and output the translet class(es)
            if (!_parser.errorsFound() && stylesheet != null) {

                // Class synchronization is needed for BCEL
                synchronized (xsltc.getClass()) {

            if (!_parser.errorsFound()) {
                // Check that the transformation went well before returning
                final byte[][] bytecodes = xsltc.getBytecodes();
                if (bytecodes != null) {
                    _templates =
                    new TemplatesImpl(xsltc.getBytecodes(), transletName,
                        _parser.getOutputProperties(), _indentNumber, _tfactory);

                    // Set URIResolver on templates object
                    if (_uriResolver != null) {
            else {
                StringBuffer errorMessage = new StringBuffer();
                Vector errors = _parser.getErrors();
                final int count = errors.size();
                for (int i = 0; i < count; i++) {
                    if (errorMessage.length() > 0)
                throw new SAXException(ErrorMsg.JAXP_COMPILE_ERR, new TransformerException(errorMessage.toString()));
        catch (CompilerException e) {
            throw new SAXException(ErrorMsg.JAXP_COMPILE_ERR, e);
public voidendElement(java.lang.String uri, java.lang.String localname, java.lang.String qname)
Just forward SAX2 event to parser object.

        _parser.endElement(uri, localname, qname);
public voidendPrefixMapping(java.lang.String prefix)
Just forward SAX2 event to parser object.

public java.lang.StringgetSystemId()
Implements javax.xml.transform.sax.TemplatesHandler.getSystemId() Get the base ID (URI or system ID) from where relative URLs will be resolved.

The systemID that was set with setSystemId(String id)

	return _systemId;
public javax.xml.transform.TemplatesgetTemplates()
Implements javax.xml.transform.sax.TemplatesHandler.getTemplates() When a TemplatesHandler object is used as a ContentHandler or DocumentHandler for the parsing of transformation instructions, it creates a Templates object, which the caller can get once the SAX events have been completed.

The Templates object that was created during the SAX event process, or null if no Templates object has been created.

        return _templates;
public voidignorableWhitespace(char[] ch, int start, int length)
Just forward SAX2 event to parser object.

        _parser.ignorableWhitespace(ch, start, length);
public org.xml.sax.InputSourceloadSource(java.lang.String href, java.lang.String context, xsltc)
This method implements XSLTC's SourceLoader interface. It is used to glue a TrAX URIResolver to the XSLTC compiler's Input and Import classes.

href The URI of the document to load
context The URI of the currently loaded document
xsltc The compiler that resuests the document
An InputSource with the loaded document

	try {
	    // A _uriResolver must be set if this method is called
	    final Source source = _uriResolver.resolve(href, context);
	    if (source != null) {
		return Util.getInputSource(xsltc, source);
	catch (TransformerException e) {
	    // Falls through
	return null;
public voidprocessingInstruction(java.lang.String name, java.lang.String value)
Just forward SAX2 event to parser object.

        _parser.processingInstruction(name, value);
public voidsetDocumentLocator(org.xml.sax.Locator locator)
Set internal system Id and forward SAX2 event to parser object.

public voidsetSystemId(java.lang.String id)
Implements javax.xml.transform.sax.TemplatesHandler.setSystemId() Get the base ID (URI or system ID) from where relative URLs will be resolved.

id Base URI for this stylesheet

	_systemId = id;
public voidsetURIResolver(javax.xml.transform.URIResolver resolver)
Store URIResolver needed for Transformers.

	_uriResolver = resolver;
public voidskippedEntity(java.lang.String name)
Just forward SAX2 event to parser object.

public voidstartDocument()
Re-initialize parser and forward SAX2 event.

        XSLTC xsltc = _parser.getXSLTC();
        xsltc.init();   // calls _parser.init()
public voidstartElement(java.lang.String uri, java.lang.String localname, java.lang.String qname, org.xml.sax.Attributes attributes)
Just forward SAX2 event to parser object.

        _parser.startElement(uri, localname, qname, attributes);
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)
Just forward SAX2 event to parser object.

        _parser.startPrefixMapping(prefix, uri);