📄 mode.java
字号:
for (int i = 0; i < patterns.size(); i++) { final LocationPathPattern lppToCompare = (LocationPathPattern)patterns.elementAt(i); if (pattern.noSmallerThan(lppToCompare)) { inserted = true; patterns.insertElementAt(pattern, i); break; } } if (inserted == false) { patterns.addElement(pattern); } } } /** * Complete test sequences of a given type by adding all patterns * from a given group. */ private void completeTestSequences(int nodeType, Vector patterns) { if (patterns != null) { if (_patternGroups[nodeType] == null) { _patternGroups[nodeType] = patterns; } else { final int m = patterns.size(); for (int j = 0; j < m; j++) { addPattern(nodeType, (LocationPathPattern) patterns.elementAt(j)); } } } } /** * Build test sequences. The first step is to complete the test sequences * by including patterns of "*" and "node()" kernel to all element test * sequences, and of "@*" to all attribute test sequences. */ private void prepareTestSequences() { final Vector starGroup = _patternGroups[DTM.ELEMENT_NODE]; final Vector atStarGroup = _patternGroups[DTM.ATTRIBUTE_NODE]; // Complete test sequence for "text()" with "child::node()" completeTestSequences(DTM.TEXT_NODE, _childNodeGroup); // Complete test sequence for "*" with "child::node()" completeTestSequences(DTM.ELEMENT_NODE, _childNodeGroup); // Complete test sequence for "pi()" with "child::node()" completeTestSequences(DTM.PROCESSING_INSTRUCTION_NODE, _childNodeGroup); // Complete test sequence for "comment()" with "child::node()" completeTestSequences(DTM.COMMENT_NODE, _childNodeGroup); // Complete test sequence for "@*" with "attribute::node()" completeTestSequences(DTM.ATTRIBUTE_NODE, _attribNodeGroup); final Vector names = _stylesheet.getXSLTC().getNamesIndex(); if (starGroup != null || atStarGroup != null || _childNodeGroup != null || _attribNodeGroup != null) { final int n = _patternGroups.length; // Complete test sequence for user-defined types for (int i = DTM.NTYPES; i < n; i++) { if (_patternGroups[i] == null) continue; final String name = (String) names.elementAt(i - DTM.NTYPES); if (isAttributeName(name)) { // If an attribute then copy "@*" to its test sequence completeTestSequences(i, atStarGroup); // And also copy "attribute::node()" to its test sequence completeTestSequences(i, _attribNodeGroup); } else { // If an element then copy "*" to its test sequence completeTestSequences(i, starGroup); // And also copy "child::node()" to its test sequence completeTestSequences(i, _childNodeGroup); } } } _testSeq = new TestSeq[DTM.NTYPES + names.size()]; final int n = _patternGroups.length; for (int i = 0; i < n; i++) { final Vector patterns = _patternGroups[i]; if (patterns != null) { final TestSeq testSeq = new TestSeq(patterns, i, this);// System.out.println("testSeq[" + i + "] = " + testSeq); testSeq.reduce(); _testSeq[i] = testSeq; testSeq.findTemplates(_neededTemplates); } } if (_childNodeGroup != null && _childNodeGroup.size() > 0) { _childNodeTestSeq = new TestSeq(_childNodeGroup, -1, this); _childNodeTestSeq.reduce(); _childNodeTestSeq.findTemplates(_neededTemplates); }/* if (_attribNodeGroup != null && _attribNodeGroup.size() > 0) { _attribNodeTestSeq = new TestSeq(_attribNodeGroup, -1, this); _attribNodeTestSeq.reduce(); _attribNodeTestSeq.findTemplates(_neededTemplates); }*/ if (_idxGroup != null && _idxGroup.size() > 0) { _idxTestSeq = new TestSeq(_idxGroup, this); _idxTestSeq.reduce(); _idxTestSeq.findTemplates(_neededTemplates); } if (_rootPattern != null) { // doesn't matter what is 'put', only key matters _neededTemplates.put(_rootPattern.getTemplate(), this); } } private void compileNamedTemplate(Template template, ClassGenerator classGen) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = new InstructionList(); String methodName = Util.escape(template.getName().toString()); int numParams = 0; if (template.isSimpleNamedTemplate()) { Vector parameters = template.getParameters(); numParams = parameters.size(); } // Initialize the types and names arrays for the NamedMethodGenerator. com.sun.org.apache.bcel.internal.generic.Type[] types = new com.sun.org.apache.bcel.internal.generic.Type[4 + numParams]; String[] names = new String[4 + numParams]; types[0] = Util.getJCRefType(DOM_INTF_SIG); types[1] = Util.getJCRefType(NODE_ITERATOR_SIG); types[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG); types[3] = com.sun.org.apache.bcel.internal.generic.Type.INT; names[0] = DOCUMENT_PNAME; names[1] = ITERATOR_PNAME; names[2] = TRANSLET_OUTPUT_PNAME; names[3] = NODE_PNAME; // For simple named templates, the signature of the generated method // is not fixed. It depends on the number of parameters declared in the // template. for (int i = 4; i < 4 + numParams; i++) { types[i] = Util.getJCRefType(OBJECT_SIG); names[i] = "param" + String.valueOf(i-4); } NamedMethodGenerator methodGen = new NamedMethodGenerator(ACC_PUBLIC, com.sun.org.apache.bcel.internal.generic.Type.VOID, types, names, methodName, getClassName(), il, cpg); il.append(template.compile(classGen, methodGen)); il.append(RETURN); methodGen.stripAttributes(true); methodGen.setMaxLocals(); methodGen.setMaxStack(); methodGen.removeNOPs(); classGen.addMethod(methodGen.getMethod()); } private void compileTemplates(ClassGenerator classGen, MethodGenerator methodGen, InstructionHandle next) { Enumeration templates = _namedTemplates.keys(); while (templates.hasMoreElements()) { final Template template = (Template)templates.nextElement(); compileNamedTemplate(template, classGen); } templates = _neededTemplates.keys(); while (templates.hasMoreElements()) { final Template template = (Template)templates.nextElement(); if (template.hasContents()) { // !!! TODO templates both named and matched InstructionList til = template.compile(classGen, methodGen); til.append(new GOTO_W(next)); _templateILs.put(template, til); _templateIHs.put(template, til.getStart()); } else { // empty template _templateIHs.put(template, next); } } } private void appendTemplateCode(InstructionList body) { final Enumeration templates = _neededTemplates.keys(); while (templates.hasMoreElements()) { final Object iList = _templateILs.get(templates.nextElement()); if (iList != null) { body.append((InstructionList)iList); } } } private void appendTestSequences(InstructionList body) { final int n = _testSeq.length; for (int i = 0; i < n; i++) { final TestSeq testSeq = _testSeq[i]; if (testSeq != null) { InstructionList il = testSeq.getInstructionList(); if (il != null) body.append(il); // else trivial TestSeq } } } public static void compileGetChildren(ClassGenerator classGen, MethodGenerator methodGen, int node) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); final int git = cpg.addInterfaceMethodref(DOM_INTF, GET_CHILDREN, GET_CHILDREN_SIG); il.append(methodGen.loadDOM()); il.append(new ILOAD(node)); il.append(new INVOKEINTERFACE(git, 2)); } /** * Compiles the default handling for DOM elements: traverse all children */ private InstructionList compileDefaultRecursion(ClassGenerator classGen, MethodGenerator methodGen, InstructionHandle next) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = new InstructionList(); final String applyTemplatesSig = classGen.getApplyTemplatesSig(); final int git = cpg.addInterfaceMethodref(DOM_INTF, GET_CHILDREN, GET_CHILDREN_SIG); final int applyTemplates = cpg.addMethodref(getClassName(), functionName(), applyTemplatesSig); il.append(classGen.loadTranslet()); il.append(methodGen.loadDOM()); il.append(methodGen.loadDOM()); il.append(new ILOAD(_currentIndex)); il.append(new INVOKEINTERFACE(git, 2)); il.append(methodGen.loadHandler()); il.append(new INVOKEVIRTUAL(applyTemplates)); il.append(new GOTO_W(next)); return il; } /** * Compiles the default action for DOM text nodes and attribute nodes: * output the node's text value */ private InstructionList compileDefaultText(ClassGenerator classGen, MethodGenerator methodGen, InstructionHandle next) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = new InstructionList(); final int chars = cpg.addInterfaceMethodref(DOM_INTF, CHARACTERS, CHARACTERS_SIG); il.append(methodGen.loadDOM()); il.append(new ILOAD(_currentIndex)); il.append(methodGen.loadHandler()); il.append(new INVOKEINTERFACE(chars, 3)); il.append(new GOTO_W(next)); return il; } private InstructionList compileNamespaces(ClassGenerator classGen, MethodGenerator methodGen, boolean[] isNamespace, boolean[] isAttribute, boolean attrFlag, InstructionHandle defaultTarget) { final XSLTC xsltc = classGen.getParser().getXSLTC(); final ConstantPoolGen cpg = classGen.getConstantPool(); // Append switch() statement - namespace test dispatch loop final Vector namespaces = xsltc.getNamespaceIndex(); final Vector names = xsltc.getNamesIndex(); final int namespaceCount = namespaces.size() + 1; final int namesCount = names.size(); final InstructionList il = new InstructionList(); final int[] types = new int[namespaceCount]; final InstructionHandle[] targets = new InstructionHandle[types.length]; if (namespaceCount > 0) { boolean compiled = false; // Initialize targets for namespace() switch statement for (int i = 0; i < namespaceCount; i++) { targets[i] = defaultTarget; types[i] = i; } // Add test sequences for known namespace types for (int i = DTM.NTYPES; i < (DTM.NTYPES+namesCount); i++) { if ((isNamespace[i]) && (isAttribute[i] == attrFlag)) { String name = (String)names.elementAt(i-DTM.NTYPES); String namespace = name.substring(0,name.lastIndexOf(':')); final int type = xsltc.registerNamespace(namespace); if ((i < _testSeq.length) && (_testSeq[i] != null)) { targets[type] = (_testSeq[i]).compile(classGen, methodGen, defaultTarget); compiled = true; } } } // Return "null" if no test sequences were compiled if (!compiled) return(null); // Append first code in applyTemplates() - get type of current node final int getNS = cpg.addInterfaceMethodref(DOM_INTF, "getNamespaceType", "(I)I"); il.append(methodGen.loadDOM()); il.append(new ILOAD(_currentIndex)); il.append(new INVOKEINTERFACE(getNS, 2)); il.append(new SWITCH(types, targets, defaultTarget)); return(il); } else { return(null); } } /** * Compiles the applyTemplates() method and adds it to the translet. * This is the main dispatch method. */ public void compileApplyTemplates(ClassGenerator classGen) { final XSLTC xsltc = classGen.getParser().getXSLTC(); final ConstantPoolGen cpg = classGen.getConstantPool(); final Vector names = xsltc.getNamesIndex(); // Create the applyTemplates() method final com.sun.org.apache.bcel.internal.generic.Type[] argTypes = new com.sun.org.apache.bcel.internal.generic.Type[3]; argTypes[0] = Util.getJCRefType(DOM_INTF_SIG); argTypes[1] = Util.getJCRefType(NODE_ITERATOR_SIG); argTypes[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG); 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, com.sun.org.apache.bcel.internal.generic.Type.VOID, argTypes, argNames, functionName(), getClassName(), mainIL, classGen.getConstantPool());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -