%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.task.TorqueDataSQLTask |
|
|
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 | 0 | 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 | 0 | this.dataXmlFile = getProject().resolveFile(dataXmlFile).toString(); |
73 | 0 | } |
74 | ||
75 | /** |
|
76 | * Gets the DataXmlFile attribute of the TorqueDataSQLTask object |
|
77 | * |
|
78 | * @return The DataXmlFile value |
|
79 | */ |
|
80 | public String getDataXmlFile() |
|
81 | { |
|
82 | 0 | 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 | 0 | 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 | 0 | targetDatabase = v; |
104 | 0 | } |
105 | ||
106 | /** |
|
107 | * Gets the DataDTD attribute of the TorqueDataSQLTask object |
|
108 | * |
|
109 | * @return The DataDTD value |
|
110 | */ |
|
111 | public String getDataDTD() |
|
112 | { |
|
113 | 0 | 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 | 0 | this.dataDTD = getProject().resolveFile(dataDTD).toString(); |
124 | 0 | } |
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 | 0 | super.initControlContext(); |
135 | ||
136 | 0 | if (dataXmlFile == null && filesets.isEmpty()) |
137 | { |
|
138 | 0 | throw new BuildException("You must specify an XML data file or " |
139 | + "a fileset of XML data files!"); |
|
140 | } |
|
141 | ||
142 | try |
|
143 | { |
|
144 | 0 | Database db = (Database) getDataModels().get(0); |
145 | ||
146 | List data; |
|
147 | ||
148 | 0 | if (dataXmlFile != null) |
149 | { |
|
150 | 0 | XmlToData dataXmlParser = new XmlToData(db, dataDTD); |
151 | 0 | data = dataXmlParser.parseFile(dataXmlFile); |
152 | } |
|
153 | else |
|
154 | { |
|
155 | 0 | data = new ArrayList(); |
156 | ||
157 | // Deal with the filesets. |
|
158 | 0 | for (int i = 0; i < filesets.size(); i++) |
159 | { |
|
160 | 0 | FileSet fs = (FileSet) filesets.get(i); |
161 | 0 | DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
162 | 0 | File srcDir = fs.getDir(getProject()); |
163 | ||
164 | 0 | String[] dataModelFiles = ds.getIncludedFiles(); |
165 | ||
166 | // Make a transaction for each file |
|
167 | 0 | for (int j = 0; j < dataModelFiles.length; j++) |
168 | { |
|
169 | 0 | File f = new File(srcDir, dataModelFiles[j]); |
170 | 0 | XmlToData dataXmlParser = new XmlToData(db, dataDTD); |
171 | 0 | List newData = dataXmlParser.parseFile(f.toString()); |
172 | ||
173 | 0 | for (Iterator it = newData.iterator(); it.hasNext();) |
174 | { |
|
175 | 0 | data.add(it.next()); |
176 | } |
|
177 | } |
|
178 | } |
|
179 | } |
|
180 | 0 | context.put("data", data); |
181 | ||
182 | // Place our model in the context. |
|
183 | 0 | context.put("appData", db); |
184 | ||
185 | // Place the target database in the context. |
|
186 | 0 | context.put("targetDatabase", targetDatabase); |
187 | ||
188 | 0 | Properties p = new Properties(); |
189 | 0 | FileInputStream fis = new FileInputStream(getSqlDbMap()); |
190 | 0 | p.load(fis); |
191 | 0 | fis.close(); |
192 | ||
193 | 0 | p.setProperty(getOutputFile(), db.getName()); |
194 | 0 | p.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map"); |
195 | } |
|
196 | 0 | catch (EngineException ee) |
197 | { |
|
198 | 0 | throw new BuildException(ee); |
199 | } |
|
200 | 0 | catch (SAXException se) |
201 | { |
|
202 | 0 | throw new BuildException(se); |
203 | 0 | } |
204 | ||
205 | 0 | return context; |
206 | } |
|
207 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |