KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > runtime > PreparedMediatorStatement


1 /*
2  * This file belongs to the XQuark distribution. Copyright (C) 2004 XQuark Group.
3  *
4  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7  *
8  * You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it at http://www.gnu.org/licenses/lgpl.html
9  *
10  * For more information on this software, see http://www.xquark.org.
11  */

12
13 package org.xquark.mediator.runtime;
14
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.math.BigDecimal JavaDoc;
18 import java.util.*;
19
20 import org.xquark.mediator.plan.ExecutionPlan;
21 import org.xquark.schema.SchemaException;
22 import org.xquark.schema.SimpleType;
23 import org.xquark.schema.Type;
24 import org.xquark.xml.xdbc.PreparedXMLStatement;
25 import org.xquark.xml.xdbc.XMLDBCException;
26 import org.xquark.xml.xdbc.XMLResultSet;
27 import org.xquark.xpath.datamodel.TypedDocumentImpl;
28 import org.xquark.xpath.datamodel.TypedValue;
29 import org.xquark.xpath.datamodel.TypedValueImpl;
30 import org.xquark.xquery.parser.*;
31
32 public class PreparedMediatorStatement extends MediatorStatement implements PreparedXMLStatement {
33
34     ExecutionPlan plan = null;
35
36     String JavaDoc query = null;
37
38     // external variables
39
HashMap variables = new HashMap();
40
41     protected static TypedDocumentImpl docimpl = null;
42     static {
43         docimpl = new TypedDocumentImpl();
44     }
45
46     /**
47      * @param connection
48      * @throws XMLDBCException
49      */

50     public PreparedMediatorStatement(_MediatorConnection connection, String JavaDoc baseURI, String JavaDoc query, Map cachedStatements) throws XMLDBCException {
51         super(connection);
52         this.query = query;
53         if (cachedStatements != null) {
54             plan = (ExecutionPlan) cachedStatements.get(query);
55         }
56         if (plan == null) {
57             plan = compileXQuery(query, baseURI);
58             if (cachedStatements != null) {
59                 cachedStatements.put(query, plan);
60             }
61         }
62     }
63
64     public HashMap getVariables() {
65         return variables;
66     }
67
68     private Variable getVariable(String JavaDoc ns, String JavaDoc varName) throws XMLDBCException {
69         HashMap varMap = plan.getModule().getVariables();
70         if (varMap == null)
71             throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined.");
72         varMap = (HashMap) varMap.get(ns);
73         if (varMap == null)
74             throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined.");
75         Variable var = (Variable) varMap.get(varName);
76         if (var == null)
77             throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined.");
78         return var;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setExternalVariable(java.lang.String, java.lang.String, java.lang.String)
85      */

86     public void setExternalVariable(String JavaDoc ns, String JavaDoc varName, String JavaDoc value) throws XMLDBCException {
87         Variable var = getVariable(ns, varName);
88         if (!var.getQType().isAtom())
89             throw new XMLDBCException("Variable " + var.getStringValue() + " cannot be set with string value.");
90         SimpleType st = var.getQType().getSimpleType();
91         Object JavaDoc obj = null;
92         try {
93             obj = st.convert(value);
94         } catch (SchemaException se) {
95             throw new XMLDBCException("Illegal string value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se);
96         }
97         HashMap map = (HashMap) variables.get(ns);
98         if (map == null) {
99             map = new HashMap();
100             variables.put(ns, map);
101         }
102         TypedValue typedValue = new TypedValueImpl(docimpl, value);
103         typedValue.setTypedValue(obj);
104         typedValue.setType(st);
105         map.put(varName, typedValue);
106     }
107
108     /*
109      * (non-Javadoc)
110      *
111      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setExternalVariable(java.lang.String, java.lang.String)
112      */

113     public void setExternalVariable(String JavaDoc varName, String JavaDoc value) throws XMLDBCException {
114         setExternalVariable(null, varName, value);
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setObject(java.lang.String, java.lang.String, java.lang.Object)
121      */

122     public void setObject(String JavaDoc ns, String JavaDoc varName, Object JavaDoc value) throws XMLDBCException {
123         Variable var = getVariable(ns, varName);
124         if (!var.getQType().isAtom())
125             throw new XMLDBCException("Variable " + var.getStringValue() + " complex type not supported yet.");
126         SimpleType st = var.getQType().getSimpleType();
127         // test for sequence
128
if (value instanceof ArrayList) {
129             if (true)
130                 throw new XMLDBCException("Variable " + var.getStringValue() + " sequence not supported yet.");
131             ArrayList list = (ArrayList) value;
132             if (list.size() > 1 && !var.getQType().isMultiple())
133                 throw new XMLDBCException("Illegal value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive());
134             ArrayList typeValues = new ArrayList(list.size());
135             for (int i = 0; i < list.size(); i++) {
136                 Object JavaDoc valuei = list.get(i);
137                 Object JavaDoc obj = null;
138                 try {
139                     obj = st.convertObject(valuei);
140                 } catch (SchemaException se) {
141                     throw new XMLDBCException("Illegal value " + valuei + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se);
142                 }
143                 TypedValue typedValue = new TypedValueImpl(docimpl, valuei.toString());
144                 typedValue.setTypedValue(obj);
145                 typedValue.setType(st);
146                 typeValues.add(typedValue);
147             }
148             HashMap map = (HashMap) variables.get(ns);
149             if (map == null) {
150                 map = new HashMap();
151                 variables.put(ns, map);
152             }
153             map.put(varName, typeValues);
154         } else {
155             Object JavaDoc obj = null;
156             try {
157                 obj = st.convertObject(value);
158             } catch (SchemaException se) {
159                 throw new XMLDBCException("Illegal value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se);
160             }
161             HashMap map = (HashMap) variables.get(ns);
162             if (map == null) {
163                 map = new HashMap();
164                 variables.put(ns, map);
165             }
166             TypedValue typedValue = new TypedValueImpl(docimpl, value.toString());
167             typedValue.setTypedValue(obj);
168             typedValue.setType(st);
169             map.put(varName, typedValue);
170         }
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setObject(java.lang.String, java.lang.Object)
177      */

178     public void setObject(String JavaDoc varName, Object JavaDoc value) throws XMLDBCException {
179         setObject(null, varName, value);
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBigDecimal(java.lang.String, java.lang.String, java.math.BigDecimal)
186      */

187     public void setBigDecimal(String JavaDoc ns, String JavaDoc varName, BigDecimal JavaDoc value) throws XMLDBCException {
188         setObject(ns, varName, value);
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBigDecimal(java.lang.String, java.math.BigDecimal)
195      */

196     public void setBigDecimal(String JavaDoc varName, BigDecimal JavaDoc value) throws XMLDBCException {
197         setBigDecimal(null, varName, value);
198     }
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setInputStream(java.lang.String, java.lang.String, java.io.InputStream, int)
204      */

205     public void setInputStream(String JavaDoc ns, String JavaDoc varName, InputStream JavaDoc value, int length) throws XMLDBCException {
206         byte[] bytes = new byte[length];
207         try {
208             int res = value.read(bytes, 0, length);
209             setBytes(ns, varName, bytes);
210         } catch (IOException JavaDoc ioe) {
211             throw new XMLDBCException(ioe.getMessage(), ioe);
212         }
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setInputStream(java.lang.String, java.io.InputStream, int)
219      */

220     public void setInputStream(String JavaDoc varName, InputStream JavaDoc value, int length) throws XMLDBCException {
221         setInputStream(null, varName, value, length);
222     }
223
224     /*
225      * (non-Javadoc)
226      *
227      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBytes(java.lang.String, java.lang.String, byte[])
228      */

229     public void setBytes(String JavaDoc ns, String JavaDoc varName, byte[] value) throws XMLDBCException {
230         setObject(ns, varName, value);
231     }
232
233     /*
234      * (non-Javadoc)
235      *
236      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBytes(java.lang.String, byte[])
237      */

238     public void setBytes(String JavaDoc varName, byte[] value) throws XMLDBCException {
239         setBytes(null, varName, value);
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBoolean(java.lang.String, java.lang.String, boolean)
246      */

247     public void setBoolean(String JavaDoc ns, String JavaDoc varName, boolean value) throws XMLDBCException {
248         setObject(ns, varName, new Boolean JavaDoc(value));
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setBoolean(java.lang.String, boolean)
255      */

256     public void setBoolean(String JavaDoc varName, boolean value) throws XMLDBCException {
257         setBoolean(null, varName, value);
258     }
259
260     /*
261      * (non-Javadoc)
262      *
263      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setDate(java.lang.String, java.lang.String, java.util.Date)
264      */

265     public void setDate(String JavaDoc ns, String JavaDoc varName, Date value) throws XMLDBCException {
266         setObject(ns, varName, value);
267     }
268
269     /*
270      * (non-Javadoc)
271      *
272      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setDate(java.lang.String, java.util.Date)
273      */

274     public void setDate(String JavaDoc varName, Date value) throws XMLDBCException {
275         setDate(null, varName, value);
276     }
277
278     /*
279      * (non-Javadoc)
280      *
281      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setDouble(java.lang.String, java.lang.String, double)
282      */

283     public void setDouble(String JavaDoc ns, String JavaDoc varName, double value) throws XMLDBCException {
284         setObject(ns, varName, new Double JavaDoc(value));
285     }
286
287     /*
288      * (non-Javadoc)
289      *
290      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setDouble(java.lang.String, double)
291      */

292     public void setDouble(String JavaDoc varName, double value) throws XMLDBCException {
293         setDouble(null, varName, value);
294     }
295
296     /*
297      * (non-Javadoc)
298      *
299      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setFloat(java.lang.String, java.lang.String, float)
300      */

301     public void setFloat(String JavaDoc ns, String JavaDoc varName, float value) throws XMLDBCException {
302         setObject(ns, varName, new Float JavaDoc(value));
303     }
304
305     /*
306      * (non-Javadoc)
307      *
308      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setFloat(java.lang.String, float)
309      */

310     public void setFloat(String JavaDoc varName, float value) throws XMLDBCException {
311         setFloat(null, varName, value);
312     }
313
314     /*
315      * (non-Javadoc)
316      *
317      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setLong(java.lang.String, java.lang.String, long)
318      */

319     public void setLong(String JavaDoc ns, String JavaDoc varName, long value) throws XMLDBCException {
320         setObject(ns, varName, new Long JavaDoc(value));
321     }
322
323     /*
324      * (non-Javadoc)
325      *
326      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setLong(java.lang.String, long)
327      */

328     public void setLong(String JavaDoc varName, long value) throws XMLDBCException {
329         setLong(null, varName, value);
330     }
331
332     /*
333      * (non-Javadoc)
334      *
335      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setString(java.lang.String, java.lang.String, java.lang.String)
336      */

337     public void setString(String JavaDoc ns, String JavaDoc varName, String JavaDoc value) throws XMLDBCException {
338         setObject(ns, varName, value);
339     }
340
341     /*
342      * (non-Javadoc)
343      *
344      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setString(java.lang.String, java.lang.String)
345      */

346     public void setString(String JavaDoc varName, String JavaDoc value) throws XMLDBCException {
347         setString(null, varName, value);
348     }
349
350     /*
351      * (non-Javadoc)
352      *
353      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setSequence(java.lang.String, java.lang.String, java.lang.String)
354      */

355     public void setSequence(String JavaDoc ns, String JavaDoc varName, List value) throws XMLDBCException {
356         setObject(ns, varName, value);
357     }
358
359     /*
360      * (non-Javadoc)
361      *
362      * @see org.xquark.xml.xdbc.PreparedXMLStatement#setSequence(java.lang.String, java.lang.String)
363      */

364     public void setSequence(String JavaDoc varName, List value) throws XMLDBCException {
365         setSequence(null, varName, value);
366     }
367
368     /*
369      * (non-Javadoc)
370      *
371      * @see org.xquark.xml.xdbc.PreparedXMLStatement#clearVariables()
372      */

373     public void clearVariables() throws XMLDBCException {
374         variables.clear();
375     }
376
377     /*
378      * (non-Javadoc)
379      *
380      * @see org.xquark.xml.xdbc.PreparedXMLStatement#execute()
381      */

382     public boolean execute() throws XMLDBCException {
383         // TODO Auto-generated method stub
384
throw new XMLDBCException("Not supported yet");
385         //return false;
386
}
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.xquark.xml.xdbc.PreparedXMLStatement#executeQuery()
392      */

393     public XMLResultSet executeQuery() throws XMLDBCException {
394         if (currentResultSet != null) {
395             currentResultSet.close();
396         }
397         if (plan == null) {
398             currentResultSet = null;
399             return null;
400         }
401         try {
402             currentResultSet = plan.executeQuery(this);
403             return currentResultSet;
404         } catch (XQueryException xe) {
405             currentResultSet = null;
406             throw new XMLDBCException(xe.getMessage(), xe);
407         }
408     }
409
410     public void close() throws XMLDBCException {
411         super.close();
412         super.closePreparedStatements();
413     }
414
415     public void closePreparedStatements() throws XMLDBCException {
416         // do nothing
417
}
418
419     public Collection getParameters() {
420         return variables.values();
421     }
422
423     public javax.xml.namespace.QName JavaDoc[] getParameterNames() {
424         ArrayList names = new ArrayList();
425         XQueryModule module = plan.getModule();
426         if (module.getDeclList() != null) {
427             for (int i = 0; i < module.getDeclList().size(); i++) {
428                 QName name = (QName) module.getDeclList().get(i);
429                 HashMap map = null;
430                 if (module.getVariables() != null) {
431                     map = (HashMap) module.getVariables().get(name.getNameSpace());
432                     if (map != null) {
433                         Variable vari = (Variable) map.get(name.getLocalName());
434                         if (vari != null && vari.getExpression() == null) {
435                             names.add(new javax.xml.namespace.QName JavaDoc(vari.getVarName().getNameSpace(), vari.getVarName().getLocalName()));
436                         }
437                     }
438                 }
439             }
440         }
441         javax.xml.namespace.QName JavaDoc qnames[] = new javax.xml.namespace.QName JavaDoc[names.size()];
442         return (javax.xml.namespace.QName JavaDoc[]) names.toArray(qnames);
443     }
444
445     public javax.xml.namespace.QName JavaDoc getParameterType(javax.xml.namespace.QName JavaDoc varName) {
446         XQueryModule module = plan.getModule();
447         org.xquark.xquery.parser.ExternalVariable extVar = (org.xquark.xquery.parser.ExternalVariable) module.getExternalVariables().get(varName);
448         Type type = extVar.getQType().getSimpleType().getBaseType();
449         return new javax.xml.namespace.QName JavaDoc(type.getNamespace(), type.getName());
450     }
451
452 }
Popular Tags