View Javadoc

1   package org.apache.torque.task;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.BufferedWriter;
20  import java.io.FileWriter;
21  
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.Project;
24  import org.apache.tools.ant.Task;
25  
26  import org.apache.torque.engine.database.model.Database;
27  import org.apache.torque.engine.database.transform.SQLToAppData;
28  
29  /***
30   * An ant task for creating an xml schema from an sql schema
31   *
32   * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
33   * @author <a href="jvanzyl@zenplex.com">Jason van Zyl</a>
34   * @version $Id: TorqueSQLTransformTask.java 239624 2005-08-24 12:18:03Z henning $
35   */
36  public class TorqueSQLTransformTask extends Task
37  {
38      /*** SQL input file. */
39      private String inputFile;
40  
41      /*** XML descriptor output file. */
42      private String outputFile;
43  
44      /***
45       * Get the current input file
46       *
47       * @return the input file
48       */
49      public String getInputFile()
50      {
51          return inputFile;
52      }
53  
54      /***
55       * Set the sql input file.  This file must exist
56       *
57       * @param v the input file
58       */
59      public void setInputFile(String v)
60      {
61          inputFile = v;
62      }
63  
64      /***
65       * Get the current output file.
66       *
67       * @return the output file
68       */
69      public String getOutputFile()
70      {
71          return outputFile;
72      }
73  
74      /***
75       * Set the current output file.  If the file does not
76       * exist it will be created.  If the file exists all
77       * it's contents will be replaced.
78       *
79       * @param v the output file
80       */
81      public void setOutputFile (String v)
82      {
83          outputFile = v;
84      }
85  
86      /***
87       * Execute the task.
88       *
89       * @throws BuildException Any exceptions caught during procssing will be
90       *         rethrown wrapped into a BuildException
91       */
92      public void execute() throws BuildException
93      {
94          try
95          {
96              log("Parsing SQL Schema", Project.MSG_INFO);
97  
98              SQLToAppData sqlParser = new SQLToAppData(inputFile);
99              Database app = sqlParser.execute();
100 
101             log("Preparing to write xml schema", Project.MSG_INFO);
102             FileWriter fr = new FileWriter(outputFile);
103             BufferedWriter br = new BufferedWriter (fr);
104 
105             br.write(app.toString());
106 
107             log("Writing xml schema", Project.MSG_INFO);
108 
109             br.flush();
110             br.close();
111         }
112         catch (Exception e)
113         {
114             throw new BuildException(e);
115         }
116     }
117 }