1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler; 21 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 26 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE; 27 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL; 28 import com.sun.org.apache.bcel.internal.generic.InstructionList; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType; 35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType; 36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util; 39 import com.sun.org.apache.xml.internal.utils.XMLChar; 40 41 45 final class ApplyTemplates extends Instruction { 46 private Expression _select; 47 private Type _type = null; 48 private QName _modeName; 49 private String _functionName; 50 51 public void display(int indent) { 52 indent(indent); 53 Util.println("ApplyTemplates"); 54 indent(indent + IndentIncrement); 55 Util.println("select " + _select.toString()); 56 if (_modeName != null) { 57 indent(indent + IndentIncrement); 58 Util.println("mode " + _modeName); 59 } 60 } 61 62 public boolean hasWithParams() { 63 return hasContents(); 64 } 65 66 public void parseContents(Parser parser) { 67 final String select = getAttribute("select"); 68 final String mode = getAttribute("mode"); 69 70 if (select.length() > 0) { 71 _select = parser.parseExpression(this, "select", null); 72 73 } 74 75 if (mode.length() > 0) { 76 if (!XMLChar.isValidQName(mode)) { 77 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this); 78 parser.reportError(Constants.ERROR, err); 79 } 80 _modeName = parser.getQNameIgnoreDefaultNs(mode); 81 } 82 83 _functionName = 85 parser.getTopLevelStylesheet().getMode(_modeName).functionName(); 86 parseChildren(parser); } 88 89 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 90 if (_select != null) { 91 _type = _select.typeCheck(stable); 92 if (_type instanceof NodeType || _type instanceof ReferenceType) { 93 _select = new CastExpr(_select, Type.NodeSet); 94 _type = Type.NodeSet; 95 } 96 if (_type instanceof NodeSetType||_type instanceof ResultTreeType) { 97 typeCheckContents(stable); return Type.Void; 99 } 100 throw new TypeCheckError(this); 101 } 102 else { 103 typeCheckContents(stable); return Type.Void; 105 } 106 } 107 108 112 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 113 boolean setStartNodeCalled = false; 114 final Stylesheet stylesheet = classGen.getStylesheet(); 115 final ConstantPoolGen cpg = classGen.getConstantPool(); 116 final InstructionList il = methodGen.getInstructionList(); 117 final int current = methodGen.getLocalIndex("current"); 118 119 final Vector sortObjects = new Vector (); 121 final Enumeration children = elements(); 122 while (children.hasMoreElements()) { 123 final Object child = children.nextElement(); 124 if (child instanceof Sort) { 125 sortObjects.addElement(child); 126 } 127 } 128 129 if (stylesheet.hasLocalParams() || hasContents()) { 131 il.append(classGen.loadTranslet()); 132 final int pushFrame = cpg.addMethodref(TRANSLET_CLASS, 133 PUSH_PARAM_FRAME, 134 PUSH_PARAM_FRAME_SIG); 135 il.append(new INVOKEVIRTUAL(pushFrame)); 136 translateContents(classGen, methodGen); 138 } 139 140 141 il.append(classGen.loadTranslet()); 142 143 if ((_type != null) && (_type instanceof ResultTreeType)) { 145 if (sortObjects.size() > 0) { 147 ErrorMsg err = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this); 148 getParser().reportError(WARNING, err); 149 } 150 _select.translate(classGen, methodGen); 152 _type.translateTo(classGen, methodGen, Type.NodeSet); 154 } 155 else { 156 il.append(methodGen.loadDOM()); 157 158 if (sortObjects.size() > 0) { 160 Sort.translateSortIterator(classGen, methodGen, 161 _select, sortObjects); 162 int setStartNode = cpg.addInterfaceMethodref(NODE_ITERATOR, 163 SET_START_NODE, 164 "(I)"+ 165 NODE_ITERATOR_SIG); 166 il.append(methodGen.loadCurrentNode()); 167 il.append(new INVOKEINTERFACE(setStartNode,2)); 168 setStartNodeCalled = true; 169 } 170 else { 171 if (_select == null) 172 Mode.compileGetChildren(classGen, methodGen, current); 173 else 174 _select.translate(classGen, methodGen); 175 } 176 } 177 178 if (_select != null && !setStartNodeCalled) { 179 _select.startIterator(classGen, methodGen); 180 } 181 182 final String className = classGen.getStylesheet().getClassName(); 184 il.append(methodGen.loadHandler()); 185 final String applyTemplatesSig = classGen.getApplyTemplatesSig(); 186 final int applyTemplates = cpg.addMethodref(className, 187 _functionName, 188 applyTemplatesSig); 189 il.append(new INVOKEVIRTUAL(applyTemplates)); 190 191 if (stylesheet.hasLocalParams() || hasContents()) { 193 il.append(classGen.loadTranslet()); 194 final int popFrame = cpg.addMethodref(TRANSLET_CLASS, 195 POP_PARAM_FRAME, 196 POP_PARAM_FRAME_SIG); 197 il.append(new INVOKEVIRTUAL(popFrame)); 198 } 199 } 200 } 201 | Popular Tags |