Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ContextSupport |
|
| 1.8333333333333333;1.833 |
1 | package org.jaxen; |
|
2 | ||
3 | /* |
|
4 | $Id: ContextSupport.java,v 1.10 2005/06/01 11:19:59 elharo Exp $ |
|
5 | ||
6 | Copyright 2003 (C) The Werken Company. All Rights Reserved. |
|
7 | |
|
8 | Redistribution and use of this software and associated documentation |
|
9 | ("Software"), with or without modification, are permitted provided |
|
10 | that the following conditions are met: |
|
11 | ||
12 | 1. Redistributions of source code must retain copyright |
|
13 | statements and notices. Redistributions must also contain a |
|
14 | copy of this document. |
|
15 | |
|
16 | 2. Redistributions in binary form must reproduce the |
|
17 | above copyright notice, this list of conditions and the |
|
18 | following disclaimer in the documentation and/or other |
|
19 | materials provided with the distribution. |
|
20 | |
|
21 | 3. The name "jaxen" must not be used to endorse or promote |
|
22 | products derived from this Software without prior written |
|
23 | permission of The Werken Company. For written permission, |
|
24 | please contact bob@werken.com. |
|
25 | |
|
26 | 4. Products derived from this Software may not be called "jaxen" |
|
27 | nor may "jaxen" appear in their names without prior written |
|
28 | permission of The Werken Company. "jaxen" is a registered |
|
29 | trademark of The Werken Company. |
|
30 | |
|
31 | 5. Due credit should be given to The Werken Company. |
|
32 | (http://jaxen.werken.com/). |
|
33 | |
|
34 | THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS |
|
35 | ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT |
|
36 | NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
37 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
|
38 | THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
39 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
40 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
41 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
42 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|
43 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
44 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
45 | OF THE POSSIBILITY OF SUCH DAMAGE. |
|
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 | // Constructors |
|
86 | // ---------------------------------------------------------------------- |
|
87 | ||
88 | /** Construct an empty <code>ContextSupport</code>. |
|
89 | */ |
|
90 | public ContextSupport() |
|
91 | 12 | { |
92 | // intentionally left blank |
|
93 | 12 | } |
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 | 4890 | { |
107 | 4890 | setNamespaceContext( namespaceContext ); |
108 | 4890 | setFunctionContext( functionContext ); |
109 | 4890 | setVariableContext( variableContext ); |
110 | ||
111 | 4890 | this.navigator = navigator; |
112 | 4890 | } |
113 | ||
114 | // ---------------------------------------------------------------------- |
|
115 | // Instance methods |
|
116 | // ---------------------------------------------------------------------- |
|
117 | ||
118 | /** Set the <code>NamespaceContext</code>. |
|
119 | * |
|
120 | * @param namespaceContext the namespace context |
|
121 | */ |
|
122 | public void setNamespaceContext(NamespaceContext namespaceContext) |
|
123 | { |
|
124 | 5178 | this.namespaceContext = namespaceContext; |
125 | 5178 | } |
126 | ||
127 | /** Retrieve the <code>NamespaceContext</code>. |
|
128 | * |
|
129 | * @return the namespace context |
|
130 | */ |
|
131 | public NamespaceContext getNamespaceContext() |
|
132 | { |
|
133 | 13482 | 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 | 5064 | this.functionContext = functionContext; |
143 | 5064 | } |
144 | ||
145 | /** Retrieve the <code>FunctionContext</code>. |
|
146 | * |
|
147 | * @return the function context |
|
148 | */ |
|
149 | public FunctionContext getFunctionContext() |
|
150 | { |
|
151 | 12774 | 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 | 5082 | this.variableContext = variableContext; |
161 | 5082 | } |
162 | ||
163 | /** Retrieve the <code>VariableContext</code>. |
|
164 | * |
|
165 | * @return the variable context |
|
166 | */ |
|
167 | public VariableContext getVariableContext() |
|
168 | { |
|
169 | 258 | return this.variableContext; |
170 | } |
|
171 | ||
172 | /** Retrieve the <code>Navigator</code>. |
|
173 | * |
|
174 | * @return the navigator |
|
175 | */ |
|
176 | public Navigator getNavigator() |
|
177 | { |
|
178 | 2959236 | 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 | 13308 | if ("xml".equals(prefix)) { |
193 | 18 | return "http://www.w3.org/XML/1998/namespace"; |
194 | } |
|
195 | 13290 | NamespaceContext context = getNamespaceContext(); |
196 | ||
197 | 13290 | if ( context != null ) |
198 | { |
|
199 | 13290 | return context.translateNamespacePrefixToUri( prefix ); |
200 | } |
|
201 | ||
202 | 0 | 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 | 84 | VariableContext context = getVariableContext(); |
221 | ||
222 | 84 | if ( context != null ) |
223 | { |
|
224 | 84 | return context.getVariableValue( namespaceURI, prefix, localName ); |
225 | } |
|
226 | else |
|
227 | { |
|
228 | 0 | 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 | 12594 | FunctionContext context = getFunctionContext(); |
248 | ||
249 | 12594 | if ( context != null ) |
250 | { |
|
251 | 12594 | return context.getFunction( namespaceURI, prefix, localName ); |
252 | } |
|
253 | else |
|
254 | { |
|
255 | 0 | throw new UnresolvableException( "No function context installed" ); |
256 | } |
|
257 | } |
|
258 | } |