1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import java.util.Vector ; 23 24 import org.apache.bcel.generic.ConstantPoolGen; 25 import org.apache.bcel.generic.INVOKEINTERFACE; 26 import org.apache.bcel.generic.INVOKESPECIAL; 27 import org.apache.bcel.generic.INVOKESTATIC; 28 import org.apache.bcel.generic.InstructionList; 29 import org.apache.bcel.generic.NEW; 30 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 31 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 32 import org.apache.xalan.xsltc.compiler.util.NodeSetType; 33 import org.apache.xalan.xsltc.compiler.util.ReferenceType; 34 import org.apache.xalan.xsltc.compiler.util.Type; 35 import org.apache.xalan.xsltc.compiler.util.TypeCheckError; 36 37 42 class FilterExpr extends Expression { 43 44 47 private Expression _primary; 48 49 52 private final Vector _predicates; 53 54 public FilterExpr(Expression primary, Vector predicates) { 55 _primary = primary; 56 _predicates = predicates; 57 primary.setParent(this); 58 } 59 60 protected Expression getExpr() { 61 if (_primary instanceof CastExpr) 62 return ((CastExpr)_primary).getExpr(); 63 else 64 return _primary; 65 } 66 67 public void setParser(Parser parser) { 68 super.setParser(parser); 69 _primary.setParser(parser); 70 if (_predicates != null) { 71 final int n = _predicates.size(); 72 for (int i = 0; i < n; i++) { 73 final Expression exp = (Expression)_predicates.elementAt(i); 74 exp.setParser(parser); 75 exp.setParent(this); 76 } 77 } 78 } 79 80 public String toString() { 81 return "filter-expr(" + _primary + ", " + _predicates + ")"; 82 } 83 84 91 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 92 Type ptype = _primary.typeCheck(stable); 93 94 if (ptype instanceof NodeSetType == false) { 95 if (ptype instanceof ReferenceType) { 96 _primary = new CastExpr(_primary, Type.NodeSet); 97 } 98 else { 99 throw new TypeCheckError(this); 100 } 101 } 102 103 int n = _predicates.size(); 105 for (int i = 0; i < n; i++) { 106 Predicate pred = (Predicate) _predicates.elementAt(i); 107 pred.dontOptimize(); 108 pred.typeCheck(stable); 109 } 110 return _type = Type.NodeSet; 111 } 112 113 117 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 118 if (_predicates.size() > 0) { 119 translatePredicates(classGen, methodGen); 120 } 121 else { 122 _primary.translate(classGen, methodGen); 123 } 124 } 125 126 132 public void translatePredicates(ClassGenerator classGen, 133 MethodGenerator methodGen) { 134 final ConstantPoolGen cpg = classGen.getConstantPool(); 135 final InstructionList il = methodGen.getInstructionList(); 136 137 if (_predicates.size() == 0) { 139 translate(classGen, methodGen); 140 } 141 else { 142 final int initCNLI = cpg.addMethodref(CURRENT_NODE_LIST_ITERATOR, 144 "<init>", 145 "("+NODE_ITERATOR_SIG+"Z"+ 146 CURRENT_NODE_LIST_FILTER_SIG + 147 NODE_SIG+TRANSLET_SIG+")V"); 148 149 Predicate predicate = (Predicate)_predicates.lastElement(); 150 _predicates.remove(predicate); 151 152 il.append(new NEW(cpg.addClass(CURRENT_NODE_LIST_ITERATOR))); 154 il.append(DUP); 155 156 translatePredicates(classGen, methodGen); 158 159 il.append(ICONST_1); 161 predicate.translate(classGen, methodGen); 162 il.append(methodGen.loadCurrentNode()); 163 il.append(classGen.loadTranslet()); 164 il.append(new INVOKESPECIAL(initCNLI)); 165 } 166 } 167 } 168 | Popular Tags |