1 21 package org.dbunit.dataset.xml; 22 23 import org.dbunit.dataset.Column; 24 import org.dbunit.dataset.DataSetException; 25 import org.dbunit.dataset.IDataSet; 26 import org.dbunit.dataset.ITableMetaData; 27 import org.dbunit.dataset.datatype.DataType; 28 import org.dbunit.dataset.datatype.TypeCastException; 29 import org.dbunit.dataset.stream.DataSetProducerAdapter; 30 import org.dbunit.dataset.stream.IDataSetConsumer; 31 import org.dbunit.util.xml.XmlWriter; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.OutputStreamWriter ; 36 import java.io.Writer ; 37 38 43 public class FlatXmlWriter implements IDataSetConsumer 44 { 45 private static final String DEFAULT_ENCODING = "UTF8"; 46 private static final String DATASET = "dataset"; 47 48 private XmlWriter _xmlWriter; 49 private ITableMetaData _activeMetaData; 50 private int _activeRowCount; 51 private boolean _includeEmptyTable = false; 52 private String _systemId = null; 53 54 public FlatXmlWriter(Writer writer) 55 { 56 _xmlWriter = new XmlWriter(writer); 57 _xmlWriter.enablePrettyPrint(true); 58 } 59 60 public FlatXmlWriter(OutputStream out) throws IOException 61 { 62 _xmlWriter = new XmlWriter(new OutputStreamWriter (out, DEFAULT_ENCODING)); 63 _xmlWriter.enablePrettyPrint(true); 64 } 65 66 public FlatXmlWriter(Writer writer, String encoding) 67 { 68 _xmlWriter = new XmlWriter(writer, encoding); 69 _xmlWriter.enablePrettyPrint(true); 70 } 71 72 public void setIncludeEmptyTable(boolean includeEmptyTable) 73 { 74 _includeEmptyTable = includeEmptyTable; 75 } 76 77 public void setDocType(String systemId) 78 { 79 _systemId = systemId; 80 } 81 82 public void write(IDataSet dataSet) throws DataSetException 83 { 84 DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet); 85 provider.setConsumer(this); 86 provider.produce(); 87 } 88 89 92 public void startDataSet() throws DataSetException 93 { 94 try 95 { 96 _xmlWriter.writeDeclaration(); 97 _xmlWriter.writeDoctype(_systemId, null); 98 _xmlWriter.writeElement(DATASET); 99 } 100 catch (IOException e) 101 { 102 throw new DataSetException(e); 103 } 104 } 105 106 public void endDataSet() throws DataSetException 107 { 108 try 109 { 110 _xmlWriter.endElement(); 111 _xmlWriter.close(); 112 } 113 catch (IOException e) 114 { 115 throw new DataSetException(e); 116 } 117 } 118 119 public void startTable(ITableMetaData metaData) throws DataSetException 120 { 121 _activeMetaData = metaData; 122 _activeRowCount = 0; 123 } 124 125 public void endTable() throws DataSetException 126 { 127 if (_includeEmptyTable && _activeRowCount == 0) 128 { 129 try 130 { 131 String tableName = _activeMetaData.getTableName(); 132 _xmlWriter.writeEmptyElement(tableName); 133 } 134 catch (IOException e) 135 { 136 throw new DataSetException(e); 137 } 138 } 139 140 _activeMetaData = null; 141 } 142 143 public void row(Object [] values) throws DataSetException 144 { 145 try 146 { 147 String tableName = _activeMetaData.getTableName(); 148 _xmlWriter.writeElement(tableName); 149 150 Column[] columns = _activeMetaData.getColumns(); 151 for (int i = 0; i < columns.length; i++) 152 { 153 String columnName = columns[i].getColumnName(); 154 Object value = values[i]; 155 156 if (value == null) 158 { 159 continue; 160 } 161 162 try 163 { 164 String stringValue = DataType.asString(value); 165 _xmlWriter.writeAttribute(columnName, stringValue); 166 } 167 catch (TypeCastException e) 168 { 169 throw new DataSetException("table=" + 170 _activeMetaData.getTableName() + ", row=" + i + 171 ", column=" + columnName + 172 ", value=" + value, e); 173 } 174 } 175 176 _activeRowCount++; 177 _xmlWriter.endElement(); 178 } 179 catch (IOException e) 180 { 181 throw new DataSetException(e); 182 } 183 } 184 } 185 | Popular Tags |