KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > sql > SQLConnect


1 package com.icl.saxon.sql;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.style.*;
5 import com.icl.saxon.expr.*;
6 import org.xml.sax.SAXException JavaDoc;
7 import org.xml.sax.AttributeList JavaDoc;
8 import java.sql.*;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.TransformerConfigurationException JavaDoc;
11
12 /**
13 * An sql:connect element in the stylesheet.<BR>
14 */

15
16 public class SQLConnect extends StyleElement {
17
18     Expression database;
19     Expression driver;
20     Expression user;
21     Expression password;
22
23     /**
24     * Determine whether this node is an instruction.
25     * @return true - it is an instruction
26     */

27
28     public boolean isInstruction() {
29         return true;
30     }
31
32     /**
33     * Determine whether this type of element is allowed to contain a template-body
34     * @return true: yes, it may contain a template-body
35     */

36
37     public boolean mayContainTemplateBody() {
38         return true;
39     }
40
41     public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
42                
43         // Get mandatory database attribute
44

45         String JavaDoc dbAtt = attributeList.getValue("database");
46         if (dbAtt==null)
47             reportAbsence("database");
48         database = makeAttributeValueTemplate(dbAtt);
49
50         // Get driver attribute
51

52         String JavaDoc dbDriver = attributeList.getValue("driver");
53         if (dbDriver==null) {
54             if (dbAtt.substring(0,9).equals("jdbc:odbc")) {
55                 dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
56             } else {
57                 reportAbsence("driver");
58             }
59         }
60         driver = makeAttributeValueTemplate(dbDriver);
61     
62     
63         // Get and expand user attribute, which defaults to empty string
64

65         String JavaDoc userAtt = attributeList.getValue("user");
66         if (userAtt==null) {
67             user = new StringValue("");
68         } else {
69             user = makeAttributeValueTemplate(userAtt);
70         }
71         
72         // Get and expand password attribute, which defaults to empty string
73

74         String JavaDoc pwdAtt = attributeList.getValue("password");
75         if (pwdAtt==null) {
76             password = new StringValue("");
77         } else {
78             password = makeAttributeValueTemplate(pwdAtt);
79         }
80     }
81
82     public void validate() throws TransformerConfigurationException JavaDoc {
83         checkWithinTemplate();
84     }
85
86     public void process( Context context ) throws TransformerException JavaDoc {
87
88         // Establish the JDBC connection
89

90         Connection connection = null; // JDBC Database Connection
91
Statement sql = null; // JDBC SQL Statement
92

93         String JavaDoc dbString = database.evaluateAsString(context);
94         String JavaDoc dbDriverString = driver.evaluateAsString(context);
95         String JavaDoc userString = user.evaluateAsString(context);
96         String JavaDoc pwdString = password.evaluateAsString(context);
97
98         try {
99             // the following hack is necessary to load JDBC drivers
100
Class.forName(dbDriverString);
101
102             connection = DriverManager.getConnection(dbString, userString, pwdString);
103             sql = connection.createStatement();
104         } catch (Exception JavaDoc ex) {
105             throw new TransformerException JavaDoc("JDBC Connection Failure: " + ex.getMessage());
106         }
107
108         NodeInfo sourceDoc = (context.getCurrentNodeInfo()).getDocumentRoot();
109         context.getController().setUserData(sourceDoc, "sql:connection", connection);
110         context.getController().setUserData(sourceDoc, "sql:statement", sql);
111
112     }
113 }
114
115 //
116
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
117
// you may not use this file except in compliance with the License. You may obtain a copy of the
118
// License at http://www.mozilla.org/MPL/
119
//
120
// Software distributed under the License is distributed on an "AS IS" basis,
121
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
122
// See the License for the specific language governing rights and limitations under the License.
123
//
124
// The Original Code is: all this file.
125
//
126
// The Initial Developer of the Original Code is
127
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Additional Contributor(s): Rick Bonnett [rbonnett@acadia.net]
132
//
133
Popular Tags