| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.functions; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.StaticProperty; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 14 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 | import net.sf.saxon.om.Item; |
| 16 | import net.sf.saxon.om.Sequence; |
| 17 | import net.sf.saxon.om.SequenceIterator; |
| 18 | import net.sf.saxon.om.SequenceTool; |
| 19 | import net.sf.saxon.trans.XPathException; |
| 20 | import net.sf.saxon.tree.iter.ReversibleIterator; |
| 21 | import net.sf.saxon.value.SequenceExtent; |
| 22 | |
| 23 | /** |
| 24 | * Implement XPath function fn:reverse() |
| 25 | */ |
| 26 | |
| 27 | public class Reverse extends SystemFunction { |
| 28 | |
| 29 | public int getSpecialProperties(Expression[] arguments) { |
| 30 | int baseProps = arguments[0].getSpecialProperties(); |
| 31 | if ((baseProps & StaticProperty.REVERSE_DOCUMENT_ORDER) != 0) { |
| 32 | return (baseProps & |
| 33 | ~StaticProperty.REVERSE_DOCUMENT_ORDER) | |
| 34 | StaticProperty.ORDERED_NODESET; |
| 35 | } else if ((baseProps & StaticProperty.ORDERED_NODESET) != 0) { |
| 36 | return (baseProps & |
| 37 | ~StaticProperty.ORDERED_NODESET) | |
| 38 | StaticProperty.REVERSE_DOCUMENT_ORDER; |
| 39 | } else { |
| 40 | return baseProps; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // /** |
| 45 | // * Perform optimisation of an expression and its subexpressions. |
| 46 | // * <p>This method is called after all references to functions and variables have been resolved |
| 47 | // * to the declaration of the function or variable, and after all type checking has been done.</p> |
| 48 | // * |
| 49 | // * @param visitor an expression visitor |
| 50 | // * @param contextItemType the static type of "." at the point where this expression is invoked. |
| 51 | // * The parameter is set to null if it is known statically that the context item will be undefined. |
| 52 | // * If the type of the context item is not known statically, the argument is set to |
| 53 | // * {@link net.sf.saxon.type.Type#ITEM_TYPE} |
| 54 | // * @return the original expression, rewritten if appropriate to optimize execution |
| 55 | // * @throws net.sf.saxon.trans.XPathException |
| 56 | // * if an error is discovered during this phase |
| 57 | // * (typically a type error) |
| 58 | // */ |
| 59 | // @Override |
| 60 | // public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 61 | // Expression e2 = super.optimize(visitor, contextItemType); |
| 62 | // if (e2 != this) { |
| 63 | // ExpressionTool.copyLocationInfo(this, e2); |
| 64 | // return e2; |
| 65 | // } |
| 66 | // if (!Cardinality.allowsMany(getArg(0).getCardinality())) { |
| 67 | // return getArg(0); |
| 68 | // } |
| 69 | // return this; |
| 70 | // } |
| 71 | |
| 72 | |
| 73 | public static <T extends Item<?>> SequenceIterator<T> getReverseIterator(SequenceIterator<T> forwards) throws XPathException { |
| 74 | if (forwards instanceof ReversibleIterator) { |
| 75 | return ((ReversibleIterator<T>) forwards).getReverseIterator(); |
| 76 | } else { |
| 77 | SequenceExtent<T> extent = new SequenceExtent<>(forwards); |
| 78 | return extent.reverseIterate(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 84 | return SequenceTool.toLazySequence(getReverseIterator(arguments[0].iterate())); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Allow the function to create an optimized call based on the values of the actual arguments |
| 89 | * |
| 90 | * @param visitor the expression visitor |
| 91 | * @param contextInfo information about the context item |
| 92 | * @param arguments the supplied arguments to the function call. Note: modifying the contents |
| 93 | * of this array should not be attempted, it is likely to have no effect. |
| 94 | * @return either a function call on this function, or an expression that delivers |
| 95 | * the same result, or null indicating that no optimization has taken place |
| 96 | * @throws XPathException if an error is detected |
| 97 | */ |
| 98 | @Override |
| 99 | public Expression makeOptimizedFunctionCall( |
| 100 | ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) |
| 101 | throws XPathException { |
| 102 | // When reverse() has only a zero-or-one argument, there is no need to reverse |
| 103 | // This often occurs in reverse-axis steps |
| 104 | if (arguments[0].getCardinality() == StaticProperty.ALLOWS_ZERO_OR_ONE) { |
| 105 | return arguments[0]; |
| 106 | } |
| 107 | return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | public String getStreamerName() { |
| 112 | return "Reverse"; |
| 113 | } |
| 114 | } |
| 115 |