2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

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  
18  package org.apache.shale.tiger.managed.rules;
19  
20  import java.util.Map;
21  import java.util.Set;
22  import org.apache.commons.digester.Rule;
23  import org.apache.shale.tiger.config.FacesConfigConfig;
24  import org.apache.shale.tiger.managed.config.ManagedBeanConfig;
25  import org.apache.shale.tiger.managed.config.ManagedPropertyConfig;
26  import org.xml.sax.Attributes;
27  
28  /***
29   * <p>Digester rule for processing a <code>&lt;managed-bean&gt;</code>
30   * element.</p>
31   */
32  public class ManagedBeanRule extends Rule {
33  
34      /*** Creates a new instance of ManagedBeanRule. */
35      public ManagedBeanRule() {
36      }
37  
38      /*** <p>Fully qualified class name of our configuration element bean.</p> */
39      private static final String CLASS_NAME =
40              "org.apache.shale.tiger.managed.config.ManagedBeanConfig";
41  
42      /***
43       * <p>Create a new {@link ManagedBeanConfig} and push it on to the
44       * Digester stack.</p>
45       *
46       * @param namespace Namespace URI of the matching element
47       * @param name Local name of the matching element
48       * @param attributes Attribute list of the matching element
49       *
50       * @exception Exception if a parsing error occurs
51       */
52      public void begin(String namespace, String name,
53                        Attributes attributes) throws Exception {
54  
55          Class clazz = digester.getClassLoader().loadClass(CLASS_NAME);
56          digester.push(clazz.newInstance());
57  
58      }
59  
60  
61      /***
62       * <p>No body processing for this element.</p>
63       *
64       * @param namespace Namespace URI of the matching element
65       * @param name Local name of the matching element
66       *
67       * @throws Exception if a parsing error occurs
68       */
69      public void body(String namespace, String name) throws Exception {
70      }
71  
72  
73      /***
74       * <p>Pop the {@link ManagedBeanConfig} instance from the stack,
75       * and either add it or merge it with parent information.</p>
76       *
77       * @param namespace Namespace URI of the matching element
78       * @param name Local name of the matching element
79       *
80       * @exception IllegalStateException if the popped object is not
81       *  of the correct type
82       *
83       * @exception Exception if an error occurs
84       */
85      public void end(String namespace, String name) throws Exception {
86  
87          ManagedBeanConfig config = (ManagedBeanConfig) digester.pop();
88          FacesConfigConfig parent = (FacesConfigConfig) digester.peek();
89          ManagedBeanConfig previous = parent.getManagedBean(config.getName());
90          if (previous == null) {
91              parent.addManagedBean(config);
92          } else {
93              merge(config, previous);
94          }
95  
96      }
97  
98  
99      /***
100      * <p>Merge properties from <code>config</code> into
101      * <code>previous</code>.</p>
102      *
103      * @param config Newly constructed bean
104      * @param previous Previous bean to merge into
105      */
106     static void merge(ManagedBeanConfig config, ManagedBeanConfig previous) {
107 
108         if (config.getType() != null) {
109             previous.setType(config.getType());
110         }
111         if (config.getScope() != null) {
112             previous.setScope(config.getScope());
113         }
114         Set<Map.Entry<String,ManagedPropertyConfig>> properties =
115                 config.getProperties().entrySet();
116         for (Map.Entry<String,ManagedPropertyConfig> property : properties) {
117             ManagedPropertyConfig prevProperty =
118                     previous.getProperty(property.getValue().getName());
119             if (prevProperty == null) {
120                 previous.addProperty(property.getValue());
121             } else {
122                 ManagedPropertyRule.merge(property.getValue(), prevProperty);
123             }
124         }
125         if (config.getListEntries() != null) {
126             if (previous.getListEntries() != null) {
127                 ListEntriesRule.merge(config.getListEntries(), previous.getListEntries());
128             } else {
129                 previous.setListEntries(config.getListEntries());
130             }
131         }
132         if (config.getMapEntries() != null) {
133             if (previous.getMapEntries() != null) {
134                 MapEntriesRule.merge(config.getMapEntries(), previous.getMapEntries());
135             } else {
136                 previous.setMapEntries(config.getMapEntries());
137             }
138         }
139 
140     }
141 
142 
143 }