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:
022: package org.dbunit.dataset.xml;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.*;
028: import org.dbunit.dataset.stream.IDataSetConsumer;
029: import org.dbunit.dataset.stream.IDataSetProducer;
030: import org.xml.sax.InputSource;
031:
032: import java.io.*;
033: import java.util.*;
034:
035: /**
036: * @author Manuel Laflamme
037: * @version $Revision: 554 $
038: * @since Apr 4, 2002
039: */
040: public class FlatDtdDataSet extends AbstractDataSet implements
041: IDataSetConsumer {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger logger = LoggerFactory
047: .getLogger(FlatDtdDataSet.class);
048:
049: private final List _tableNames = new ArrayList();
050: private final Map _tableMap = new HashMap();
051: private boolean _ready = false;
052:
053: public FlatDtdDataSet() {
054: }
055:
056: public FlatDtdDataSet(InputStream in) throws DataSetException,
057: IOException {
058: this (new FlatDtdProducer(new InputSource(in)));
059: }
060:
061: public FlatDtdDataSet(Reader reader) throws DataSetException,
062: IOException {
063: this (new FlatDtdProducer(new InputSource(reader)));
064: }
065:
066: public FlatDtdDataSet(IDataSetProducer producer)
067: throws DataSetException {
068: producer.setConsumer(this );
069: producer.produce();
070: }
071:
072: /**
073: * Write the specified dataset to the specified output stream as DTD.
074: */
075: public static void write(IDataSet dataSet, OutputStream out)
076: throws IOException, DataSetException {
077: logger.debug("write(dataSet=" + dataSet + ", out=" + out
078: + ") - start");
079:
080: write(dataSet, new OutputStreamWriter(out));
081: }
082:
083: /**
084: * Write the specified dataset to the specified writer as DTD.
085: */
086: public static void write(IDataSet dataSet, Writer out)
087: throws IOException, DataSetException {
088: logger.debug("write(dataSet=" + dataSet + ", out=" + out
089: + ") - start");
090:
091: FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
092: datasetWriter.write(dataSet);
093: }
094:
095: ////////////////////////////////////////////////////////////////////////////
096: // AbstractDataSet class
097:
098: protected ITableIterator createIterator(boolean reversed)
099: throws DataSetException {
100: logger.debug("createIterator(reversed=" + reversed
101: + ") - start");
102:
103: // Verify producer notifications completed
104: if (!_ready) {
105: throw new IllegalStateException("Not ready!");
106: }
107:
108: String[] names = (String[]) _tableNames.toArray(new String[0]);
109: ITable[] tables = new ITable[names.length];
110: for (int i = 0; i < names.length; i++) {
111: String tableName = names[i];
112: ITable table = (ITable) _tableMap.get(tableName);
113: if (table == null) {
114: throw new NoSuchTableException(tableName);
115: }
116:
117: tables[i] = table;
118: }
119:
120: return new DefaultTableIterator(tables, reversed);
121: }
122:
123: ////////////////////////////////////////////////////////////////////////////
124: // IDataSet interface
125:
126: public String[] getTableNames() throws DataSetException {
127: logger.debug("getTableNames() - start");
128:
129: // Verify producer notifications completed
130: if (!_ready) {
131: throw new IllegalStateException("Not ready!");
132: }
133:
134: return (String[]) _tableNames.toArray(new String[0]);
135: }
136:
137: public ITableMetaData getTableMetaData(String tableName)
138: throws DataSetException {
139: logger.debug("getTableMetaData(tableName=" + tableName
140: + ") - start");
141:
142: // Verify producer notifications completed
143: if (!_ready) {
144: throw new IllegalStateException("Not ready!");
145: }
146:
147: String upperTableName = tableName.toUpperCase();
148: if (_tableMap.containsKey(upperTableName)) {
149: ITable table = (ITable) _tableMap.get(upperTableName);
150: return table.getTableMetaData();
151: }
152:
153: throw new NoSuchTableException(tableName);
154: }
155:
156: public ITable getTable(String tableName) throws DataSetException {
157: logger.debug("getTable(tableName=" + tableName + ") - start");
158:
159: // Verify producer notifications completed
160: if (!_ready) {
161: throw new IllegalStateException("Not ready!");
162: }
163:
164: String upperTableName = tableName.toUpperCase();
165: if (_tableMap.containsKey(upperTableName)) {
166: return (ITable) _tableMap.get(upperTableName);
167: }
168:
169: throw new NoSuchTableException(tableName);
170: }
171:
172: ////////////////////////////////////////////////////////////////////////
173: // IDataSetConsumer interface
174:
175: public void startDataSet() throws DataSetException {
176: logger.debug("startDataSet() - start");
177:
178: _ready = false;
179: }
180:
181: public void endDataSet() throws DataSetException {
182: logger.debug("endDataSet() - start");
183:
184: _ready = true;
185: }
186:
187: public void startTable(ITableMetaData metaData)
188: throws DataSetException {
189: logger.debug("startTable(metaData=" + metaData + ") - start");
190:
191: String tableName = metaData.getTableName();
192: _tableNames.add(tableName);
193: _tableMap.put(tableName.toUpperCase(), new DefaultTable(
194: metaData));
195: }
196:
197: public void endTable() throws DataSetException {
198: // no op
199: }
200:
201: public void row(Object[] values) throws DataSetException {
202: // no op
203: }
204: }
|