1 19 20 package org.apache.cayenne.conf; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.util.Util; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.SAXParseException ; 33 import org.xml.sax.XMLReader ; 34 import org.xml.sax.helpers.DefaultHandler ; 35 36 41 public class ConfigLoader { 42 43 protected XMLReader parser; 44 protected ConfigLoaderDelegate delegate; 45 46 47 public ConfigLoader(ConfigLoaderDelegate delegate) throws Exception { 48 if (delegate == null) { 49 throw new IllegalArgumentException ("Delegate must not be null."); 50 } 51 52 this.delegate = delegate; 53 parser = Util.createXmlReader(); 54 } 55 56 61 public ConfigLoaderDelegate getDelegate() { 62 return delegate; 63 } 64 65 71 public boolean loadDomains(InputStream in) { 72 DefaultHandler handler = new RootHandler(); 73 parser.setContentHandler(handler); 74 parser.setErrorHandler(handler); 75 76 try { 77 delegate.startedLoading(); 78 parser.parse(new InputSource (in)); 79 delegate.finishedLoading(); 80 } 81 catch (IOException ioex) { 82 getDelegate().loadError(ioex); 83 } 84 catch (SAXException saxex) { 85 getDelegate().loadError(saxex); 86 } 87 88 return !getDelegate().getStatus().hasFailures(); 90 } 91 92 94 97 private class RootHandler extends DefaultHandler { 98 99 105 public void startElement( 106 String namespaceURI, 107 String localName, 108 String qName, 109 Attributes attrs) throws SAXException { 110 if (localName.equals("domains")) { 111 delegate.shouldLoadProjectVersion(attrs.getValue("", "project-version")); 112 new DomainsHandler(parser, this); 113 } 114 else { 115 throw new SAXParseException ("<domains> should be the root element. <" 116 + localName 117 + "> is unexpected.", null); 118 } 119 } 120 } 121 122 125 private class DomainsHandler extends AbstractHandler { 126 127 133 public DomainsHandler(XMLReader parser, ContentHandler parentHandler) { 134 super(parser, parentHandler); 135 } 136 137 141 public void startElement( 142 String namespaceURI, 143 String localName, 144 String qName, 145 Attributes atts) throws SAXException { 146 if (localName.equals("domain")) { 147 new DomainHandler(getParser(), this).init(localName, atts); 148 } 149 else if (localName.equals("view")) { 150 new ViewHandler(getParser(), this).init(atts); 151 } 152 else { 153 String message = "<domain> or <view> are only valid children of <domains>. <" 154 + localName 155 + "> is unexpected."; 156 throw new SAXParseException (message, null); 157 } 158 } 159 } 160 161 private class ViewHandler extends AbstractHandler { 162 163 public ViewHandler(XMLReader parser, ContentHandler parentHandler) { 164 super(parser, parentHandler); 165 } 166 167 public void init(Attributes attrs) { 168 String name = attrs.getValue("", "name"); 169 String location = attrs.getValue("", "location"); 170 delegate.shouldRegisterDataView(name, location); 171 } 172 } 173 174 177 private class DomainHandler extends AbstractHandler { 178 179 private String domainName; 180 private Map properties; 181 private Map mapLocations; 182 183 public DomainHandler(XMLReader parser, ContentHandler parentHandler) { 184 super(parser, parentHandler); 185 } 186 187 public void init(String name, Attributes attrs) { 188 domainName = attrs.getValue("", "name"); 189 mapLocations = new HashMap (); 190 properties = new HashMap (); 191 delegate.shouldLoadDataDomain(domainName); 192 } 193 194 public void startElement( 195 String namespaceURI, 196 String localName, 197 String qName, 198 Attributes atts) throws SAXException { 199 200 if (localName.equals("property")) { 201 new PropertyHandler(getParser(), this).init(atts, properties); 202 } 203 else if (localName.equals("map")) { 204 loadProperties(); 207 208 new MapHandler(getParser(), this).init( 209 localName, 210 atts, 211 domainName, 212 mapLocations); 213 } 214 else if (localName.equals("node")) { 215 loadMaps(); 218 219 new NodeHandler(getParser(), this).init(localName, atts, domainName); 220 } 221 else { 222 String message = "<node> or <map> should be the children of <domain>. <" 223 + localName 224 + "> is unexpected."; 225 throw new SAXParseException (message, null); 226 } 227 } 228 229 protected void finished() { 230 loadProperties(); 231 loadMaps(); 232 } 233 234 private void loadProperties() { 235 if (properties.size() > 0) { 236 delegate.shouldLoadDataDomainProperties(domainName, properties); 238 239 properties.clear(); 241 } 242 } 243 244 private void loadMaps() { 245 if (mapLocations.size() > 0) { 246 delegate.shouldLoadDataMaps(domainName, mapLocations); 248 mapLocations.clear(); 250 } 251 } 252 } 253 254 private class PropertyHandler extends AbstractHandler { 255 256 public PropertyHandler(XMLReader parser, ContentHandler parentHandler) { 257 super(parser, parentHandler); 258 } 259 260 public void init(Attributes attrs, Map properties) { 261 262 String name = attrs.getValue("", "name"); 263 String value = attrs.getValue("", "value"); 264 if (name != null && value != null) { 265 properties.put(name, value); 266 } 267 } 268 } 269 270 private class MapHandler extends AbstractHandler { 271 272 protected String domainName; 273 protected String mapName; 274 protected String location; 275 private Map mapLocations; 276 277 public MapHandler(XMLReader parser, ContentHandler parentHandler) { 278 super(parser, parentHandler); 279 } 280 281 public void init(String name, Attributes attrs, String domainName, Map locations) { 282 this.domainName = domainName; 283 this.mapLocations = locations; 284 mapName = attrs.getValue("", "name"); 285 location = attrs.getValue("", "location"); 286 } 287 288 public void startElement( 289 String namespaceURI, 290 String localName, 291 String qName, 292 Attributes attrs) throws SAXException { 293 if (localName.equals("dep-map-ref")) { 294 295 new DepMapRefHandler(getParser(), this).init(localName, attrs); 298 } 299 else { 300 throw new SAXParseException ( 301 "<dep-map-ref> should be the only map child. <" 302 + localName 303 + "> is unexpected.", 304 null); 305 } 306 } 307 308 protected void finished() { 309 mapLocations.put(mapName, location); 310 } 311 } 312 313 314 private class NodeHandler extends AbstractHandler { 315 316 protected String nodeName; 317 protected String domainName; 318 319 public NodeHandler(XMLReader parser, ContentHandler parentHandler) { 320 super(parser, parentHandler); 321 } 322 323 public void init(String name, Attributes attrs, String domainName) { 324 this.domainName = domainName; 325 326 nodeName = attrs.getValue("", "name"); 327 String dataSrcLocation = attrs.getValue("", "datasource"); 328 String adapterClass = attrs.getValue("", "adapter"); 329 String factoryName = attrs.getValue("", "factory"); 330 delegate.shouldLoadDataNode( 331 domainName, 332 nodeName, 333 dataSrcLocation, 334 adapterClass, 335 factoryName); 336 } 337 338 public void startElement( 339 String namespaceURI, 340 String localName, 341 String qName, 342 Attributes attrs) throws SAXException { 343 344 if (localName.equals("map-ref")) { 345 new MapRefHandler(getParser(), this).init( 346 localName, 347 attrs, 348 domainName, 349 nodeName); 350 } 351 else { 352 throw new SAXParseException ("<map-ref> should be the only node child. <" 353 + localName 354 + "> is unexpected.", null); 355 } 356 } 357 } 358 359 private class DepMapRefHandler extends AbstractHandler { 361 362 public DepMapRefHandler(XMLReader parser, ContentHandler parentHandler) { 363 super(parser, parentHandler); 364 } 365 366 public void init(String name, Attributes attrs) { 367 } 368 } 369 370 private class MapRefHandler extends AbstractHandler { 371 372 public MapRefHandler(XMLReader parser, ContentHandler parentHandler) { 373 super(parser, parentHandler); 374 } 375 376 public void init(String name, Attributes attrs, String domainName, String nodeName) { 377 String mapName = attrs.getValue("", "name"); 378 delegate.shouldLinkDataMap(domainName, nodeName, mapName); 379 } 380 } 381 } 382 | Popular Tags |