| 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.XPathContext; |
| 12 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 | import net.sf.saxon.expr.sort.AtomicComparer; |
| 15 | import net.sf.saxon.om.Sequence; |
| 16 | import net.sf.saxon.om.SequenceIterator; |
| 17 | import net.sf.saxon.om.SequenceTool; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.type.AtomicType; |
| 20 | import net.sf.saxon.type.BuiltInAtomicType; |
| 21 | import net.sf.saxon.type.ItemType; |
| 22 | import net.sf.saxon.type.Type; |
| 23 | import net.sf.saxon.value.AtomicValue; |
| 24 | import net.sf.saxon.value.Int64Value; |
| 25 | import net.sf.saxon.value.IntegerValue; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * The XPath 2.0 index-of() function, with the collation already known |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | public class IndexOf extends CollatingFunctionFixed { |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * For an expression that returns an integer or a sequence of integers, get |
| 38 | * a lower and upper bound on the values of the integers that may be returned, from |
| 39 | * static analysis. The default implementation returns null, meaning "unknown" or |
| 40 | * "not applicable". Other implementations return an array of two IntegerValue objects, |
| 41 | * representing the lower and upper bounds respectively. The values |
| 42 | * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that |
| 43 | * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH |
| 44 | * are used to indicate values limited by the size of a string or the size of a sequence. |
| 45 | * |
| 46 | * @return the lower and upper bounds of integer values in the result, or null to indicate |
| 47 | * unknown or not applicable. |
| 48 | */ |
| 49 | @Override |
| 50 | public IntegerValue[] getIntegerBounds() { |
| 51 | return new IntegerValue[]{Int64Value.PLUS_ONE, Expression.MAX_SEQUENCE_LENGTH}; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, Expression[] arguments) { |
| 56 | ItemType type0 = arguments[0].getItemType(); |
| 57 | ItemType type1 = arguments[1].getItemType(); |
| 58 | if (type0 instanceof AtomicType && type1 instanceof AtomicType) { |
| 59 | preAllocateComparer((AtomicType) type0, (AtomicType) type1, visitor.getStaticContext()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private static class IndexIterator implements SequenceIterator<Int64Value> { |
| 64 | private int index = 0; |
| 65 | private SequenceIterator base; |
| 66 | private BuiltInAtomicType searchType; |
| 67 | private AtomicComparer comparer; |
| 68 | private AtomicValue key; |
| 69 | |
| 70 | public IndexIterator(SequenceIterator base, BuiltInAtomicType searchType, AtomicValue key, AtomicComparer comparer) { |
| 71 | this.base = base; |
| 72 | this.searchType = searchType; |
| 73 | this.key = key; |
| 74 | this.comparer = comparer; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Close the iterator. This indicates to the supplier of the data that the client |
| 79 | * does not require any more items to be delivered by the iterator. This may enable the |
| 80 | * supplier to release resources. After calling close(), no further calls on the |
| 81 | * iterator should be made; if further calls are made, the effect of such calls is undefined. |
| 82 | * <p>(Currently, closing an iterator is important only when the data is being "pushed" in |
| 83 | * another thread. Closing the iterator terminates that thread and means that it needs to do |
| 84 | * no additional work. Indeed, failing to close the iterator may cause the push thread to hang |
| 85 | * waiting for the buffer to be emptied.)</p> |
| 86 | * |
| 87 | * @since 9.1 |
| 88 | */ |
| 89 | public void close() { |
| 90 | base.close(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the next item in the sequence. This method changes the state of the |
| 95 | * iterator, in particular it affects the result of subsequent calls of |
| 96 | * position() and current(). |
| 97 | * |
| 98 | * @return the next item, or null if there are no more items. Once a call |
| 99 | * on next() has returned null, no further calls should be made. The preferred |
| 100 | * action for an iterator if subsequent calls on next() are made is to return |
| 101 | * null again, and all implementations within Saxon follow this rule. |
| 102 | * @throws net.sf.saxon.trans.XPathException if an error occurs retrieving the next item |
| 103 | * @since 8.4 |
| 104 | */ |
| 105 | public Int64Value next() throws XPathException { |
| 106 | AtomicValue baseItem; |
| 107 | while ((baseItem = (AtomicValue) base.next()) != null) { |
| 108 | index++; |
| 109 | if (Type.isGuaranteedComparable(searchType, baseItem.getPrimitiveType(), false) && |
| 110 | comparer.comparesEqual(baseItem, key)) { |
| 111 | return new Int64Value(index); |
| 112 | } |
| 113 | } |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get properties of this iterator, as a bit-significant integer. |
| 119 | * |
| 120 | * @return the properties of this iterator. This will be some combination of |
| 121 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 122 | * and {@link #LOOKAHEAD}. It is always |
| 123 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 124 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 125 | * @since 8.6 |
| 126 | */ |
| 127 | public int getProperties() { |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Evaluate the expression |
| 136 | * |
| 137 | * @param context the dynamic evaluation context |
| 138 | * @param arguments the values of the arguments, supplied as SequenceIterators |
| 139 | * @return the result of the evaluation, in the form of a SequenceIterator |
| 140 | * @throws net.sf.saxon.trans.XPathException |
| 141 | * if a dynamic error occurs during the evaluation of the expression |
| 142 | */ |
| 143 | public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 144 | AtomicComparer comparer = getAtomicComparer(context); |
| 145 | SequenceIterator seq = arguments[0].iterate(); |
| 146 | AtomicValue val = (AtomicValue) arguments[1].head(); |
| 147 | BuiltInAtomicType searchType = val.getPrimitiveType(); |
| 148 | return SequenceTool.toLazySequence(new IndexIterator(seq, searchType, val, comparer)); |
| 149 | } |
| 150 | |
| 151 | public String getStreamerName() { |
| 152 | return "IndexOf"; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 |