1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.axes;
19
20 import org.apache.commons.jxpath.Function;
21 import org.apache.commons.jxpath.JXPathContext;
22 import org.apache.commons.jxpath.NodeSet;
23 import org.apache.commons.jxpath.ri.EvalContext;
24 import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
25 import org.apache.commons.jxpath.ri.QName;
26 import org.apache.commons.jxpath.ri.model.NodePointer;
27
28
29
30
31 public class RootContext extends EvalContext {
32
33
34
35
36 public static final Object UNKNOWN_VALUE = new Object();
37
38 private static final int MAX_REGISTER = 4;
39 private final JXPathContextReferenceImpl jxpathContext;
40 private final NodePointer pointer;
41 private Object[] registers;
42 private int availableRegister;
43
44
45
46
47
48
49
50 public RootContext(final JXPathContextReferenceImpl jxpathContext, final NodePointer pointer) {
51 super(null);
52 this.jxpathContext = jxpathContext;
53 this.pointer = pointer;
54 if (pointer != null) {
55 pointer.setNamespaceResolver(jxpathContext.getNamespaceResolver());
56 }
57 }
58
59
60
61
62
63
64 public EvalContext getAbsoluteRootContext() {
65 return jxpathContext.getAbsoluteRootContext();
66 }
67
68
69
70
71
72
73
74 public EvalContext getConstantContext(final Object constant) {
75 if (constant instanceof NodeSet) {
76 return new NodeSetContext(new RootContext(jxpathContext, null), (NodeSet) constant);
77 }
78 NodePointer pointer;
79 if (constant instanceof NodePointer) {
80 pointer = (NodePointer) constant;
81 } else {
82 pointer = NodePointer.newNodePointer(new QName(null, ""), constant, null);
83 }
84 return new InitialContext(new RootContext(jxpathContext, pointer));
85 }
86
87 @Override
88 public NodePointer getCurrentNodePointer() {
89 return pointer;
90 }
91
92 @Override
93 public int getCurrentPosition() {
94 throw new UnsupportedOperationException();
95 }
96
97
98
99
100
101
102
103
104 public Function getFunction(final QName functionName, final Object[] parameters) {
105 return jxpathContext.getFunction(functionName, parameters);
106 }
107
108 @Override
109 public JXPathContext getJXPathContext() {
110 return jxpathContext;
111 }
112
113
114
115
116
117
118
119 public Object getRegisteredValue(final int id) {
120 if (registers == null || id >= MAX_REGISTER || id == -1) {
121 return UNKNOWN_VALUE;
122 }
123 return registers[id];
124 }
125
126 @Override
127 public RootContext getRootContext() {
128 return this;
129 }
130
131 @Override
132 public Object getValue() {
133 return pointer;
134 }
135
136
137
138
139
140
141
142 public EvalContext getVariableContext(final QName variableName) {
143 return new InitialContext(new RootContext(jxpathContext, jxpathContext.getVariablePointer(variableName)));
144 }
145
146 @Override
147 public boolean nextNode() {
148 throw new UnsupportedOperationException();
149 }
150
151 @Override
152 public boolean nextSet() {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public boolean setPosition(final int position) {
158 throw new UnsupportedOperationException();
159 }
160
161
162
163
164
165
166
167 public int setRegisteredValue(final Object value) {
168 if (registers == null) {
169 registers = new Object[MAX_REGISTER];
170 for (int i = 0; i < MAX_REGISTER; i++) {
171 registers[i] = UNKNOWN_VALUE;
172 }
173 }
174 if (availableRegister >= MAX_REGISTER) {
175 return -1;
176 }
177 registers[availableRegister] = value;
178 availableRegister++;
179 return availableRegister - 1;
180 }
181
182 @Override
183 public String toString() {
184 return super.toString() + ":" + pointer.asPath();
185 }
186 }