KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.util.Util;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.SAXParseException JavaDoc;
33 import org.xml.sax.XMLReader JavaDoc;
34 import org.xml.sax.helpers.DefaultHandler JavaDoc;
35
36 /**
37  * Class that performs runtime loading of Cayenne configuration.
38  *
39  * @author Andrus Adamchik
40  */

41 public class ConfigLoader {
42
43     protected XMLReader JavaDoc parser;
44     protected ConfigLoaderDelegate delegate;
45
46     /** Creates new ConfigLoader. */
47     public ConfigLoader(ConfigLoaderDelegate delegate) throws Exception JavaDoc {
48         if (delegate == null) {
49             throw new IllegalArgumentException JavaDoc("Delegate must not be null.");
50         }
51
52         this.delegate = delegate;
53         parser = Util.createXmlReader();
54     }
55
56     /**
57      * Returns the delegate.
58      *
59      * @return ConfigLoaderDelegate
60      */

61     public ConfigLoaderDelegate getDelegate() {
62         return delegate;
63     }
64
65     /**
66      * Parses XML input, invoking delegate methods to interpret loaded XML.
67      *
68      * @param in
69      * @return boolean
70      */

71     public boolean loadDomains(InputStream JavaDoc in) {
72         DefaultHandler JavaDoc handler = new RootHandler();
73         parser.setContentHandler(handler);
74         parser.setErrorHandler(handler);
75
76         try {
77             delegate.startedLoading();
78             parser.parse(new InputSource JavaDoc(in));
79             delegate.finishedLoading();
80         }
81         catch (IOException JavaDoc ioex) {
82             getDelegate().loadError(ioex);
83         }
84         catch (SAXException JavaDoc saxex) {
85             getDelegate().loadError(saxex);
86         }
87
88         // return true if no failures
89
return !getDelegate().getStatus().hasFailures();
90     }
91
92     // SAX handlers start below
93

94     /**
95      * Handler for the root element. Its only child must be the "domains" element.
96      */

97     private class RootHandler extends DefaultHandler JavaDoc {
98
99         /**
100          * Handles the start of a datadomains element. A domains handler is created and
101          * initialised with the element name and attributes.
102          *
103          * @exception SAXException if the tag given is not <code>"domains"</code>
104          */

105         public void startElement(
106                 String JavaDoc namespaceURI,
107                 String JavaDoc localName,
108                 String JavaDoc qName,
109                 Attributes JavaDoc attrs) throws SAXException JavaDoc {
110             if (localName.equals("domains")) {
111                 delegate.shouldLoadProjectVersion(attrs.getValue("", "project-version"));
112                 new DomainsHandler(parser, this);
113             }
114             else {
115                 throw new SAXParseException JavaDoc("<domains> should be the root element. <"
116                         + localName
117                         + "> is unexpected.", null);
118             }
119         }
120     }
121
122     /**
123      * Handler for the top level "project" element.
124      */

125     private class DomainsHandler extends AbstractHandler {
126
127         /**
128          * Constructor which just delegates to the superconstructor.
129          *
130          * @param parentHandler The handler which should be restored to the parser at the
131          * end of the element. Must not be <code>null</code>.
132          */

133         public DomainsHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
134             super(parser, parentHandler);
135         }
136
137         /**
138          * Handles the start of a top-level element within the project. An appropriate
139          * handler is created and initialised with the details of the element.
140          */

141         public void startElement(
142                 String JavaDoc namespaceURI,
143                 String JavaDoc localName,
144                 String JavaDoc qName,
145                 Attributes JavaDoc atts) throws SAXException JavaDoc {
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 JavaDoc message = "<domain> or <view> are only valid children of <domains>. <"
154                         + localName
155                         + "> is unexpected.";
156                 throw new SAXParseException JavaDoc(message, null);
157             }
158         }
159     }
160
161     private class ViewHandler extends AbstractHandler {
162
163         public ViewHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
164             super(parser, parentHandler);
165         }
166
167         public void init(Attributes JavaDoc attrs) {
168             String JavaDoc name = attrs.getValue("", "name");
169             String JavaDoc location = attrs.getValue("", "location");
170             delegate.shouldRegisterDataView(name, location);
171         }
172     }
173
174     /**
175      * Handler for the "domain" element.
176      */

177     private class DomainHandler extends AbstractHandler {
178
179         private String JavaDoc domainName;
180         private Map JavaDoc properties;
181         private Map JavaDoc mapLocations;
182
183         public DomainHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
184             super(parser, parentHandler);
185         }
186
187         public void init(String JavaDoc name, Attributes JavaDoc attrs) {
188             domainName = attrs.getValue("", "name");
189             mapLocations = new HashMap JavaDoc();
190             properties = new HashMap JavaDoc();
191             delegate.shouldLoadDataDomain(domainName);
192         }
193
194         public void startElement(
195                 String JavaDoc namespaceURI,
196                 String JavaDoc localName,
197                 String JavaDoc qName,
198                 Attributes JavaDoc atts) throws SAXException JavaDoc {
199
200             if (localName.equals("property")) {
201                 new PropertyHandler(getParser(), this).init(atts, properties);
202             }
203             else if (localName.equals("map")) {
204                 // "map" elements go after "property" elements
205
// must flush properties if there are any
206
loadProperties();
207
208                 new MapHandler(getParser(), this).init(
209                         localName,
210                         atts,
211                         domainName,
212                         mapLocations);
213             }
214             else if (localName.equals("node")) {
215                 // "node" elements go after "map" elements
216
// must flush maps if there are any
217
loadMaps();
218
219                 new NodeHandler(getParser(), this).init(localName, atts, domainName);
220             }
221             else {
222                 String JavaDoc message = "<node> or <map> should be the children of <domain>. <"
223                         + localName
224                         + "> is unexpected.";
225                 throw new SAXParseException JavaDoc(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                 // load all properties
237
delegate.shouldLoadDataDomainProperties(domainName, properties);
238
239                 // clean properties to avoid loading them twice
240
properties.clear();
241             }
242         }
243
244         private void loadMaps() {
245             if (mapLocations.size() > 0) {
246                 // load all maps
247
delegate.shouldLoadDataMaps(domainName, mapLocations);
248                 // clean map locations to avoid loading maps twice
249
mapLocations.clear();
250             }
251         }
252     }
253
254     private class PropertyHandler extends AbstractHandler {
255
256         public PropertyHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
257             super(parser, parentHandler);
258         }
259
260         public void init(Attributes JavaDoc attrs, Map JavaDoc properties) {
261
262             String JavaDoc name = attrs.getValue("", "name");
263             String JavaDoc 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 JavaDoc domainName;
273         protected String JavaDoc mapName;
274         protected String JavaDoc location;
275         private Map JavaDoc mapLocations;
276
277         public MapHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
278             super(parser, parentHandler);
279         }
280
281         public void init(String JavaDoc name, Attributes JavaDoc attrs, String JavaDoc domainName, Map JavaDoc 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 JavaDoc namespaceURI,
290                 String JavaDoc localName,
291                 String JavaDoc qName,
292                 Attributes JavaDoc attrs) throws SAXException JavaDoc {
293             if (localName.equals("dep-map-ref")) {
294
295                 // this is no longer supported, but kept as noop
296
// for backwards compatibility
297
new DepMapRefHandler(getParser(), this).init(localName, attrs);
298             }
299             else {
300                 throw new SAXParseException JavaDoc(
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     /** Handles processing of "node" element. */
314     private class NodeHandler extends AbstractHandler {
315
316         protected String JavaDoc nodeName;
317         protected String JavaDoc domainName;
318
319         public NodeHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
320             super(parser, parentHandler);
321         }
322
323         public void init(String JavaDoc name, Attributes JavaDoc attrs, String JavaDoc domainName) {
324             this.domainName = domainName;
325
326             nodeName = attrs.getValue("", "name");
327             String JavaDoc dataSrcLocation = attrs.getValue("", "datasource");
328             String JavaDoc adapterClass = attrs.getValue("", "adapter");
329             String JavaDoc 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 JavaDoc namespaceURI,
340                 String JavaDoc localName,
341                 String JavaDoc qName,
342                 Attributes JavaDoc attrs) throws SAXException JavaDoc {
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 JavaDoc("<map-ref> should be the only node child. <"
353                         + localName
354                         + "> is unexpected.", null);
355             }
356         }
357     }
358
359     // this handler is deprecated, but is kept around for backwards compatibility
360
private class DepMapRefHandler extends AbstractHandler {
361
362         public DepMapRefHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
363             super(parser, parentHandler);
364         }
365
366         public void init(String JavaDoc name, Attributes JavaDoc attrs) {
367         }
368     }
369
370     private class MapRefHandler extends AbstractHandler {
371
372         public MapRefHandler(XMLReader JavaDoc parser, ContentHandler JavaDoc parentHandler) {
373             super(parser, parentHandler);
374         }
375
376         public void init(String JavaDoc name, Attributes JavaDoc attrs, String JavaDoc domainName, String JavaDoc nodeName) {
377             String JavaDoc mapName = attrs.getValue("", "name");
378             delegate.shouldLinkDataMap(domainName, nodeName, mapName);
379         }
380     }
381 }
382
Popular Tags