01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: import org.apache.commons.chain.Catalog;
19: import org.apache.commons.chain.Chain;
20: import org.apache.commons.chain.Command;
21: import org.apache.commons.digester.Rule;
22: import org.xml.sax.Attributes;
23:
24: /**
25: * <p>Digester rule that will cause the top-most element on the Digester
26: * stack (if it is a {@link Command} to be registered with the next-to-top
27: * element on the Digester stack (if it is a {@link Catalog} or {@link Chain}).
28: * To be registered with a {@link Catalog}, the top-most element must contain
29: * a value for the specified attribute that contains the name under which
30: * it should be registered.</p>
31: *
32: * @author Craig R. McClanahan
33: * @version $Revision: 387850 $ $Date: 2006-03-22 12:52:57 +0000 (Wed, 22 Mar 2006) $
34: */
35: class ConfigRegisterRule extends Rule {
36:
37: // ----------------------------------------------------------- Constructors
38:
39: /**
40: * <p>Construct a new instance of this rule that looks for an attribute
41: * with the specified name.</p>
42: *
43: * @param nameAttribute Name of the attribute containing the name under
44: * which this command should be registered
45: */
46: public ConfigRegisterRule(String nameAttribute) {
47: super ();
48: this .nameAttribute = nameAttribute;
49: }
50:
51: // ----------------------------------------------------- Instance Variables
52:
53: /**
54: * <p>The name of the attribute under which we can retrieve the name
55: * this command should be registered with.</p>
56: */
57: private String nameAttribute = null;
58:
59: // --------------------------------------------------------- Public Methods
60:
61: /**
62: * <p>Register the top {@link Command} if appropriate.</p>
63: *
64: * @param namespace the namespace URI of the matching element, or an
65: * empty string if the parser is not namespace aware or the element has
66: * no namespace
67: * @param name the local name if the parser is namespace aware, or just
68: * the element name otherwise
69: * @param attributes The attribute list of this element
70: */
71: public void begin(String namespace, String name,
72: Attributes attributes) throws Exception {
73:
74: // Is the top object a Command?
75: Object top = digester.peek(0);
76: if ((top == null) || !(top instanceof Command)) {
77: return;
78: }
79: Command command = (Command) top;
80:
81: // Is the next object a Catalog or a Chain?
82: Object next = digester.peek(1);
83: if (next == null) {
84: return;
85: }
86:
87: // Register the top element appropriately
88: if (next instanceof Catalog) {
89: String nameValue = attributes.getValue(nameAttribute);
90: if (nameValue != null) {
91: ((Catalog) next).addCommand(nameValue, command);
92: }
93: } else if (next instanceof Chain) {
94: ((Chain) next).addCommand(command);
95: }
96:
97: }
98:
99: }
|