KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > XmlProducer


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 JavaDoc;
30
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * @author Manuel Laflamme
40  * @since Apr 30, 2003
41  * @version $Revision: 1.6 $
42  */

43 public class XmlProducer extends DefaultHandler JavaDoc
44         implements IDataSetProducer, ContentHandler, ErrorHandler
45 {
46     private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
47
48     private static final String JavaDoc DATASET = "dataset";
49     private static final String JavaDoc TABLE = "table";
50     private static final String JavaDoc NAME = "name";
51     private static final String JavaDoc COLUMN = "column";
52     private static final String JavaDoc ROW = "row";
53     private static final String JavaDoc VALUE = "value";
54     private static final String JavaDoc NULL = "null";
55     private static final String JavaDoc 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 JavaDoc _activeTableName;
64     private ITableMetaData _activeMetaData;
65
66     private List JavaDoc _activeColumnNames;
67     private StringBuffer JavaDoc _activeCharacters;
68     private List JavaDoc _activeRowValues;
69
70     public XmlProducer(InputSource inputSource)
71     {
72         _inputSource = inputSource;
73     }
74
75     private ITableMetaData createMetaData(String JavaDoc tableName, List JavaDoc _columnNames)
76     {
77         Column[] columns = new Column[_columnNames.size()];
78         for (int i = 0; i < columns.length; i++)
79         {
80             String JavaDoc columnName = (String JavaDoc)_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     ////////////////////////////////////////////////////////////////////////////
94
// IDataSetProducer interface
95

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 JavaDoc 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 JavaDoc e)
115         {
116             throw new DataSetException(e);
117         }
118         catch (SAXException e)
119         {
120             Exception JavaDoc exception = e.getException() == null ? e : e.getException();
121             throw new DataSetException(exception);
122         }
123         catch (IOException JavaDoc e)
124         {
125             throw new DataSetException(e);
126         }
127     }
128
129     ////////////////////////////////////////////////////////////////////////////
130
// EntityResolver interface
131

132     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
133             throws SAXException
134     {
135         InputStream JavaDoc in = getClass().getClassLoader().getResourceAsStream(
136                 "org/dbunit/dataset/xml/dataset.dtd");
137         return (new InputSource(in));
138     }
139
140     ////////////////////////////////////////////////////////////////////////
141
// ContentHandler interface
142

143     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
144             Attributes attributes) throws SAXException
145     {
146         try
147         {
148             // dataset
149
if (qName.equals(DATASET))
150             {
151                 _consumer.startDataSet();
152                 return;
153             }
154
155             // table
156
if (qName.equals(TABLE))
157             {
158                 _activeTableName = attributes.getValue(NAME);
159                 _activeColumnNames = new LinkedList JavaDoc();
160                 return;
161             }
162
163             // column
164
if (qName.equals(COLUMN))
165             {
166                 _activeCharacters = new StringBuffer JavaDoc();
167                 return;
168             }
169
170             // row
171
if (qName.equals(ROW))
172             {
173                 // End of metadata at first row
174
if (_activeColumnNames != null)
175                 {
176                     _activeMetaData = createMetaData(_activeTableName,
177                             _activeColumnNames);
178                     _consumer.startTable(_activeMetaData);
179                     _activeColumnNames = null;
180
181                 }
182
183                 _activeRowValues = new LinkedList JavaDoc();
184                 return;
185             }
186
187             // value
188
if (qName.equals(VALUE))
189             {
190                 _activeCharacters = new StringBuffer JavaDoc();
191                 return;
192             }
193
194             // null
195
if (qName.equals(NULL))
196             {
197                 _activeRowValues.add(null);
198                 return;
199             }
200
201             // none
202
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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException
215     {
216         try
217         {
218             // dataset
219
if (qName.equals(DATASET))
220             {
221                 _consumer.endDataSet();
222                 return;
223             }
224
225             // table
226
if (qName.equals(TABLE))
227             {
228                 // End of metadata
229
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             // column
244
if (qName.equals(COLUMN))
245             {
246                 _activeColumnNames.add(_activeCharacters.toString());
247                 _activeCharacters = null;
248                 return;
249             }
250
251             // row
252
if (qName.equals(ROW))
253             {
254                 Object JavaDoc[] values = new Object JavaDoc[_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             // value
265
if (qName.equals(VALUE))
266             {
267                 _activeRowValues.add(_activeCharacters.toString());
268                 _activeCharacters = null;
269                 return;
270             }
271
272             // null
273
if (qName.equals(NULL))
274             {
275                 // Nothing to do, already processed in startElement()
276
return;
277             }
278
279             // none
280
if (qName.equals(NONE))
281             {
282                 // Nothing to do, already processed in startElement()
283
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     ////////////////////////////////////////////////////////////////////////////
302
// ErrorHandler interface
303

304 // public void warning(SAXParseException e)
305
// throws SAXException
306
// {
307
// throw e;
308
// }
309

310     public void error(SAXParseException e)
311             throws SAXException
312     {
313         throw e;
314     }
315
316 // public void fatalError(SAXParseException e)
317
// throws SAXException
318
// {
319
// throw e;
320
// }
321

322
323 }
324
Popular Tags