1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import org.apache.bcel.generic.BranchHandle; 26 import org.apache.bcel.generic.ConstantPoolGen; 27 import org.apache.bcel.generic.GOTO; 28 import org.apache.bcel.generic.IFGT; 29 import org.apache.bcel.generic.InstructionHandle; 30 import org.apache.bcel.generic.InstructionList; 31 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 32 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 33 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 34 import org.apache.xalan.xsltc.compiler.util.NodeSetType; 35 import org.apache.xalan.xsltc.compiler.util.NodeType; 36 import org.apache.xalan.xsltc.compiler.util.ReferenceType; 37 import org.apache.xalan.xsltc.compiler.util.ResultTreeType; 38 import org.apache.xalan.xsltc.compiler.util.Type; 39 import org.apache.xalan.xsltc.compiler.util.TypeCheckError; 40 import org.apache.xalan.xsltc.compiler.util.Util; 41 42 47 final class ForEach extends Instruction { 48 49 private Expression _select; 50 private Type _type; 51 52 public void display(int indent) { 53 indent(indent); 54 Util.println("ForEach"); 55 indent(indent + IndentIncrement); 56 Util.println("select " + _select.toString()); 57 displayContents(indent + IndentIncrement); 58 } 59 60 public void parseContents(Parser parser) { 61 _select = parser.parseExpression(this, "select", null); 62 63 parseChildren(parser); 64 65 if (_select.isDummy()) { 67 reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select"); 68 } 69 } 70 71 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 72 _type = _select.typeCheck(stable); 73 74 if (_type instanceof ReferenceType || _type instanceof NodeType) { 75 _select = new CastExpr(_select, Type.NodeSet); 76 typeCheckContents(stable); 77 return Type.Void; 78 } 79 if (_type instanceof NodeSetType||_type instanceof ResultTreeType) { 80 typeCheckContents(stable); 81 return Type.Void; 82 } 83 throw new TypeCheckError(this); 84 } 85 86 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 87 final ConstantPoolGen cpg = classGen.getConstantPool(); 88 final InstructionList il = methodGen.getInstructionList(); 89 90 il.append(methodGen.loadCurrentNode()); 92 il.append(methodGen.loadIterator()); 93 94 final Vector sortObjects = new Vector (); 96 Enumeration children = elements(); 97 while (children.hasMoreElements()) { 98 final Object child = children.nextElement(); 99 if (child instanceof Sort) { 100 sortObjects.addElement(child); 101 } 102 } 103 104 if ((_type != null) && (_type instanceof ResultTreeType)) { 105 il.append(methodGen.loadDOM()); 107 108 if (sortObjects.size() > 0) { 110 ErrorMsg msg = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this); 111 getParser().reportError(WARNING, msg); 112 } 113 114 _select.translate(classGen, methodGen); 116 _type.translateTo(classGen, methodGen, Type.NodeSet); 118 il.append(SWAP); 120 il.append(methodGen.storeDOM()); 121 } 122 else { 123 if (sortObjects.size() > 0) { 125 Sort.translateSortIterator(classGen, methodGen, 126 _select, sortObjects); 127 } 128 else { 129 _select.translate(classGen, methodGen); 130 } 131 132 if (_type instanceof ReferenceType == false) { 133 il.append(methodGen.loadContextNode()); 134 il.append(methodGen.setStartNode()); 135 } 136 } 137 138 139 il.append(methodGen.storeIterator()); 141 142 initializeVariables(classGen, methodGen); 144 145 final BranchHandle nextNode = il.append(new GOTO(null)); 146 final InstructionHandle loop = il.append(NOP); 147 148 translateContents(classGen, methodGen); 149 150 nextNode.setTarget(il.append(methodGen.loadIterator())); 151 il.append(methodGen.nextNode()); 152 il.append(DUP); 153 il.append(methodGen.storeCurrentNode()); 154 il.append(new IFGT(loop)); 155 156 if ((_type != null) && (_type instanceof ResultTreeType)) { 158 il.append(methodGen.storeDOM()); 159 } 160 161 il.append(methodGen.storeIterator()); 163 il.append(methodGen.storeCurrentNode()); 164 } 165 166 182 public void initializeVariables(ClassGenerator classGen, 183 MethodGenerator methodGen) { 184 final int n = elementCount(); 185 for (int i = 0; i < n; i++) { 186 final Object child = getContents().elementAt(i); 187 if (child instanceof Variable) { 188 Variable var = (Variable)child; 189 var.initialize(classGen, methodGen); 190 } 191 } 192 } 193 194 } 195 | Popular Tags |