| 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.tree.tiny; |
| 9 | |
| 10 | import net.sf.saxon.om.*; |
| 11 | import net.sf.saxon.pattern.NameTest; |
| 12 | import net.sf.saxon.pattern.NodeTest; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.AxisIterator; |
| 15 | import net.sf.saxon.type.SchemaType; |
| 16 | import net.sf.saxon.type.Type; |
| 17 | |
| 18 | /** |
| 19 | * AttributeIterator is an iterator over all the attribute nodes of an Element in the TinyTree. |
| 20 | */ |
| 21 | |
| 22 | final class AttributeIterator implements AxisIterator, AtomizedValueIterator<NodeInfo> { |
| 23 | |
| 24 | private TinyTree tree; |
| 25 | private int element; |
| 26 | private NodeTest nodeTest; |
| 27 | private int index; |
| 28 | private int currentNodeNr; |
| 29 | |
| 30 | /** |
| 31 | * Constructor. Note: this constructor will only be called if the relevant node |
| 32 | * is an element and if it has one or more attributes. Otherwise an EmptyEnumeration |
| 33 | * will be constructed instead. |
| 34 | * |
| 35 | * @param tree: the containing TinyTree |
| 36 | * @param element: the node number of the element whose attributes are required |
| 37 | * @param nodeTest: condition to be applied to the names of the attributes selected |
| 38 | */ |
| 39 | |
| 40 | AttributeIterator(/*@NotNull*/ TinyTree tree, int element, NodeTest nodeTest) { |
| 41 | |
| 42 | this.nodeTest = nodeTest; |
| 43 | this.tree = tree; |
| 44 | this.element = element; |
| 45 | index = tree.alpha[element]; |
| 46 | currentNodeNr = -1; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Move to the next node in the iteration. On completion, currentNodeNr points |
| 51 | * to the next attribute. |
| 52 | * @return true if there are more items in the sequence |
| 53 | */ |
| 54 | |
| 55 | private boolean moveToNext() { |
| 56 | while (true) { |
| 57 | if (index >= tree.numberOfAttributes || tree.attParent[index] != element) { |
| 58 | index = Integer.MAX_VALUE; |
| 59 | currentNodeNr = -1; |
| 60 | return false; |
| 61 | } |
| 62 | SchemaType typeCode = tree.getAttributeType(index); |
| 63 | if (nodeTest.matches(Type.ATTRIBUTE, new CodedName(tree.attCode[index] & NamePool.FP_MASK, "", tree.getNamePool()), typeCode)) { |
| 64 | currentNodeNr = index++; |
| 65 | if (nodeTest instanceof NameTest) { |
| 66 | // there can only be one match, so abandon the search after this node |
| 67 | index = Integer.MAX_VALUE; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | index++; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the next item in the sequence. <BR> |
| 77 | * |
| 78 | * @return the next Item. If there are no more nodes, return null. |
| 79 | */ |
| 80 | |
| 81 | /*@Nullable*/ |
| 82 | public NodeInfo next() { |
| 83 | if (moveToNext()) { |
| 84 | return tree.getAttributeNode(currentNodeNr); |
| 85 | } else { |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Deliver the atomic value that is next in the atomized result |
| 92 | * |
| 93 | * @return the next atomic value |
| 94 | * @throws net.sf.saxon.trans.XPathException |
| 95 | * if a failure occurs reading or atomizing the next value |
| 96 | */ |
| 97 | public AtomicSequence nextAtomizedValue() throws XPathException { |
| 98 | if (moveToNext()) { |
| 99 | return tree.getTypedValueOfAttribute(null, currentNodeNr); |
| 100 | } else { |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get properties of this iterator, as a bit-significant integer. |
| 107 | * |
| 108 | * @return the properties of this iterator. This will be some combination of |
| 109 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 110 | * and {@link #LOOKAHEAD}. It is always |
| 111 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 112 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 113 | */ |
| 114 | @Override |
| 115 | public int getProperties() { |
| 116 | return SequenceIterator.ATOMIZING; |
| 117 | } |
| 118 | } |
| 119 |