Debian Codegraph @

net/sf/saxon/tree/linked/AttributeEnumeration.java

raw symbols SCIP
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.linked;
9
10 import net.sf.saxon.om.AttributeCollection;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.pattern.NameTest;
13 import net.sf.saxon.pattern.NodeTest;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.iter.LookaheadIterator;
16 import net.sf.saxon.tree.util.AttributeCollectionImpl;
17 import net.sf.saxon.type.Type;
18
19 /**
20 * AttributeEnumeration is an enumeration of all the attribute nodes of an Element.
21 */
22
23 final class AttributeEnumeration implements AxisIterator, LookaheadIterator<NodeInfo> {
24
25 private ElementImpl element;
26 private AttributeCollectionImpl attributes;
27 private NodeTest nodeTest;
28 /*@Nullable*/ private NodeInfo next;
29 private int index;
30 private int length;
31
32 /**
33 * Constructor
34 *
35 * @param node: the element whose attributes are required. This may be any type of node,
36 * but if it is not an element the enumeration will be empty
37 * @param nodeTest: condition to be applied to the names of the attributes selected
38 */
39
40 public AttributeEnumeration(/*@NotNull*/ NodeImpl node, NodeTest nodeTest) {
41
42 this.nodeTest = nodeTest;
43
44 if (node.getNodeKind() == Type.ELEMENT) {
45 element = (ElementImpl) node;
46 attributes = (AttributeCollectionImpl) element.getAttributeList();
47 AttributeCollection attlist = element.getAttributeList();
48 index = 0;
49
50 if (nodeTest instanceof NameTest) {
51 NameTest test = (NameTest) nodeTest;
52 index = attlist.getIndexByFingerprint(test.getFingerprint());
53
54 if (index < 0) {
55 next = null;
56 } else {
57 next = new AttributeImpl(element, index);
58 index = 0;
59 length = 0; // force iteration to select one node only
60 }
61
62 } else {
63 index = 0;
64 length = attlist.getLength();
65 advance();
66 }
67 } else { // if it's not an element, or if we're not looking for attributes,
68 // then there's nothing to find
69 next = null;
70 index = 0;
71 length = 0;
72 }
73 }
74
75 /**
76 * Test if there are mode nodes still to come.
77 * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
78 */
79
80 public boolean hasNext() {
81 return next != null;
82 }
83
84 /**
85 * Get the next node in the iteration, or null if there are no more.
86 */
87
88 /*@Nullable*/
89 public NodeInfo next() {
90 if (next == null) {
91 return null;
92 } else {
93 NodeInfo current = next;
94 advance();
95 return current;
96 }
97 }
98
99 /**
100 * Move to the next node in the enumeration.
101 */
102
103 private void advance() {
104 while (true) {
105 if (index >= length) {
106 next = null;
107 return;
108 } else if (attributes.isDeleted(index)) {
109 index++;
110 } else {
111 next = new AttributeImpl(element, index);
112 index++;
113 if (nodeTest.matchesNode(next)) {
114 return;
115 }
116 }
117 }
118 }
119
120 /**
121 * Get properties of this iterator, as a bit-significant integer.
122 *
123 * @return the properties of this iterator. This will be some combination of
124 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
125 * and {@link #LOOKAHEAD}. It is always
126 * acceptable to return the value zero, indicating that there are no known special properties.
127 * It is acceptable for the properties of the iterator to change depending on its state.
128 */
129
130 public int getProperties() {
131 return LOOKAHEAD;
132 }
133 }
134