📄 predicate.java
字号:
if (_nthPositionFilter) { SyntaxTreeNode parent = getParent(); _nthDescendant = (parent instanceof Step) && (parent.getParent() instanceof AbsoluteLocationPath); return _type = Type.NodeSet; } } // Reset optimization flags _nthPositionFilter = _nthDescendant = false; // Otherwise, expand [e] to [position() = e] final QName position = getParser().getQNameIgnoreDefaultNs("position"); final PositionCall positionCall = new PositionCall(position); positionCall.setParser(getParser()); positionCall.setParent(this); _exp = new EqualityExpr(Operators.EQ, positionCall, _exp); if (_exp.typeCheck(stable) != Type.Boolean) { _exp = new CastExpr(_exp, Type.Boolean); } return _type = Type.Boolean; } else { // All other types will be handled as boolean values if (texp instanceof BooleanType == false) { _exp = new CastExpr(_exp, Type.Boolean); } return _type = Type.Boolean; } } /** * Create a new "Filter" class implementing * <code>CurrentNodeListFilter</code>. Allocate registers for local * variables and local parameters passed in the closure to test(). * Notice that local variables need to be "unboxed". */ private void compileFilter(ClassGenerator classGen, MethodGenerator methodGen) { TestGenerator testGen; LocalVariableGen local; FilterGenerator filterGen; _className = getXSLTC().getHelperClassName(); filterGen = new FilterGenerator(_className, "java.lang.Object", toString(), ACC_PUBLIC | ACC_SUPER, new String[] { CURRENT_NODE_LIST_FILTER }, classGen.getStylesheet()); final ConstantPoolGen cpg = filterGen.getConstantPool(); final int length = (_closureVars == null) ? 0 : _closureVars.size(); // Add a new instance variable for each var in closure for (int i = 0; i < length; i++) { VariableBase var = ((VariableRefBase) _closureVars.get(i)).getVariable(); filterGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(var.getEscapedName()), cpg.addUtf8(var.getType().toSignature()), null, cpg.getConstantPool())); } final InstructionList il = new InstructionList(); testGen = new TestGenerator(ACC_PUBLIC | ACC_FINAL, com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN, new com.sun.org.apache.bcel.internal.generic.Type[] { com.sun.org.apache.bcel.internal.generic.Type.INT, com.sun.org.apache.bcel.internal.generic.Type.INT, com.sun.org.apache.bcel.internal.generic.Type.INT, com.sun.org.apache.bcel.internal.generic.Type.INT, Util.getJCRefType(TRANSLET_SIG), Util.getJCRefType(NODE_ITERATOR_SIG) }, new String[] { "node", "position", "last", "current", "translet", "iterator" }, "test", _className, il, cpg); // Store the dom in a local variable local = testGen.addLocalVariable("document", Util.getJCRefType(DOM_INTF_SIG), null, null); final String className = classGen.getClassName(); il.append(filterGen.loadTranslet()); il.append(new CHECKCAST(cpg.addClass(className))); il.append(new GETFIELD(cpg.addFieldref(className, DOM_FIELD, DOM_INTF_SIG))); il.append(new ASTORE(local.getIndex())); // Store the dom index in the test generator testGen.setDomIndex(local.getIndex()); _exp.translate(filterGen, testGen); il.append(IRETURN); testGen.stripAttributes(true); testGen.setMaxLocals(); testGen.setMaxStack(); testGen.removeNOPs(); filterGen.addEmptyConstructor(ACC_PUBLIC); filterGen.addMethod(testGen.getMethod()); getXSLTC().dumpClass(filterGen.getJavaClass()); } /** * Returns true if the predicate is a test for the existance of an * element or attribute. All we have to do is to get the first node * from the step, check if it is there, and then return true/false. */ public boolean isBooleanTest() { return (_exp instanceof BooleanExpr); } /** * Method to see if we can optimise the predicate by using a specialised * iterator for expressions like '/foo/bar[@attr = $var]', which are * very common in many stylesheets */ public boolean isNodeValueTest() { if (!_canOptimize) return false; return (getStep() != null && getCompareValue() != null); } /** * Returns the step in an expression of the form 'step = value'. * Null is returned if the expression is not of the right form. * Optimization if off if null is returned. */ public Step getStep() { // Returned cached value if called more than once if (_step != null) { return _step; } // Nothing to do if _exp is null if (_exp == null) { return null; } // Ignore if not equality expression if (_exp instanceof EqualityExpr) { EqualityExpr exp = (EqualityExpr)_exp; Expression left = exp.getLeft(); Expression right = exp.getRight(); // Unwrap and set _step if appropriate if (left instanceof CastExpr) { left = ((CastExpr) left).getExpr(); } if (left instanceof Step) { _step = (Step) left; } // Unwrap and set _step if appropriate if (right instanceof CastExpr) { right = ((CastExpr)right).getExpr(); } if (right instanceof Step) { _step = (Step)right; } } return _step; } /** * Returns the value in an expression of the form 'step = value'. * A value may be either a literal string or a variable whose * type is string. Optimization if off if null is returned. */ public Expression getCompareValue() { // Returned cached value if called more than once if (_value != null) { return _value; } // Nothing to to do if _exp is null if (_exp == null) { return null; } // Ignore if not an equality expression if (_exp instanceof EqualityExpr) { EqualityExpr exp = (EqualityExpr) _exp; Expression left = exp.getLeft(); Expression right = exp.getRight(); // Return if left is literal string if (left instanceof LiteralExpr) { _value = left; return _value; } // Return if left is a variable reference of type string if (left instanceof VariableRefBase && left.getType() == Type.String) { _value = left; return _value; } // Return if right is literal string if (right instanceof LiteralExpr) { _value = right; return _value; } // Return if left is a variable reference whose type is string if (right instanceof VariableRefBase && right.getType() == Type.String) { _value = right; return _value; } } return null; } /** * Translate a predicate expression. This translation pushes * two references on the stack: a reference to a newly created * filter object and a reference to the predicate's closure. */ public void translateFilter(ClassGenerator classGen, MethodGenerator methodGen) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); // Compile auxiliary class for filter compileFilter(classGen, methodGen); // Create new instance of filter il.append(new NEW(cpg.addClass(_className))); il.append(DUP); il.append(new INVOKESPECIAL(cpg.addMethodref(_className, "<init>", "()V"))); // Initialize closure variables final int length = (_closureVars == null) ? 0 : _closureVars.size(); for (int i = 0; i < length; i++) { VariableRefBase varRef = (VariableRefBase) _closureVars.get(i); VariableBase var = varRef.getVariable(); Type varType = var.getType(); il.append(DUP); // Find nearest closure implemented as an inner class Closure variableClosure = _parentClosure; while (variableClosure != null) { if (variableClosure.inInnerClass()) break; variableClosure = variableClosure.getParentClosure(); } // Use getfield if in an inner class if (variableClosure != null) { il.append(ALOAD_0); il.append(new GETFIELD( cpg.addFieldref(variableClosure.getInnerClassName(), var.getEscapedName(), varType.toSignature()))); } else { // Use a load of instruction if in translet class il.append(var.loadInstruction()); } // Store variable in new closure il.append(new PUTFIELD( cpg.addFieldref(_className, var.getEscapedName(), varType.toSignature()))); } } /** * Translate a predicate expression. If non of the optimizations apply * then this translation pushes two references on the stack: a reference * to a newly created filter object and a reference to the predicate's * closure. See class <code>Step</code> for further details. */ public void translate(ClassGenerator classGen, MethodGenerator methodGen) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); if (_nthPositionFilter || _nthDescendant) { _exp.translate(classGen, methodGen); } else if (isNodeValueTest() && (getParent() instanceof Step)) { _value.translate(classGen, methodGen); il.append(new CHECKCAST(cpg.addClass(STRING_CLASS))); il.append(new PUSH(cpg, ((EqualityExpr)_exp).getOp())); } else { translateFilter(classGen, methodGen); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -