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 ; 7 import org.xml.sax.AttributeList ; 8 import java.sql.*; 9 import javax.xml.transform.TransformerException ; 10 import javax.xml.transform.TransformerConfigurationException ; 11 12 15 16 public class SQLConnect extends StyleElement { 17 18 Expression database; 19 Expression driver; 20 Expression user; 21 Expression password; 22 23 27 28 public boolean isInstruction() { 29 return true; 30 } 31 32 36 37 public boolean mayContainTemplateBody() { 38 return true; 39 } 40 41 public void prepareAttributes() throws TransformerConfigurationException { 42 43 45 String dbAtt = attributeList.getValue("database"); 46 if (dbAtt==null) 47 reportAbsence("database"); 48 database = makeAttributeValueTemplate(dbAtt); 49 50 52 String 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 65 String userAtt = attributeList.getValue("user"); 66 if (userAtt==null) { 67 user = new StringValue(""); 68 } else { 69 user = makeAttributeValueTemplate(userAtt); 70 } 71 72 74 String 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 { 83 checkWithinTemplate(); 84 } 85 86 public void process( Context context ) throws TransformerException { 87 88 90 Connection connection = null; Statement sql = null; 93 String dbString = database.evaluateAsString(context); 94 String dbDriverString = driver.evaluateAsString(context); 95 String userString = user.evaluateAsString(context); 96 String pwdString = password.evaluateAsString(context); 97 98 try { 99 Class.forName(dbDriverString); 101 102 connection = DriverManager.getConnection(dbString, userString, pwdString); 103 sql = connection.createStatement(); 104 } catch (Exception ex) { 105 throw new TransformerException ("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 | Popular Tags |