1 package org.apache.torque.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 473814 2006-11-11 22:30:30Z tv $
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 }