1 package org.apache.torque.task; 2 3 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Properties ; 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 ; 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 49 public class TorqueDataSQLTask extends TorqueDataModelTask 50 { 51 52 private String dataXmlFile; 53 54 private String dataDTD; 55 56 60 private String targetDatabase; 61 62 67 public void setDataXmlFile(String dataXmlFile) 68 { 69 this.dataXmlFile = project.resolveFile(dataXmlFile).toString(); 70 } 71 72 77 public String getDataXmlFile() 78 { 79 return dataXmlFile; 80 } 81 82 87 public String getTargetDatabase() 88 { 89 return targetDatabase; 90 } 91 92 98 public void setTargetDatabase(String v) 99 { 100 targetDatabase = v; 101 } 102 103 108 public String getDataDTD() 109 { 110 return dataDTD; 111 } 112 113 118 public void setDataDTD(String dataDTD) 119 { 120 this.dataDTD = project.resolveFile(dataDTD).toString(); 121 } 122 123 129 public Context initControlContext() throws Exception 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 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 (); 153 154 for (int i = 0; i < filesets.size(); i++) 156 { 157 FileSet fs = (FileSet) filesets.get(i); 158 DirectoryScanner ds = fs.getDirectoryScanner(project); 159 File srcDir = fs.getDir(project); 160 161 String [] dataModelFiles = ds.getIncludedFiles(); 162 163 for (int j = 0; j < dataModelFiles.length; j++) 165 { 166 File f = new File (srcDir, dataModelFiles[j]); 167 XmlToData dataXmlParser = new XmlToData(db, dataDTD); 168 List newData = dataXmlParser.parseFile(f.toString()); 169 170 for (Iterator it = newData.iterator(); it.hasNext();) 171 { 172 data.add(it.next()); 173 } 174 } 175 } 176 } 177 context.put("data", data); 178 179 context.put("appData", db); 181 182 context.put("targetDatabase", targetDatabase); 184 185 Properties p = new Properties (); 186 FileInputStream fis = new FileInputStream (getSqlDbMap()); 187 p.load(fis); 188 fis.close(); 189 190 p.setProperty(getOutputFile(), db.getName()); 191 p.store(new FileOutputStream (getSqlDbMap()), "Sqlfile -> Database map"); 192 } 193 catch (EngineException ee) 194 { 195 throw new BuildException(ee); 196 } 197 catch (SAXException se) 198 { 199 throw new BuildException(se); 200 } 201 202 return context; 203 } 204 } 205 | Popular Tags |