KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > task > TorqueDataSQLTask


1 package org.apache.torque.task;
2
3 /*
4  * Copyright 2001-2004 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.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
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 JavaDoc;
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,v 1.7 2004/02/22 06:27:41 jmcnally Exp $
48  */

49 public class TorqueDataSQLTask extends TorqueDataModelTask
50 {
51     /** the XML data file */
52     private String JavaDoc dataXmlFile;
53     /** the data dtd file */
54     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc
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 JavaDoc 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 JavaDoc();
153                 
154                 // Deal with the filesets.
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 JavaDoc srcDir = fs.getDir(project);
160
161                     String JavaDoc[] dataModelFiles = ds.getIncludedFiles();
162                     
163                     // Make a transaction for each file
164
for (int j = 0; j < dataModelFiles.length; j++)
165                     {
166                         File JavaDoc f = new File JavaDoc(srcDir, dataModelFiles[j]);
167                         XmlToData dataXmlParser = new XmlToData(db, dataDTD);
168                         List JavaDoc newData = dataXmlParser.parseFile(f.toString());
169
170                         for (Iterator JavaDoc it = newData.iterator(); it.hasNext();)
171                         {
172                             data.add(it.next());
173                         }
174                     }
175                 }
176             }
177             context.put("data", data);
178
179             // Place our model in the context.
180
context.put("appData", db);
181
182             // Place the target database in the context.
183
context.put("targetDatabase", targetDatabase);
184
185             Properties JavaDoc p = new Properties JavaDoc();
186             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(getSqlDbMap());
187             p.load(fis);
188             fis.close();
189             
190             p.setProperty(getOutputFile(), db.getName());
191             p.store(new FileOutputStream JavaDoc(getSqlDbMap()), "Sqlfile -> Database map");
192         }
193         catch (EngineException ee)
194         {
195             throw new BuildException(ee);
196         }
197         catch (SAXException JavaDoc se)
198         {
199             throw new BuildException(se);
200         }
201
202         return context;
203     }
204 }
205
Popular Tags