1 package org.apache.torque.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Properties;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.DirectoryScanner;
30 import org.apache.tools.ant.types.FileSet;
31
32 import org.apache.velocity.context.Context;
33
34 import org.xml.sax.SAXException;
35
36 import org.apache.torque.engine.EngineException;
37 import org.apache.torque.engine.database.model.Database;
38 import org.apache.torque.engine.database.transform.XmlToData;
39
40 /***
41 * An extended Texen task used for generating SQL source from an XML data file
42 *
43 * @author <a href="mailto:jvanzyl@periapt.com"> Jason van Zyl </a>
44 * @author <a href="mailto:jmcnally@collab.net"> John McNally </a>
45 * @author <a href="mailto:fedor.karpelevitch@home.com"> Fedor Karpelevitch </a>
46 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47 * @version $Id: TorqueDataSQLTask.java 239626 2005-08-24 12:19:51Z henning $
48 */
49 public class TorqueDataSQLTask extends TorqueDataModelTask
50 {
51 /*** the XML data file */
52 private String dataXmlFile;
53 /*** the data dtd file */
54 private String dataDTD;
55
56 /***
57 * The target database(s) we are generating SQL for. Right now we can only
58 * deal with a single target, but we will support multiple targets soon.
59 */
60 private String targetDatabase;
61
62 /***
63 * Sets the DataXmlFile attribute of the TorqueDataSQLTask object
64 *
65 * @param dataXmlFile The new DataXmlFile value
66 */
67 public void setDataXmlFile(String dataXmlFile)
68 {
69 this.dataXmlFile = project.resolveFile(dataXmlFile).toString();
70 }
71
72 /***
73 * Gets the DataXmlFile attribute of the TorqueDataSQLTask object
74 *
75 * @return The DataXmlFile value
76 */
77 public String getDataXmlFile()
78 {
79 return dataXmlFile;
80 }
81
82 /***
83 * Get the current target database.
84 *
85 * @return String target database(s)
86 */
87 public String getTargetDatabase()
88 {
89 return targetDatabase;
90 }
91
92 /***
93 * Set the current target database. This is where generated java classes
94 * will live.
95 *
96 * @param v The new TargetDatabase value
97 */
98 public void setTargetDatabase(String v)
99 {
100 targetDatabase = v;
101 }
102
103 /***
104 * Gets the DataDTD attribute of the TorqueDataSQLTask object
105 *
106 * @return The DataDTD value
107 */
108 public String getDataDTD()
109 {
110 return dataDTD;
111 }
112
113 /***
114 * Sets the DataDTD attribute of the TorqueDataSQLTask object
115 *
116 * @param dataDTD The new DataDTD value
117 */
118 public void setDataDTD(String dataDTD)
119 {
120 this.dataDTD = project.resolveFile(dataDTD).toString();
121 }
122
123 /***
124 * Set up the initial context for generating the SQL from the XML schema.
125 *
126 * @return the context
127 * @throws Exception If there is an error parsing the data xml.
128 */
129 public Context initControlContext() throws Exception
130 {
131 super.initControlContext();
132
133 if (dataXmlFile == null && filesets.isEmpty())
134 {
135 throw new BuildException("You must specify an XML data file or "
136 + "a fileset of XML data files!");
137 }
138
139 try
140 {
141 Database db = (Database) getDataModels().get(0);
142
143 List data;
144
145 if (dataXmlFile != null)
146 {
147 XmlToData dataXmlParser = new XmlToData(db, dataDTD);
148 data = dataXmlParser.parseFile(dataXmlFile);
149 }
150 else
151 {
152 data = new ArrayList();
153
154
155 for (int i = 0; i < filesets.size(); i++)
156 {
157 FileSet fs = (FileSet) filesets.get(i);
158 DirectoryScanner ds = fs.getDirectoryScanner(project);
159 File srcDir = fs.getDir(project);
160
161 String[] dataModelFiles = ds.getIncludedFiles();
162
163
164 for (int j = 0; j < dataModelFiles.length; j++)
165 {
166 File f = new File(srcDir, dataModelFiles[j]);
167 XmlToData dataXmlParser = new XmlToData(db, dataDTD);
168 List newData = dataXmlParser.parseFile(f.toString());
169
170 for (Iterator it = newData.iterator(); it.hasNext();)
171 {
172 data.add(it.next());
173 }
174 }
175 }
176 }
177 context.put("data", data);
178
179
180 context.put("appData", db);
181
182
183 context.put("targetDatabase", targetDatabase);
184
185 Properties p = new Properties();
186 FileInputStream fis = new FileInputStream(getSqlDbMap());
187 p.load(fis);
188 fis.close();
189
190 p.setProperty(getOutputFile(), db.getName());
191 p.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map");
192 }
193 catch (EngineException ee)
194 {
195 throw new BuildException(ee);
196 }
197 catch (SAXException se)
198 {
199 throw new BuildException(se);
200 }
201
202 return context;
203 }
204 }