KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > xpath > JAXPVariable


1 package net.sf.saxon.xpath;
2 import net.sf.saxon.expr.Binding;
3 import net.sf.saxon.expr.BindingReference;
4 import net.sf.saxon.expr.VariableDeclaration;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.EmptySequence;
8 import net.sf.saxon.value.QNameValue;
9 import net.sf.saxon.value.SequenceType;
10 import net.sf.saxon.value.Value;
11 import net.sf.saxon.om.ValueRepresentation;
12
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.xpath.XPathVariableResolver JavaDoc;
15
16
17 /**
18 * An object representing an XPath variable for use in the JAXP XPath API. The object
19  * is created at compile time when the parser tries to bind a variable reference; the
20  * value is fetched at run-time from the XPathVariableResolver. With this interface,
21  * there is no way of reporting a static error if the variable has not been declared.
22  * <p>
23  * In Saxon terms, this class is both a VariableDeclaration and a Binding. Unlike
24  * a normal VariableDeclaration, it isn't created in advance, but is created on demand
25  * when the parser encounters a variable reference. This actually means that if the
26  * XPath expression contains two references to the same variable, two VariableDeclarations
27  * will be created; however, they will be indistinguishable to the VariableResolver.
28  * Acting as a VariableDeclaration, the object goes through the motions of fixing up
29  * a binding to a variable reference (in practice, of course, there is exactly one
30  * reference to the variable). Acting as a run-time binding, it then evaluates the
31  * variable by calling the XPathVariableResolver supplied by the API caller. If no
32  * XPathVariableResolver was supplied, an error is reported when a variable is encountered;
33  * but if the variable resolver doesn't recognize the variable name, it returns null,
34  * which is treated as an empty sequence.
35  * </p>
36 */

37
38 public final class JAXPVariable implements VariableDeclaration, Binding {
39
40     private QNameValue name;
41     private XPathVariableResolver JavaDoc resolver;
42
43     /**
44     * Private constructor: for use only be the protected factory method make()
45     */

46
47     public JAXPVariable(QNameValue name, XPathVariableResolver JavaDoc resolver) {
48         this.name = name;
49         this.resolver = resolver;
50     };
51
52     /**
53      * Indicate whether the binding is local or global. A global binding is one that has a fixed
54      * value for the life of a query or transformation; any other binding is local.
55      */

56
57     public boolean isGlobal() {
58         return true;
59     }
60
61     /**
62     * Test whether it is permitted to assign to the variable using the saxon:assign
63     * extension element. This will only be for an XSLT global variable where the extra
64     * attribute saxon:assignable="yes" is present.
65     */

66
67     public final boolean isAssignable() {
68         return false;
69     }
70
71     /**
72      * If this is a local variable held on the local stack frame, return the corresponding slot number.
73      * In other cases, return -1.
74      */

75
76     public int getLocalSlotNumber() {
77         return -1;
78     }
79
80     /**
81      * Get the name of the variable. Used for diagnostic purposes only.
82      * @return the name of the variable, as a string (containing the raw QName)
83      */

84
85     public String JavaDoc getVariableName() {
86         return name.getStringValue();
87     }
88
89     /**
90      * Establish the fingerprint of the name of this variable.
91      * Dummy implementation, not used.
92      * @return -1, always
93      */

94
95     public int getNameCode() {
96         return -1;
97     }
98
99     /**
100     * Method called by the XPath expression parser to register a reference to this variable.
101     * This method should not be called by users of the API.
102     */

103
104     public void registerReference(BindingReference ref) {
105         ref.setStaticType(SequenceType.ANY_SEQUENCE, null, 0);
106         ref.fixup(this);
107     }
108
109     /**
110      * Get the value of the variable. This method is used by the XPath execution engine
111      * to retrieve the value.
112      * @param context The dynamic evaluation context
113      * @return The value of the variable
114      */

115
116     public ValueRepresentation evaluateVariable(XPathContext context) throws XPathException {
117         Object JavaDoc value = resolver.resolveVariable(
118                 (QName JavaDoc)name.makeQName(
119                         context.getController().getConfiguration()));
120         if (value == null) {
121             return EmptySequence.getInstance();
122         }
123         return Value.convertJavaObjectToXPath(
124                 value, SequenceType.ANY_SEQUENCE, context.getController().getConfiguration());
125     }
126
127     QName JavaDoc makeQName(QNameValue in) {
128         return new QName JavaDoc(in.getNamespaceURI(), in.getLocalName(), in.getPrefix());
129     }
130 }
131
132 //
133
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
134
// you may not use this file except in compliance with the License. You may obtain a copy of the
135
// License at http://www.mozilla.org/MPL/
136
//
137
// Software distributed under the License is distributed on an "AS IS" basis,
138
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
139
// See the License for the specific language governing rights and limitations under the License.
140
//
141
// The Original Code is: all this file.
142
//
143
// The Initial Developer of the Original Code is Michael H. Kay
144
//
145
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
146
//
147
// Contributor(s): none.
148
//
149
Popular Tags