1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.ant;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.nio.charset.Charset;
29 import java.nio.charset.StandardCharsets;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.apache.tools.ant.Project;
36 import org.dbunit.DatabaseUnitException;
37 import org.dbunit.database.DatabaseSequenceFilter;
38 import org.dbunit.database.IDatabaseConnection;
39 import org.dbunit.dataset.FilteredDataSet;
40 import org.dbunit.dataset.IDataSet;
41 import org.dbunit.dataset.csv.CsvDataSetWriter;
42 import org.dbunit.dataset.excel.XlsDataSet;
43 import org.dbunit.dataset.filter.ITableFilter;
44 import org.dbunit.dataset.xml.FlatDtdDataSet;
45 import org.dbunit.dataset.xml.FlatXmlWriter;
46 import org.dbunit.dataset.xml.XmlDataSet;
47 import org.dbunit.dataset.yaml.YamlDataSet;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class Export extends AbstractStep
64 {
65
66
67
68
69 private static final Logger logger = LoggerFactory.getLogger(Export.class);
70
71 private File _dest;
72 private String _format = FORMAT_FLAT;
73 private String _doctype = null;
74 private Charset _encoding = StandardCharsets.UTF_8;
75 private List _tables = new ArrayList();
76
77 public Export()
78 {
79 }
80
81 private String getAbsolutePath(File filename)
82 {
83 return filename != null ? filename.getAbsolutePath() : "null";
84 }
85
86 public File getDest()
87 {
88 return _dest;
89 }
90
91 public String getFormat()
92 {
93 return _format;
94 }
95
96 public List getTables()
97 {
98 return _tables;
99 }
100
101 public void setDest(File dest)
102 {
103 logger.debug("setDest(dest={}) - start", dest);
104 _dest = dest;
105 }
106
107 public void setFormat(String format)
108 {
109 logger.debug("setFormat(format={}) - start", format);
110
111 if (format.equalsIgnoreCase(FORMAT_FLAT)
112 || format.equalsIgnoreCase(FORMAT_XML)
113 || format.equalsIgnoreCase(FORMAT_DTD)
114 || format.equalsIgnoreCase(FORMAT_CSV)
115 || format.equalsIgnoreCase(FORMAT_XLS)
116 || format.equalsIgnoreCase(FORMAT_YML))
117 {
118 _format = format;
119 }
120 else
121 {
122 throw new IllegalArgumentException("Type must be one of: 'flat'(default), 'xml', 'dtd', 'xls' or 'yml' but was: " + format);
123 }
124 }
125
126
127
128
129
130 public Charset getEncoding()
131 {
132 return this._encoding;
133 }
134
135 public void setEncoding(String encoding)
136 {
137 setEncoding(Charset.forName(encoding));
138 }
139
140 public void setEncoding(Charset encoding)
141 {
142 this._encoding = encoding;
143 }
144
145 public void addTable(Table table)
146 {
147 logger.debug("addTable(table={}) - start", table);
148 _tables.add(table);
149 }
150
151 public void addQuery(Query query)
152 {
153 logger.debug("addQuery(query={}) - start", query);
154 _tables.add(query);
155 }
156
157 public void addQuerySet(QuerySet querySet) {
158 logger.debug("addQuerySet(querySet={}) - start", querySet);
159 _tables.add(querySet);
160 }
161
162
163 public String getDoctype()
164 {
165 return _doctype;
166 }
167
168 public void setDoctype(String doctype)
169 {
170 logger.debug("setDoctype(doctype={}) - start", doctype);
171 _doctype = doctype;
172 }
173
174 public void execute(IDatabaseConnection connection) throws DatabaseUnitException
175 {
176 logger.debug("execute(connection={}) - start", connection);
177
178 try
179 {
180 if (_dest == null)
181 {
182 throw new DatabaseUnitException("'_dest' is a required attribute of the <export> step.");
183 }
184
185 IDataSet dataset = getExportDataSet(connection);
186 log("dataset tables: " + Arrays.asList(dataset.getTableNames()), Project.MSG_VERBOSE);
187
188
189
190 if (_format.equals(FORMAT_CSV))
191 {
192 CsvDataSetWriter.write(dataset, _dest);
193 }
194 else
195 {
196 OutputStream out = new FileOutputStream(_dest);
197 try
198 {
199 if (_format.equalsIgnoreCase(FORMAT_FLAT))
200 {
201 FlatXmlWriter writer = new FlatXmlWriter(out, getEncoding());
202 writer.setDocType(_doctype);
203 writer.write(dataset);
204 }
205 else if (_format.equalsIgnoreCase(FORMAT_XML))
206 {
207 XmlDataSet.write(dataset, out, getEncoding());
208 }
209 else if (_format.equalsIgnoreCase(FORMAT_DTD))
210 {
211
212 FlatDtdDataSet.write(dataset, out);
213 }
214 else if (_format.equalsIgnoreCase(FORMAT_XLS))
215 {
216 XlsDataSet.write(dataset, out);
217 }
218 else if (_format.equalsIgnoreCase(FORMAT_YML))
219 {
220 YamlDataSet.write(dataset, out);
221 }
222 else
223 {
224 throw new IllegalArgumentException("The given format '"+_format+"' is not supported.");
225 }
226
227 }
228 finally
229 {
230 out.close();
231 }
232 }
233
234 log("Successfully wrote file '" + _dest + "'", Project.MSG_INFO);
235
236 }
237 catch (SQLException e)
238 {
239 throw new DatabaseUnitException(e);
240 }
241 catch (IOException e)
242 {
243 throw new DatabaseUnitException(e);
244 }
245 }
246
247
248
249
250
251
252
253
254 protected IDataSet getExportDataSet(IDatabaseConnection connection)
255 throws DatabaseUnitException, SQLException
256 {
257 IDataSet dataset = getDatabaseDataSet(connection, this._tables);
258 if (isOrdered())
259 {
260
261 ITableFilter filter = new DatabaseSequenceFilter(connection);
262 dataset = new FilteredDataSet(filter, dataset);
263 }
264 return dataset;
265 }
266
267 public String getLogMessage()
268 {
269 return "Executing export: "
270 + "\n in format: " + _format
271 + " to datafile: " + getAbsolutePath(_dest);
272 }
273
274
275 public String toString()
276 {
277 final StringBuilder result = new StringBuilder();
278 result.append("Export: ");
279 result.append(" dest=" + getAbsolutePath(_dest));
280 result.append(", format= " + _format);
281 result.append(", doctype= " + _doctype);
282 result.append(", tables= " + _tables);
283
284 return result.toString();
285 }
286 }