1 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 ; 32 import org.xml.sax.ext.LexicalHandler ; 33 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.parsers.SAXParser ; 36 import javax.xml.parsers.SAXParserFactory ; 37 import java.io.IOException ; 38 import java.io.StringReader ; 39 import java.util.*; 40 41 46 public class FlatDtdProducer implements IDataSetProducer, EntityResolver, DeclHandler , LexicalHandler 47 { 48 private static final IDataSetConsumer EMPTY_CONSUMER = new DefaultConsumer(); 49 50 private static final String XML_CONTENT = 51 "<?xml version=\"1.0\"?>" + 52 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" + 53 "<dataset/>"; 54 private static final String DECL_HANDLER_PROPERTY_NAME = 55 "http://xml.org/sax/properties/declaration-handler"; 56 private static final String LEXICAL_HANDLER_PROPERTY_NAME = 57 "http://xml.org/sax/properties/lexical-handler"; 58 59 private static final String REQUIRED = "#REQUIRED"; 60 private static final String IMPLIED = "#IMPLIED"; 61 62 private InputSource _inputSource; 63 private IDataSetConsumer _consumer = EMPTY_CONSUMER; 64 65 private String _rootName; 66 private String _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 handler) 79 throws SAXNotRecognizedException, SAXNotSupportedException 80 { 81 xmlReader.setProperty(DECL_HANDLER_PROPERTY_NAME, handler); 82 } 83 84 public static void setLexicalHandler(XMLReader xmlReader, LexicalHandler 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 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 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 (XML_CONTENT))); 114 } 115 catch (ParserConfigurationException e) 116 { 117 throw new DataSetException(e); 118 } 119 catch (SAXException e) 120 { 121 Exception exception = e.getException() == null ? e : e.getException(); 122 throw new DataSetException(exception); 123 } 124 catch (IOException e) 125 { 126 throw new DataSetException(e); 127 } 128 } 129 130 133 public InputSource resolveEntity(String publicId, String systemId) 134 throws SAXException 135 { 136 return _inputSource; 137 } 138 139 142 public void elementDecl(String name, String model) throws SAXException 143 { 144 if (name.equals(_rootName)) 146 { 147 _rootModel = model; 149 } 150 else if (!_columnListMap.containsKey(name)) 151 { 152 _columnListMap.put(name, createColumnList()); 153 } 154 } 155 156 public void attributeDecl(String elementName, String attributeName, 157 String type, String mode, String value) throws SAXException 158 { 159 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 name, String value) throws SAXException 173 { 174 } 176 177 public void externalEntityDecl(String name, String publicId, 178 String systemId) throws SAXException 179 { 180 } 182 183 186 public void startDTD(String name, String publicId, String 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 String rootModel = _rootModel.substring(1, _rootModel.length() - 1); 208 209 String delim = (rootModel.indexOf(",") != -1) ? "," : "|"; 212 StringTokenizer tokenizer = new StringTokenizer(rootModel, delim); 213 while (tokenizer.hasMoreTokens()) 214 { 215 String tableName = tokenizer.nextToken(); 216 217 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 name) throws SAXException 240 { 241 } 243 244 public void endEntity(String name) throws SAXException 245 { 246 } 248 249 public void startCDATA() throws SAXException 250 { 251 } 253 254 public void endCDATA() throws SAXException 255 { 256 } 258 259 public void comment(char ch[], int start, int length) throws SAXException 260 { 261 } 263 } 264 | Popular Tags |