KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > query > DynamicQueryContext


1 package net.sf.saxon.query;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.StandardErrorListener;
5 import net.sf.saxon.functions.Component;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.trans.DynamicError;
8 import net.sf.saxon.om.Item;
9 import net.sf.saxon.om.NodeInfo;
10 import net.sf.saxon.value.DateTimeValue;
11
12 import javax.xml.transform.ErrorListener JavaDoc;
13 import javax.xml.transform.URIResolver JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * This object represents a dynamic context for query execution. This class is used
18  * by the application writer to set up aspects of the dynamic context; it is not used
19  * operationally (or modified) by the XQuery processor itself, which copies all required
20  * information into its own internal representation.
21  */

22
23 public class DynamicQueryContext {
24
25     private Item contextItem;
26     private HashMap JavaDoc parameters;
27     private Configuration config;
28     private URIResolver JavaDoc uriResolver;
29     private ErrorListener errorListener;
30
31     private DateTimeValue currentDateTime;
32
33     public DynamicQueryContext(Configuration config) {
34         this.config = config;
35         uriResolver = config.getURIResolver();
36         errorListener = config.getErrorListener();
37         if (errorListener instanceof StandardErrorListener) {
38             ((StandardErrorListener)errorListener).setRecoveryPolicy(Configuration.DO_NOT_RECOVER);
39         }
40     }
41
42     /**
43      * Set the context item for evaluating the expression to be a node. If this method is not called,
44      * the context node will be undefined. The context node is available as the value of
45      * the expression ".".
46      * To obtain a NodeInfo by parsing a source document, see the method
47      * {@link net.sf.saxon.query.StaticQueryContext#buildDocument buildDocument}
48      * in class QueryProcessor.
49      *
50      * @param node The node that is to be the context node for the query
51      * @since 8.4
52      */

53
54     public void setContextNode(NodeInfo node) {
55         if (node==null) {
56             throw new NullPointerException JavaDoc("Context node cannot be null");
57         }
58         contextItem = node;
59     }
60
61     /**
62      * Set the context item for evaluating the expression. If this method is not called,
63      * the context node will be undefined. The context item is available as the value of
64      * the expression ".",.
65      * To obtain a node by parsing a source document, see the method
66      * {@link net.sf.saxon.query.StaticQueryContext#buildDocument buildDocument}
67      * in class QueryProcessor.
68      * @param item The item that is to be the context item for the query
69      * @since 8.4
70      */

71
72     public void setContextItem(Item item) {
73         if (item==null) {
74             throw new NullPointerException JavaDoc("Context item cannot be null");
75         }
76         contextItem = item;
77     }
78
79     /**
80      * Get the context node for the query, as set using setContextNode()
81      * or getContextItem() (provided the item is a node).
82      * @return the context node if set, or null otherwise.
83      */

84
85     public NodeInfo getContextNode() {
86         return (contextItem instanceof NodeInfo ?
87                 (NodeInfo)contextItem :
88                 null);
89     }
90
91      /**
92      * Get the context item for the query, as set using setContextItem() or setContextNode().
93      * @return the context item if set, or null otherwise.
94      * @since 8.4
95      */

96
97     public Item getContextItem() {
98         return contextItem;
99     }
100
101     /**
102      * Set a parameter for the query.
103      *
104      * @param expandedName The name of the parameter in "{uri}local-name" format.
105      * It is not an error to supply a value for a parameter that has not been
106      * declared, the parameter will simply be ignored. If the parameter has
107      * been declared in the query (as an external global variable) then it
108      * will be initialized with the value supplied.
109      * @param value The value of the parameter. This can be any valid Java
110      * object. It follows the same conversion rules as a value returned
111      * from a Saxon extension function. An error will occur at query
112      * execution time if the supplied value cannot be converted to the required
113      * type as declared in the query. For precise control of the type of the
114      * value, instantiate one of the classes in the net.sf.saxon.value package,
115      * for example net.sf.saxon.value.DayTimeDuration.
116      * @since 8.4
117      */

118
119     public void setParameter(String JavaDoc expandedName, Object JavaDoc value) {
120         if (parameters==null) {
121             parameters = new HashMap JavaDoc(10);
122         }
123         parameters.put(expandedName, value);
124     }
125
126     /**
127      * Reset the parameters to an empty list.
128      */

129
130     public void clearParameters() {
131         parameters = null;
132     }
133
134     /**
135      * Get the actual value of a parameter to the query.
136      *
137      * @param expandedName the name of the required parameter, in
138      * "{uri}local-name" format
139      * @return the value of the parameter, if it exists, or null otherwise
140      */

141
142     public Object JavaDoc getParameter(String JavaDoc expandedName) {
143         if (parameters==null) return null;
144         return parameters.get(expandedName);
145     }
146
147     /**
148      * Get the supplied parameters as a HashMap
149      */

150
151     protected HashMap JavaDoc getParameters() {
152         return parameters;
153     }
154
155     /**
156      * Set an object that will be used to resolve URIs used in
157      * fn:document() and related functions.
158      *
159      * @param resolver An object that implements the URIResolver interface, or
160      * null.
161      * @since 8.4
162      */

163
164     public void setURIResolver(URIResolver JavaDoc resolver) {
165         // System.err.println("Setting uriresolver to " + resolver + " on " + this);
166
uriResolver = resolver;
167     }
168
169     /**
170      * Get the URI resolver.
171      *
172      * @return the user-supplied URI resolver if there is one, or the
173      * system-defined one otherwise
174      * @since 8.4
175      */

176
177     public URIResolver JavaDoc getURIResolver() {
178         return uriResolver;
179     }
180
181     /**
182      * Set the error listener. The error listener receives reports of all run-time
183      * errors and can decide how to report them.
184      *
185      * @param listener the ErrorListener to be used
186      * @since 8.4
187      */

188
189     public void setErrorListener(ErrorListener listener) {
190         errorListener = listener;
191     }
192
193     /**
194      * Get the error listener.
195      *
196      * @return the ErrorListener in use
197      * @since 8.4
198      */

199
200     public ErrorListener getErrorListener() {
201         return errorListener;
202     }
203
204
205     /**
206      * Get the date and time set previously using {@link #setCurrentDateTime(net.sf.saxon.value.DateTimeValue)}
207      * or null if none has been set.
208      * @return the current date and time, if it has been set.
209      * @since 8.5
210      */

211
212     public DateTimeValue getCurrentDateTime() {
213         return currentDateTime;
214     }
215
216     /**
217      * Set a value to be used as the current date and time for the query. By default, the "real" current date and
218      * time are used. The main purpose of this method is that it allows repeatable results to be achieved when
219      * testing queries
220      * @param dateTime The value to be used as the current date and time. This must include a timezone.
221      * @since 8.5
222      */

223
224     public void setCurrentDateTime(DateTimeValue dateTime) throws XPathException {
225         this.currentDateTime = dateTime;
226         if (dateTime.getComponent(Component.TIMEZONE) == null) {
227             throw new DynamicError("Supplied date/time must include a timezone");
228         }
229     }
230
231 }
232
233
234 //
235
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
236
// you may not use this file except in compliance with the License. You may obtain a copy of the
237
// License at http://www.mozilla.org/MPL/
238
//
239
// Software distributed under the License is distributed on an "AS IS" basis,
240
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
241
// See the License for the specific language governing rights and limitations under the License.
242
//
243
// The Original Code is: all this file.
244
//
245
// The Initial Developer of the Original Code is Michael H. Kay
246
//
247
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
248
//
249
// Contributor(s): none
250
//
Popular Tags