1 21 22 package org.dbunit.dataset.xml; 23 24 import org.dbunit.dataset.*; 25 import org.dbunit.dataset.stream.IDataSetConsumer; 26 import org.dbunit.dataset.stream.IDataSetProducer; 27 import org.xml.sax.InputSource ; 28 29 import java.io.*; 30 import java.util.*; 31 32 37 public class FlatDtdDataSet extends AbstractDataSet implements IDataSetConsumer 38 { 39 private final List _tableNames = new ArrayList(); 40 private final Map _tableMap = new HashMap(); 41 private boolean _ready = false; 42 43 public FlatDtdDataSet() 44 { 45 } 46 47 public FlatDtdDataSet(InputStream in) throws DataSetException, IOException 48 { 49 this(new FlatDtdProducer(new InputSource (in))); 50 } 51 52 public FlatDtdDataSet(Reader reader) throws DataSetException, IOException 53 { 54 this(new FlatDtdProducer(new InputSource (reader))); 55 } 56 57 public FlatDtdDataSet(IDataSetProducer producer) throws DataSetException 58 { 59 producer.setConsumer(this); 60 producer.produce(); 61 } 62 63 66 public static void write(IDataSet dataSet, OutputStream out) 67 throws IOException, DataSetException 68 { 69 write(dataSet, new OutputStreamWriter(out)); 70 } 71 72 75 public static void write(IDataSet dataSet, Writer out) 76 throws IOException, DataSetException 77 { 78 FlatDtdWriter datasetWriter = new FlatDtdWriter(out); 79 datasetWriter.write(dataSet); 80 } 81 82 85 protected ITableIterator createIterator(boolean reversed) 86 throws DataSetException 87 { 88 if (!_ready) 90 { 91 throw new IllegalStateException ("Not ready!"); 92 } 93 94 String [] names = (String [])_tableNames.toArray(new String [0]); 95 ITable[] tables = new ITable[names.length]; 96 for (int i = 0; i < names.length; i++) 97 { 98 String tableName = names[i]; 99 ITable table = (ITable)_tableMap.get(tableName); 100 if (table == null) 101 { 102 throw new NoSuchTableException(tableName); 103 } 104 105 tables[i] = table; 106 } 107 108 return new DefaultTableIterator(tables, reversed); 109 } 110 111 114 public String [] getTableNames() throws DataSetException 115 { 116 if (!_ready) 118 { 119 throw new IllegalStateException ("Not ready!"); 120 } 121 122 return (String [])_tableNames.toArray(new String [0]); 123 } 124 125 public ITableMetaData getTableMetaData(String tableName) throws DataSetException 126 { 127 if (!_ready) 129 { 130 throw new IllegalStateException ("Not ready!"); 131 } 132 133 String upperTableName = tableName.toUpperCase(); 134 if (_tableMap.containsKey(upperTableName)) 135 { 136 ITable table = (ITable)_tableMap.get(upperTableName); 137 return table.getTableMetaData(); 138 } 139 140 throw new NoSuchTableException(tableName); 141 } 142 143 public ITable getTable(String tableName) throws DataSetException 144 { 145 if (!_ready) 147 { 148 throw new IllegalStateException ("Not ready!"); 149 } 150 151 String upperTableName = tableName.toUpperCase(); 152 if (_tableMap.containsKey(upperTableName)) 153 { 154 return (ITable)_tableMap.get(upperTableName); 155 } 156 157 throw new NoSuchTableException(tableName); 158 } 159 160 163 public void startDataSet() throws DataSetException 164 { 165 _ready = false; 166 } 167 168 public void endDataSet() throws DataSetException 169 { 170 _ready = true; 171 } 172 173 public void startTable(ITableMetaData metaData) throws DataSetException 174 { 175 String tableName = metaData.getTableName(); 176 _tableNames.add(tableName); 177 _tableMap.put(tableName.toUpperCase(), new DefaultTable(metaData)); 178 } 179 180 public void endTable() throws DataSetException 181 { 182 } 184 185 public void row(Object [] values) throws DataSetException 186 { 187 } 189 } | Popular Tags |