FileDocCategorySizeDatePackage
Template.javaAPI DocJava SE 6 API9922Tue Jun 10 00:22:30 BST 2008com.sun.org.apache.xalan.internal.xsltc.compiler

Template

public final class Template extends TopLevelElement
author
Jacek Ambroziak
author
Santiago Pericas-Geertsen
author
Morten Jorgensen
author
Erwin Bolwidt

Fields Summary
private QName
_name
private QName
_mode
private Pattern
_pattern
private double
_priority
private int
_position
private boolean
_disabled
private boolean
_compiled
private boolean
_simplified
private boolean
_isSimpleNamedTemplate
private Vector
_parameters
private Stylesheet
_stylesheet
Constructors Summary
Methods Summary
public voidaddParameter(com.sun.org.apache.xalan.internal.xsltc.compiler.Param param)

    	_parameters.addElement(param);
    
public intcompareTo(java.lang.Object template)
Compare this template to another. First checks priority, then position.

	Template other = (Template)template;
	if (_priority > other._priority)
	    return 1;
	else if (_priority < other._priority)
	    return -1;
	else if (_position > other._position)
	    return 1;
	else if (_position < other._position)
	    return -1;
	else
	    return 0;
    
public voiddisable()

	_disabled = true;
    
public booleandisabled()

	return(_disabled);
    
public voiddisplay(int indent)

	Util.println('\n");
	indent(indent);
	if (_name != null) {
	    indent(indent);
	    Util.println("name = " + _name);
	}
	else if (_pattern != null) {
	    indent(indent);
	    Util.println("match = " + _pattern.toString());
	}
	if (_mode != null) {
	    indent(indent);
	    Util.println("mode = " + _mode);
	}
	displayContents(indent + IndentIncrement);
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.QNamegetModeName()

	return _mode;
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.QNamegetName()

	return _name;
    
public java.util.VectorgetParameters()

    	return _parameters;
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.PatterngetPattern()

	return _pattern;
    
public intgetPosition()

	return(_position);
    
public doublegetPriority()

	return _priority;
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.StylesheetgetStylesheet()


       
	return _stylesheet;
    
public booleanhasParams()

    
       
	return _parameters.size() > 0;
    
public booleanisNamed()

	return _name != null;
    
public booleanisSimpleNamedTemplate()

    	return _isSimpleNamedTemplate;
    
public booleanisSimplified()

	return(_simplified);
    
public voidparseContents(com.sun.org.apache.xalan.internal.xsltc.compiler.Parser parser)


	final String name     = getAttribute("name");
	final String mode     = getAttribute("mode");
	final String match    = getAttribute("match");
	final String priority = getAttribute("priority");

	_stylesheet = super.getStylesheet();

	if (name.length() > 0) {
            if (!XML11Char.isXML11ValidQName(name)) {
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
                parser.reportError(Constants.ERROR, err);           
            }                
	    _name = parser.getQNameIgnoreDefaultNs(name);
	}
	
	if (mode.length() > 0) {
            if (!XML11Char.isXML11ValidQName(mode)) {
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this);
                parser.reportError(Constants.ERROR, err);           
            } 		
	    _mode = parser.getQNameIgnoreDefaultNs(mode);
	}
	
	if (match.length() > 0) {
	    _pattern = parser.parsePattern(this, "match", null);
	}

	if (priority.length() > 0) {
	    _priority = Double.parseDouble(priority);
	}
	else {
	    if (_pattern != null)
		_priority = _pattern.getPriority();
	    else
		_priority = Double.NaN;
	}

	_position = parser.getTemplateIndex();

	// Add the (named) template to the symbol table
	if (_name != null) {
	    Template other = parser.getSymbolTable().addTemplate(this);
	    if (!resolveNamedTemplates(other, parser)) {
		ErrorMsg err =
		    new ErrorMsg(ErrorMsg.TEMPLATE_REDEF_ERR, _name, this);
		parser.reportError(Constants.ERROR, err);
	    }
	    // Is this a simple named template?
	    if (_pattern == null && _mode == null) {
	    	_isSimpleNamedTemplate = true;
	    }
	}

	if (_parent instanceof Stylesheet) {
	    ((Stylesheet)_parent).addTemplate(this);
	}
	
	parser.setTemplate(this);	// set current template
	parseChildren(parser);
	parser.setTemplate(null);	// clear template
    
public voidparseSimplified(com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet stylesheet, com.sun.org.apache.xalan.internal.xsltc.compiler.Parser parser)
When the parser realises that it is dealign with a simplified stylesheet it will create an empty Stylesheet object with the root element of the stylesheet (a LiteralElement object) as its only child. The Stylesheet object will then create this Template object and invoke this method to force some specific behaviour. What we need to do is: o) create a pattern matching on the root node o) add the LRE root node (the only child of the Stylesheet) as our only child node o) set the empty Stylesheet as our parent o) set this template as the Stylesheet's only child


	_stylesheet = stylesheet;
	setParent(stylesheet);

	_name = null;
	_mode = null;
	_priority = Double.NaN;
	_pattern = parser.parsePattern(this, "/");

	final Vector contents = _stylesheet.getContents();
	final SyntaxTreeNode root = (SyntaxTreeNode)contents.elementAt(0);

	if (root instanceof LiteralElement) {
	    addElement(root);
	    root.setParent(this);
	    contents.set(0, this);
	    parser.setTemplate(this);
	    root.parseContents(parser);
	    parser.setTemplate(null);
	}
    
private booleanresolveNamedTemplates(com.sun.org.apache.xalan.internal.xsltc.compiler.Template other, com.sun.org.apache.xalan.internal.xsltc.compiler.Parser parser)


	if (other == null) return true;

	SymbolTable stable = parser.getSymbolTable();

	final int us = this.getImportPrecedence();
	final int them = other.getImportPrecedence();

	if (us > them) {
	    other.disable();
	    return true;
	}
	else if (us < them) {
	    stable.addTemplate(other);
	    this.disable();
	    return true;
	}
	else {
	    return false;
	}
    
public voidsetName(com.sun.org.apache.xalan.internal.xsltc.compiler.QName qname)

	if (_name == null) _name = qname;
    
public voidsetSimplified()

	_simplified = true;
    
public voidtranslate(com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator classGen, com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator methodGen)

	final ConstantPoolGen cpg = classGen.getConstantPool();
	final InstructionList il = methodGen.getInstructionList();

	if (_disabled) return;
	// bug fix #4433133, add a call to named template from applyTemplates 
	String className = classGen.getClassName();

	if (_compiled && isNamed()){
	    String methodName = Util.escape(_name.toString());
	    il.append(classGen.loadTranslet());
	    il.append(methodGen.loadDOM());
	    il.append(methodGen.loadIterator());
	    il.append(methodGen.loadHandler()); 
	    il.append(methodGen.loadCurrentNode()); 
	    il.append(new INVOKEVIRTUAL(cpg.addMethodref(className,
							 methodName,
							 "("
							 + DOM_INTF_SIG
							 + NODE_ITERATOR_SIG
							 + TRANSLET_OUTPUT_SIG
							 + "I)V")));
	    return;
	}

	if (_compiled) return;
	_compiled = true; 
		
	// %OPT% Special handling for simple named templates.
	if (_isSimpleNamedTemplate && methodGen instanceof NamedMethodGenerator) {
	    int numParams = _parameters.size();
	    NamedMethodGenerator namedMethodGen = (NamedMethodGenerator)methodGen;
            
            // Update load/store instructions to access Params from the stack
	    for (int i = 0; i < numParams; i++) {
	    	Param param = (Param)_parameters.elementAt(i);
	    	param.setLoadInstruction(namedMethodGen.loadParameter(i));
	    	param.setStoreInstruction(namedMethodGen.storeParameter(i));
	    }
	}
        
        translateContents(classGen, methodGen);
	il.setPositions(true);
    
public com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypetypeCheck(com.sun.org.apache.xalan.internal.xsltc.compiler.SymbolTable stable)

	if (_pattern != null) {
	    _pattern.typeCheck(stable);
	}

	return typeCheckContents(stable);