KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > conf > ConfigLoader


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.conf;
58
59 import java.io.IOException JavaDoc;
60 import java.io.InputStream JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import org.objectstyle.cayenne.util.AbstractHandler;
65 import org.objectstyle.cayenne.util.Util;
66 import org.xml.sax.Attributes JavaDoc;
67 import org.xml.sax.ContentHandler JavaDoc;
68 import org.xml.sax.InputSource JavaDoc;
69 import org.xml.sax.SAXException JavaDoc;
70 import org.xml.sax.SAXParseException JavaDoc;
71 import org.xml.sax.XMLReader JavaDoc;
72 import org.xml.sax.helpers.DefaultHandler JavaDoc;
73
74 /**
75  * Class that performs runtime loading of Cayenne configuration.
76  *
77  * @author Andrei Adamchik
78  */

79 public class ConfigLoader {
80     protected XMLReader JavaDoc parser;
81     protected ConfigLoaderDelegate delegate;
82
83     /** Creates new ConfigLoader. */
84     public ConfigLoader(ConfigLoaderDelegate delegate) throws Exception JavaDoc {
85         if (delegate == null) {
86             throw new IllegalArgumentException JavaDoc("Delegate must not be null.");
87         }
88
89         this.delegate = delegate;
90         parser = Util.createXmlReader();
91     }
92
93     /**
94       * Returns the delegate.
95       * @return ConfigLoaderDelegate
96       */

97     public ConfigLoaderDelegate getDelegate() {
98         return delegate;
99     }
100
101     /**
102      * Parses XML input, invoking delegate methods to interpret loaded XML.
103      *
104      * @param in
105      * @return boolean
106      */

107     public boolean loadDomains(InputStream JavaDoc in) {
108         DefaultHandler JavaDoc handler = new RootHandler();
109         parser.setContentHandler(handler);
110         parser.setErrorHandler(handler);
111
112         try {
113             delegate.startedLoading();
114             parser.parse(new InputSource JavaDoc(in));
115             delegate.finishedLoading();
116         }
117         catch (IOException JavaDoc ioex) {
118             getDelegate().loadError(ioex);
119         }
120         catch (SAXException JavaDoc saxex) {
121             getDelegate().loadError(saxex);
122         }
123
124         // return true if no failures
125
return !getDelegate().getStatus().hasFailures();
126     }
127
128     // SAX handlers start below
129

130     /**
131      * Handler for the root element. Its only child must be the "domains" element.
132      */

133     private class RootHandler extends DefaultHandler JavaDoc {
134
135         /**
136          * Handles the start of a datadomains element. A domains handler is created
137          * and initialised with the element name and attributes.
138          *
139          * @exception SAXException if the tag given is not
140          * <code>"domains"</code>
141          */

142         public void startElement(
143             String JavaDoc namespaceURI,
144             String JavaDoc localName,
145             String JavaDoc qName,
146             Attributes JavaDoc attrs)
147             throws SAXException JavaDoc {
148             if (localName.equals("domains")) {
149                 delegate.shouldLoadProjectVersion(attrs.getValue("", "project-version"));
150                 new DomainsHandler(parser, this);
151             }
152             else {
153                 throw new SAXParseException JavaDoc(
154                     "<domains> should be the root element. <"
155                         + localName
156                         + "> is unexpected.",
157                     null);
158             }
159         }
160     }
161
162     /**
163     * Handler for the top level "project" element.
164     */

165     private class DomainsHandler extends AbstractHandler {
166
167         /**
168          * Constructor which just delegates to the superconstructor.
169          *
170          * @param parentHandler The handler which should be restored to the
171          * parser at the end of the element.
172          * Must not be <code>null</code>.
173          */

174         public DomainsHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
175             super(parser, parentHandler);
176         }
177
178         /**
179          * Handles the start of a top-level element within the project. An
180          * appropriate handler is created and initialised with the details
181          * of the element.
182          */

183         public void startElement(
184             String JavaDoc namespaceURI,
185             String JavaDoc localName,
186             String JavaDoc qName,
187             Attributes JavaDoc atts)
188             throws SAXException JavaDoc {
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 JavaDoc message =
197                     "<domain> or <view> are only valid children of <domains>. <"
198                         + localName
199                         + "> is unexpected.";
200                 throw new SAXParseException JavaDoc(message, null);
201             }
202         }
203     }
204
205     private class ViewHandler extends AbstractHandler {
206
207         public ViewHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
208             super(parser, parentHandler);
209         }
210
211         public void init(Attributes JavaDoc attrs) throws SAXException JavaDoc {
212
213             String JavaDoc name = attrs.getValue("", "name");
214             String JavaDoc location = attrs.getValue("", "location");
215             delegate.shouldRegisterDataView(name, location);
216         }
217     }
218
219     /**
220       * Handler for the "domain" element.
221       */

222     private class DomainHandler extends AbstractHandler {
223         private String JavaDoc domainName;
224         private Map JavaDoc properties;
225         private Map JavaDoc mapLocations;
226
227         public DomainHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
228             super(parser, parentHandler);
229         }
230
231         public void init(String JavaDoc name, Attributes JavaDoc attrs) throws SAXException JavaDoc {
232             domainName = attrs.getValue("", "name");
233             mapLocations = new HashMap JavaDoc();
234             properties = new HashMap JavaDoc();
235             delegate.shouldLoadDataDomain(domainName);
236         }
237
238         public void startElement(
239             String JavaDoc namespaceURI,
240             String JavaDoc localName,
241             String JavaDoc qName,
242             Attributes JavaDoc atts)
243             throws SAXException JavaDoc {
244
245             if (localName.equals("property")) {
246                 new PropertyHandler(getParser(), this).init(atts, properties);
247             }
248             else if (localName.equals("map")) {
249                 // "map" elements go after "property" elements
250
// must flush properties if there are any
251
loadProperties();
252
253                 new MapHandler(getParser(), this).init(
254                     localName,
255                     atts,
256                     domainName,
257                     mapLocations);
258             }
259             else if (localName.equals("node")) {
260                 // "node" elements go after "map" elements
261
// must flush maps if there are any
262
loadMaps();
263
264                 new NodeHandler(getParser(), this).init(localName, atts, domainName);
265             }
266             else {
267                 String JavaDoc message =
268                     "<node> or <map> should be the children of <domain>. <"
269                         + localName
270                         + "> is unexpected.";
271                 throw new SAXParseException JavaDoc(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                 // load all properties
283
delegate.shouldLoadDataDomainProperties(domainName, properties);
284
285                 // clean properties to avoid loading them twice
286
properties.clear();
287             }
288         }
289
290         private void loadMaps() {
291             if (mapLocations.size() > 0) {
292                 // load all maps
293
delegate.shouldLoadDataMaps(domainName, mapLocations);
294                 // clean map locations to avoid loading maps twice
295
mapLocations.clear();
296             }
297         }
298     }
299
300     private class PropertyHandler extends AbstractHandler {
301
302         public PropertyHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
303             super(parser, parentHandler);
304         }
305
306         public void init(Attributes JavaDoc attrs, Map JavaDoc properties) throws SAXException JavaDoc {
307
308             String JavaDoc name = attrs.getValue("", "name");
309             String JavaDoc 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 JavaDoc domainName;
318         protected String JavaDoc mapName;
319         protected String JavaDoc location;
320         private Map JavaDoc mapLocations;
321
322         public MapHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
323             super(parser, parentHandler);
324         }
325
326         public void init(String JavaDoc name, Attributes JavaDoc attrs, String JavaDoc domainName, Map JavaDoc locations)
327             throws SAXException JavaDoc {
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 JavaDoc namespaceURI,
336             String JavaDoc localName,
337             String JavaDoc qName,
338             Attributes JavaDoc attrs)
339             throws SAXException JavaDoc {
340             if (localName.equals("dep-map-ref")) {
341
342                 // this is no longer supported, but kept as noop
343
// for backwards compatibility
344
new DepMapRefHandler(getParser(), this).init(localName, attrs);
345             }
346             else {
347                 throw new SAXParseException JavaDoc(
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     /** Handles processing of "node" element. */
361     private class NodeHandler extends AbstractHandler {
362         protected String JavaDoc nodeName;
363         protected String JavaDoc domainName;
364
365         public NodeHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
366             super(parser, parentHandler);
367         }
368
369         public void init(String JavaDoc name, Attributes JavaDoc attrs, String JavaDoc domainName)
370             throws SAXException JavaDoc {
371             this.domainName = domainName;
372
373             nodeName = attrs.getValue("", "name");
374             String JavaDoc dataSrcLocation = attrs.getValue("", "datasource");
375             String JavaDoc adapterClass = attrs.getValue("", "adapter");
376             String JavaDoc 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 JavaDoc namespaceURI,
387             String JavaDoc localName,
388             String JavaDoc qName,
389             Attributes JavaDoc attrs)
390             throws SAXException JavaDoc {
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 JavaDoc(
401                     "<map-ref> should be the only node child. <"
402                         + localName
403                         + "> is unexpected.",
404                     null);
405             }
406         }
407     }
408
409     // this handler is deprecated, but is kept around for backwards compatibility
410
private class DepMapRefHandler extends AbstractHandler {
411
412         public DepMapRefHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
413             super(parser, parentHandler);
414         }
415
416         public void init(String JavaDoc name, Attributes JavaDoc attrs)
417             throws SAXException JavaDoc {
418         }
419     }
420
421     private class MapRefHandler extends AbstractHandler {
422         public MapRefHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
423             super(parser, parentHandler);
424         }
425
426         public void init(
427             String JavaDoc name,
428             Attributes JavaDoc attrs,
429             String JavaDoc domainName,
430             String JavaDoc nodeName)
431             throws SAXException JavaDoc {
432             String JavaDoc mapName = attrs.getValue("", "name");
433             delegate.shouldLinkDataMap(domainName, nodeName, mapName);
434         }
435     }
436 }
437
Popular Tags