001: package org.apache.torque.task;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025:
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Properties;
030:
031: import org.apache.tools.ant.BuildException;
032: import org.apache.tools.ant.DirectoryScanner;
033: import org.apache.tools.ant.types.FileSet;
034:
035: import org.apache.velocity.context.Context;
036:
037: import org.xml.sax.SAXException;
038:
039: import org.apache.torque.engine.EngineException;
040: import org.apache.torque.engine.database.model.Database;
041: import org.apache.torque.engine.database.transform.XmlToData;
042:
043: /**
044: * An extended Texen task used for generating SQL source from an XML data file
045: *
046: * @author <a href="mailto:jvanzyl@periapt.com"> Jason van Zyl </a>
047: * @author <a href="mailto:jmcnally@collab.net"> John McNally </a>
048: * @author <a href="mailto:fedor.karpelevitch@home.com"> Fedor Karpelevitch </a>
049: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
050: * @version $Id: TorqueDataSQLTask.java 473814 2006-11-11 22:30:30Z tv $
051: */
052: public class TorqueDataSQLTask extends TorqueDataModelTask {
053: /** the XML data file */
054: private String dataXmlFile;
055: /** the data dtd file */
056: private String dataDTD;
057:
058: /**
059: * The target database(s) we are generating SQL for. Right now we can only
060: * deal with a single target, but we will support multiple targets soon.
061: */
062: private String targetDatabase;
063:
064: /**
065: * Sets the DataXmlFile attribute of the TorqueDataSQLTask object
066: *
067: * @param dataXmlFile The new DataXmlFile value
068: */
069: public void setDataXmlFile(String dataXmlFile) {
070: this .dataXmlFile = getProject().resolveFile(dataXmlFile)
071: .toString();
072: }
073:
074: /**
075: * Gets the DataXmlFile attribute of the TorqueDataSQLTask object
076: *
077: * @return The DataXmlFile value
078: */
079: public String getDataXmlFile() {
080: return dataXmlFile;
081: }
082:
083: /**
084: * Get the current target database.
085: *
086: * @return String target database(s)
087: */
088: public String getTargetDatabase() {
089: return targetDatabase;
090: }
091:
092: /**
093: * Set the current target database. This is where generated java classes
094: * will live.
095: *
096: * @param v The new TargetDatabase value
097: */
098: public void setTargetDatabase(String v) {
099: targetDatabase = v;
100: }
101:
102: /**
103: * Gets the DataDTD attribute of the TorqueDataSQLTask object
104: *
105: * @return The DataDTD value
106: */
107: public String getDataDTD() {
108: return dataDTD;
109: }
110:
111: /**
112: * Sets the DataDTD attribute of the TorqueDataSQLTask object
113: *
114: * @param dataDTD The new DataDTD value
115: */
116: public void setDataDTD(String dataDTD) {
117: this .dataDTD = getProject().resolveFile(dataDTD).toString();
118: }
119:
120: /**
121: * Set up the initial context for generating the SQL from the XML schema.
122: *
123: * @return the context
124: * @throws Exception If there is an error parsing the data xml.
125: */
126: public Context initControlContext() throws Exception {
127: super .initControlContext();
128:
129: if (dataXmlFile == null && filesets.isEmpty()) {
130: throw new BuildException(
131: "You must specify an XML data file or "
132: + "a fileset of XML data files!");
133: }
134:
135: try {
136: Database db = (Database) getDataModels().get(0);
137:
138: List data;
139:
140: if (dataXmlFile != null) {
141: XmlToData dataXmlParser = new XmlToData(db, dataDTD);
142: data = dataXmlParser.parseFile(dataXmlFile);
143: } else {
144: data = new ArrayList();
145:
146: // Deal with the filesets.
147: for (int i = 0; i < filesets.size(); i++) {
148: FileSet fs = (FileSet) filesets.get(i);
149: DirectoryScanner ds = fs
150: .getDirectoryScanner(getProject());
151: File srcDir = fs.getDir(getProject());
152:
153: String[] dataModelFiles = ds.getIncludedFiles();
154:
155: // Make a transaction for each file
156: for (int j = 0; j < dataModelFiles.length; j++) {
157: File f = new File(srcDir, dataModelFiles[j]);
158: XmlToData dataXmlParser = new XmlToData(db,
159: dataDTD);
160: List newData = dataXmlParser.parseFile(f
161: .toString());
162:
163: for (Iterator it = newData.iterator(); it
164: .hasNext();) {
165: data.add(it.next());
166: }
167: }
168: }
169: }
170: context.put("data", data);
171:
172: // Place our model in the context.
173: context.put("appData", db);
174:
175: // Place the target database in the context.
176: context.put("targetDatabase", targetDatabase);
177:
178: Properties p = new Properties();
179: FileInputStream fis = new FileInputStream(getSqlDbMap());
180: p.load(fis);
181: fis.close();
182:
183: p.setProperty(getOutputFile(), db.getName());
184: p.store(new FileOutputStream(getSqlDbMap()),
185: "Sqlfile -> Database map");
186: } catch (EngineException ee) {
187: throw new BuildException(ee);
188: } catch (SAXException se) {
189: throw new BuildException(se);
190: }
191:
192: return context;
193: }
194: }
|