1 21 22 package org.dbunit.ant; 23 24 import org.dbunit.DatabaseUnitException; 25 import org.dbunit.database.IDatabaseConnection; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.dataset.csv.CsvDataSetWriter; 28 import org.dbunit.dataset.xml.FlatDtdDataSet; 29 import org.dbunit.dataset.xml.FlatXmlWriter; 30 import org.dbunit.dataset.xml.XmlDataSet; 31 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.OutputStream ; 36 import java.util.ArrayList ; 37 import java.util.List ; 38 39 51 public class Export extends AbstractStep 52 { 53 54 private File _dest; 55 private String _format = FORMAT_FLAT; 56 private String _doctype = null; 57 private List _tables = new ArrayList (); 58 59 public Export() 60 { 61 } 62 63 private String getAbsolutePath(File filename) 64 { 65 return filename != null ? filename.getAbsolutePath() : "null"; 66 } 67 68 public File getDest() 69 { 70 return _dest; 71 } 72 73 public String getFormat() 74 { 75 return _format; 76 } 77 78 public List getTables() 79 { 80 return _tables; 81 } 82 83 public void setDest(File dest) 84 { 85 _dest = dest; 86 } 87 88 public void setFormat(String format) 89 { 90 if (format.equalsIgnoreCase(FORMAT_FLAT) 91 || format.equalsIgnoreCase(FORMAT_XML) 92 || format.equalsIgnoreCase(FORMAT_DTD) 93 || format.equalsIgnoreCase(FORMAT_CSV)) 94 { 95 _format = format; 96 } 97 else 98 { 99 throw new IllegalArgumentException ("Type must be one of: 'flat'(default), 'xml', or 'dtd' but was: " + format); 100 } 101 } 102 103 public void addTable(Table table) 104 { 105 _tables.add(table); 106 } 107 108 public void addQuery(Query query) 109 { 110 _tables.add(query); 111 } 112 113 public String getDoctype() 114 { 115 return _doctype; 116 } 117 118 public void setDoctype(String doctype) 119 { 120 _doctype = doctype; 121 } 122 123 public void execute(IDatabaseConnection connection) throws DatabaseUnitException 124 { 125 try 126 { 127 if (_dest == null) 128 { 129 throw new DatabaseUnitException("'_dest' is a required attribute of the <export> step."); 130 } 131 132 IDataSet dataset = getDatabaseDataSet(connection, _tables, true); 133 134 if (_format.equals(FORMAT_CSV)) 136 { 137 CsvDataSetWriter.write(dataset, _dest); 138 } 139 else 140 { 141 OutputStream out = new FileOutputStream (_dest); 142 try 143 { 144 if (_format.equalsIgnoreCase(FORMAT_FLAT)) 145 { 146 FlatXmlWriter writer = new FlatXmlWriter(out); 147 writer.setDocType(_doctype); 148 writer.write(dataset); 149 } 150 else if (_format.equalsIgnoreCase(FORMAT_XML)) 151 { 152 XmlDataSet.write(dataset, out); 153 } 154 else if (_format.equalsIgnoreCase(FORMAT_DTD)) 155 { 156 FlatDtdDataSet.write(dataset, out); 157 } 158 } 159 finally 160 { 161 out.close(); 162 } 163 } 164 } 165 catch (IOException e) 166 { 167 throw new DatabaseUnitException(e); 168 } 169 } 170 171 public String getLogMessage() 172 { 173 return "Executing export: " 174 + "\n in format: " + _format 175 + " to datafile: " + getAbsolutePath(_dest); 176 } 177 178 179 public String toString() 180 { 181 StringBuffer result = new StringBuffer (); 182 result.append("Export: "); 183 result.append(" dest=" + getAbsolutePath(_dest)); 184 result.append(", format= " + _format); 185 result.append(", doctype= " + _doctype); 186 result.append(", tables= " + _tables); 187 188 return result.toString(); 189 } 190 } 191 192 193 | Popular Tags |