View Javadoc
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  import org.apache.commons.digester.Rule;
20  import org.xml.sax.Attributes;
21  
22  /**
23   * Digester rule that will dynamically register a new set of rules
24   * for a specified element name and default implementation class. This
25   * allows "alias" elements to be created for {@link org.apache.commons.chain.Chain}
26   * and {@link org.apache.commons.chain.Command}
27   * implementation classes that are commonly used. Besides factoring out
28   * the class names to make changes easier, this also makes configuration
29   * files <strong>much</strong> easier to read and write.
30   *
31   * @version $Revision$ $Date$
32   */
33  class ConfigDefineRule extends Rule {
34  
35      // ----------------------------------------------------- Instance Variables
36  
37      /**
38       * The name of the attribute under which we can retrieve the
39       * fully qualified class name of the implementation class for this
40       * new element.
41       */
42      private final String classAttribute;
43  
44      /**
45       * The name of the attribute under which we can retrieve the name
46       * this element for which rules should be created.
47       */
48      private final String nameAttribute;
49  
50      // ----------------------------------------------------------- Constructors
51  
52      /**
53       * Construct a new instance of this rule that will in turn
54       * dynamically register appropriate rules for a new alias element.
55       *
56       * @param nameAttribute Name of the attribute containing the name
57       *        of the new element for which rules should generated
58       * @param classAttribute Name of the attribute containing the
59       *        implementation class for the new chain or command
60       */
61      ConfigDefineRule(String nameAttribute, String classAttribute) {
62          this.nameAttribute = nameAttribute;
63          this.classAttribute = classAttribute;
64      }
65  
66      // --------------------------------------------------------- Public Methods
67  
68      /**
69       * Register new rules for the specified name and class.
70       *
71       * @param namespace the namespace URI of the matching element, or an
72       *        empty string if the parser is not namespace aware or the
73       *        element has no namespace
74       * @param name the local name if the parser is namespace aware, or
75       *        just the element name otherwise
76       * @param attributes The attribute list of this element
77       */
78      @Override
79      public void begin(String namespace, String name, Attributes attributes)
80          throws Exception {
81  
82          // Extract the actual name and implementation class to use
83          String nameValue = attributes.getValue(nameAttribute);
84          String classValue = attributes.getValue(classAttribute);
85  
86          // Add rules for this new element
87          digester.addObjectCreate("*/" + nameValue, classValue);
88          digester.addSetProperties("*/" + nameValue);
89          digester.addRule("*/" + nameValue,
90                           new ConfigRegisterRule(nameAttribute));
91      }
92  }