KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > config > ConfigCatalogRule


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

16 package org.apache.commons.chain.config;
17
18
19 import org.apache.commons.chain.Catalog;
20 import org.apache.commons.chain.CatalogFactory;
21 import org.apache.commons.digester.Rule;
22 import org.xml.sax.Attributes JavaDoc;
23
24
25 /**
26  * <p>Digester rule that will cause the top-most element on the Digester
27  * stack (if it is a {@link Catalog} to be registered with the
28  * {@link CatalogFactory} instance for our application. If the attribute
29  * specified to our constructor has a value, that will be used as the name
30  * under which to register this {@link Catalog}. Otherwise, this will
31  * become the default {@link Catalog} for this application.</p>
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 1.2 $ $Date: 2004/11/30 05:52:23 $
35  */

36 class ConfigCatalogRule extends Rule {
37
38
39     // ----------------------------------------------------------- Constructors
40

41
42     /**
43      * <p>Construct a new instance of this rule that looks for an attribute
44      * with the specified name.</p>
45      *
46      * @param nameAttribute Name of the attribute containing the name under
47      * which this command should be registered
48      * @param catalogClass Name of the implementation class for newly
49      * created {@link Catalog} instances
50      */

51     public ConfigCatalogRule(String JavaDoc nameAttribute, String JavaDoc catalogClass) {
52         super();
53         this.nameAttribute = nameAttribute;
54         this.catalogClass = catalogClass;
55     }
56
57
58     // ----------------------------------------------------- Instance Variables
59

60
61     /**
62      * <p>The fully qualified class name of a {@link Catalog} class to use for
63      * instantiating new instances.</p>
64      */

65     private String JavaDoc catalogClass = null;
66
67
68     /**
69      * <p>The name of the attribute under which we can retrieve the name
70      * this catalog should be registered with (if any).</p>
71      */

72     private String JavaDoc nameAttribute = null;
73
74
75     // --------------------------------------------------------- Public Methods
76

77
78     /**
79      * <p>Retrieve or create a {@link Catalog} with the name specified by
80      * the <code>nameAttribute</code> attribute, or the default {@link Catalog}
81      * if there is no such attribute defined. Push it onto the top of the
82      * stack.</p>
83      *
84      * @param namespace the namespace URI of the matching element, or an
85      * empty string if the parser is not namespace aware or the element has
86      * no namespace
87      * @param name the local name if the parser is namespace aware, or just
88      * the element name otherwise
89      * @param attributes The attribute list of this element
90      */

91     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
92         throws Exception JavaDoc {
93
94         // Retrieve any current Catalog with the specified name
95
Catalog catalog = null;
96         CatalogFactory factory = CatalogFactory.getInstance();
97         String JavaDoc nameValue = attributes.getValue(nameAttribute);
98         if (nameValue == null) {
99             catalog = factory.getCatalog();
100         } else {
101             catalog = factory.getCatalog(nameValue);
102         }
103
104         // Create and register a new Catalog instance if necessary
105
if (catalog == null) {
106             Class JavaDoc clazz = digester.getClassLoader().loadClass(catalogClass);
107             catalog = (Catalog) clazz.newInstance();
108             if (nameValue == null) {
109                 factory.setCatalog(catalog);
110             } else {
111                 factory.addCatalog(nameValue, catalog);
112             }
113         }
114
115         // Push this Catalog onto the top of the stack
116
digester.push(catalog);
117
118     }
119
120
121 }
122
Popular Tags