KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.sf.saxon.value.StringValue;
12
13 import java.sql.Connection JavaDoc;
14 import java.sql.DriverManager JavaDoc;
15
16 /**
17 * An sql:connect element in the stylesheet.
18 */

19
20 public class SQLConnect extends ExtensionInstruction {
21
22     Expression database;
23     Expression driver;
24     Expression user;
25     Expression password;
26
27     public boolean mayContainSequenceConstructor() {
28         return false;
29     }
30
31     public void prepareAttributes() throws XPathException {
32
33         // Get mandatory database attribute
34

35         String JavaDoc dbAtt = attributeList.getValue("", "database");
36         if (dbAtt==null) {
37             reportAbsence("database");
38             dbAtt = ""; // for error recovery
39
}
40         database = makeAttributeValueTemplate(dbAtt);
41
42         // Get driver attribute
43

44         String JavaDoc dbDriver = attributeList.getValue("", "driver");
45         if (dbDriver==null) {
46             if (dbAtt.length()>9 && dbAtt.substring(0,9).equals("jdbc:odbc")) {
47                 dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
48             } else {
49                 reportAbsence("driver");
50             }
51         }
52         driver = makeAttributeValueTemplate(dbDriver);
53
54
55         // Get and expand user attribute, which defaults to empty string
56

57         String JavaDoc userAtt = attributeList.getValue("", "user");
58         if (userAtt==null) {
59             user = StringValue.EMPTY_STRING;
60         } else {
61             user = makeAttributeValueTemplate(userAtt);
62         }
63
64         // Get and expand password attribute, which defaults to empty string
65

66         String JavaDoc pwdAtt = attributeList.getValue("", "password");
67         if (pwdAtt==null) {
68             password = StringValue.EMPTY_STRING;
69         } else {
70             password = makeAttributeValueTemplate(pwdAtt);
71         }
72     }
73
74     public void validate() throws XPathException {
75         super.validate();
76         database = typeCheck("database", database);
77         driver = typeCheck("driver", driver);
78         user = typeCheck("user", user);
79         password = typeCheck("password", password);
80     }
81
82     public Expression compile(Executable exec) throws XPathException {
83         return new ConnectInstruction(database, driver, user, password);
84     }
85
86     private static class ConnectInstruction extends SimpleExpression {
87
88         public static final int DATABASE = 0;
89         public static final int DRIVER = 1;
90         public static final int USER = 2;
91         public static final int PASSWORD = 3;
92
93         public ConnectInstruction(Expression database,
94             Expression driver, Expression user, Expression password) {
95
96             Expression[] subs = {database, driver, user, password};
97             setArguments(subs);
98         };
99
100         /**
101          * A subclass must provide one of the methods evaluateItem(), iterate(), or process().
102          * This method indicates which of the three is provided.
103          */

104
105         public int getImplementationMethod() {
106             return Expression.EVALUATE_METHOD;
107         }
108
109         public int computeCardinality() {
110             return StaticProperty.EXACTLY_ONE;
111         }
112
113         public String JavaDoc getExpressionType() {
114             return "sql:connect";
115         }
116
117         public Item evaluateItem(XPathContext context) throws XPathException {
118
119             // Establish the JDBC connection
120

121             Connection JavaDoc connection = null; // JDBC Database Connection
122

123             String JavaDoc dbString = arguments[DATABASE].evaluateAsString(context);
124             String JavaDoc dbDriverString = arguments[DRIVER].evaluateAsString(context);
125             String JavaDoc userString = arguments[USER].evaluateAsString(context);
126             String JavaDoc pwdString = arguments[PASSWORD].evaluateAsString(context);
127
128             try {
129                 // the following hack is necessary to load JDBC drivers
130
Class.forName(dbDriverString);
131                 connection = DriverManager.getConnection(dbString, userString, pwdString);
132             } catch (Exception JavaDoc ex) {
133                 dynamicError("JDBC Connection Failure: " + ex.getMessage(), context);
134             }
135
136             return new ObjectValue(connection);
137
138         }
139     }
140 }
141
142 //
143
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
144
// you may not use this file except in compliance with the License. You may obtain a copy of the
145
// License at http://www.mozilla.org/MPL/
146
//
147
// Software distributed under the License is distributed on an "AS IS" basis,
148
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
149
// See the License for the specific language governing rights and limitations under the License.
150
//
151
// The Original Code is: all this file.
152
//
153
// The Initial Developer of the Original Code is Michael H. Kay.
154
//
155
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
156
//
157
// Additional Contributor(s): Rick Bonnett [rbonnett@acadia.net]
158
//
159
Popular Tags