//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2018 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon.expr; import net.sf.saxon.Configuration; import net.sf.saxon.expr.parser.*; import net.sf.saxon.om.*; import net.sf.saxon.pattern.*; import net.sf.saxon.trace.ExpressionPresenter; import net.sf.saxon.trans.XPathException; import net.sf.saxon.tree.iter.AxisIterator; import net.sf.saxon.tree.util.FastStringBuffer; import net.sf.saxon.type.*; import net.sf.saxon.value.Cardinality; import net.sf.saxon.z.IntHashSet; import net.sf.saxon.z.IntIterator; import net.sf.saxon.z.IntSet; import java.util.*; /** * An AxisExpression is always obtained by simplifying a PathExpression. * It represents a PathExpression that starts at the context node, and uses * a simple node-test with no filters. For example "*", "title", "./item", * "@*", or "ancestor::chapter*". *
An AxisExpression delivers nodes in axis order (not in document order). * To get nodes in document order, in the case of a reverse axis, the expression * should be wrapped in a call on reverse().
*/ public final class AxisExpression extends Expression { private byte axis; /*@Nullable*/ private NodeTest test; /*@Nullable*/ private ItemType itemType = null; private ContextItemStaticInfo staticInfo = ContextItemStaticInfo.DEFAULT; private int computedCardinality = -1; private boolean doneTypeCheck = false; private boolean doneOptimize = false; /** * Constructor for an AxisExpression whose origin is the context item * * @param axis The axis to be used in this AxisExpression: relevant constants are defined * in class {@link net.sf.saxon.om.AxisInfo}. * @param nodeTest The conditions to be satisfied by selected nodes. May be null, * indicating that any node on the axis is acceptable * @see net.sf.saxon.om.AxisInfo */ public AxisExpression(byte axis, /*@Nullable*/ NodeTest nodeTest) { this.axis = axis; this.test = nodeTest; } /** * Set the axis * * @param axis the new axis */ public void setAxis(byte axis) { this.axis = axis; } /** * Get a name identifying the kind of expression, in terms meaningful to a user. * * @return a name identifying the kind of expression, in terms meaningful to a user. * The name will always be in the form of a lexical XML QName, and should match the name used * in explain() output displaying the expression. */ public String getExpressionName() { return "axisStep"; } /** * Simplify an expression */ /*@NotNull*/ public Expression simplify() throws XPathException { Expression e2 = super.simplify(); if (e2 != this) { return e2; } if ((test == null || test == AnyNodeTest.getInstance()) && (axis == AxisInfo.PARENT || axis == AxisInfo.ANCESTOR)) { // get more precise type information for parent/ancestor nodes test = MultipleNodeKindTest.PARENT_NODE; } return this; } /** * Type-check the expression */ /*@NotNull*/ public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { ItemType contextItemType = contextInfo.getItemType(); boolean noWarnings = doneOptimize || (doneTypeCheck && this.staticInfo.getItemType().equals(contextItemType)); doneTypeCheck = true; if (contextItemType == ErrorType.getInstance()) { // There is no context item. In principle we could raise XPTY0020 ("Context item is not a node"), // which is a type error and therefore can be thrown statically. But many test cases expect // XPDY0002 ("Context item absent") which for inexplicable reasons is a dynamic error rather than // a type error, and therefore cannot be raised until execution time. XPathException err = new XPathException("Axis step " + this + " cannot be used here: the context item is absent"); //err.setIsTypeError(true); err.setErrorCode("XPDY0002"); err.setLocation(getLocation()); throw err; } else { staticInfo = contextInfo; } Configuration config = visitor.getConfiguration(); if (contextItemType.getGenre() != Genre.NODE) { TypeHierarchy th = config.getTypeHierarchy(); int relation = th.relationship(contextItemType, AnyNodeTest.getInstance()); if (relation == TypeHierarchy.DISJOINT) { XPathException err = new XPathException("Axis step " + this + " cannot be used here: the context item is not a node"); err.setIsTypeError(true); err.setErrorCode("XPTY0020"); err.setLocation(getLocation()); throw err; } else if (relation == TypeHierarchy.OVERLAPS || relation == TypeHierarchy.SUBSUMES) { // need to insert a dynamic check of the context item type Expression thisExp = checkPlausibility(visitor, contextInfo, !noWarnings); if (Literal.isEmptySequence(thisExp)) { return thisExp; } ContextItemExpression exp = new ContextItemExpression(); ExpressionTool.copyLocationInfo(this, exp); RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.AXIS_STEP, "", axis); role.setErrorCode("XPTY0020"); ItemChecker checker = new ItemChecker(exp, AnyNodeTest.getInstance(), role); ExpressionTool.copyLocationInfo(this, checker); SimpleStepExpression step = new SimpleStepExpression(checker, thisExp); ExpressionTool.copyLocationInfo(this, step); return step; } } if (visitor.getStaticContext().getOptimizerOptions().isSet(OptimizerOptions.VOID_EXPRESSIONS)) { return checkPlausibility(visitor, contextInfo, !noWarnings); } else { return this; } } private Expression checkPlausibility(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, boolean warnings) throws XPathException { StaticContext env = visitor.getStaticContext(); Configuration config = env.getConfiguration(); ItemType contextType = contextInfo.getItemType(); if (!(contextType instanceof NodeTest)) { contextType = AnyNodeTest.getInstance(); } // New code in terms of UTypes // Test whether the requested nodetest is consistent with the requested axis if (test != null && !AxisInfo.getTargetUType(UType.ANY_NODE, axis).overlaps(test.getUType())) { if (warnings) { visitor.issueWarning("The " + AxisInfo.axisName[axis] + " axis will never select " + test.getUType().toStringWithIndefiniteArticle(), getLocation()); } return Literal.makeEmptySequence(); } if (test instanceof NameTest && axis == AxisInfo.NAMESPACE && !((NameTest) test).getNamespaceURI().isEmpty()) { if (warnings) { visitor.issueWarning("The names of namespace nodes are never prefixed, so this axis step will never select anything", getLocation()); } return Literal.makeEmptySequence(); } // Test whether the axis ever selects anything, when starting at this context node UType originUType = contextType.getUType(); UType targetUType = AxisInfo.getTargetUType(originUType, axis); UType testUType = test == null ? UType.ANY_NODE : test.getUType(); if (targetUType.equals(UType.VOID)) { if (warnings) { visitor.issueWarning("The " + AxisInfo.axisName[axis] + " axis starting at " + originUType.toStringWithIndefiniteArticle() + " will never select anything", getLocation()); } return Literal.makeEmptySequence(); } // Test whether the axis ever selects a node of the right kind, when starting at this context node if (!targetUType.overlaps(testUType)) { if (warnings) { visitor.issueWarning("The " + AxisInfo.axisName[axis] + " axis starting at " + originUType.toStringWithIndefiniteArticle() + " will never select " + test.getUType().toStringWithIndefiniteArticle(), getLocation()); } return Literal.makeEmptySequence(); } // For an X-or-self axis, if X never selects anything, then substitute the self axis. byte nonSelf = AxisInfo.excludeSelfAxis[axis]; UType kind = test == null ? UType.ANY_NODE : test.getUType(); if (axis != nonSelf) { UType nonSelfTarget = AxisInfo.getTargetUType(originUType, nonSelf); if (!nonSelfTarget.overlaps(testUType)) { axis = AxisInfo.SELF; targetUType = AxisInfo.getTargetUType(originUType, axis); } } ItemType target = targetUType.toItemType(); if (test == null || test instanceof AnyNodeTest) { itemType = target; } else if (target instanceof AnyNodeTest || targetUType.subsumes(test.getUType())) { itemType = test; } else { itemType = new CombinedNodeTest((NodeTest) target, Token.INTERSECT, test); } int origin = contextType.getPrimitiveType(); if (test != null) { // If the content type of the context item is known, see whether the node test can select anything if (contextType instanceof DocumentNodeTest && kind.equals(UType.ELEMENT)) { NodeTest elementTest = ((DocumentNodeTest) contextType).getElementTest(); OptionalThis method is called after all references to functions and variables have been resolved * to the declaration of the function or variable, and after all type checking has been done.
* * @param visitor an expression visitor * @param contextInfo the static type of "." at the point where this expression is invoked. * The parameter is set to null if it is known statically that the context item will be undefined. * If the type of the context item is not known statically, the argument is set to * {@link net.sf.saxon.type.Type#ITEM_TYPE} * @return the original expression, rewritten if appropriate to optimize execution */ public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) { doneOptimize = true; // This ensures no more warnings about empty axes, because (a) we've probably output the // warning already, and (b) we're now looking at a different expression from what the user // wrote. In particular, prevent spurious warnings after function inlining. staticInfo = contextInfo; return this; } /** * Return the estimated cost of evaluating an expression. This is a very crude measure based * on the syntactic form of the expression (we have no knowledge of data values). We take * the cost of evaluating a simple scalar comparison or arithmetic expression as 1 (one), * and we assume that a sequence has length 5. The resulting estimates may be used, for * example, to reorder the predicates in a filter expression so cheaper predicates are * evaluated first. */ @Override public int getCost() { switch (axis) { case AxisInfo.SELF: case AxisInfo.PARENT: case AxisInfo.ATTRIBUTE: return 1; case AxisInfo.CHILD: case AxisInfo.FOLLOWING_SIBLING: case AxisInfo.PRECEDING_SIBLING: case AxisInfo.ANCESTOR: case AxisInfo.ANCESTOR_OR_SELF: return 5; default: return 20; } } /** * Is this expression the same as another expression? */ public boolean equals(Object other) { return other instanceof AxisExpression && axis == ((AxisExpression) other).axis && Objects.equals(test, ((AxisExpression) other).test); } /** * get HashCode for comparing two expressions */ public int computeHashCode() { // generate an arbitrary hash code that depends on the axis and the node test int h = 9375162 + axis << 20; if (test != null) { h ^= test.getPrimitiveType() << 16; h ^= test.getFingerprint(); } return h; } /** * Copy an expression. This makes a deep copy. * * @param rebindings a mutable list of (old binding, new binding) pairs * that is used to update the bindings held in any * local variable references that are copied. * @return the copy of the original expression */ /*@NotNull*/ public Expression copy(RebindingMap rebindings) { AxisExpression a2 = new AxisExpression(axis, test); a2.itemType = itemType; a2.staticInfo = staticInfo; a2.computedCardinality = computedCardinality; a2.doneTypeCheck = doneTypeCheck; a2.doneOptimize = doneOptimize; ExpressionTool.copyLocationInfo(this, a2); return a2; } /** * Get the static properties of this expression (other than its type). The result is * bit-signficant. These properties are used for optimizations. In general, if * property bit is set, it is true, but if it is unset, the value is unknown. */ public int computeSpecialProperties() { return StaticProperty.CONTEXT_DOCUMENT_NODESET | StaticProperty.SINGLE_DOCUMENT_NODESET | StaticProperty.NO_NODES_NEWLY_CREATED | (AxisInfo.isForwards[axis] ? StaticProperty.ORDERED_NODESET : StaticProperty.REVERSE_DOCUMENT_ORDER) | (AxisInfo.isPeerAxis[axis] || isPeerNodeTest(test) ? StaticProperty.PEER_NODESET : 0) | (AxisInfo.isSubtreeAxis[axis] ? StaticProperty.SUBTREE_NODESET : 0) | (axis == AxisInfo.ATTRIBUTE || axis == AxisInfo.NAMESPACE ? StaticProperty.ATTRIBUTE_NS_NODESET : 0); } /** * Determine whether a node test is a peer node test. A peer node test is one that, if it * matches a node, cannot match any of its descendants. For example, text() is a peer node-test. * * @param test the node test * @return true if nodes selected by this node-test will never contain each other as descendants */ private static boolean isPeerNodeTest(NodeTest test) { if (test == null) { return false; } UType uType = test.getUType(); if (uType.overlaps(UType.ELEMENT)) { // can match elements; for the moment, assume these can contain each other return false; } else if (uType.overlaps(UType.DOCUMENT)) { // can match documents; return false if we can also match non-documents return uType.equals(UType.DOCUMENT); } else { return true; } } /** * Determine the data type of the items returned by this expression * * @return Type.NODE or a subtype, based on the NodeTest in the axis step, plus * information about the content type if this is known from schema analysis */ /*@NotNull*/ public final ItemType getItemType() { if (itemType != null) { return itemType; } int p = AxisInfo.principalNodeType[axis]; switch (p) { case Type.ATTRIBUTE: case Type.NAMESPACE: return NodeKindTest.makeNodeKindTest(p); default: if (test == null) { return AnyNodeTest.getInstance(); } else { return test; } } } /** * Get the static type of the expression as a UType, following precisely the type * inference rules defined in the XSLT 3.0 specification. * * @param contextItemType the static type of the context item for the expression evaluation * @return the static item type of the expression according to the XSLT 3.0 defined rules */ @Override public UType getStaticUType(UType contextItemType) { // See W3C bug 30032 UType reachable = AxisInfo.getTargetUType(contextItemType, axis); if (test == null) { return reachable; } else { return reachable.intersection(test.getUType()); } } /** * Determine the intrinsic dependencies of an expression, that is, those which are not derived * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency * on the context position, while (position()+1) does not. The default implementation * of the method returns 0, indicating "no dependencies". * * @return a set of bit-significant flags identifying the "intrinsic" * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty */ @Override public int getIntrinsicDependencies() { return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; } /** * Determine the cardinality of the result of this expression */ public final int computeCardinality() { if (computedCardinality != -1) { // This takes care of the case where cardinality was computed during type checking of the child axis return computedCardinality; } NodeTest originNodeType; NodeTest nodeTest = test; ItemType contextItemType = staticInfo.getItemType(); if (contextItemType instanceof NodeTest) { originNodeType = (NodeTest) contextItemType; } else if (contextItemType instanceof AnyItemType) { originNodeType = AnyNodeTest.getInstance(); } else { // context item not a node - we'll report a type error somewhere along the line return StaticProperty.ALLOWS_ZERO_OR_MORE; } if (axis == AxisInfo.ATTRIBUTE && nodeTest instanceof NameTest) { SchemaType contentType = originNodeType.getContentType(); if (contentType instanceof ComplexType) { try { return ((ComplexType) contentType).getAttributeUseCardinality(nodeTest.getMatchingNodeName()); } catch (SchemaException err) { // shouldn't happen; play safe return StaticProperty.ALLOWS_ZERO_OR_ONE; } } else if (contentType instanceof SimpleType) { return StaticProperty.EMPTY; } return StaticProperty.ALLOWS_ZERO_OR_ONE; } else if (axis == AxisInfo.DESCENDANT && nodeTest instanceof NameTest && nodeTest.getPrimitiveType() == Type.ELEMENT) { SchemaType contentType = originNodeType.getContentType(); if (contentType instanceof ComplexType) { try { return ((ComplexType) contentType).getDescendantElementCardinality(nodeTest.getFingerprint()); } catch (SchemaException err) { // shouldn't happen; play safe return StaticProperty.ALLOWS_ZERO_OR_MORE; } } else { return StaticProperty.EMPTY; } } else if (axis == AxisInfo.SELF) { return StaticProperty.ALLOWS_ZERO_OR_ONE; } else { return StaticProperty.ALLOWS_ZERO_OR_MORE; } // the parent axis isn't handled by this class } /** * Determine whether the expression can be evaluated without reference to the part of the context * document outside the subtree rooted at the context node. * * @return true if the expression has no dependencies on the context node, or if the only dependencies * on the context node are downward selections using the self, child, descendant, attribute, and namespace * axes. */ public boolean isSubtreeExpression() { return AxisInfo.isSubtreeAxis[axis]; } /** * Get the axis * * @return the axis number, for example {@link net.sf.saxon.om.AxisInfo#CHILD} */ public byte getAxis() { return axis; } /** * Get the NodeTest. Returns null if the AxisExpression can return any node. * * @return the node test, or null if all nodes are returned */ public NodeTest getNodeTest() { return test; } /** * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited * by an expression in a source tree. * * @param pathMap the PathMap to which the expression should be added * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added * @return the pathMapNode representing the focus established by this expression, in the case where this * expression is the first operand of a path expression or filter expression */ public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { if (pathMapNodeSet == null) { ContextItemExpression cie = new ContextItemExpression(); //cie.setContainer(getContainer()); pathMapNodeSet = new PathMap.PathMapNodeSet(pathMap.makeNewRoot(cie)); } return pathMapNodeSet.createArc(axis, test == null ? AnyNodeTest.getInstance() : test); } /** * Ask whether there is a possibility that the context item will be undefined * * @return true if this is a possibility */ public boolean isContextPossiblyUndefined() { return staticInfo.isPossiblyAbsent(); } public ContextItemStaticInfo getContextItemStaticInfo() { return staticInfo; } /** * Convert this expression to an equivalent XSLT pattern * * @param config the Saxon configuration * @return the equivalent pattern * @throws net.sf.saxon.trans.XPathException if conversion is not possible */ @Override public Pattern toPattern(Configuration config) throws XPathException { NodeTest test = getNodeTest(); Pattern pat; if (test == null) { test = AnyNodeTest.getInstance(); } if (test instanceof AnyNodeTest && (axis == AxisInfo.CHILD || axis == AxisInfo.DESCENDANT || axis == AxisInfo.SELF)) { test = AnyChildNodeTest.getInstance(); } int kind = test.getPrimitiveType(); if (axis == AxisInfo.SELF) { pat = new NodeTestPattern(test); } else if (axis == AxisInfo.ATTRIBUTE) { if (kind == Type.NODE) { // attribute::node() matches any attribute, and only an attribute pat = new NodeTestPattern(NodeKindTest.ATTRIBUTE); } else if (!AxisInfo.containsNodeKind(axis, kind)) { // for example, attribute::comment() pat = new NodeTestPattern(ErrorType.getInstance()); } else { pat = new NodeTestPattern(test); } } else if (axis == AxisInfo.CHILD || axis == AxisInfo.DESCENDANT || axis == AxisInfo.DESCENDANT_OR_SELF) { if (kind != Type.NODE && !AxisInfo.containsNodeKind(axis, kind)) { pat = new NodeTestPattern(ErrorType.getInstance()); } else { pat = new NodeTestPattern(test); } } else if (axis == AxisInfo.NAMESPACE) { if (kind == Type.NODE) { // namespace::node() matches any attribute, and only an attribute pat = new NodeTestPattern(NodeKindTest.NAMESPACE); } else if (!AxisInfo.containsNodeKind(axis, kind)) { // for example, namespace::comment() pat = new NodeTestPattern(ErrorType.getInstance()); } else { pat = new NodeTestPattern(test); } } else { throw new XPathException("Only downwards axes are allowed in a pattern", "XTSE0340"); } ExpressionTool.copyLocationInfo(this, pat); return pat; } @Override public int getImplementationMethod() { return ITERATE_METHOD; } /** * Evaluate the path-expression in a given context to return a NodeSet * * @param context the evaluation context */ /*@NotNull*/ public SequenceIterator> iterate(XPathContext context) throws XPathException { Item item = context.getContextItem(); if (item == null) { // Might as well do the test anyway, whether or not contextMaybeUndefined is set XPathException err = new XPathException("The context item for axis step " + this + " is absent"); err.setErrorCode("XPDY0002"); err.setXPathContext(context); err.setLocation(getLocation()); err.setIsTypeError(true); throw err; } try { if (test == null) { return ((NodeInfo) item).iterateAxis(axis); } else { return ((NodeInfo) item).iterateAxis(axis, test); } } catch (ClassCastException cce) { XPathException err = new XPathException("The context item for axis step " + this + " is not a node"); err.setErrorCode("XPTY0020"); err.setXPathContext(context); err.setLocation(getLocation()); err.setIsTypeError(true); throw err; } catch (UnsupportedOperationException err) { if (err.getCause() instanceof XPathException) { XPathException ec = (XPathException) err.getCause(); ec.maybeSetLocation(getLocation()); ec.maybeSetContext(context); throw ec; } else { // the namespace axis is not supported for all tree implementations dynamicError(err.getMessage(), "XPST0010", context); return null; } } } /** * Iterate the axis from a given starting node, without regard to context * * @param origin the starting node * @return the iterator over the axis */ public AxisIterator iterate(NodeInfo origin) { if (test == null) { return origin.iterateAxis(axis); } else { return origin.iterateAxis(axis, test); } } /** * Diagnostic print of expression structure. The abstract expression tree * is written to the supplied output destination. */ public void export(ExpressionPresenter destination) throws XPathException { destination.startElement("axis", this); destination.emitAttribute("name", AxisInfo.axisName[axis]); destination.emitAttribute("nodeTest", test == null ? "node()" : test.toString()); if ("JS".equals(((ExpressionPresenter.ExportOptions) destination.getOptions()).target)) { NodeTest known = AnyNodeTest.getInstance(); if (axis == AxisInfo.ATTRIBUTE) { known = NodeKindTest.makeNodeKindTest(Type.ATTRIBUTE); } else if (axis == AxisInfo.NAMESPACE) { known = NodeKindTest.makeNodeKindTest(Type.NAMESPACE); } int targetVersion = ((ExpressionPresenter.ExportOptions) destination.getOptions()).targetVersion; try { destination.emitAttribute("jsTest", test == null ? "return true;" : test.generateJavaScriptItemTypeTest(known, targetVersion)); } catch (XPathException e) { e.maybeSetLocation(getLocation()); throw e; } } destination.endElement(); } /** * Represent the expression as a string. The resulting string will be a valid XPath 3.0 expression * with no dependencies on namespace bindings other than the binding of the prefix "xs" to the XML Schema * namespace. * * @return the expression as a string in XPath 3.0 syntax */ public String toString() { FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16); fsb.append(AxisInfo.axisName[axis]); fsb.append("::"); fsb.append(test == null ? "node()" : test.toString()); return fsb.toString(); } @Override public String toShortString() { FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16); if (axis == AxisInfo.CHILD) { // no action } else if (axis == AxisInfo.ATTRIBUTE) { fsb.append("@"); } else { fsb.append(AxisInfo.axisName[axis]); fsb.append("::"); } if (test == null) { fsb.append("node()"); } else if (test instanceof NameTest) { if (((NameTest) test).getNodeKind() != AxisInfo.principalNodeType[axis]) { fsb.append(test.toString()); } else { fsb.append(test.getMatchingNodeName().getDisplayName()); } } else { fsb.append(test.toString()); } return fsb.toString(); } @Override public String getStreamerName() { return "AxisExpression"; } /** * Find any necessary preconditions for the satisfaction of this expression * as a set of boolean expressions to be evaluated on the context node * * @return A set of conditions, or null if none have been computed */ public Set