KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > trans > Variable


1 package net.sf.saxon.trans;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.expr.Binding;
4 import net.sf.saxon.expr.BindingReference;
5 import net.sf.saxon.expr.VariableDeclaration;
6 import net.sf.saxon.expr.XPathContext;
7 import net.sf.saxon.om.ValueRepresentation;
8 import net.sf.saxon.value.EmptySequence;
9 import net.sf.saxon.value.QNameValue;
10 import net.sf.saxon.value.SequenceType;
11 import net.sf.saxon.value.Value;
12
13
14 /**
15 * An object representing an XPath variable for use in the standalone XPath API. The object
16 * can only be created by calling the declareVariable method of class StandaloneContext.
17 */

18
19 public final class Variable implements VariableDeclaration, Binding {
20
21     private QNameValue name;
22     private ValueRepresentation value;
23     private Configuration config;
24
25     /**
26     * Private constructor: for use only by the protected factory method make()
27     */

28
29     private Variable() {};
30
31     /**
32     * Factory method, for use by the declareVariable method of class StandaloneContext
33     */

34
35     public static Variable make(QNameValue name, Configuration config) {
36         Variable v = new Variable();
37         v.name = name;
38         v.config = config;
39         return v;
40     }
41
42     /**
43      * Factory method, retained for backwards compatibility
44      * @param qname the lexical QName of the variable name
45      * @deprecated since 8.5: use {@link #make(QNameValue, Configuration)}
46      */

47
48     public static Variable make(String JavaDoc qname, Configuration config) throws XPathException {
49         Variable v = new Variable();
50         int colon = qname.indexOf(':');
51         if (colon < 0) {
52             v.name = new QNameValue("", "", qname);
53         } else {
54             v.name = new QNameValue(qname.substring(0, colon), "http://saxon.sf.net/", qname.substring(colon+1));
55         }
56         v.config = config;
57         return v;
58     }
59
60
61     /**
62      * Indicate whether the binding is local or global. A global binding is one that has a fixed
63      * value for the life of a query or transformation; any other binding is local.
64      */

65
66     public boolean isGlobal() {
67         return true;
68     }
69
70     /**
71     * Test whether it is permitted to assign to the variable using the saxon:assign
72     * extension element. This will only be for an XSLT global variable where the extra
73     * attribute saxon:assignable="yes" is present.
74     */

75
76     public final boolean isAssignable() {
77         return false;
78     }
79
80     /**
81      * If this is a local variable held on the local stack frame, return the corresponding slot number.
82      * In other cases, return -1.
83      */

84
85     public int getLocalSlotNumber() {
86         return -1;
87     }
88
89     /**
90      * Get the name of the variable. Used for diagnostic purposes only.
91      * @return the name of the variable, as a string (containing the raw QName)
92      */

93
94     public String JavaDoc getVariableName() {
95         return name.toString();
96     }
97
98     /**
99      * Establish the nameCode of the name of this variable.
100      * @return the nameCode
101      */

102
103     public int getNameCode() {
104         return name.allocateNameCode(config.getNamePool());
105     }
106
107     /**
108      * Assign a value to the variable. This value may be changed between successive evaluations of
109      * a compiled XPath expression that references the variable.
110      * @param value the value of the variable, as a Java object. This is converted to the "best fit"
111      * XPath data type.
112      * @throws net.sf.saxon.trans.XPathException if the Java value cannot be converted to an XPath type
113      */

114
115     public void setValue(Object JavaDoc value) throws XPathException {
116         this.value = Value.convertJavaObjectToXPath(value, SequenceType.ANY_SEQUENCE, config);
117         if (this.value==null) {
118             this.value = EmptySequence.getInstance();
119         }
120     }
121
122     /**
123      * Assign a value to the variable. This value may be changed between successive evaluations of
124      * a compiled XPath expression that references the variable.
125      * @param value the value of the variable, which must be an instance of a class
126      * representing a value in the XPath model.
127      */

128
129     public void setXPathValue(ValueRepresentation value) {
130         this.value = value;
131         if (this.value==null) {
132             this.value = EmptySequence.getInstance();
133         }
134     }
135
136     /**
137     * Method called by the XPath expression parser to register a reference to this variable.
138     * This method should not be called by users of the API.
139     */

140
141     public void registerReference(BindingReference ref) {
142         ref.setStaticType(SequenceType.ANY_SEQUENCE, null, 0);
143         ref.fixup(this);
144     }
145
146     /**
147      * Get the value of the variable. This method is used by the XPath execution engine
148      * to retrieve the value.
149      * @param context The dynamic evaluation context
150      * @return The value of the variable
151      */

152
153     public ValueRepresentation evaluateVariable(XPathContext context) {
154         return value;
155     }
156 }
157
158 //
159
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
160
// you may not use this file except in compliance with the License. You may obtain a copy of the
161
// License at http://www.mozilla.org/MPL/
162
//
163
// Software distributed under the License is distributed on an "AS IS" basis,
164
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
165
// See the License for the specific language governing rights and limitations under the License.
166
//
167
// The Original Code is: all this file.
168
//
169
// The Initial Developer of the Original Code is Michael H. Kay
170
//
171
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
172
//
173
// Contributor(s): none.
174
//
175
Popular Tags