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.Chain;
21 import org.apache.commons.chain.Command;
22 import org.apache.commons.digester.Rule;
23 import org.xml.sax.Attributes;
24
25
26 /***
27 * <p>Digester rule that will cause the top-most element on the Digester
28 * stack (if it is a {@link Command} to be registered with the next-to-top
29 * element on the Digester stack (if it is a {@link Catalog} or {@link Chain}).
30 * To be registered with a {@link Catalog}, the top-most element must contain
31 * a value for the specified attribute that contains the name under which
32 * it should be registered.</p>
33 *
34 * @author Craig R. McClanahan
35 * @version $Revision: 387850 $ $Date: 2006-03-22 12:52:57 +0000 (Wed, 22 Mar 2006) $
36 */
37 class ConfigRegisterRule extends Rule {
38
39
40
41
42
43 /***
44 * <p>Construct a new instance of this rule that looks for an attribute
45 * with the specified name.</p>
46 *
47 * @param nameAttribute Name of the attribute containing the name under
48 * which this command should be registered
49 */
50 public ConfigRegisterRule(String nameAttribute) {
51 super();
52 this.nameAttribute = nameAttribute;
53 }
54
55
56
57
58
59 /***
60 * <p>The name of the attribute under which we can retrieve the name
61 * this command should be registered with.</p>
62 */
63 private String nameAttribute = null;
64
65
66
67
68
69 /***
70 * <p>Register the top {@link Command} if appropriate.</p>
71 *
72 * @param namespace the namespace URI of the matching element, or an
73 * empty string if the parser is not namespace aware or the element has
74 * no namespace
75 * @param name the local name if the parser is namespace aware, or just
76 * the element name otherwise
77 * @param attributes The attribute list of this element
78 */
79 public void begin(String namespace, String name, Attributes attributes)
80 throws Exception {
81
82
83 Object top = digester.peek(0);
84 if ((top == null)
85 || !(top instanceof Command)) {
86 return;
87 }
88 Command command = (Command) top;
89
90
91 Object next = digester.peek(1);
92 if (next == null) {
93 return;
94 }
95
96
97 if (next instanceof Catalog) {
98 String nameValue = attributes.getValue(nameAttribute);
99 if (nameValue != null) {
100 ((Catalog) next).addCommand(nameValue, command);
101 }
102 } else if (next instanceof Chain) {
103 ((Chain) next).addCommand(command);
104 }
105
106 }
107
108
109 }