fedor       01/05/04 14:58:59

  Modified:    src/java/org/apache/turbine/torque/engine/database/model
                        Database.java Table.java
  Added:       src/java/org/apache/turbine/torque TorqueDataDTDTask.java
                        TorqueDataDumpTask.java TorqueDataSQLTask.java
               src/java/org/apache/turbine/torque/engine/database/transform
                        XmlToData.java
  Log:
  stuff for Data dump/load
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/torque/TorqueDataDTDTask.java
  
  Index: TorqueDataDTDTask.java
  ===================================================================
  package org.apache.turbine.torque;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  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. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``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 APACHE SOFTWARE FOUNDATION 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 Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Date;
  
  import org.apache.velocity.context.Context;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.texen.ant.TexenTask;
  
  import org.apache.turbine.torque.engine.database.model.AppData;
  import org.apache.turbine.torque.engine.database.transform.XmlToAppData;
  
  /**
   * An extended Texen task used for generating SQL source from
   * an XML schema describing a database structure.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]>Fedor Karpelevitch</a>
   * @version $Id: TorqueDataDTDTask.java,v 1.1 2001/05/04 21:58:54 fedor Exp $
   */
  public class TorqueDataDTDTask extends TexenTask
  {
      /**
       * Application model. In this case a database model.
       */
      private AppData app;
  
      /**
       * XML that describes the database model, this is transformed
       * into the application model object.
       */
      private String xmlFile;
      
      /**
       * Get the xml schema describing the application
       * model.
       *
       * @return String xml schema file.
       */
      public String getXmlFile ()
      {
          return xmlFile;
      }
  
      /**
       * Set the xml schema describing the application
       * model.
       *
       * @param String xml schema file.
       */
      public void setXmlFile(String v)
      {
          xmlFile = v;
      }
  
      /**
       * Set up the initialial context for generating the
       * SQL from the XML schema.
       */
      public Context initControlContext()
      {
          /*
           * Create a new Velocity context.
           */
          Context context = new VelocityContext();
          
          /*
           * Transform the XML database schema into an
           * object that represents our model.
           */
          XmlToAppData xmlParser = new XmlToAppData();
          app = xmlParser.parseFile(xmlFile);
          
          /*
           * Place our model in the context.
           */
          context.put("appData", app);
  
          return context;
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/torque/TorqueDataDumpTask.java
  
  Index: TorqueDataDumpTask.java
  ===================================================================
  package org.apache.turbine.torque;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  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. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``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 APACHE SOFTWARE FOUNDATION 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 Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Date;
  import java.util.Iterator;
  import java.util.Properties;
  import java.util.NoSuchElementException;
  
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  import org.apache.velocity.context.Context;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.texen.ant.TexenTask;
  import org.apache.velocity.runtime.configuration.Configuration;
  
  import org.apache.turbine.torque.engine.database.model.AppData;
  import org.apache.turbine.torque.engine.database.model.Database;
  import org.apache.turbine.torque.engine.database.transform.XmlToAppData;
  
  /**
   * An extended Texen task used for dumping data from db into XML
   *
   * @author <a href="mailto:[EMAIL PROTECTED]>Fedor Karpelevitch</a>
   * @version $Id: TorqueDataDumpTask.java,v 1.1 2001/05/04 21:58:54 fedor Exp $
   */
  public class TorqueDataDumpTask extends TexenTask
  {
      /**
       * Application model. In this case a database model.
       */
      private AppData app;
  
      private String databaseName;
  
      /**
       * XML that describes the database model, this is transformed
       * into the application model object.
       */
      private String xmlFile;
  
      /**
       * Get the xml schema describing the application
       * model.
       *
       * @return String xml schema file.
       */
      public String getXmlFile ()
      {
          return xmlFile;
      }
  
      /**
       * Set the xml schema describing the application
       * model.
       *
       * @param String xml schema file.
       */
      public void setXmlFile(String v)
      {
          xmlFile = v;
      }
  
      /**
       *  Get the database name to dump
       *
       *
       */
      public String getDatabaseName ()
      {
          return databaseName;
      }
  
      /**
       * Set the database name
       *
       * @param String database name
       */
      public void setDatabaseName(String v)
      {
          databaseName = v;
      }
  
      /**
       *  Initializes initial context
       */
      public Context initControlContext()
      {
          /*
           * Create a new Velocity context.
           */
          Context context = new VelocityContext();
  
          /*
           * Transform the XML database schema into an
           * object that represents our model.
           */
          XmlToAppData xmlParser = new XmlToAppData();
          app = xmlParser.parseFile(xmlFile);
  
          /*
           * Place our model in the context.
           */
          Database dbm = app.getDatabase(databaseName);
          if (dbm==null)
          {
              dbm = app.getDatabases()[0];
          }
          context.put("databaseModel", dbm);
  
          context.put("dataset", "all");
  
          Configuration props = getContextProperties();
  
          String dbUrl = (String)props.getProperty("databaseUrl");
          String dbDriver = (String)props.getProperty("databaseDriver");
          String dbUser = (String)props.getProperty("databaseUser");
          String dbPassword = (String)props.getProperty("databasePassword");
  
          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);
          try
          {
              Class.forName(dbDriver);
              System.err.println("DB driver sucessfuly instantiated");
  
              // Attemtp to connect to a database.
  
              Connection conn = DriverManager.getConnection(dbUrl,
                                                       dbUser,
                                                       dbPassword);
              System.err.println("DB connection established");
              context.put("tableTool", new TableTool(conn));
          }
          catch (SQLException se)
          {
              System.err.println("SQLException while connecting to DB:");
              se.printStackTrace();
          }
          catch (ClassNotFoundException cnfe)
          {
              System.err.println("cannot load driver:");
              cnfe.printStackTrace();
          }
  
          return context;
      }
  
      /**
       *  A nasty do-it-all tool class. It serves as:
       *  <ul>
       *      <li>context tool to fetch a table iterator</li>
       *      <li>the abovenamed iterator which iterates over the table</li>
       *      <li>getter for the table fields</li>
       *  </ul>
       */
      public class TableTool implements Iterator
      {
          private Connection conn;
          private ResultSet rs;
          private boolean isEmpty;
  
          public TableTool(Connection conn)
          {
              this.conn = conn;
          }
  
          public TableTool(ResultSet rs) throws SQLException
          {
              this.rs = rs;
              this.isEmpty = !rs.isBeforeFirst();
          }
  
          public TableTool fetch(String tableName) throws SQLException
          {
              System.err.println();
              System.err.print("fetching table " + tableName);
              return
                  new TableTool(conn
                      .createStatement()
                          .executeQuery("SELECT * FROM "+tableName));
          }
  
  
          public boolean hasNext()
          {
              try
              {
                  return ! (rs.isLast() || this.isEmpty);
              }
              catch (SQLException se)
              {
                  System.err.println("SQLException :");
                  se.printStackTrace();
              }
              return false;
          }
  
          public Object next() throws NoSuchElementException
          {
              try
              {
                  System.err.print(".");
                  rs.next();
              }
              catch (SQLException se)
              {
                  System.err.println("SQLException while iterating:");
                  se.printStackTrace();
                  throw new NoSuchElementException(se.getMessage());
              }
              return this;
          }
  
          public String get(String columnName)
          {
              try
              {
                  return rs.getString(columnName);
              }
              catch (SQLException se)
              {
                  System.err.println("SQLException fetching value " +
                      columnName+":"+ se.getMessage());
              }
              return null;
          }
  
          public void remove() throws UnsupportedOperationException
          {
              throw new UnsupportedOperationException();
          }
      }
  }
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/torque/TorqueDataSQLTask.java
  
  Index: TorqueDataSQLTask.java
  ===================================================================
  package org.apache.turbine.torque;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  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. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``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 APACHE SOFTWARE FOUNDATION 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 Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Date;
  import java.util.List;
  
  import org.apache.velocity.context.Context;
  import org.apache.velocity.VelocityContext;
  import org.apache.velocity.texen.ant.TexenTask;
  
  import org.apache.turbine.torque.engine.database.model.AppData;
  import org.apache.turbine.torque.engine.database.model.Database;
  import org.apache.turbine.torque.engine.database.transform.XmlToAppData;
  import org.apache.turbine.torque.engine.database.transform.XmlToData;
  
  /**
   * An extended Texen task used for generating SQL source from
   * an XML schema describing a database structure.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
   * @author <a href="mailto:[EMAIL PROTECTED]>Fedor Karpelevitch</a>
   * @version $Id: TorqueDataSQLTask.java,v 1.1 2001/05/04 21:58:54 fedor Exp $
   */
  public class TorqueDataSQLTask extends TexenTask
  {
      /**
       * Application model. In this case a database model.
       */
      private AppData app;
  
      /**
       * XML that describes the database model, this is transformed
       * into the application model object.
       */
      private String xmlFile;
      private String dataXmlFile;
      private String dataDTD;
  
      /**
       * The target database(s) we are generating SQL
       * for. Right now we can only deal with a single
       * target, but we will support multiple targets
       * soon.
       */
      private String targetDatabase;
  
      private String databaseName;
  
      /**
       * Get the xml schema describing the application
       * model.
       *
       * @return String xml schema file.
       */
      public String getXmlFile ()
      {
          return xmlFile;
      }
  
      /**
       * Set the xml schema describing the application
       * model.
       *
       * @param String xml schema file.
       */
      public void setXmlFile(String v)
      {
          xmlFile = v;
      }
  
      public void setDataXmlFile(String v)
      {
          dataXmlFile = v;
      }
  
      public String getDataXmlFile ()
      {
          return dataXmlFile;
      }
      /**
       * Get the current target database.
       *
       * @return String target database(s)
       */
      public String getTargetDatabase ()
      {
          return targetDatabase;
      }
  
      /**
       * Set the current target database.  This is where
       * generated java classes will live.
       *
       * @param String target database(s)
       */
      public void setTargetDatabase (String v)
      {
          targetDatabase = v;
      }
  
      public String getDatabaseName ()
      {
          return databaseName;
      }
  
      public void setDatabaseName (String v)
      {
          databaseName = v;
      }
  
      public String getDataDTD ()
      {
          return dataDTD;
      }
  
      public void setDataDTD (String v)
      {
          dataDTD = v;
      }
  
      /**
       * Set up the initialial context for generating the
       * SQL from the XML schema.
       */
      public Context initControlContext()
      {
          /*
           * Create a new Velocity context.
           */
          Context context = new VelocityContext();
          
          /*
           * Transform the XML database schema into an
           * object that represents our model.
           */
          XmlToAppData xmlParser = new XmlToAppData();
          app = xmlParser.parseFile(xmlFile);
  
          Database db = app.getDatabase(databaseName);
          if (db==null)
          {
              db = app.getDatabases()[0];
          }
          try
          {
              XmlToData dataXmlParser = new XmlToData(db,dataDTD);
              List data = dataXmlParser.parseFile(dataXmlFile);
              context.put("data", data);
          }
          catch (Exception e)
          {
              System.err.println("Exception parsing data XML:");
              e.printStackTrace();
          }
          /*
           * Place our model in the context.
           */
          context.put("appData", app);
  
          /*
           * Place the target database in the context.
           */
          context.put("targetDatabase", targetDatabase);
  
          return context;
      }
  }
  
  
  
  
  
  
  1.4       +15 -10    
jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Database.java
  
  Index: Database.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Database.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Database.java     2001/04/02 20:49:52     1.3
  +++ Database.java     2001/05/04 21:58:55     1.4
  @@ -69,7 +69,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]>Leon Messerschmidt</a>
    * @author <a href="mailto:[EMAIL PROTECTED]>John McNally</a>
  - * @version $Id: Database.java,v 1.3 2001/04/02 20:49:52 jmcnally Exp $
  + * @version $Id: Database.java,v 1.4 2001/05/04 21:58:55 fedor Exp $
    */
   public class Database
   {
  @@ -81,6 +81,8 @@
       private String baseClass;
       private String basePeer;
       private AppData dbParent;
  +    private Hashtable tablesByName = new Hashtable();
  +    private Hashtable tablesByJavaName = new Hashtable();
   
       /**
        * Default Constructor
  @@ -196,18 +198,19 @@
        */
       public Table getTable (String name)
       {
  -        for (Iterator i = tableList.iterator() ; i.hasNext() ;)
  -        {
  -            Table tbl = (Table ) i.next();
  -            if (tbl.getName().equals(name))
  -            {
  -                return tbl;
  -            }
  -        }
  -        return null;
  +        return (Table)tablesByName.get(name);
       }
   
       /**
  +     * Return the table with the specified javaName.
  +     * @return A Table object.  If it does not exist it returns null
  +     */
  +    public Table getTableByJavaName (String javaName)
  +    {
  +        return (Table)tablesByJavaName.get(javaName);
  +    }
  +    
  +    /**
        * An utility method to add a new table from
        * an xml attribute.
        */
  @@ -227,6 +230,8 @@
       {
           tbl.setDatabase (this);
           tableList.add(tbl);
  +        tablesByName.put(tbl.getName(), tbl);
  +        tablesByJavaName.put(tbl.getJavaName(), tbl);
       }
   
       /**
  
  
  
  1.11      +17 -13    
jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Table.java
  
  Index: Table.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/model/Table.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Table.java        2001/04/20 00:25:43     1.10
  +++ Table.java        2001/05/04 21:58:56     1.11
  @@ -69,7 +69,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Leon Messerschmidt</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
  - * @version $Id: Table.java,v 1.10 2001/04/20 00:25:43 jmcnally Exp $
  + * @version $Id: Table.java,v 1.11 2001/05/04 21:58:56 fedor Exp $
    */
   public class Table
   {
  @@ -92,7 +92,8 @@
       // private String pkg;
       private String baseClass;
       private String basePeer;
  -    
  +    private Hashtable columnsByName = new Hashtable();
  +    private Hashtable columnsByJavaName = new Hashtable();
   
       /**
        * Default Constructor
  @@ -203,6 +204,8 @@
               inheritanceColumn = col;
           }
           columnList.add(col);
  +        columnsByName.put(col.getName(), col);
  +        columnsByJavaName.put(col.getJavaName(), col);
           col.setPosition(columnList.size());
       }
   
  @@ -550,7 +553,7 @@
       }
   
       /**
  -     * Returns an Array containing all the FKs in the table
  +     * Returns an Array containing all the UKs in the table
        */
       public Unique[] getUnices()
       {
  @@ -565,20 +568,21 @@
   
   
       /**
  -     * Returns a Spesified column.
  +     * Returns a specified column.
        * @return Return a Column object or null if it does not exist.
        */
       public Column getColumn(String name)
       {
  -        for (Iterator iter = columnList.iterator(); iter.hasNext(); )
  -        {
  -            Column col = (Column) iter.next();
  -            if (col.getName().equals(name))
  -            {
  -                return col;
  -            }
  -        }
  -        return null;
  +        return (Column)columnsByName.get(name);
  +    }
  +
  +    /**
  +     * Returns a specified column.
  +     * @return Return a Column object or null if it does not exist.
  +     */
  +    public Column getColumnByJavaName(String javaName)
  +    {
  +        return (Column)columnsByJavaName.get(javaName);
       }
   
       /**
  
  
  
  1.1                  
jakarta-turbine/src/java/org/apache/turbine/torque/engine/database/transform/XmlToData.java
  
  Index: XmlToData.java
  ===================================================================
  package org.apache.turbine.torque.engine.database.transform;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  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. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``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 APACHE SOFTWARE FOUNDATION 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 Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  // JDK classes
  import java.io.*;
  import java.util.*;
  import java.net.URL;
  import java.net.MalformedURLException;
  
  // Xerces classes
  import org.xml.sax.*;
  import org.xml.sax.helpers.*;
  import org.apache.xerces.parsers.*;
  
  // Turbine
  import org.apache.turbine.services.db.*;
  import org.apache.turbine.torque.engine.database.model.*;
  import org.apache.turbine.util.Log;
  
  /**
   * A Class that is used to parse an input
   * xml schema file and creates and AppData java structure.
   * It uses apache Xerces to do the xml parsing.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Leon Messerschmidt</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Poeschl</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Fedor Karpelevitch</a>
   * @version $Id: XmlToData.java,v 1.1 2001/05/04 21:58:59 fedor Exp $
   */
  public class XmlToData extends DefaultHandler implements EntityResolver
  {
      private Database database;
      private String errorMessage;
      private Vector data;
      private String dtdFileName;
      private File dtdFile;
      private InputSource dataDTD;
  
      /**
       * Default custructor
       */
      public XmlToData(Database database, String dtdFilePath) throws
              MalformedURLException, IOException
      {
          this.database = database;
          dtdFile = new File(dtdFilePath);
          this.dtdFileName = "file://" + dtdFile.getName();
          dataDTD = new InputSource(dtdFile.toURL().openStream());
          errorMessage = "";
      }
  
  
      /**
       *
       */
      public List parseFile(String xmlFile)
      {
          try
          {
              data = new Vector();
  
              SAXParser parser = new SAXParser();
  
              // set the Resolver for the DTD
              parser.setEntityResolver(this);
  
              // We don't use an external content handler - we use this object
              parser.setContentHandler(this);
  
              // Validate the input file
              parser.setFeature
                  ("http://apache.org/xml/features/validation/dynamic";, true);
              parser.setFeature("http://xml.org/sax/features/validation";, true);
  
              parser.setErrorHandler(this);
  
              FileReader fr = new FileReader (xmlFile);
              BufferedReader br = new BufferedReader (fr);
              try
              {
                  InputSource is = new InputSource (br);
                  parser.parse(is);
              }
              finally
              {
                  br.close();
              }
          }
          catch (Exception e)
          {
              //System.out.println("Error : "+e);
              e.printStackTrace();
          }
          if ( errorMessage.length() > 0 )
          {
              System.out.println("ERROR in data file!!!\n" + errorMessage);
          }
  
          return data;
      }
  
  
  
      /**
       * Handles opening elements of the xml file.
       */
      public void startElement(String uri, String localName, String rawName,
                               Attributes attributes)
      {
          try
          {
              if (rawName.equals("dataset"))
              {
                  //ignore <dataset> for now.
              }
              else
              {
                  Table table = database.getTableByJavaName(rawName);
                  Vector columnValues = new Vector();
                  for (int i=0; i<attributes.getLength(); i++)
                  {
                      Column col = table
                          .getColumnByJavaName(attributes.getLocalName(i));
                      String value = attributes.getValue(i);
                      columnValues.add(new ColumnValue(col, value));
                  }
                  data.add(new DataRow(table, columnValues));
              }
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  
      /**
       * Warning callback.
       *
       * @exception spe The parse exception that caused the callback to be
       *                invoked.
       */
      public void warning(SAXParseException spe)
      {
          System.out.println("Warning Line: " + spe.getLineNumber() +
                             " Row: " + spe.getColumnNumber() +
                             " Msg: " + spe.getMessage());
      }
  
      /**
       * Error callback.
       *
       * @exception spe The parse exception that caused the callback to be
       *                invoked.
       */
      public void error(SAXParseException spe)
      {
          System.out.println("Error Line: " + spe.getLineNumber() +
                             " Row: " + spe.getColumnNumber() +
                             " Msg: " + spe.getMessage());
      }
  
      /**
       * Fatal error callback.
       *
       * @exception spe The parse exception that caused the callback to be
       *                invoked.
       */
      public void fatalError(SAXParseException spe)
      {
          System.out.println("Fatal Error Line: " + spe.getLineNumber() +
                             " Row: " + spe.getColumnNumber() +
                             " Msg: " + spe.getMessage());
      }
  
  
      /**
       * called by the XML parser
       *
       * @return an InputSource for the database.dtd file
       */
      public InputSource resolveEntity(String publicId, String systemId)
      {
          if (dataDTD != null &&
              dtdFileName.equals(systemId))
          {
              System.out.println("Resolver: used " + dtdFile.getPath());
              return dataDTD;
          }
          else
          {
              System.out.println("Resolver: used " + systemId);
              return getInputSource(systemId);
          }
      }
  
      /**
       * get an InputSource for an URL String
       *
       * @param urlString
       * @return an InputSource for the URL String
       */
      public InputSource getInputSource(String urlString)
      {
          try
          {
              URL url = new URL(urlString);
              return new InputSource(url.openStream());
          }
          catch (IOException ex)
          {
              ex.printStackTrace();
          }
          return new InputSource();
      }
  
      public class DataRow
      {
          private Table table;
          private Vector columnValues;
  
          public DataRow(Table table, Vector columnValues)
          {
              this.table = table;
              this.columnValues = columnValues;
          }
  
          public Table getTable()
          {
              return table;
          }
  
          public Vector getColumnValues()
          {
              return columnValues;
          }
      }
  
      public class ColumnValue
      {
          private Column col;
          private String val;
  
          public ColumnValue(Column col, String val)
          {
              this.col = col;
              this.val = val;
          }
  
          public Column getColumn()
          {
              return col;
          }
  
          public String getValue()
          {
              return val;
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to