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