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
51 /*** Supporting context information for resolving
52 * namespace prefixes, functions, and variables.
53 *
54 * <p>
55 * <b>NOTE:</b> This class is not typically used directly,
56 * but is exposed for writers of implementation-specific
57 * XPath packages.
58 * </p>
59 *
60 * @see org.jaxen.dom4j.Dom4jXPath XPath for dom4j
61 * @see org.jaxen.jdom.JDOMXPath XPath for JDOM
62 * @see org.jaxen.dom.DOMXPath XPath for W3C DOM
63 *
64 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
65 *
66 * @version $Id: ContextSupport.java,v 1.9 2005/02/10 17:40:02 elharo Exp $
67 */
68 public class ContextSupport
69 implements Serializable
70 {
71
72
73
74
75 /*** Function context. */
76 private transient FunctionContext functionContext;
77
78 /*** Namespace context. */
79 private NamespaceContext namespaceContext;
80
81 /*** Variable context. */
82 private VariableContext variableContext;
83
84 /*** Model navigator. */
85 private Navigator navigator;
86
87
88
89
90
91 /*** Construct an empty <code>ContextSupport</code>.
92 */
93 public ContextSupport()
94 {
95
96 }
97
98 /*** Construct.
99 *
100 * @param namespaceContext the NamespaceContext
101 * @param functionContext the FunctionContext
102 * @param variableContext the VariableContext
103 * @param navigator the model navigator
104 */
105 public ContextSupport(NamespaceContext namespaceContext,
106 FunctionContext functionContext,
107 VariableContext variableContext,
108 Navigator navigator)
109 {
110 setNamespaceContext( namespaceContext );
111 setFunctionContext( functionContext );
112 setVariableContext( variableContext );
113
114 this.navigator = navigator;
115 }
116
117
118
119
120
121 /*** Set the <code>NamespaceContext</code>.
122 *
123 * @param namespaceContext the namespace context
124 */
125 public void setNamespaceContext(NamespaceContext namespaceContext)
126 {
127 this.namespaceContext = namespaceContext;
128 }
129
130 /*** Retrieve the <code>NamespaceContext</code>.
131 *
132 * @return the namespace context
133 */
134 public NamespaceContext getNamespaceContext()
135 {
136 return this.namespaceContext;
137 }
138
139 /*** Set the <code>FunctionContext</code>.
140 *
141 * @param functionContext the function context
142 */
143 public void setFunctionContext(FunctionContext functionContext)
144 {
145 this.functionContext = functionContext;
146 }
147
148 /*** Retrieve the <code>FunctionContext</code>.
149 *
150 * @return the function context
151 */
152 public FunctionContext getFunctionContext()
153 {
154 return this.functionContext;
155 }
156
157 /*** Set the <code>VariableContext</code>.
158 *
159 * @param variableContext the variable context
160 */
161 public void setVariableContext(VariableContext variableContext)
162 {
163 this.variableContext = variableContext;
164 }
165
166 /*** Retrieve the <code>VariableContext</code>.
167 *
168 * @return the variable context
169 */
170 public VariableContext getVariableContext()
171 {
172 return this.variableContext;
173 }
174
175 /*** Retrieve the <code>Navigator</code>.
176 *
177 * @return the navigator
178 */
179 public Navigator getNavigator()
180 {
181 return this.navigator;
182 }
183
184
185
186 /*** Translate a namespace prefix to its URI.
187 *
188 * @param prefix The prefix
189 *
190 * @return the namespace URI mapped to the prefix
191 */
192 public String translateNamespacePrefixToUri(String prefix)
193 {
194
195 if ("xml".equals(prefix)) {
196 return "http://www.w3.org/XML/1998/namespace";
197 }
198 NamespaceContext context = getNamespaceContext();
199
200 if ( context != null )
201 {
202 return context.translateNamespacePrefixToUri( prefix );
203 }
204
205 return null;
206 }
207
208 /*** Retrieve a variable value.
209 *
210 * @param namespaceURI the function namespace URI
211 * @param prefix the function prefix
212 * @param localName the function name
213 *
214 * @return the variable value.
215 *
216 * @throws UnresolvableException if unable to locate a bound variable.
217 */
218 public Object getVariableValue( String namespaceURI,
219 String prefix,
220 String localName )
221 throws UnresolvableException
222 {
223 VariableContext context = getVariableContext();
224
225 if ( context != null )
226 {
227 return context.getVariableValue( namespaceURI, prefix, localName );
228 }
229 else
230 {
231 throw new UnresolvableException( "No variable context installed" );
232 }
233 }
234
235 /*** Retrieve a <code>Function</code>.
236 *
237 * @param namespaceURI the function namespace URI
238 * @param prefix the function prefix
239 * @param localName the function name
240 *
241 * @return the function object
242 *
243 * @throws UnresolvableException if unable to locate a bound function
244 */
245 public Function getFunction( String namespaceURI,
246 String prefix,
247 String localName )
248 throws UnresolvableException
249 {
250 FunctionContext context = getFunctionContext();
251
252 if ( context != null )
253 {
254 return context.getFunction( namespaceURI, prefix, localName );
255 }
256 else
257 {
258 throw new UnresolvableException( "No function context installed" );
259 }
260 }
261 }