1 21 package org.dbunit.dataset.csv; 22 23 import org.dbunit.dataset.*; 24 import org.dbunit.dataset.datatype.DataType; 25 import org.dbunit.dataset.datatype.TypeCastException; 26 import org.dbunit.dataset.stream.DataSetProducerAdapter; 27 import org.dbunit.dataset.stream.IDataSetConsumer; 28 29 import java.io.File ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.io.Writer ; 33 34 39 public class CsvDataSetWriter implements IDataSetConsumer { 40 41 44 45 private static final String NULL = "null"; 46 private static final String NONE = "none"; 47 private static final String FIELD_SEPARATOR = ", "; 48 private static final String QUOTE = "\""; 49 private static final String ESCAPE = "\\"; 50 51 private Writer writer; 52 private ITableMetaData _activeMetaData; 53 private String theDirectory; 54 private static char testExport; 55 56 public CsvDataSetWriter(String theDirectory) { 57 setTheDirectory(theDirectory); 58 } 59 60 public CsvDataSetWriter(File theDirectory) { 61 setTheDirectory(theDirectory.getAbsolutePath()); 62 } 63 64 public void write(IDataSet dataSet) throws DataSetException { 65 DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet); 66 provider.setConsumer(this); 67 provider.produce(); 68 } 69 70 public void startDataSet() throws DataSetException { 71 try { 72 new File (getTheDirectory()).mkdirs(); 73 } catch (Exception e) { 74 throw new DataSetException("Error while creating the destination directory '" + getTheDirectory() + "'", e); 75 } 76 } 77 78 public void endDataSet() throws DataSetException {} 79 80 public void startTable(ITableMetaData metaData) throws DataSetException { 81 try { 82 _activeMetaData = metaData; 83 String tableName = _activeMetaData.getTableName(); 84 setWriter(new FileWriter (getTheDirectory() + File.separator + tableName + ".csv")); 85 writeColumnNames(); 86 getWriter().write(System.getProperty("line.separator")); 87 } catch (IOException e) { 88 throw new DataSetException(e); 89 } 90 91 } 92 93 private void writeColumnNames() throws DataSetException, IOException { 94 Column[] columns = _activeMetaData.getColumns(); 95 for (int i = 0; i < columns.length; i++) { 96 String columnName = columns[i].getColumnName(); 97 getWriter().write(columnName); 98 if (i < columns.length - 1) getWriter().write(FIELD_SEPARATOR); 99 } 100 } 101 102 public void endTable() throws DataSetException { 103 try { 104 getWriter().close(); 105 _activeMetaData = null; 106 } catch (IOException e) { 107 throw new DataSetException(e); 108 } 109 } 110 111 public void row(Object [] values) throws DataSetException { 112 try { 113 114 Column[] columns = _activeMetaData.getColumns(); 115 for (int i = 0; i < columns.length; i++) { 116 String columnName = columns[i].getColumnName(); 117 Object value = values[i]; 118 119 if (value == null) { 121 getWriter().write(NULL); 122 } 123 else if (value == ITable.NO_VALUE) { 125 getWriter().write(NONE); 126 } 127 else { 129 try { 130 String stringValue = DataType.asString(value); 131 final String quoted = quote(stringValue); 132 getWriter().write(quoted); 133 } catch (TypeCastException e) { 134 throw new DataSetException("table=" + 135 _activeMetaData.getTableName() + ", row=" + i + 136 ", column=" + columnName + 137 ", value=" + value, e); 138 } 139 } 140 if (i < columns.length - 1) getWriter().write(","); 141 } 142 getWriter().write(System.getProperty("line.separator")); 143 } catch (IOException e) { 144 throw new DataSetException(e); 145 } 146 } 147 148 private String quote(String stringValue) { 149 return new StringBuffer (QUOTE).append(escape(stringValue)).append(QUOTE).toString(); 150 } 151 152 protected static String escape(String stringValue) { 153 char [] array = stringValue.toCharArray(); 154 testExport = QUOTE.toCharArray()[0]; 155 final char escape = ESCAPE.toCharArray()[0]; 156 StringBuffer buffer = new StringBuffer (); 157 for (int i = 0; i < array.length; i++) { 158 char c = array[i]; 159 if (c == testExport || c == escape) { 160 buffer.append('\\'); 161 } 162 buffer.append(c); 163 } 164 return buffer.toString(); 165 } 166 167 public Writer getWriter() { 168 return writer; 169 } 170 171 public void setWriter(Writer writer) { 172 this.writer = writer; 173 } 174 175 public String getTheDirectory() { 176 return theDirectory; 177 } 178 179 public void setTheDirectory(String theDirectory) { 180 this.theDirectory = theDirectory; 181 } 182 183 public static void write(IDataSet dataset, File dest) throws DataSetException { 184 CsvDataSetWriter writer = new CsvDataSetWriter(dest); 185 writer.write(dataset); 186 } 187 188 protected void finalize() throws Throwable { 189 if (getWriter() != null) { 190 getWriter().close(); 191 } 192 } 193 } 194 | Popular Tags |