1 21 package org.dbunit.dataset.xml; 22 23 import org.dbunit.dataset.*; 24 import org.dbunit.dataset.datatype.DataType; 25 import org.dbunit.dataset.stream.DefaultConsumer; 26 import org.dbunit.dataset.stream.IDataSetConsumer; 27 import org.dbunit.dataset.stream.IDataSetProducer; 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.DefaultHandler ; 30 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.parsers.SAXParserFactory ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 38 43 public class XmlProducer extends DefaultHandler 44 implements IDataSetProducer, ContentHandler, ErrorHandler 45 { 46 private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer(); 47 48 private static final String DATASET = "dataset"; 49 private static final String TABLE = "table"; 50 private static final String NAME = "name"; 51 private static final String COLUMN = "column"; 52 private static final String ROW = "row"; 53 private static final String VALUE = "value"; 54 private static final String NULL = "null"; 55 private static final String NONE = "none"; 56 57 private final InputSource _inputSource; 58 private boolean _validating = false; 59 60 private IDataSetConsumer _consumer = EMPTY_CONSUMER; 61 62 63 private String _activeTableName; 64 private ITableMetaData _activeMetaData; 65 66 private List _activeColumnNames; 67 private StringBuffer _activeCharacters; 68 private List _activeRowValues; 69 70 public XmlProducer(InputSource inputSource) 71 { 72 _inputSource = inputSource; 73 } 74 75 private ITableMetaData createMetaData(String tableName, List _columnNames) 76 { 77 Column[] columns = new Column[_columnNames.size()]; 78 for (int i = 0; i < columns.length; i++) 79 { 80 String columnName = (String )_columnNames.get(i); 81 columns[i] = new Column(columnName, DataType.UNKNOWN); 82 } 83 DefaultTableMetaData metaData = 84 new DefaultTableMetaData(tableName, columns); 85 return metaData; 86 } 87 88 public void setValidating(boolean validating) 89 { 90 _validating = validating; 91 } 92 93 96 public void setConsumer(IDataSetConsumer consumer) throws DataSetException 97 { 98 _consumer = consumer; 99 } 100 101 public void produce() throws DataSetException 102 { 103 try 104 { 105 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 106 saxParserFactory.setValidating(_validating); 107 XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader(); 108 109 xmlReader.setContentHandler(this); 110 xmlReader.setEntityResolver(this); 111 xmlReader.setErrorHandler(this); 112 xmlReader.parse(_inputSource); 113 } 114 catch (ParserConfigurationException e) 115 { 116 throw new DataSetException(e); 117 } 118 catch (SAXException e) 119 { 120 Exception exception = e.getException() == null ? e : e.getException(); 121 throw new DataSetException(exception); 122 } 123 catch (IOException e) 124 { 125 throw new DataSetException(e); 126 } 127 } 128 129 132 public InputSource resolveEntity(String publicId, String systemId) 133 throws SAXException 134 { 135 InputStream in = getClass().getClassLoader().getResourceAsStream( 136 "org/dbunit/dataset/xml/dataset.dtd"); 137 return (new InputSource(in)); 138 } 139 140 143 public void startElement(String uri, String localName, String qName, 144 Attributes attributes) throws SAXException 145 { 146 try 147 { 148 if (qName.equals(DATASET)) 150 { 151 _consumer.startDataSet(); 152 return; 153 } 154 155 if (qName.equals(TABLE)) 157 { 158 _activeTableName = attributes.getValue(NAME); 159 _activeColumnNames = new LinkedList (); 160 return; 161 } 162 163 if (qName.equals(COLUMN)) 165 { 166 _activeCharacters = new StringBuffer (); 167 return; 168 } 169 170 if (qName.equals(ROW)) 172 { 173 if (_activeColumnNames != null) 175 { 176 _activeMetaData = createMetaData(_activeTableName, 177 _activeColumnNames); 178 _consumer.startTable(_activeMetaData); 179 _activeColumnNames = null; 180 181 } 182 183 _activeRowValues = new LinkedList (); 184 return; 185 } 186 187 if (qName.equals(VALUE)) 189 { 190 _activeCharacters = new StringBuffer (); 191 return; 192 } 193 194 if (qName.equals(NULL)) 196 { 197 _activeRowValues.add(null); 198 return; 199 } 200 201 if (qName.equals(NONE)) 203 { 204 _activeRowValues.add(ITable.NO_VALUE); 205 return; 206 } 207 } 208 catch (DataSetException e) 209 { 210 throw new SAXException(e); 211 } 212 } 213 214 public void endElement(String uri, String localName, String qName) throws SAXException 215 { 216 try 217 { 218 if (qName.equals(DATASET)) 220 { 221 _consumer.endDataSet(); 222 return; 223 } 224 225 if (qName.equals(TABLE)) 227 { 228 if (_activeColumnNames != null) 230 { 231 _activeMetaData = createMetaData(_activeTableName, 232 _activeColumnNames); 233 _consumer.startTable(_activeMetaData); 234 _activeColumnNames = null; 235 } 236 237 _consumer.endTable(); 238 _activeTableName = null; 239 _activeMetaData = null; 240 return; 241 } 242 243 if (qName.equals(COLUMN)) 245 { 246 _activeColumnNames.add(_activeCharacters.toString()); 247 _activeCharacters = null; 248 return; 249 } 250 251 if (qName.equals(ROW)) 253 { 254 Object [] values = new Object [_activeMetaData.getColumns().length]; 255 for (int i = 0; i < values.length; i++) 256 { 257 values[i] = (i >= _activeRowValues.size()) ? ITable.NO_VALUE : _activeRowValues.get(i); 258 } 259 _consumer.row(values); 260 _activeRowValues = null; 261 return; 262 } 263 264 if (qName.equals(VALUE)) 266 { 267 _activeRowValues.add(_activeCharacters.toString()); 268 _activeCharacters = null; 269 return; 270 } 271 272 if (qName.equals(NULL)) 274 { 275 return; 277 } 278 279 if (qName.equals(NONE)) 281 { 282 return; 284 } 285 } 286 catch (DataSetException e) 287 { 288 throw new SAXException(e); 289 } 290 } 291 292 public void characters(char ch[], int start, int length) 293 throws SAXException 294 { 295 if (_activeCharacters != null) 296 { 297 _activeCharacters.append(ch, start, length); 298 } 299 } 300 301 304 310 public void error(SAXParseException e) 311 throws SAXException 312 { 313 throw e; 314 } 315 316 322 323 } 324 | Popular Tags |