1 package org.jaxen;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.io.Serializable;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
53
54 /*** Wrapper around implementation-specific objects used
55 * as the context of an expression evaluation.
56 *
57 * <p>
58 * <b>NOTE:</b> This class is not typically used directly,
59 * but is exposed for writers of implementation-specific
60 * XPath packages.
61 * </p>
62 *
63 * <p>
64 * The <code>Context</code> bundles utilities together
65 * for evaluation of the expression. It wraps the provided
66 * objects for ease-of-passage through the expression AST.
67 * </p>
68 *
69 * @see ContextSupport
70 * @see BaseXPath
71 * @see org.jaxen.dom4j.Dom4jXPath XPath for dom4j
72 * @see org.jaxen.jdom.JDOMXPath XPath for JDOM
73 * @see org.jaxen.dom.DOMXPath XPath for W3C DOM
74 *
75 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
76 */
77 public class Context
78 implements Serializable
79 {
80
81
82
83
84 /*** Context-support. */
85 private ContextSupport contextSupport;
86
87 /*** Context node-set. */
88 private List nodeSet;
89
90 /*** Current context size. */
91 private int size;
92
93 /*** Current context position. */
94 private int position;
95
96
97
98
99
100 /*** Construct.
101 *
102 * @param contextSupport The context-support.
103 */
104 public Context(ContextSupport contextSupport)
105 {
106 this.contextSupport = contextSupport;
107 this.nodeSet = Collections.EMPTY_LIST;
108 }
109
110
111
112
113
114 /*** Set the context node-set.
115 *
116 * @param nodeSet The context node-set.
117 */
118 public void setNodeSet(List nodeSet)
119 {
120 this.nodeSet = nodeSet;
121 }
122
123 /*** Retrieve the context node-set.
124 *
125 * @return The context node-set.
126 */
127 public List getNodeSet()
128 {
129 return this.nodeSet;
130 }
131
132 /*** Set the <code>ContextSupport</code>.
133 *
134 * @param contextSupport The context-support.
135 */
136 public void setContextSupport(ContextSupport contextSupport)
137 {
138 this.contextSupport = contextSupport;
139 }
140
141 /*** Retrieve the <code>ContextSupport</code>.
142 *
143 * @return The context-support.
144 */
145 public ContextSupport getContextSupport()
146 {
147 return this.contextSupport;
148 }
149
150 /*** Retrieve the current <code>Navigator</code>.
151 *
152 * @return The navigator.
153 */
154 public Navigator getNavigator()
155 {
156 return getContextSupport().getNavigator();
157 }
158
159 /*** Translate a namespace prefix to its URI.
160 *
161 * @param prefix The prefix.
162 *
163 * @return The naemspace URI mapped to the prefix.
164 */
165 public String translateNamespacePrefixToUri(String prefix)
166 {
167 return getContextSupport().translateNamespacePrefixToUri( prefix );
168 }
169
170 /*** Retrieve a variable value.
171 *
172 * @param namespaceURI The function namespace URI.
173 * @param prefix The function prefix.
174 * @param localName The function name.
175 *
176 * @return The variable value.
177 *
178 * @throws UnresolvableException If unable to locate a bound variable.
179 */
180 public Object getVariableValue(String namespaceURI,
181 String prefix,
182 String localName)
183 throws UnresolvableException
184 {
185 return getContextSupport().getVariableValue( namespaceURI,
186 prefix,
187 localName );
188 }
189
190 /*** Retrieve a <code>Function</code>.
191 *
192 * @param namespaceURI The function namespace URI.
193 * @param prefix The function prefix.
194 * @param localName The function name.
195 *
196 * @return The function object.
197 *
198 * @throws UnresolvableException If unable to locate a bound function.
199 */
200 public Function getFunction(String namespaceURI,
201 String prefix,
202 String localName)
203 throws UnresolvableException
204 {
205 return getContextSupport().getFunction( namespaceURI,
206 prefix,
207 localName );
208 }
209
210
211
212
213
214 /*** Set the current size in the context node-set.
215 *
216 * @param size The size.
217 */
218 public void setSize(int size)
219 {
220 this.size = size;
221 }
222
223 /*** Retrieve the size of the context node-set.
224 *
225 * @return The size.
226 */
227 public int getSize()
228 {
229 return this.size;
230 }
231
232 /*** Set the current position in the context node-set.
233 *
234 * @param position The position
235 */
236 public void setPosition(int position)
237 {
238 this.position = position;
239 }
240
241 /*** Retrieve current position in the context node-set.
242 *
243 * @return The current position.
244 */
245 public int getPosition()
246 {
247 return this.position;
248 }
249
250
251
252
253
254 /*** Create a type-safe shallow copy.
255 *
256 * @return The duplicate.
257 */
258 public Context duplicate()
259 {
260 Context dupe = new Context( getContextSupport() );
261
262 List thisNodeSet = getNodeSet();
263
264 if ( thisNodeSet != null )
265 {
266 List dupeNodeSet = new ArrayList( thisNodeSet.size() );
267 dupeNodeSet.addAll( thisNodeSet );
268 dupe.setNodeSet( dupeNodeSet );
269 }
270
271 return dupe;
272 }
273 }