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