1 package org.apache.torque.task;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.File;
58 import java.io.FileInputStream;
59 import java.io.FileOutputStream;
60
61 import java.util.ArrayList;
62 import java.util.Iterator;
63 import java.util.List;
64 import java.util.Properties;
65
66 import org.apache.tools.ant.BuildException;
67 import org.apache.tools.ant.DirectoryScanner;
68 import org.apache.tools.ant.types.FileSet;
69
70 import org.apache.velocity.context.Context;
71
72 import org.xml.sax.SAXException;
73
74 import org.apache.torque.engine.EngineException;
75 import org.apache.torque.engine.database.model.AppData;
76 import org.apache.torque.engine.database.model.Database;
77 import org.apache.torque.engine.database.transform.XmlToData;
78
79 /***
80 * An extended Texen task used for generating SQL source from an XML data file
81 *
82 * @author <a href="mailto:jvanzyl@periapt.com"> Jason van Zyl </a>
83 * @author <a href="mailto:jmcnally@collab.net"> John McNally </a>
84 * @author <a href="mailto:fedor.karpelevitch@home.com"> Fedor Karpelevitch </a>
85 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
86 * @version $Id: TorqueDataSQLTask.java,v 1.5 2003/07/28 09:11:36 henning Exp $
87 */
88 public class TorqueDataSQLTask extends TorqueDataModelTask
89 {
90 /*** the XML data file */
91 private String dataXmlFile;
92 /*** the data dtd file */
93 private String dataDTD;
94
95 /***
96 * The target database(s) we are generating SQL for. Right now we can only
97 * deal with a single target, but we will support multiple targets soon.
98 */
99 private String targetDatabase;
100
101 /***
102 * Sets the DataXmlFile attribute of the TorqueDataSQLTask object
103 *
104 * @param dataXmlFile The new DataXmlFile value
105 */
106 public void setDataXmlFile(String dataXmlFile)
107 {
108 this.dataXmlFile = project.resolveFile(dataXmlFile).toString();
109 }
110
111 /***
112 * Gets the DataXmlFile attribute of the TorqueDataSQLTask object
113 *
114 * @return The DataXmlFile value
115 */
116 public String getDataXmlFile()
117 {
118 return dataXmlFile;
119 }
120
121 /***
122 * Get the current target database.
123 *
124 * @return String target database(s)
125 */
126 public String getTargetDatabase()
127 {
128 return targetDatabase;
129 }
130
131 /***
132 * Set the current target database. This is where generated java classes
133 * will live.
134 *
135 * @param v The new TargetDatabase value
136 */
137 public void setTargetDatabase(String v)
138 {
139 targetDatabase = v;
140 }
141
142 /***
143 * Gets the DataDTD attribute of the TorqueDataSQLTask object
144 *
145 * @return The DataDTD value
146 */
147 public String getDataDTD()
148 {
149 return dataDTD;
150 }
151
152 /***
153 * Sets the DataDTD attribute of the TorqueDataSQLTask object
154 *
155 * @param dataDTD The new DataDTD value
156 */
157 public void setDataDTD(String dataDTD)
158 {
159 this.dataDTD = project.resolveFile(dataDTD).toString();
160 }
161
162 /***
163 * Set up the initial context for generating the SQL from the XML schema.
164 *
165 * @return the context
166 * @throws Exception If there is an error parsing the data xml.
167 */
168 public Context initControlContext() throws Exception
169 {
170 super.initControlContext();
171
172 if (dataXmlFile == null && filesets.isEmpty())
173 {
174 throw new BuildException("You must specify an XML data file or "
175 + "a fileset of XML data files!");
176 }
177
178 try
179 {
180 AppData app = (AppData) getDataModels().get(0);
181 Database db = app.getDatabase();
182
183 List data;
184
185 if (dataXmlFile != null)
186 {
187 XmlToData dataXmlParser = new XmlToData(db, dataDTD);
188 data = dataXmlParser.parseFile(dataXmlFile);
189 }
190 else
191 {
192 data = new ArrayList();
193
194 // Deal with the filesets.
195 for (int i = 0; i < filesets.size(); i++)
196 {
197 FileSet fs = (FileSet) filesets.get(i);
198 DirectoryScanner ds = fs.getDirectoryScanner(project);
199 File srcDir = fs.getDir(project);
200
201 String[] dataModelFiles = ds.getIncludedFiles();
202
203 // Make a transaction for each file
204 for (int j = 0; j < dataModelFiles.length; j++)
205 {
206 File f = new File(srcDir, dataModelFiles[j]);
207 XmlToData dataXmlParser = new XmlToData(db, dataDTD);
208 List newData = dataXmlParser.parseFile(f.toString());
209
210 for (Iterator it = newData.iterator(); it.hasNext();)
211 {
212 data.add(it.next());
213 }
214 }
215 }
216 }
217 context.put("data", data);
218
219 // Place our model in the context.
220 context.put("appData", app);
221
222 // Place the target database in the context.
223 context.put("targetDatabase", targetDatabase);
224
225 Properties p = new Properties();
226 FileInputStream fis = new FileInputStream(getSqlDbMap());
227 p.load(fis);
228 fis.close();
229
230 p.setProperty(getOutputFile(), db.getName());
231 p.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map");
232 }
233 catch (EngineException ee)
234 {
235 throw new BuildException(ee);
236 }
237 catch (SAXException se)
238 {
239 throw new BuildException(se);
240 }
241
242 return context;
243 }
244 }
This page was automatically generated by Maven