1 package org.jaxen; 2 3 /* 4 $Id$ 5 6 Copyright 2003 The Werken Company. All Rights Reserved. 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions are 10 met: 11 12 * Redistributions of source code must retain the above copyright 13 notice, this list of conditions and the following disclaimer. 14 15 * Redistributions in binary form must reproduce the above copyright 16 notice, this list of conditions and the following disclaimer in the 17 documentation and/or other materials provided with the distribution. 18 19 * Neither the name of the Jaxen Project nor the names of its 20 contributors may be used to endorse or promote products derived 21 from this software without specific prior written permission. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 26 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 27 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 35 */ 36 37 import java.io.Serializable; 38 39 /** Supporting context information for resolving 40 * namespace prefixes, functions, and variables. 41 * 42 * <p> 43 * <strong>NOTE:</strong> This class is not typically used directly, 44 * but is exposed for writers of implementation-specific 45 * XPath packages. 46 * </p> 47 * 48 * @see org.jaxen.dom4j.Dom4jXPath XPath for dom4j 49 * @see org.jaxen.jdom.JDOMXPath XPath for JDOM 50 * @see org.jaxen.dom.DOMXPath XPath for W3C DOM 51 * 52 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 53 * 54 * @version $Id$ 55 */ 56 public class ContextSupport implements Serializable { 57 58 /** 59 * 60 */ 61 private static final long serialVersionUID = 4494082174713652559L; 62 63 /** Function context. */ 64 private transient FunctionContext functionContext; 65 66 /** Namespace context. */ 67 private NamespaceContext namespaceContext; 68 69 /** Variable context. */ 70 private VariableContext variableContext; 71 72 /** Model navigator. */ 73 private Navigator navigator; 74 75 // ---------------------------------------------------------------------- 76 // Constructors 77 // ---------------------------------------------------------------------- 78 79 /** Construct an empty <code>ContextSupport</code>. 80 */ 81 public ContextSupport() 82 { 83 // intentionally left blank 84 } 85 86 /** Create a new ContextSupport object. 87 * 88 * @param namespaceContext the NamespaceContext 89 * @param functionContext the FunctionContext 90 * @param variableContext the VariableContext 91 * @param navigator the model navigator 92 */ 93 public ContextSupport(NamespaceContext namespaceContext, 94 FunctionContext functionContext, 95 VariableContext variableContext, 96 Navigator navigator) 97 { 98 setNamespaceContext( namespaceContext ); 99 setFunctionContext( functionContext ); 100 setVariableContext( variableContext ); 101 102 this.navigator = navigator; 103 } 104 105 // ---------------------------------------------------------------------- 106 // Instance methods 107 // ---------------------------------------------------------------------- 108 109 /** Set the <code>NamespaceContext</code>. 110 * 111 * @param namespaceContext the namespace context 112 */ 113 public void setNamespaceContext(NamespaceContext namespaceContext) 114 { 115 this.namespaceContext = namespaceContext; 116 } 117 118 /** Retrieve the <code>NamespaceContext</code>. 119 * 120 * @return the namespace context 121 */ 122 public NamespaceContext getNamespaceContext() 123 { 124 return this.namespaceContext; 125 } 126 127 /** Set the <code>FunctionContext</code>. 128 * 129 * @param functionContext the function context 130 */ 131 public void setFunctionContext(FunctionContext functionContext) 132 { 133 this.functionContext = functionContext; 134 } 135 136 /** Retrieve the <code>FunctionContext</code>. 137 * 138 * @return the function context 139 */ 140 public FunctionContext getFunctionContext() 141 { 142 return this.functionContext; 143 } 144 145 /** Set the <code>VariableContext</code>. 146 * 147 * @param variableContext the variable context 148 */ 149 public void setVariableContext(VariableContext variableContext) 150 { 151 this.variableContext = variableContext; 152 } 153 154 /** Retrieve the <code>VariableContext</code>. 155 * 156 * @return the variable context 157 */ 158 public VariableContext getVariableContext() 159 { 160 return this.variableContext; 161 } 162 163 /** Retrieve the <code>Navigator</code>. 164 * 165 * @return the navigator 166 */ 167 public Navigator getNavigator() 168 { 169 return this.navigator; 170 } 171 172 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 173 174 /** Translate a namespace prefix to its URI. 175 * 176 * @param prefix The prefix 177 * 178 * @return the namespace URI mapped to the prefix 179 */ 180 public String translateNamespacePrefixToUri(String prefix) 181 { 182 183 if ("xml".equals(prefix)) { 184 return "http://www.w3.org/XML/1998/namespace"; 185 } 186 NamespaceContext context = getNamespaceContext(); 187 188 if ( context != null ) 189 { 190 return context.translateNamespacePrefixToUri( prefix ); 191 } 192 193 return null; 194 } 195 196 /** Retrieve a variable value. 197 * 198 * @param namespaceURI the function namespace URI 199 * @param prefix the function prefix 200 * @param localName the function name 201 * 202 * @return the variable value. 203 * 204 * @throws UnresolvableException if unable to locate a bound variable. 205 */ 206 public Object getVariableValue( String namespaceURI, 207 String prefix, 208 String localName ) 209 throws UnresolvableException 210 { 211 VariableContext context = getVariableContext(); 212 213 if ( context != null ) 214 { 215 return context.getVariableValue( namespaceURI, prefix, localName ); 216 } 217 else 218 { 219 throw new UnresolvableException( "No variable context installed" ); 220 } 221 } 222 223 /** Retrieve a <code>Function</code>. 224 * 225 * @param namespaceURI the function namespace URI 226 * @param prefix the function prefix 227 * @param localName the function name 228 * 229 * @return the function object 230 * 231 * @throws UnresolvableException if unable to locate a bound function 232 */ 233 public Function getFunction( String namespaceURI, 234 String prefix, 235 String localName ) 236 throws UnresolvableException 237 { 238 FunctionContext context = getFunctionContext(); 239 240 if ( context != null ) 241 { 242 return context.getFunction( namespaceURI, prefix, localName ); 243 } 244 else 245 { 246 throw new UnresolvableException( "No function context installed" ); 247 } 248 } 249 250 }