001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.dataset.xml;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.Column;
027: import org.dbunit.dataset.DataSetException;
028: import org.dbunit.dataset.IDataSet;
029: import org.dbunit.dataset.ITableMetaData;
030: import org.dbunit.dataset.datatype.DataType;
031: import org.dbunit.dataset.datatype.TypeCastException;
032: import org.dbunit.dataset.stream.DataSetProducerAdapter;
033: import org.dbunit.dataset.stream.IDataSetConsumer;
034: import org.dbunit.util.xml.XmlWriter;
035:
036: import java.io.IOException;
037: import java.io.OutputStream;
038: import java.io.OutputStreamWriter;
039: import java.io.Writer;
040:
041: /**
042: * @author Manuel Laflamme
043: * @since Apr 19, 2003
044: * @version $Revision: 554 $
045: */
046: public class FlatXmlWriter implements IDataSetConsumer {
047:
048: /**
049: * Logger for this class
050: */
051: private static final Logger logger = LoggerFactory
052: .getLogger(FlatXmlWriter.class);
053:
054: private static final String DEFAULT_ENCODING = "UTF8";
055: private static final String DATASET = "dataset";
056:
057: private XmlWriter _xmlWriter;
058: private ITableMetaData _activeMetaData;
059: private int _activeRowCount;
060: private boolean _includeEmptyTable = false;
061: private String _systemId = null;
062:
063: public FlatXmlWriter(Writer writer) {
064: _xmlWriter = new XmlWriter(writer);
065: _xmlWriter.enablePrettyPrint(true);
066: }
067:
068: public FlatXmlWriter(OutputStream out) throws IOException {
069: _xmlWriter = new XmlWriter(new OutputStreamWriter(out,
070: DEFAULT_ENCODING));
071: _xmlWriter.enablePrettyPrint(true);
072: }
073:
074: public FlatXmlWriter(Writer writer, String encoding) {
075: _xmlWriter = new XmlWriter(writer, encoding);
076: _xmlWriter.enablePrettyPrint(true);
077: }
078:
079: public void setIncludeEmptyTable(boolean includeEmptyTable) {
080: logger.debug("setIncludeEmptyTable(includeEmptyTable="
081: + includeEmptyTable + ") - start");
082:
083: _includeEmptyTable = includeEmptyTable;
084: }
085:
086: public void setDocType(String systemId) {
087: logger.debug("setDocType(systemId=" + systemId + ") - start");
088:
089: _systemId = systemId;
090: }
091:
092: public void write(IDataSet dataSet) throws DataSetException {
093: logger.debug("write(dataSet=" + dataSet + ") - start");
094:
095: DataSetProducerAdapter provider = new DataSetProducerAdapter(
096: dataSet);
097: provider.setConsumer(this );
098: provider.produce();
099: }
100:
101: ////////////////////////////////////////////////////////////////////////////
102: // IDataSetConsumer interface
103:
104: public void startDataSet() throws DataSetException {
105: logger.debug("startDataSet() - start");
106:
107: try {
108: _xmlWriter.writeDeclaration();
109: _xmlWriter.writeDoctype(_systemId, null);
110: _xmlWriter.writeElement(DATASET);
111: } catch (IOException e) {
112: logger.error("startDataSet()", e);
113:
114: throw new DataSetException(e);
115: }
116: }
117:
118: public void endDataSet() throws DataSetException {
119: logger.debug("endDataSet() - start");
120:
121: try {
122: _xmlWriter.endElement();
123: _xmlWriter.close();
124: } catch (IOException e) {
125: logger.error("endDataSet()", e);
126:
127: throw new DataSetException(e);
128: }
129: }
130:
131: public void startTable(ITableMetaData metaData)
132: throws DataSetException {
133: logger.debug("startTable(metaData=" + metaData + ") - start");
134:
135: _activeMetaData = metaData;
136: _activeRowCount = 0;
137: }
138:
139: public void endTable() throws DataSetException {
140: logger.debug("endTable() - start");
141:
142: if (_includeEmptyTable && _activeRowCount == 0) {
143: try {
144: String tableName = _activeMetaData.getTableName();
145: _xmlWriter.writeEmptyElement(tableName);
146: } catch (IOException e) {
147: logger.error("endTable()", e);
148:
149: throw new DataSetException(e);
150: }
151: }
152:
153: _activeMetaData = null;
154: }
155:
156: public void row(Object[] values) throws DataSetException {
157: logger.debug("row(values=" + values + ") - start");
158:
159: try {
160: String tableName = _activeMetaData.getTableName();
161: _xmlWriter.writeElement(tableName);
162:
163: Column[] columns = _activeMetaData.getColumns();
164: for (int i = 0; i < columns.length; i++) {
165: String columnName = columns[i].getColumnName();
166: Object value = values[i];
167:
168: // Skip null value
169: if (value == null) {
170: continue;
171: }
172:
173: try {
174: String stringValue = DataType.asString(value);
175: _xmlWriter.writeAttribute(columnName, stringValue);
176: } catch (TypeCastException e) {
177: logger.error("row()", e);
178:
179: throw new DataSetException("table="
180: + _activeMetaData.getTableName() + ", row="
181: + i + ", column=" + columnName + ", value="
182: + value, e);
183: }
184: }
185:
186: _activeRowCount++;
187: _xmlWriter.endElement();
188: } catch (IOException e) {
189: logger.error("row()", e);
190:
191: throw new DataSetException(e);
192: }
193: }
194: }
|