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

For more information, please explore the Attic.

Coverage Report - org.apache.shale.tiger.managed.rules.ManagedBeanRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagedBeanRule
100%
28/28
N/A
0
 
 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  21
     public ManagedBeanRule() {
 36  21
     }
 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  186
         Class clazz = digester.getClassLoader().loadClass(CLASS_NAME);
 56  186
         digester.push(clazz.newInstance());
 57  
 
 58  186
     }
 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  186
         ManagedBeanConfig config = (ManagedBeanConfig) digester.pop();
 88  186
         FacesConfigConfig parent = (FacesConfigConfig) digester.peek();
 89  186
         ManagedBeanConfig previous = parent.getManagedBean(config.getName());
 90  186
         if (previous == null) {
 91  126
             parent.addManagedBean(config);
 92  126
         } else {
 93  60
             merge(config, previous);
 94  
         }
 95  
 
 96  186
     }
 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  60
         if (config.getType() != null) {
 109  60
             previous.setType(config.getType());
 110  
         }
 111  60
         if (config.getScope() != null) {
 112  60
             previous.setScope(config.getScope());
 113  
         }
 114  60
         Set<Map.Entry<String,ManagedPropertyConfig>> properties =
 115  
                 config.getProperties().entrySet();
 116  60
         for (Map.Entry<String,ManagedPropertyConfig> property : properties) {
 117  123
             ManagedPropertyConfig prevProperty =
 118  
                     previous.getProperty(property.getValue().getName());
 119  123
             if (prevProperty == null) {
 120  26
                 previous.addProperty(property.getValue());
 121  26
             } else {
 122  97
                 ManagedPropertyRule.merge(property.getValue(), prevProperty);
 123  
             }
 124  123
         }
 125  60
         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  60
         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  60
     }
 141  
 
 142  
 
 143  
 }