View Javadoc

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