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