FileDocCategorySizeDatePackage
TemplatesHandlerImpl.javaAPI DocJava SE 5 API10516Fri Aug 26 14:55:40 BST 2005com.sun.org.apache.xalan.internal.xsltc.trax

TemplatesHandlerImpl

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

Fields Summary
private String
_systemId
System ID for this stylesheet.
private int
_indentNumber
Number of spaces to add for output indentation.
private URIResolver
_uriResolver
This URIResolver is passed to all Transformers.
private TransformerFactoryImpl
_tfactory
A reference to the transformer factory that this templates object belongs to.
private Parser
_parser
A reference to XSLTC's parser object.
private TemplatesImpl
_templates
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.

        _parser.endDocument();

        // 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");
            }
            xsltc.setClassName(transletName);

            // 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);
                stylesheet.setSystemId(_systemId);
                stylesheet.setParentStylesheet(null);

                // Set a document loader (for xsl:include/import) if defined
                if (_uriResolver != null) {
                    stylesheet.setSourceLoader(this);
                }

                _parser.setCurrentStylesheet(stylesheet);

                // Set it as top-level in the XSLTC object
                xsltc.setStylesheet(stylesheet);

                // Create AST under the Stylesheet element
                _parser.createAST(stylesheet);
            }

            // Generate the bytecodes and output the translet class(es)
            if (!_parser.errorsFound() && stylesheet != null) {
                stylesheet.setMultiDocument(xsltc.isMultiDocument());
                stylesheet.setHasIdCall(xsltc.hasIdCall());

                // Class synchronization is needed for BCEL
                synchronized (xsltc.getClass()) {
                    stylesheet.translate();
                }
            }

            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) {
                        _templates.setURIResolver(_uriResolver);
                    }
                }
            }
            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)
                        errorMessage.append('\n");
                    errorMessage.append(errors.elementAt(i).toString());
                }
                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.

        _parser.endPrefixMapping(prefix);
    
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.

return
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.

return
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, com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC 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.

param
href The URI of the document to load
param
context The URI of the currently loaded document
param
xsltc The compiler that resuests the document
return
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.

        setSystemId(locator.getSystemId());
        _parser.setDocumentLocator(locator);
    
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.

param
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.

        _parser.skippedEntity(name);
    
public voidstartDocument()
Re-initialize parser and forward SAX2 event.

        XSLTC xsltc = _parser.getXSLTC();
        xsltc.init();   // calls _parser.init()
        xsltc.setOutputType(XSLTC.BYTEARRAY_OUTPUT);
        _parser.startDocument();
    
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);