//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 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.pattern; import com.saxonica.ee.stream.Streamability; import com.saxonica.ee.stream.Sweep; import com.saxonica.ee.trans.ContextItemStaticInfoEE; import net.sf.saxon.expr.*; import net.sf.saxon.expr.instruct.SlotManager; import net.sf.saxon.expr.parser.ContextItemStaticInfo; import net.sf.saxon.expr.parser.ExpressionTool; import net.sf.saxon.expr.parser.ExpressionVisitor; import net.sf.saxon.expr.parser.RebindingMap; import net.sf.saxon.functions.Current; import net.sf.saxon.om.Item; import net.sf.saxon.om.NodeInfo; import net.sf.saxon.trace.ExpressionPresenter; import net.sf.saxon.trans.XPathException; import net.sf.saxon.tree.iter.ManualIterator; import net.sf.saxon.type.ItemType; import net.sf.saxon.type.Type; import net.sf.saxon.type.UType; /** * Class for handling patterns with simple non-positional boolean predicates */ public class BasePatternWithPredicate extends Pattern implements PatternWithPredicate { Operand basePatternOp; Operand predicateOp; public BasePatternWithPredicate(Pattern basePattern, Expression predicate) { basePatternOp = new Operand(this, basePattern, OperandRole.ATOMIC_SEQUENCE); predicateOp = new Operand(this, predicate, OperandRole.FOCUS_CONTROLLED_ACTION); adoptChildExpression(getBasePattern()); adoptChildExpression(getPredicate()); } public Expression getPredicate() { return predicateOp.getChildExpression(); } public Pattern getBasePattern() { return (Pattern)basePatternOp.getChildExpression(); } /** * Replace any calls on current() by a variable reference bound to the supplied binding */ @Override public void bindCurrent(LocalBinding binding) { Expression predicate = getPredicate(); if (predicate.isCallOn(Current.class)) { predicateOp.setChildExpression(new LocalVariableReference(binding)); } else if (ExpressionTool.callsFunction(predicate, Current.FN_CURRENT, false)) { replaceCurrent(predicate, binding); } getBasePattern().bindCurrent(binding); } /** * Ask whether the pattern is anchored on a call on current-group() * * @return true if calls on matchesBeneathAnchor should test with all nodes in the * current group as anchor nodes. If false, only the first node in a group is * treated as the anchor node */ public boolean matchesCurrentGroup() { return getBasePattern().matchesCurrentGroup(); } /** * Get the immediate sub-expressions of this expression, with information about the relationship * of each expression to its parent expression. Default implementation * works off the results of iterateSubExpressions() *
If the expression is a Callable, then it is required that the order of the operands * returned by this function is the same as the order of arguments supplied to the corresponding * call() method.
* * @return an iterator containing the sub-expressions of this expression */ @Override public IterableThis 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 Type#ITEM_TYPE} * @return the original expression, rewritten if appropriate to optimize execution * @throws XPathException if an error is discovered during this phase * (typically a type error) */ @Override public Pattern optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { basePatternOp.setChildExpression(getBasePattern().optimize(visitor, contextInfo)); ContextItemStaticInfo cit = visitor.getConfiguration().makeContextItemStaticInfo( getBasePattern().getItemType(), false); predicateOp.setChildExpression(getPredicate().optimize(visitor, cit)); predicateOp.setChildExpression(visitor.obtainOptimizer().eliminateCommonSubexpressions(getPredicate())); return this; } /** * Convert the pattern to a typed pattern, in which an element name is treated as * schema-element(N) * * @param val either "strict" or "lax" depending on the value of xsl:mode/@typed * @return either the original pattern unchanged, or a new pattern as the result of the * conversion * @throws net.sf.saxon.trans.XPathException if the pattern cannot be converted */ @Override public Pattern convertToTypedPattern(String val) throws XPathException { Pattern b2 = getBasePattern().convertToTypedPattern(val); if (b2 == getBasePattern()) { return this; } else { return new BasePatternWithPredicate(b2, getPredicate()); } } //#ifdefined STREAM @Override public boolean isMotionless() { ContextItemStaticInfoEE cio = (ContextItemStaticInfoEE)getConfiguration().makeContextItemStaticInfo(getItemType(), false); cio.setContextPostureStriding(); Streamability.getStreamability(getPredicate(), cio, null); return getBasePattern().isMotionless() && Streamability.getSweep(getPredicate()) == Sweep.MOTIONLESS; } //#endif /** * Display the pattern for diagnostics */ public String toString() { return getBasePattern() + "[" + getPredicate() + "]"; } @Override public String toShortString() { return getBasePattern().toShortString() + "[" + getPredicate().toShortString() + "]"; } /** * Copy a pattern. This makes a deep copy. * * @return the copy of the original pattern * @param rebindings variables that need to be re-bound */ /*@NotNull*/ public Pattern copy(RebindingMap rebindings) { BasePatternWithPredicate n = new BasePatternWithPredicate( getBasePattern().copy(rebindings), getPredicate().copy(rebindings)); ExpressionTool.copyLocationInfo(this, n); return n; } @Override public boolean equals(Object obj) { return obj instanceof BasePatternWithPredicate && ((BasePatternWithPredicate)obj).getBasePattern().isEqual(getBasePattern()) && ((BasePatternWithPredicate) obj).getPredicate().isEqual(getPredicate()); } @Override public int computeHashCode() { return getBasePattern().hashCode() ^ getPredicate().hashCode(); } public void export(ExpressionPresenter presenter) throws XPathException { presenter.startElement("p.withPredicate"); getBasePattern().export(presenter); getPredicate().export(presenter); presenter.endElement(); } }