View Javadoc

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.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: 1.5 $ $Date: 2004/11/30 05:52:23 $
36   */
37  class ConfigRegisterRule extends Rule {
38  
39  
40      // ----------------------------------------------------------- Constructors
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      // ----------------------------------------------------- Instance Variables
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      // --------------------------------------------------------- Public Methods
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          // Is the top object a Command?
83          Object top = digester.peek(0);
84          if ((top == null)
85              || !(top instanceof Command)) {
86              return;
87          }
88          Command command = (Command) top;
89  
90          // Is the next object a Catalog or a Chain?
91          Object next = digester.peek(1);
92          if (next == null) {
93              return;
94          }
95  
96          // Register the top element appropriately
97          if (next instanceof Catalog) {
98              Catalog catalog = (Catalog) next;
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 }