📄 step.java
字号:
/* * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: Step.java,v 1.6 2006/06/06 22:34:34 spericas Exp $ */package com.sun.org.apache.xalan.internal.xsltc.compiler;import java.util.Vector;import com.sun.org.apache.bcel.internal.generic.ALOAD;import com.sun.org.apache.bcel.internal.generic.ASTORE;import com.sun.org.apache.bcel.internal.generic.CHECKCAST;import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;import com.sun.org.apache.bcel.internal.generic.ICONST;import com.sun.org.apache.bcel.internal.generic.ILOAD;import com.sun.org.apache.bcel.internal.generic.ISTORE;import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;import com.sun.org.apache.bcel.internal.generic.InstructionList;import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;import com.sun.org.apache.bcel.internal.generic.NEW;import com.sun.org.apache.bcel.internal.generic.PUSH;import com.sun.org.apache.xalan.internal.xsltc.DOM;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;import com.sun.org.apache.xml.internal.dtm.Axis;import com.sun.org.apache.xml.internal.dtm.DTM;/** * @author Jacek Ambroziak * @author Santiago Pericas-Geertsen * @author Morten Jorgensen */final class Step extends RelativeLocationPath { /** * This step's axis as defined in class Axis. */ private int _axis; /** * A vector of predicates (filters) defined on this step - may be null */ private Vector _predicates; /** * Some simple predicates can be handled by this class (and not by the * Predicate class) and will be removed from the above vector as they are * handled. We use this boolean to remember if we did have any predicates. */ private boolean _hadPredicates = false; /** * Type of the node test. */ private int _nodeType; public Step(int axis, int nodeType, Vector predicates) { _axis = axis; _nodeType = nodeType; _predicates = predicates; } /** * Set the parser for this element and all child predicates */ public void setParser(Parser parser) { super.setParser(parser); if (_predicates != null) { final int n = _predicates.size(); for (int i = 0; i < n; i++) { final Predicate exp = (Predicate)_predicates.elementAt(i); exp.setParser(parser); exp.setParent(this); } } } /** * Define the axis (defined in Axis class) for this step */ public int getAxis() { return _axis; } /** * Get the axis (defined in Axis class) for this step */ public void setAxis(int axis) { _axis = axis; } /** * Returns the node-type for this step */ public int getNodeType() { return _nodeType; } /** * Returns the vector containing all predicates for this step. */ public Vector getPredicates() { return _predicates; } /** * Returns the vector containing all predicates for this step. */ public void addPredicates(Vector predicates) { if (_predicates == null) { _predicates = predicates; } else { _predicates.addAll(predicates); } } /** * Returns 'true' if this step has a parent pattern. * This method will return 'false' if this step occurs on its own under * an element like <xsl:for-each> or <xsl:apply-templates>. */ private boolean hasParentPattern() { final SyntaxTreeNode parent = getParent(); return (parent instanceof ParentPattern || parent instanceof ParentLocationPath || parent instanceof UnionPathExpr || parent instanceof FilterParentPath); } /** * Returns 'true' if this step has a parent location path. */ private boolean hasParentLocationPath() { return getParent() instanceof ParentLocationPath; } /** * Returns 'true' if this step has any predicates */ private boolean hasPredicates() { return _predicates != null && _predicates.size() > 0; } /** * Returns 'true' if this step is used within a predicate */ private boolean isPredicate() { SyntaxTreeNode parent = this; while (parent != null) { parent = parent.getParent(); if (parent instanceof Predicate) return true; } return false; } /** * True if this step is the abbreviated step '.' */ public boolean isAbbreviatedDot() { return _nodeType == NodeTest.ANODE && _axis == Axis.SELF; } /** * True if this step is the abbreviated step '..' */ public boolean isAbbreviatedDDot() { return _nodeType == NodeTest.ANODE && _axis == Axis.PARENT; } /** * Type check this step. The abbreviated steps '.' and '@attr' are * assigned type node if they have no predicates. All other steps * have type node-set. */ public Type typeCheck(SymbolTable stable) throws TypeCheckError { // Save this value for later - important for testing for special // combinations of steps and patterns than can be optimised _hadPredicates = hasPredicates(); // Special case for '.' // in the case where '.' has a context such as book/. // or .[false()] we can not optimize the nodeset to a single node. if (isAbbreviatedDot()) { _type = (hasParentPattern() || hasPredicates() || hasParentLocationPath()) ? Type.NodeSet : Type.Node; } else { _type = Type.NodeSet; } // Type check all predicates (expressions applied to the step) if (_predicates != null) { final int n = _predicates.size(); for (int i = 0; i < n; i++) { final Expression pred = (Expression)_predicates.elementAt(i); pred.typeCheck(stable); } } // Return either Type.Node or Type.NodeSet return _type; } /** * Translate a step by pushing the appropriate iterator onto the stack. * The abbreviated steps '.' and '@attr' do not create new iterators * if they are not part of a LocationPath and have no filters. * In these cases a node index instead of an iterator is pushed * onto the stack. */ public void translate(ClassGenerator classGen, MethodGenerator methodGen) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); if (hasPredicates()) { translatePredicates(classGen, methodGen); } else { int star = 0; String name = null; final XSLTC xsltc = getParser().getXSLTC(); if (_nodeType >= DTM.NTYPES) { final Vector ni = xsltc.getNamesIndex(); name = (String)ni.elementAt(_nodeType-DTM.NTYPES); star = name.lastIndexOf('*'); } // If it is an attribute, but not '@*', '@pre:*' or '@node()', // and has no parent if (_axis == Axis.ATTRIBUTE && _nodeType != NodeTest.ATTRIBUTE && _nodeType != NodeTest.ANODE && !hasParentPattern() && star == 0) { int iter = cpg.addInterfaceMethodref(DOM_INTF, "getTypedAxisIterator", "(II)"+NODE_ITERATOR_SIG); il.append(methodGen.loadDOM()); il.append(new PUSH(cpg, Axis.ATTRIBUTE)); il.append(new PUSH(cpg, _nodeType));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -