Coverage Report - org.apache.myfaces.view.facelets.tag.composite.RenderFacetHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
RenderFacetHandler
0%
0/29
0%
0/16
4.333
 
 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.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 
 28  
 import javax.faces.component.UIComponent;
 29  
 import javax.faces.view.facelets.ComponentConfig;
 30  
 import javax.faces.view.facelets.ComponentHandler;
 31  
 import javax.faces.view.facelets.FaceletContext;
 32  
 import javax.faces.view.facelets.TagAttribute;
 33  
 import javax.faces.view.facelets.TagException;
 34  
 
 35  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
 36  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 37  
 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
 38  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 39  
 
 40  
 /**
 41  
  * Render the facet defined on the composite component body to the current location
 42  
  * 
 43  
  * @author Leonardo Uribe (latest modification by $Author$)
 44  
  * @version $Revision$ $Date$
 45  
  */
 46  
 @JSFFaceletTag(name="composite:renderFacet")
 47  
 public class RenderFacetHandler extends ComponentHandler
 48  
 {
 49  0
     private static final Logger log = Logger.getLogger(RenderFacetHandler.class.getName());
 50  
     
 51  
     public static final String RENDER_FACET_USED = "org.apache.myfaces.RENDER_FACET_USED";
 52  
     
 53  
     /**
 54  
      * The name that identify the current facet.
 55  
      */
 56  
     @JSFFaceletAttribute(name="name",
 57  
             className="javax.el.ValueExpression",
 58  
             deferredValueType="java.lang.String",
 59  
             required=true)
 60  
     protected final TagAttribute _name;
 61  
     
 62  
     /**
 63  
      * Define if the facet to be inserted is required or not for every instance of
 64  
      * this composite component.
 65  
      */
 66  
     @JSFFaceletAttribute(name="required",
 67  
             className="javax.el.ValueExpression",
 68  
             deferredValueType="boolean")
 69  
     protected final TagAttribute _required;
 70  
     
 71  
     public RenderFacetHandler(ComponentConfig config)
 72  
     {
 73  0
         super(config);
 74  0
         _name = getRequiredAttribute("name");
 75  0
         _required = getAttribute("required");
 76  0
     }
 77  
 
 78  
     @SuppressWarnings("unchecked")
 79  
     @Override
 80  
     public void apply(FaceletContext ctx, UIComponent parent)
 81  
             throws IOException
 82  
     {
 83  0
         if (((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata())
 84  
         {
 85  0
             String facetName = _name.getValue(ctx);
 86  
             
 87  0
             UIComponent compositeBaseParent
 88  
                     = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
 89  
             
 90  0
             CompositeComponentBeanInfo beanInfo = 
 91  
                 (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
 92  
                 .get(UIComponent.BEANINFO_KEY);
 93  
             
 94  0
             if (beanInfo == null)
 95  
             {
 96  0
                 if (log.isLoggable(Level.SEVERE))
 97  
                 {
 98  0
                     log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
 99  
                 }
 100  0
                 return;
 101  
             }
 102  
             
 103  0
             BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); 
 104  
 
 105  0
             List<String> facetList = (List<String>) beanDescriptor.getValue(RENDER_FACET_USED);
 106  
             
 107  0
             if (facetList == null)
 108  
             {
 109  
                 //2. If not found create it and set
 110  0
                 facetList = new ArrayList<String>();
 111  0
                 beanDescriptor.setValue(
 112  
                         RENDER_FACET_USED,
 113  
                         facetList);
 114  
             }
 115  
             
 116  0
             facetList.add(facetName);
 117  
             
 118  
             // Do not call super.apply(ctx, parent), because it forces component creation,
 119  
             // and in this step it is not necessary. Also, it changes the order of 
 120  
             // the generated ids.
 121  0
         }
 122  
         else
 123  
         {
 124  0
             super.apply(ctx, parent);
 125  
         }
 126  0
     }
 127  
 
 128  
     @Override
 129  
     public void onComponentPopulated(FaceletContext ctx, UIComponent c,
 130  
             UIComponent parent)
 131  
     {
 132  0
         if (!((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata())
 133  
         {
 134  0
             UIComponent parentCompositeComponent
 135  
                     = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
 136  
             
 137  0
             String facetName = _name.getValue(ctx);
 138  
     
 139  0
             if (_required != null && _required.getBoolean(ctx) && parentCompositeComponent.getFacet(facetName) == null)
 140  
             {
 141  0
                 throw new TagException(this.tag, "Cannot find facet with name '"+facetName+"' in composite component");
 142  
             }
 143  
             
 144  0
             c.getAttributes().put(UIComponent.FACETS_KEY, facetName);
 145  
         }
 146  0
     }
 147  
 }