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.EvalContext;
20 import org.apache.commons.jxpath.ri.QName;
21 import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
22 import org.apache.commons.jxpath.ri.compiler.NodeTest;
23 import org.apache.commons.jxpath.ri.model.NodeIterator;
24 import org.apache.commons.jxpath.ri.model.NodePointer;
25
26
27
28
29
30
31
32 public class NamespaceContext extends EvalContext {
33 private NodeTest nodeTest;
34 private boolean setStarted = false;
35 private NodeIterator iterator;
36 private NodePointer currentNodePointer;
37
38
39
40
41
42 public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
43 super(parentContext);
44 this.nodeTest = nodeTest;
45 }
46
47 public NodePointer getCurrentNodePointer() {
48 return currentNodePointer;
49 }
50
51 public void reset() {
52 setStarted = false;
53 iterator = null;
54 super.reset();
55 }
56
57 public boolean setPosition(int position) {
58 if (position < getCurrentPosition()) {
59 reset();
60 }
61
62 while (getCurrentPosition() < position) {
63 if (!nextNode()) {
64 return false;
65 }
66 }
67 return true;
68 }
69
70 public boolean nextNode() {
71 super.setPosition(getCurrentPosition() + 1);
72 if (!setStarted) {
73 setStarted = true;
74 if (!(nodeTest instanceof NodeNameTest)) {
75 return false;
76 }
77
78 NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
79 QName testName = nodeNameTest.getNodeName();
80 if (testName.getPrefix() != null) {
81 return false;
82 }
83 if (nodeNameTest.isWildcard()) {
84 iterator =
85 parentContext.getCurrentNodePointer().namespaceIterator();
86 }
87 else {
88 currentNodePointer =
89 parentContext.getCurrentNodePointer().namespacePointer(
90 testName.getName());
91 return currentNodePointer != null;
92 }
93 }
94
95 if (iterator == null) {
96 return false;
97 }
98 if (!iterator.setPosition(iterator.getPosition() + 1)) {
99 return false;
100 }
101 currentNodePointer = iterator.getNodePointer();
102 return true;
103 }
104 }