KickJava   Java API By Example, From Geeks To Geeks.

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


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.Column;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.DefaultTableMetaData;
26 import org.dbunit.dataset.datatype.DataType;
27 import org.dbunit.dataset.stream.DefaultConsumer;
28 import org.dbunit.dataset.stream.IDataSetConsumer;
29 import org.dbunit.dataset.stream.IDataSetProducer;
30 import org.xml.sax.*;
31 import org.xml.sax.ext.DeclHandler JavaDoc;
32 import org.xml.sax.ext.LexicalHandler JavaDoc;
33
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35 import javax.xml.parsers.SAXParser JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.StringReader JavaDoc;
39 import java.util.*;
40
41 /**
42  * @author Manuel Laflamme
43  * @since Apr 27, 2003
44  * @version $Revision: 1.5 $
45  */

46 public class FlatDtdProducer implements IDataSetProducer, EntityResolver, DeclHandler JavaDoc, LexicalHandler JavaDoc
47 {
48     private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer();
49
50     private static final String JavaDoc XML_CONTENT =
51             "<?xml version=\"1.0\"?>" +
52             "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" +
53             "<dataset/>";
54     private static final String JavaDoc DECL_HANDLER_PROPERTY_NAME =
55             "http://xml.org/sax/properties/declaration-handler";
56     private static final String JavaDoc LEXICAL_HANDLER_PROPERTY_NAME =
57             "http://xml.org/sax/properties/lexical-handler";
58
59     private static final String JavaDoc REQUIRED = "#REQUIRED";
60     private static final String JavaDoc IMPLIED = "#IMPLIED";
61
62     private InputSource _inputSource;
63     private IDataSetConsumer _consumer = EMPTY_CONSUMER;
64
65     private String JavaDoc _rootName;
66     private String JavaDoc _rootModel;
67     private final Map _columnListMap = new HashMap();
68
69     public FlatDtdProducer()
70     {
71     }
72
73     public FlatDtdProducer(InputSource inputSource)
74     {
75         _inputSource = inputSource;
76     }
77
78     public static void setDeclHandler(XMLReader xmlReader, DeclHandler JavaDoc handler)
79             throws SAXNotRecognizedException, SAXNotSupportedException
80     {
81         xmlReader.setProperty(DECL_HANDLER_PROPERTY_NAME, handler);
82     }
83
84     public static void setLexicalHandler(XMLReader xmlReader, LexicalHandler JavaDoc handler)
85             throws SAXNotRecognizedException, SAXNotSupportedException
86     {
87         xmlReader.setProperty(LEXICAL_HANDLER_PROPERTY_NAME, handler);
88     }
89
90     private List createColumnList()
91     {
92         return new LinkedList();
93     }
94
95     ////////////////////////////////////////////////////////////////////////////
96
// IDataSetProducer interface
97

98     public void setConsumer(IDataSetConsumer consumer) throws DataSetException
99     {
100         _consumer = consumer;
101     }
102
103     public void produce() throws DataSetException
104     {
105         try
106         {
107             SAXParser JavaDoc saxParser = SAXParserFactory.newInstance().newSAXParser();
108             XMLReader xmlReader = saxParser.getXMLReader();
109
110             setDeclHandler(xmlReader, this);
111             setLexicalHandler(xmlReader, this);
112             xmlReader.setEntityResolver(this);
113             xmlReader.parse(new InputSource(new StringReader JavaDoc(XML_CONTENT)));
114         }
115         catch (ParserConfigurationException JavaDoc e)
116         {
117             throw new DataSetException(e);
118         }
119         catch (SAXException e)
120         {
121             Exception JavaDoc exception = e.getException() == null ? e : e.getException();
122             throw new DataSetException(exception);
123         }
124         catch (IOException JavaDoc e)
125         {
126             throw new DataSetException(e);
127         }
128     }
129
130     ////////////////////////////////////////////////////////////////////////////
131
// EntityResolver interface
132

133     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
134             throws SAXException
135     {
136         return _inputSource;
137     }
138
139     ////////////////////////////////////////////////////////////////////////////
140
// DeclHandler interface
141

142     public void elementDecl(String JavaDoc name, String JavaDoc model) throws SAXException
143     {
144         // Root element
145
if (name.equals(_rootName))
146         {
147             // The root model defines the table sequence. Keep it for later used!
148
_rootModel = model;
149         }
150         else if (!_columnListMap.containsKey(name))
151         {
152             _columnListMap.put(name, createColumnList());
153         }
154     }
155
156     public void attributeDecl(String JavaDoc elementName, String JavaDoc attributeName,
157             String JavaDoc type, String JavaDoc mode, String JavaDoc value) throws SAXException
158     {
159         // Each element attribute represent a table column
160
Column.Nullable nullable = (REQUIRED.equals(mode)) ?
161                 Column.NO_NULLS : Column.NULLABLE;
162         Column column = new Column(attributeName, DataType.UNKNOWN, nullable);
163
164         if (!_columnListMap.containsKey(elementName))
165         {
166             _columnListMap.put(elementName, createColumnList());
167         }
168         List columnList = (List)_columnListMap.get(elementName);
169         columnList.add(column);
170     }
171
172     public void internalEntityDecl(String JavaDoc name, String JavaDoc value) throws SAXException
173     {
174         // Not used!
175
}
176
177     public void externalEntityDecl(String JavaDoc name, String JavaDoc publicId,
178             String JavaDoc systemId) throws SAXException
179     {
180         // Not used!
181
}
182
183     ////////////////////////////////////////////////////////////////////////////
184
// LexicalHandler interface
185

186     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
187             throws SAXException
188     {
189         try
190         {
191             _rootName = name;
192             _consumer.startDataSet();
193         }
194         catch (DataSetException e)
195         {
196             throw new SAXException(e);
197         }
198     }
199
200     public void endDTD() throws SAXException
201     {
202         try
203         {
204             if (_rootModel != null)
205             {
206                 // Remove enclosing model parenthesis
207
String JavaDoc rootModel = _rootModel.substring(1, _rootModel.length() - 1);
208
209                 // Parse the root element model to determine the table sequence.
210
// Support all sequence or choices model but not the mix of both.
211
String JavaDoc delim = (rootModel.indexOf(",") != -1) ? "," : "|";
212                 StringTokenizer tokenizer = new StringTokenizer(rootModel, delim);
213                 while (tokenizer.hasMoreTokens())
214                 {
215                     String JavaDoc tableName = tokenizer.nextToken();
216
217                     // Prune ending occurrence operator
218
if (tableName.endsWith("*") || tableName.endsWith("?") || tableName.endsWith("+"))
219                     {
220                         tableName = tableName.substring(0, tableName.length() - 1);
221                     }
222
223                     List columnList = (List)_columnListMap.get(tableName);
224                     Column[] columns = (Column[])columnList.toArray(new Column[0]);
225
226                     _consumer.startTable(new DefaultTableMetaData(tableName, columns));
227                     _consumer.endTable();
228                 }
229             }
230
231             _consumer.endDataSet();
232         }
233         catch (DataSetException e)
234         {
235             throw new SAXException(e);
236         }
237     }
238
239     public void startEntity(String JavaDoc name) throws SAXException
240     {
241         // Not used!
242
}
243
244     public void endEntity(String JavaDoc name) throws SAXException
245     {
246         // Not used!
247
}
248
249     public void startCDATA() throws SAXException
250     {
251         // Not used!
252
}
253
254     public void endCDATA() throws SAXException
255     {
256         // Not used!
257
}
258
259     public void comment(char ch[], int start, int length) throws SAXException
260     {
261         // Not used!
262
}
263 }
264
Popular Tags