1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
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
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 nameAttribute, String catalogClass) {
52 super();
53 this.nameAttribute = nameAttribute;
54 this.catalogClass = catalogClass;
55 }
56
57
58
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 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 nameAttribute = null;
73
74
75
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 namespace, String name, Attributes attributes)
92 throws Exception {
93
94
95 Catalog catalog = null;
96 CatalogFactory factory = CatalogFactory.getInstance();
97 String nameValue = attributes.getValue(nameAttribute);
98 if (nameValue == null) {
99 catalog = factory.getCatalog();
100 } else {
101 catalog = factory.getCatalog(nameValue);
102 }
103
104
105 if (catalog == null) {
106 Class 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
116 digester.push(catalog);
117
118 }
119
120
121 }