1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.chain.config;
18
19
20 import org.apache.commons.chain.Catalog;
21 import org.apache.commons.chain.Chain;
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.digester.Rule;
24 import org.xml.sax.Attributes;
25
26
27 /**
28 * <p>Digester rule that will cause the top-most element on the Digester
29 * stack (if it is a {@link Command} to be registered with the next-to-top
30 * element on the Digester stack (if it is a {@link Catalog} or {@link Chain}).
31 * To be registered with a {@link Catalog}, the top-most element must contain
32 * a value for the specified attribute that contains the name under which
33 * it should be registered.</p>
34 *
35 * @author Craig R. McClanahan
36 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
37 */
38 class ConfigRegisterRule extends Rule {
39
40
41 // ----------------------------------------------------------- Constructors
42
43
44 /**
45 * <p>Construct a new instance of this rule that looks for an attribute
46 * with the specified name.</p>
47 *
48 * @param nameAttribute Name of the attribute containing the name under
49 * which this command should be registered
50 */
51 public ConfigRegisterRule(String nameAttribute) {
52 super();
53 this.nameAttribute = nameAttribute;
54 }
55
56
57 // ----------------------------------------------------- Instance Variables
58
59
60 /**
61 * <p>The name of the attribute under which we can retrieve the name
62 * this command should be registered with.</p>
63 */
64 private String nameAttribute = null;
65
66
67 // --------------------------------------------------------- Public Methods
68
69
70 /**
71 * <p>Register the top {@link Command} if appropriate.</p>
72 *
73 * @param namespace the namespace URI of the matching element, or an
74 * empty string if the parser is not namespace aware or the element has
75 * no namespace
76 * @param name the local name if the parser is namespace aware, or just
77 * the element name otherwise
78 * @param attributes The attribute list of this element
79 */
80 public void begin(String namespace, String name, Attributes attributes)
81 throws Exception {
82
83 // Is the top object a Command?
84 Object top = digester.peek(0);
85 if ((top == null)
86 || !(top instanceof Command)) {
87 return;
88 }
89 Command command = (Command) top;
90
91 // Is the next object a Catalog or a Chain?
92 Object next = digester.peek(1);
93 if (next == null) {
94 return;
95 }
96
97 // Register the top element appropriately
98 if (next instanceof Catalog) {
99 String nameValue = attributes.getValue(nameAttribute);
100 if (nameValue != null) {
101 ((Catalog) next).addCommand(nameValue, command);
102 }
103 } else if (next instanceof Chain) {
104 ((Chain) next).addCommand(command);
105 }
106
107 }
108
109
110 }