KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sql > SQLClose


1 package net.sf.saxon.sql;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.SimpleExpression;
4 import net.sf.saxon.expr.StaticProperty;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.instruct.Executable;
7 import net.sf.saxon.om.Item;
8 import net.sf.saxon.style.ExtensionInstruction;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.value.ObjectValue;
11
12 import java.sql.Connection JavaDoc;
13 import java.sql.SQLException JavaDoc;
14
15 /**
16 * An sql:close element in the stylesheet.
17 */

18
19 public class SQLClose extends ExtensionInstruction {
20
21     Expression connection = null;
22
23     public void prepareAttributes() throws XPathException {
24         String JavaDoc connectAtt = getAttributeList().getValue("", "connection");
25         if (connectAtt==null) {
26             reportAbsence("connection");
27         } else {
28             connection = makeExpression(connectAtt);
29         }
30     }
31
32     public void validate() throws XPathException {
33         super.validate();
34         connection = typeCheck("connection", connection);
35     }
36
37     public Expression compile(Executable exec) throws XPathException {
38         return new CloseInstruction(connection);
39     }
40
41     private static class CloseInstruction extends SimpleExpression {
42
43         public static final int CONNECTION = 0;
44
45         public CloseInstruction(Expression connect) {
46             Expression[] sub = {connect};
47             setArguments(sub);
48         }
49         /**
50          * A subclass must provide one of the methods evaluateItem(), iterate(), or process().
51          * This method indicates which of the three is provided.
52          */

53
54         public int getImplementationMethod() {
55             return Expression.EVALUATE_METHOD;
56         }
57
58         public String JavaDoc getExpressionType() {
59             return "sql:close";
60         }
61
62         public int computeCardinality() {
63             return StaticProperty.ALLOWS_ZERO_OR_ONE;
64         }
65
66         public Item evaluateItem(XPathContext context) throws XPathException {
67             Item conn = arguments[CONNECTION].evaluateItem(context);
68             if (!(conn instanceof ObjectValue && ((ObjectValue)conn).getObject() instanceof Connection JavaDoc) ) {
69                 dynamicError("Value of connection expression is not a JDBC Connection", context);
70             }
71             Connection JavaDoc connection = (Connection JavaDoc)((ObjectValue)conn).getObject();
72             try {
73                 connection.close();
74             } catch (SQLException JavaDoc ex) {
75                 dynamicError("(SQL) Failed to close connection: " + ex.getMessage(), context);
76             }
77             return null;
78         }
79     }
80 }
81
82 //
83
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
84
// you may not use this file except in compliance with the License. You may obtain a copy of the
85
// License at http://www.mozilla.org/MPL/
86
//
87
// Software distributed under the License is distributed on an "AS IS" basis,
88
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
89
// See the License for the specific language governing rights and limitations under the License.
90
//
91
// The Original Code is: all this file.
92
//
93
// The Initial Developer of the Original Code is Michael H. Kay.
94
//
95
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
96
//
97
// Additional Contributor(s): Rick Bonnett [rbonnett@acadia.net]
98
//
99
Popular Tags