1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.axes;
18
19 import org.apache.commons.jxpath.ri.Compiler;
20 import org.apache.commons.jxpath.ri.EvalContext;
21 import org.apache.commons.jxpath.ri.QName;
22 import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
23 import org.apache.commons.jxpath.ri.compiler.NodeTest;
24 import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
25 import org.apache.commons.jxpath.ri.model.NodeIterator;
26 import org.apache.commons.jxpath.ri.model.NodePointer;
27
28
29
30
31
32
33
34 public class AttributeContext extends EvalContext {
35 private static final QName WILDCARD = new QName(null, "*");
36
37 private NodeTest nodeTest;
38 private boolean setStarted = false;
39 private NodeIterator iterator;
40 private NodePointer currentNodePointer;
41
42
43
44
45
46
47 public AttributeContext(EvalContext parentContext, NodeTest nodeTest) {
48 super(parentContext);
49 this.nodeTest = nodeTest;
50 }
51
52 public NodePointer getCurrentNodePointer() {
53 return currentNodePointer;
54 }
55
56 public void reset() {
57 setStarted = false;
58 iterator = null;
59 super.reset();
60 }
61
62 public boolean setPosition(int position) {
63 if (position < getCurrentPosition()) {
64 reset();
65 }
66
67 while (getCurrentPosition() < position) {
68 if (!nextNode()) {
69 return false;
70 }
71 }
72 return true;
73 }
74
75 public boolean nextNode() {
76 super.setPosition(getCurrentPosition() + 1);
77 if (!setStarted) {
78 setStarted = true;
79 QName name;
80 if (nodeTest instanceof NodeNameTest) {
81 name = ((NodeNameTest) nodeTest).getNodeName();
82 }
83 else {
84 if (nodeTest instanceof NodeTypeTest
85 && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
86 name = WILDCARD;
87 }
88 else {
89 iterator = null;
90 return false;
91 }
92 }
93 iterator = parentContext.getCurrentNodePointer().attributeIterator(
94 name);
95 }
96 if (iterator == null) {
97 return false;
98 }
99 if (!iterator.setPosition(iterator.getPosition() + 1)) {
100 return false;
101 }
102 currentNodePointer = iterator.getNodePointer();
103 return true;
104 }
105 }