final String[] argNames = new String[3];
argNames[0] = DOCUMENT_PNAME;
argNames[1] = ITERATOR_PNAME;
argNames[2] = TRANSLET_OUTPUT_PNAME;
final InstructionList mainIL = new InstructionList();
final MethodGenerator methodGen =
new MethodGenerator(ACC_PUBLIC | ACC_FINAL,
org.apache.bcel.generic.Type.VOID,
argTypes, argNames, functionName()+'_'+max,
getClassName(), mainIL,
classGen.getConstantPool());
methodGen.addException("org.apache.xalan.xsltc.TransletException");
// Create the local variable to hold the current node
final LocalVariableGen current;
current = methodGen.addLocalVariable2("current",
org.apache.bcel.generic.Type.INT,
mainIL.getEnd());
_currentIndex = current.getIndex();
// Create the "body" instruction list that will eventually hold the
// code for the entire method (other ILs will be appended).
final InstructionList body = new InstructionList();
body.append(NOP);
// Create an instruction list that contains the default next-node
// iteration
final InstructionList ilLoop = new InstructionList();
ilLoop.append(methodGen.loadIterator());
ilLoop.append(methodGen.nextNode());
ilLoop.append(DUP);
ilLoop.append(new ISTORE(_currentIndex));
// The body of this code can get very large - large than can be handled
// by a single IFNE(body.getStart()) instruction - need workaround:
final BranchHandle ifeq = ilLoop.append(new IFEQ(null));
final BranchHandle loop = ilLoop.append(new GOTO_W(null));
ifeq.setTarget(ilLoop.append(RETURN)); // applyTemplates() ends here!
final InstructionHandle ihLoop = ilLoop.getStart();
// Compile default handling of elements (traverse children)
InstructionList ilRecurse =
compileDefaultRecursion(classGen, methodGen, ihLoop);
InstructionHandle ihRecurse = ilRecurse.getStart();
// Compile default handling of text/attribute nodes (output text)
InstructionList ilText =
compileDefaultText(classGen, methodGen, ihLoop);
InstructionHandle ihText = ilText.getStart();
// Distinguish attribute/element/namespace tests for further processing
final int[] types = new int[DOM.NTYPES + names.size()];
for (int i = 0; i < types.length; i++) {
types[i] = i;
}
final boolean[] isAttribute = new boolean[types.length];
final boolean[] isNamespace = new boolean[types.length];
for (int i = 0; i < names.size(); i++) {
final String name = (String)names.elementAt(i);
isAttribute[i+DOM.NTYPES] = isAttributeName(name);
isNamespace[i+DOM.NTYPES] = isNamespaceName(name);
}
// Compile all templates - regardless of pattern type
compileTemplateCalls(classGen, methodGen, ihLoop, min, max);
// Handle template with explicit "*" pattern
final TestSeq elemTest = _testSeq[DOM.ELEMENT];
InstructionHandle ihElem = ihRecurse;
if (elemTest != null) {
ihElem = elemTest.compile(classGen, methodGen, ihLoop);
}
// Handle template with explicit "@*" pattern
final TestSeq attrTest = _testSeq[DOM.ATTRIBUTE];
InstructionHandle ihAttr = ihLoop;
if (attrTest != null) {
ihAttr = attrTest.compile(classGen, methodGen, ihAttr);
}
// Do tests for id() and key() patterns first
InstructionList ilKey = null;
if (_idxTestSeq != null) {
loop.setTarget(_idxTestSeq.compile(classGen, methodGen, body.getStart()));
ilKey = _idxTestSeq.getInstructionList();
}
else {
loop.setTarget(body.getStart());
}
// If there is a match on node() we need to replace ihElem
// and ihText if the priority of node() is higher
if (_childNodeTestSeq != null) {
// Compare priorities of node() and "*"
double nodePrio = _childNodeTestSeq.getPriority();
int nodePos = _childNodeTestSeq.getPosition();
double elemPrio = (0 - Double.MAX_VALUE);
int elemPos = Integer.MIN_VALUE;
if (elemTest != null) {
elemPrio = elemTest.getPriority();
elemPos = elemTest.getPosition();
}
if (elemPrio == Double.NaN || elemPrio < nodePrio ||
(elemPrio == nodePrio && elemPos < nodePos))
{
ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
}
// Compare priorities of node() and text()
final TestSeq textTest = _testSeq[DOM.TEXT];
double textPrio = (0 - Double.MAX_VALUE);
int textPos = Integer.MIN_VALUE;
if (textTest != null) {
textPrio = textTest.getPriority();
textPos = textTest.getPosition();
}
if (textPrio == Double.NaN || textPrio < nodePrio ||
(textPrio == nodePrio && textPos < nodePos))
{
ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
_testSeq[DOM.TEXT] = _childNodeTestSeq;
}
}
// Handle templates with "ns:*" pattern
InstructionHandle elemNamespaceHandle = ihElem;
InstructionList nsElem = compileNamespaces(classGen, methodGen,
isNamespace, isAttribute,
false, ihElem);
if (nsElem != null) elemNamespaceHandle = nsElem.getStart();
// Handle templates with "ns:@*" pattern
InstructionList nsAttr = compileNamespaces(classGen, methodGen,
isNamespace, isAttribute,
true, ihAttr);
InstructionHandle attrNamespaceHandle = ihAttr;
if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();
// Handle templates with "ns:elem" or "ns:@attr" pattern
final InstructionHandle[] targets = new InstructionHandle[types.length];
for (int i = DOM.NTYPES; i < targets.length; i++) {
final TestSeq testSeq = _testSeq[i];