User: jvanzyl Date: 00/11/07 15:38:15 Added: src/java/org/apache/turbine/torque/ant JDBCToXMLSchema.java SqlToXml.java VTorqueTask.java XMLSchemaToHTML.java Log: - adding new torque files. Revision Changes Path 1.1 turbine/src/java/org/apache/turbine/torque/ant/JDBCToXMLSchema.java Index: JDBCToXMLSchema.java =================================================================== package org.apache.turbine.torque.ant; /* * Copyright (c) 1997-2000 The Java Apache Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine", * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and * "Java Apache Project" must not be used to endorse or promote products * derived from this software without prior written permission. * * 5. Products derived from this software may not be called "Apache JServ" * nor may "Apache" nor "Apache JServ" appear in their names without * prior written permission of the Java Apache Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many * individuals on behalf of the Java Apache Group. For more information * on the Java Apache Project and the Apache JServ Servlet Engine project, * please see <http://java.apache.org/>. * */ // Java stuff. import java.sql.*; import java.io.*; import java.util.*; // W3C stuff. import org.w3c.dom.*; // XML stuff. import org.apache.xerces.dom.*; import org.apache.xml.serialize.*; /** * This class generates an XML schema of an existing database from * JDBC metadata. * * @author <a href="mailto:[email protected]">Jason van Zyl</a> * @version $Id: JDBCToXMLSchema.java,v 1.1 2000/11/07 23:38:11 jvanzyl Exp $ */ public class JDBCToXMLSchema { /** Torque properties. */ protected Properties props; /** Name of XML database schema produced. */ protected String xmlSchema; /** JDBC URL. */ protected String dbUrl; /** JDBC driver. */ protected String dbDriver; /** JDBC user name. */ protected String dbUser; /** JDBC password. */ protected String dbPassword; /** DOM document produced. */ protected DocumentImpl doc; /** Database Node to start things off. */ protected Node database; /** Hashtable of columns that have primary keys. */ protected Hashtable primaryKeys; /** Hashtable to track what table a column belongs to. */ protected Hashtable columnTableMap; /** Map of java.sql.Types: integer -> string representation. */ protected Properties sqlTypes; XMLSerializer xmlSerializer; /** * Default constructor. */ public JDBCToXMLSchema() { //props = Utils.loadProps("config/torque.props"); //sqlTypes = Utils.loadProps("config/torque.types"); // This is just to get this thing to compile. // This has to be turned into an Ant task proper. props = new Properties(); sqlTypes = new Properties(); xmlSchema = props.getProperty("jdbcXMLSchema"); dbUrl = props.getProperty("dbUrl"); dbDriver = props.getProperty("dbDriver"); dbUser = props.getProperty("dbUser"); dbPassword = props.getProperty("dbPassword"); System.err.println("Torque - JDBCToXMLSchema starting\n"); System.err.println("Your DB settings are:"); System.err.println("driver : "+dbDriver); System.err.println("URL : "+dbUrl); System.err.println("user : "+dbUser); System.err.println("password : "+dbPassword); doc = new DocumentImpl(); doc.appendChild(doc.createComment(" Autogenerated by JDBCToXMLSchema! ")); try { generateXML(); xmlSerializer = new XMLSerializer( new PrintWriter( new FileOutputStream(xmlSchema)), new OutputFormat(Method.XML,null,true)); xmlSerializer.serialize(doc); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } System.err.println("\nTorque - JDBCToXMLSchema finished"); } /** * Generates an XML database schema from JDBC metadata. * * @exception Exception, a generic exception. */ public void generateXML() throws Exception { // Load the Interbase Driver. Class.forName(dbDriver); System.err.println("DB driver sucessfuly instantiated"); // Attemtp to connect to a database. Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword); System.err.println("DB connection established"); // Get the database Metadata. DatabaseMetaData dbMetaData = con.getMetaData(); // The database map. Vector tableList = getTableNames(dbMetaData); database = doc.createElement("database"); // Build a database-wide column -> table map. columnTableMap = new Hashtable(); for (int i = 0; i < tableList.size(); i++) { String curTable = (String) tableList.elementAt(i); Vector columns = getColumns(dbMetaData, curTable); for (int j = 0; j < columns.size(); j++) { Vector v = (Vector) columns.elementAt(j); String name = (String) v.elementAt(0); columnTableMap.put(name, curTable); } } for (int i = 0; i < tableList.size(); i++) { // Add Table. String curTable = (String) tableList.elementAt(i); // dbMap.addTable(curTable); Element table = doc.createElement("table"); table.setAttribute("name", curTable); // Add Columns. // TableMap tblMap = dbMap.getTable(curTable); Vector columns = getColumns(dbMetaData, curTable); Vector primKeys = getPrimaryKeys(dbMetaData, curTable); Vector forgnKeys = getForeignKeys(dbMetaData, curTable); // Set the primary keys. primaryKeys = new Hashtable(); for (int k = 0; k < primKeys.size(); k++) { String curPrimaryKey = (String) primKeys.elementAt(k); primaryKeys.put(curPrimaryKey, curPrimaryKey); } // Foreign keys for this table. for (int l = 0; l < forgnKeys.size(); l++) { String curForeignKey = (String) forgnKeys.elementAt(l); String foreignKeyTable = (String) columnTableMap.get(curForeignKey); System.out.println(curForeignKey + " => " + foreignKeyTable); } for (int j = 0; j < columns.size(); j++) { Vector v = (Vector) columns.elementAt(j); String name = (String) v.elementAt(0); int type = ((Integer) v.elementAt(1)).intValue(); int size = ((Integer) v.elementAt(2)).intValue(); // From DatabaseMetaData.java // // Indicates column might not allow NULL values. Huh? // Might? Boy, that's a definitive answer. /* int columnNoNulls = 0; */ // Indicates column definitely allows NULL values. /* int columnNullable = 1; */ // Indicates NULLABILITY of column is unknown. /* int columnNullableUnknown = 2; */ Integer nullType = (Integer) v.elementAt(3); Element column = doc.createElement("column"); column.setAttribute("name", name); column.setAttribute("type", (String) sqlTypes.get( new Integer(type).toString())); if (size > 0 && (type == Types.CHAR || type == Types.VARCHAR || type == Types.LONGVARCHAR)) { column.setAttribute("size", new Integer(size).toString()); } if (nullType.intValue() == 0) { column.setAttribute("null", "false"); } if (primaryKeys.containsKey(name)) { column.setAttribute("primaryKey", "true"); } table.appendChild(column); } database.appendChild(table); } doc.appendChild(database); } /** * Get all the table names in the current database that are not * system tables. * * @param dbMeta JDBC database metadata. * @return A Vector with all the tables in a database. * @exception SQLException. */ public Vector getTableNames(DatabaseMetaData dbMeta) throws SQLException { ResultSet tableNames = dbMeta.getTables("",null, "%",null); Vector tables = new Vector(); while (tableNames.next()) { String name = tableNames.getString(3); String type = tableNames.getString(4); if (type.equals("TABLE")) { tables.addElement(name); } } return tables; } /** * Retrieves all the column names and types for a given table from * JDBC metadata. It returns a vector of vectors. Each element * of the returned vector is a vector with: * * element 0 => a String object for the column name. * element 1 => an Integer object for the column type. * element 2 => size of the column. * element 3 => null type. * * @param dbMeta JDBC metadata. * @param tableName Table from which to retrieve column * information. * @return A Vector with the list of columns in tableName. */ public Vector getColumns(DatabaseMetaData dbMeta, String tableName) throws SQLException { ResultSet columnSet = dbMeta.getColumns("",null, tableName, null); Vector columns = new Vector(); while (columnSet.next()) { String name = columnSet.getString(4); Integer sqlType = new Integer(columnSet.getString(5)); Integer size = new Integer(columnSet.getInt(7)); Integer nullType = new Integer(columnSet.getInt(11)); Vector v = new Vector(); v.addElement (name); v.addElement (sqlType); v.addElement (size); v.addElement (nullType); columns.addElement (v); } return columns; } /** * Retrieves a list of primary key columns for a given table. * * @param dbMeta JDBC metadata. * @param tableName Table from which to retrieve PK information. * @return A Vector with a list of PKs for tableName. */ public Vector getPrimaryKeys(DatabaseMetaData dbMeta, String tableName) throws SQLException { ResultSet primaryKeys = dbMeta.getPrimaryKeys("",null, tableName); Vector keys = new Vector(); while (primaryKeys.next()) { keys.addElement(primaryKeys.getString(4)); } return keys; } /** * Retrieves a list of foreign key columns for a given table. * * @param dbMeta JDBC metadata. * @param tableName Table from which to retrieve FK information. * @return A Vector with a list of foreign keys in tableName. */ public Vector getForeignKeys(DatabaseMetaData dbMeta, String tableName) throws SQLException { ResultSet foreignKeys = dbMeta.getImportedKeys("",null, tableName); Vector keys = new Vector(); while (foreignKeys.next()) { keys.addElement(foreignKeys.getString(8)); } return keys; } /** * Main function. * * @param args A String[] with the command line arguments. * @exception Exception, a generic exception. */ static public void main (String[] args) throws Exception { new JDBCToXMLSchema(); } } 1.1 turbine/src/java/org/apache/turbine/torque/ant/SqlToXml.java Index: SqlToXml.java =================================================================== package org.apache.turbine.torque.ant; /* * Copyright (c) 1997-2000 The Java Apache Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine", * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and * "Java Apache Project" must not be used to endorse or promote products * derived from this software without prior written permission. * * 5. Products derived from this software may not be called "Apache JServ" * nor may "Apache" nor "Apache JServ" appear in their names without * prior written permission of the Java Apache Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many * individuals on behalf of the Java Apache Group. For more information * on the Java Apache Project and the Apache JServ Servlet Engine project, * please see <http://java.apache.org/>. * */ // JDK Stuff import java.io.*; // Ant Stuff import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.turbine.torque.model.AppData; import org.apache.turbine.torque.transform.SQLToAppData; /** * An ant task for creating an xml schema from an sql schema * * @author <a href="mailto:[email protected]>Leon Messerschmidt</a> * @version $Id: SqlToXml.java,v 1.1 2000/11/07 23:38:12 jvanzyl Exp $ */ public class SqlToXml { private String inputFile; private String outputFile; /** * Get the current input file */ public String getInputFile () { return inputFile; } /** * Set the sql input file. This file must exist */ public void setInputFile (String v) { inputFile = v; } /** * Get the current output file. */ public String getOutputFile () { return outputFile; } /** * Set the current output file. If the file does not * exist it will be created. If the file exists all * it's contents will be replaced. */ public void setOutputFile (String v) { outputFile = v; } /** * Execute the input script with WM */ public void execute () throws Exception { try { System.out.println ("Parsing SQL Schema"); SQLToAppData sqlParser = new SQLToAppData(inputFile); AppData app = sqlParser.execute(); System.out.println ("Preparing to write xml schema"); FileWriter fr = new FileWriter (outputFile); BufferedWriter br = new BufferedWriter (fr); br.write (app.toString()); System.out.println ("Writing xml schema"); br.flush(); br.close(); } catch (Throwable e) { e.printStackTrace (); } } } 1.1 turbine/src/java/org/apache/turbine/torque/ant/VTorqueTask.java Index: VTorqueTask.java =================================================================== package org.apache.turbine.torque.ant; /* * Copyright (c) 1997-2000 The Java Apache Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine", * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and * "Java Apache Project" must not be used to endorse or promote products * derived from this software without prior written permission. * * 5. Products derived from this software may not be called "Apache JServ" * nor may "Apache" nor "Apache JServ" appear in their names without * prior written permission of the Java Apache Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many * individuals on behalf of the Java Apache Group. For more information * on the Java Apache Project and the Apache JServ Servlet Engine project, * please see <http://java.apache.org/>. * */ import java.util.Date; // Velocity Stuff import org.apache.velocity.Context; import org.apache.velocity.texen.ant.TexenTask; // Turbine Stuff import org.apache.turbine.torque.model.AppData; import org.apache.turbine.torque.transform.XmlToAppData; import org.apache.turbine.torque.util.TorqueStringUtil; /** * An ant task for generating output by using Velocity * * @author <a href="mailto:[email protected]">Jason van Zyl</a> * @version $Id: VTorqueTask.java,v 1.1 2000/11/07 23:38:13 jvanzyl Exp $ */ public class VTorqueTask extends TexenTask { private AppData app; private String xmlFile; private String targetPackage; /** * Get the current xml file */ public String getXmlFile () { return xmlFile; } /** * Set the xml file */ public void setXmlFile(String v) { xmlFile = v; } /** * Get the current target package */ public String getTargetPackage () { return targetPackage; } /** * Set the current target package. This is where * generated java classes will live. */ public void setTargetPackage (String v) { targetPackage = v; } public Context initControlContext() { Context context = new Context(); app = new AppData(); XmlToAppData xmlParser = new XmlToAppData(); app = xmlParser.parseFile(xmlFile); context.put("database", app); context.put("package", targetPackage); context.put("tstrings", new TorqueStringUtil()); context.put("now", new Date().toString()); return context; } } 1.1 turbine/src/java/org/apache/turbine/torque/ant/XMLSchemaToHTML.java Index: XMLSchemaToHTML.java =================================================================== package org.apache.turbine.torque.ant; /* * Copyright (c) 1997-2000 The Java Apache Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * 4. The names "Apache JServ", "Apache JServ Servlet Engine", "Turbine", * "Apache Turbine", "Turbine Project", "Apache Turbine Project" and * "Java Apache Project" must not be used to endorse or promote products * derived from this software without prior written permission. * * 5. Products derived from this software may not be called "Apache JServ" * nor may "Apache" nor "Apache JServ" appear in their names without * prior written permission of the Java Apache Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the Java Apache * Project for use in the Apache JServ servlet engine project * <http://java.apache.org/>." * * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many * individuals on behalf of the Java Apache Group. For more information * on the Java Apache Project and the Apache JServ Servlet Engine project, * please see <http://java.apache.org/>. * */ // Java stuff. import java.io.*; // SAX stuff. import org.xml.sax.SAXException; // Xalan stuff. import org.apache.xalan.xslt.XSLTProcessorFactory; import org.apache.xalan.xslt.XSLTInputSource; import org.apache.xalan.xslt.XSLTResultTarget; import org.apache.xalan.xslt.XSLTProcessor; /** * Simple sample code to show how to run the XSL processor from the * API. * * @author <a href="mailto:[email protected]">Jason van Zyl</a> * @version $Id: XMLSchemaToHTML.java,v 1.1 2000/11/07 23:38:14 jvanzyl Exp $ */ public class XMLSchemaToHTML { /** * Main function. * * @param args A String[] with the command line arguments. * @exception java.io.IOException. * @exception java.net.MalformedURLException. * @exception org.xml.sax.SAXException. */ public static void main(String[] args) throws java.io.IOException, java.net.MalformedURLException, org.xml.sax.SAXException { // Have the XSLTProcessorFactory obtain a interface to a new // XSLTProcessor object. XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); // Have the XSLTProcessor processor object transform "foo.xml" // to System.out, using the XSLT instructions found in // "foo.xsl". XSLTInputSource xmlSchema = new XSLTInputSource(args[0]); XSLTInputSource transform = new XSLTInputSource(args[1]); FileOutputStream fos = null; try { fos = new FileOutputStream(args[2]); } catch (IOException ioe) { System.out.println(ioe); ioe.printStackTrace(); } XSLTResultTarget target = new XSLTResultTarget(fos); processor.process(xmlSchema, transform, target); } }