KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ant > Export


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * The <code>Export</code> class is the step that facilitates exporting
41  * the contents of the database and/or it's corresponding dtd to a file.
42  * The export can be performed on a full dataset or a partial one if
43  * specific table names are identified.
44  *
45  * @author Timothy Ruppert
46  * @author Ben Cox
47  * @version $Revision: 1.16 $
48  * @since Jun 10, 2002
49  * @see DbUnitTaskStep
50  */

51 public class Export extends AbstractStep
52 {
53
54     private File JavaDoc _dest;
55     private String JavaDoc _format = FORMAT_FLAT;
56     private String JavaDoc _doctype = null;
57     private List JavaDoc _tables = new ArrayList JavaDoc();
58
59     public Export()
60     {
61     }
62
63     private String JavaDoc getAbsolutePath(File JavaDoc filename)
64     {
65         return filename != null ? filename.getAbsolutePath() : "null";
66     }
67
68     public File JavaDoc getDest()
69     {
70         return _dest;
71     }
72
73     public String JavaDoc getFormat()
74     {
75         return _format;
76     }
77
78     public List JavaDoc getTables()
79     {
80         return _tables;
81     }
82
83     public void setDest(File JavaDoc dest)
84     {
85         _dest = dest;
86     }
87
88     public void setFormat(String JavaDoc 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 JavaDoc("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 JavaDoc getDoctype()
114     {
115         return _doctype;
116     }
117
118     public void setDoctype(String JavaDoc 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             // Write the dataset
135
if (_format.equals(FORMAT_CSV))
136             {
137                 CsvDataSetWriter.write(dataset, _dest);
138             }
139             else
140             {
141                 OutputStream JavaDoc out = new FileOutputStream JavaDoc(_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 JavaDoc e)
166         {
167             throw new DatabaseUnitException(e);
168         }
169     }
170
171     public String JavaDoc getLogMessage()
172     {
173         return "Executing export: "
174                 + "\n in format: " + _format
175                 + " to datafile: " + getAbsolutePath(_dest);
176     }
177
178
179     public String JavaDoc toString()
180     {
181         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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