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