1 56 57 package org.objectstyle.cayenne.conf; 58 59 import java.io.IOException ; 60 import java.io.InputStream ; 61 import java.util.HashMap ; 62 import java.util.Map ; 63 64 import org.objectstyle.cayenne.util.AbstractHandler; 65 import org.objectstyle.cayenne.util.Util; 66 import org.xml.sax.Attributes ; 67 import org.xml.sax.ContentHandler ; 68 import org.xml.sax.InputSource ; 69 import org.xml.sax.SAXException ; 70 import org.xml.sax.SAXParseException ; 71 import org.xml.sax.XMLReader ; 72 import org.xml.sax.helpers.DefaultHandler ; 73 74 79 public class ConfigLoader { 80 protected XMLReader parser; 81 protected ConfigLoaderDelegate delegate; 82 83 84 public ConfigLoader(ConfigLoaderDelegate delegate) throws Exception { 85 if (delegate == null) { 86 throw new IllegalArgumentException ("Delegate must not be null."); 87 } 88 89 this.delegate = delegate; 90 parser = Util.createXmlReader(); 91 } 92 93 97 public ConfigLoaderDelegate getDelegate() { 98 return delegate; 99 } 100 101 107 public boolean loadDomains(InputStream in) { 108 DefaultHandler handler = new RootHandler(); 109 parser.setContentHandler(handler); 110 parser.setErrorHandler(handler); 111 112 try { 113 delegate.startedLoading(); 114 parser.parse(new InputSource (in)); 115 delegate.finishedLoading(); 116 } 117 catch (IOException ioex) { 118 getDelegate().loadError(ioex); 119 } 120 catch (SAXException saxex) { 121 getDelegate().loadError(saxex); 122 } 123 124 return !getDelegate().getStatus().hasFailures(); 126 } 127 128 130 133 private class RootHandler extends DefaultHandler { 134 135 142 public void startElement( 143 String namespaceURI, 144 String localName, 145 String qName, 146 Attributes attrs) 147 throws SAXException { 148 if (localName.equals("domains")) { 149 delegate.shouldLoadProjectVersion(attrs.getValue("", "project-version")); 150 new DomainsHandler(parser, this); 151 } 152 else { 153 throw new SAXParseException ( 154 "<domains> should be the root element. <" 155 + localName 156 + "> is unexpected.", 157 null); 158 } 159 } 160 } 161 162 165 private class DomainsHandler extends AbstractHandler { 166 167 174 public DomainsHandler(XMLReader parser, ContentHandler parentHandler) { 175 super(parser, parentHandler); 176 } 177 178 183 public void startElement( 184 String namespaceURI, 185 String localName, 186 String qName, 187 Attributes atts) 188 throws SAXException { 189 if (localName.equals("domain")) { 190 new DomainHandler(getParser(), this).init(localName, atts); 191 } 192 else if (localName.equals("view")) { 193 new ViewHandler(getParser(), this).init(atts); 194 } 195 else { 196 String message = 197 "<domain> or <view> are only valid children of <domains>. <" 198 + localName 199 + "> is unexpected."; 200 throw new SAXParseException (message, null); 201 } 202 } 203 } 204 205 private class ViewHandler extends AbstractHandler { 206 207 public ViewHandler(XMLReader parser, ContentHandler parentHandler) { 208 super(parser, parentHandler); 209 } 210 211 public void init(Attributes attrs) throws SAXException { 212 213 String name = attrs.getValue("", "name"); 214 String location = attrs.getValue("", "location"); 215 delegate.shouldRegisterDataView(name, location); 216 } 217 } 218 219 222 private class DomainHandler extends AbstractHandler { 223 private String domainName; 224 private Map properties; 225 private Map mapLocations; 226 227 public DomainHandler(XMLReader parser, ContentHandler parentHandler) { 228 super(parser, parentHandler); 229 } 230 231 public void init(String name, Attributes attrs) throws SAXException { 232 domainName = attrs.getValue("", "name"); 233 mapLocations = new HashMap (); 234 properties = new HashMap (); 235 delegate.shouldLoadDataDomain(domainName); 236 } 237 238 public void startElement( 239 String namespaceURI, 240 String localName, 241 String qName, 242 Attributes atts) 243 throws SAXException { 244 245 if (localName.equals("property")) { 246 new PropertyHandler(getParser(), this).init(atts, properties); 247 } 248 else if (localName.equals("map")) { 249 loadProperties(); 252 253 new MapHandler(getParser(), this).init( 254 localName, 255 atts, 256 domainName, 257 mapLocations); 258 } 259 else if (localName.equals("node")) { 260 loadMaps(); 263 264 new NodeHandler(getParser(), this).init(localName, atts, domainName); 265 } 266 else { 267 String message = 268 "<node> or <map> should be the children of <domain>. <" 269 + localName 270 + "> is unexpected."; 271 throw new SAXParseException (message, null); 272 } 273 } 274 275 protected void finished() { 276 loadProperties(); 277 loadMaps(); 278 } 279 280 private void loadProperties() { 281 if (properties.size() > 0) { 282 delegate.shouldLoadDataDomainProperties(domainName, properties); 284 285 properties.clear(); 287 } 288 } 289 290 private void loadMaps() { 291 if (mapLocations.size() > 0) { 292 delegate.shouldLoadDataMaps(domainName, mapLocations); 294 mapLocations.clear(); 296 } 297 } 298 } 299 300 private class PropertyHandler extends AbstractHandler { 301 302 public PropertyHandler(XMLReader parser, ContentHandler parentHandler) { 303 super(parser, parentHandler); 304 } 305 306 public void init(Attributes attrs, Map properties) throws SAXException { 307 308 String name = attrs.getValue("", "name"); 309 String value = attrs.getValue("", "value"); 310 if (name != null && value != null) { 311 properties.put(name, value); 312 } 313 } 314 } 315 316 private class MapHandler extends AbstractHandler { 317 protected String domainName; 318 protected String mapName; 319 protected String location; 320 private Map mapLocations; 321 322 public MapHandler(XMLReader parser, ContentHandler parentHandler) { 323 super(parser, parentHandler); 324 } 325 326 public void init(String name, Attributes attrs, String domainName, Map locations) 327 throws SAXException { 328 this.domainName = domainName; 329 this.mapLocations = locations; 330 mapName = attrs.getValue("", "name"); 331 location = attrs.getValue("", "location"); 332 } 333 334 public void startElement( 335 String namespaceURI, 336 String localName, 337 String qName, 338 Attributes attrs) 339 throws SAXException { 340 if (localName.equals("dep-map-ref")) { 341 342 new DepMapRefHandler(getParser(), this).init(localName, attrs); 345 } 346 else { 347 throw new SAXParseException ( 348 "<dep-map-ref> should be the only map child. <" 349 + localName 350 + "> is unexpected.", 351 null); 352 } 353 } 354 355 protected void finished() { 356 mapLocations.put(mapName, location); 357 } 358 } 359 360 361 private class NodeHandler extends AbstractHandler { 362 protected String nodeName; 363 protected String domainName; 364 365 public NodeHandler(XMLReader parser, ContentHandler parentHandler) { 366 super(parser, parentHandler); 367 } 368 369 public void init(String name, Attributes attrs, String domainName) 370 throws SAXException { 371 this.domainName = domainName; 372 373 nodeName = attrs.getValue("", "name"); 374 String dataSrcLocation = attrs.getValue("", "datasource"); 375 String adapterClass = attrs.getValue("", "adapter"); 376 String factoryName = attrs.getValue("", "factory"); 377 delegate.shouldLoadDataNode( 378 domainName, 379 nodeName, 380 dataSrcLocation, 381 adapterClass, 382 factoryName); 383 } 384 385 public void startElement( 386 String namespaceURI, 387 String localName, 388 String qName, 389 Attributes attrs) 390 throws SAXException { 391 392 if (localName.equals("map-ref")) { 393 new MapRefHandler(getParser(), this).init( 394 localName, 395 attrs, 396 domainName, 397 nodeName); 398 } 399 else { 400 throw new SAXParseException ( 401 "<map-ref> should be the only node child. <" 402 + localName 403 + "> is unexpected.", 404 null); 405 } 406 } 407 } 408 409 private class DepMapRefHandler extends AbstractHandler { 411 412 public DepMapRefHandler(XMLReader parser, ContentHandler parentHandler) { 413 super(parser, parentHandler); 414 } 415 416 public void init(String name, Attributes attrs) 417 throws SAXException { 418 } 419 } 420 421 private class MapRefHandler extends AbstractHandler { 422 public MapRefHandler(XMLReader parser, ContentHandler parentHandler) { 423 super(parser, parentHandler); 424 } 425 426 public void init( 427 String name, 428 Attributes attrs, 429 String domainName, 430 String nodeName) 431 throws SAXException { 432 String mapName = attrs.getValue("", "name"); 433 delegate.shouldLinkDataMap(domainName, nodeName, mapName); 434 } 435 } 436 } 437 | Popular Tags |