View Javadoc

1   package org.jaxen;
2   
3   /*
4    $Id: ContextSupport.java,v 1.9 2005/02/10 17:40:02 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.9 2005/02/10 17:40:02 elharo Exp $
67   */
68  public class ContextSupport
69      implements Serializable
70  {
71      // ----------------------------------------------------------------------
72      //     Instance methods
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      //     Constructors
89      // ----------------------------------------------------------------------
90      
91      /*** Construct an empty <code>ContextSupport</code>.
92       */
93      public ContextSupport()
94      {
95          // intentionally left blank
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     //     Instance methods
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 }