Coverage Report - org.apache.myfaces.view.facelets.tag.composite.InsertFacetHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
InsertFacetHandler
0%
0/39
0%
0/14
3.75
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.view.facelets.tag.composite;
 20  
 
 21  
 import java.beans.BeanDescriptor;
 22  
 import java.beans.IntrospectionException;
 23  
 import java.beans.PropertyDescriptor;
 24  
 import java.io.IOException;
 25  
 import java.util.ArrayList;
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 import java.util.logging.Level;
 30  
 import java.util.logging.Logger;
 31  
 
 32  
 import javax.faces.component.UIComponent;
 33  
 import javax.faces.view.facelets.FaceletContext;
 34  
 import javax.faces.view.facelets.TagAttribute;
 35  
 import javax.faces.view.facelets.TagConfig;
 36  
 import javax.faces.view.facelets.TagException;
 37  
 import javax.faces.view.facelets.TagHandler;
 38  
 
 39  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
 40  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 41  
 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
 42  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 43  
 
 44  
 /**
 45  
  * Insert or move the facet from the composite component body to the expected location.
 46  
  * 
 47  
  * @author Leonardo Uribe (latest modification by $Author$)
 48  
  * @version $Revision$ $Date$
 49  
  */
 50  
 @JSFFaceletTag(name="composite:insertFacet")
 51  
 public class InsertFacetHandler extends TagHandler
 52  
 {
 53  
     //public static String USES_INSERT_FACET = "org.apache.myfaces.USES_INSERT_FACET";
 54  
     //public static String INSERT_FACET_TARGET_ID = "org.apache.myfaces.INSERT_FACET_TARGET_ID.";
 55  
     //public static String INSERT_FACET_ORDERING = "org.apache.myfaces.INSERT_FACET_ORDERING.";
 56  
     
 57  
     public static final String INSERT_FACET_USED = "org.apache.myfaces.INSERT_FACET_USED";
 58  
     
 59  
     /**
 60  
      * Key used to save on bean descriptor a map containing the metadata
 61  
      * information related to this tag. It will be used later to check "required" property.
 62  
      */
 63  
     public static final String INSERT_FACET_KEYS = "org.apache.myfaces.INSERT_FACET_KEYS";
 64  
     
 65  0
     private static final Logger log = Logger.getLogger(InsertFacetHandler.class.getName());
 66  
     
 67  
     /**
 68  
      * The name that identify the current facet.
 69  
      */
 70  
     @JSFFaceletAttribute(name="name",
 71  
             className="javax.el.ValueExpression",
 72  
             deferredValueType="java.lang.String",
 73  
             required=true)
 74  
     protected final TagAttribute _name;
 75  
     
 76  
     /**
 77  
      * Define if the facet to be inserted is required or not for every instance of
 78  
      * this composite component.
 79  
      */
 80  
     @JSFFaceletAttribute(name="required",
 81  
             className="javax.el.ValueExpression",
 82  
             deferredValueType="boolean")
 83  
     protected final TagAttribute _required;
 84  
     
 85  
     public InsertFacetHandler(TagConfig config)
 86  
     {
 87  0
         super(config);
 88  0
         _name = getRequiredAttribute("name");
 89  0
         _required = getAttribute("required");
 90  0
     }
 91  
     
 92  
     public String getFacetName(FaceletContext ctx)
 93  
     {
 94  0
         return _name.getValue(ctx);
 95  
     }
 96  
 
 97  
     @SuppressWarnings("unchecked")
 98  
     public void apply(FaceletContext ctx, UIComponent parent)
 99  
             throws IOException
 100  
     {
 101  0
         if (((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata())
 102  
         {
 103  0
             String facetName = _name.getValue(ctx);
 104  
             
 105  0
             UIComponent compositeBaseParent
 106  
                     = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
 107  
             
 108  0
             CompositeComponentBeanInfo beanInfo = 
 109  
                 (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
 110  
                 .get(UIComponent.BEANINFO_KEY);
 111  
             
 112  0
             if (beanInfo == null)
 113  
             {
 114  0
                 if (log.isLoggable(Level.SEVERE))
 115  
                 {
 116  0
                     log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
 117  
                 }
 118  0
                 return;
 119  
             }
 120  
             
 121  0
             BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); 
 122  
 
 123  0
             List<String> facetList = (List<String>) beanDescriptor.getValue(INSERT_FACET_USED);
 124  
             
 125  0
             if (facetList == null)
 126  
             {
 127  
                 //2. If not found create it and set
 128  0
                 facetList = new ArrayList<String>();
 129  0
                 beanDescriptor.setValue(
 130  
                         INSERT_FACET_USED,
 131  
                         facetList);
 132  
             }
 133  
             
 134  0
             facetList.add(facetName);
 135  
 
 136  0
             Map<String, PropertyDescriptor> insertFacetPropertyDescriptorMap = (Map<String, PropertyDescriptor>)
 137  
                 beanDescriptor.getValue(INSERT_FACET_KEYS);
 138  
         
 139  0
             if (insertFacetPropertyDescriptorMap == null)
 140  
             {
 141  0
                 insertFacetPropertyDescriptorMap = new HashMap<String, PropertyDescriptor>();
 142  0
                 beanDescriptor.setValue(INSERT_FACET_KEYS, insertFacetPropertyDescriptorMap);
 143  
             }
 144  
             
 145  0
             PropertyDescriptor facetDescriptor = _createFacetPropertyDescriptor(facetName, ctx);
 146  0
             insertFacetPropertyDescriptorMap.put(facetName, facetDescriptor);
 147  0
         }
 148  
         else
 149  
         {
 150  0
             String facetName = _name.getValue(ctx);
 151  
             
 152  0
             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 153  
             
 154  0
             actx.includeCompositeComponentDefinition(parent, facetName);
 155  
         }
 156  
         
 157  0
     }
 158  
     
 159  
     private PropertyDescriptor _createFacetPropertyDescriptor(String facetName, FaceletContext ctx)
 160  
     throws TagException, IOException
 161  
     {
 162  
         try
 163  
         {
 164  0
             CompositeComponentPropertyDescriptor facetPropertyDescriptor = 
 165  
                 new CompositeComponentPropertyDescriptor(facetName);
 166  
             
 167  0
             if (_required != null)
 168  
             {
 169  0
                 facetPropertyDescriptor.setValue("required", _required.getValueExpression(ctx, Boolean.class));
 170  
             }
 171  
             
 172  0
             return facetPropertyDescriptor;
 173  
         }
 174  0
         catch (IntrospectionException e)
 175  
         {
 176  0
             if (log.isLoggable(Level.SEVERE))
 177  
             {
 178  0
                 log.log(Level.SEVERE, "Cannot create PropertyDescriptor for attribute ",e);
 179  
             }
 180  0
             throw new TagException(tag,e);
 181  
         }
 182  
     }
 183  
 
 184  
 }