Coverage Report - org.apache.myfaces.view.facelets.tag.ui.CompositionHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositionHandler
0%
0/47
0%
0/22
5.667
 
 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.ui;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.Collection;
 23  
 import java.util.HashMap;
 24  
 import java.util.Map;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 
 28  
 import javax.el.ELException;
 29  
 import javax.faces.FacesException;
 30  
 import javax.faces.component.UIComponent;
 31  
 import javax.faces.view.facelets.FaceletContext;
 32  
 import javax.faces.view.facelets.FaceletException;
 33  
 import javax.faces.view.facelets.TagAttribute;
 34  
 import javax.faces.view.facelets.TagConfig;
 35  
 import javax.faces.view.facelets.TagHandler;
 36  
 
 37  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
 38  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
 39  
 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
 40  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 41  
 import org.apache.myfaces.view.facelets.TemplateClient;
 42  
 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
 43  
 
 44  
 /**
 45  
  * TODO: REFACTOR - This class could easily use a common parent with DecoratorHandler
 46  
  * 
 47  
  * @author Jacob Hookom
 48  
  * @version $Id$
 49  
  */
 50  
 @JSFFaceletTag(name="ui:composition")
 51  
 public final class CompositionHandler extends TagHandler implements TemplateClient
 52  
 {
 53  
 
 54  
     //private static final Logger log = Logger.getLogger("facelets.tag.ui.composition");
 55  0
     private static final Logger log = Logger.getLogger(CompositionHandler.class.getName());
 56  
 
 57  
     public final static String NAME = "composition";
 58  
 
 59  
     /**
 60  
      * The resolvable URI of the template to use. The content within the composition tag will 
 61  
      * be used in populating the template specified.
 62  
      */
 63  
     @JSFFaceletAttribute(
 64  
             name="template",
 65  
             className="javax.el.ValueExpression",
 66  
             deferredValueType="java.lang.String")
 67  
     protected final TagAttribute _template;
 68  
 
 69  
     protected final Map<String, DefineHandler> _handlers;
 70  
 
 71  
     protected final ParamHandler[] _params;
 72  
 
 73  
     /**
 74  
      * @param config
 75  
      */
 76  
     public CompositionHandler(TagConfig config)
 77  
     {
 78  0
         super(config);
 79  0
         _template = getAttribute("template");
 80  0
         if (_template != null)
 81  
         {
 82  0
             _handlers = new HashMap<String, DefineHandler>();
 83  0
             for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
 84  
             {
 85  0
                 _handlers.put(handler.getName(), handler);
 86  0
                 if (log.isLoggable(Level.FINE))
 87  
                 {
 88  0
                     log.fine(tag + " found Define[" + handler.getName() + "]");
 89  
                 }
 90  0
             }
 91  
 
 92  0
             Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class);
 93  0
             if (!params.isEmpty())
 94  
             {
 95  0
                 int i = 0;
 96  0
                 _params = new ParamHandler[params.size()];
 97  0
                 for (ParamHandler handler : params)
 98  
                 {
 99  0
                     _params[i++] = handler;
 100  0
                 }
 101  0
             }
 102  
             else
 103  
             {
 104  0
                 _params = null;
 105  
             }
 106  0
         }
 107  
         else
 108  
         {
 109  0
             _params = null;
 110  0
             _handlers = null;
 111  
         }
 112  0
     }
 113  
 
 114  
     /*
 115  
      * (non-Javadoc)
 116  
      * 
 117  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
 118  
      * javax.faces.component.UIComponent)
 119  
      */
 120  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 121  
             ELException
 122  
     {
 123  0
         if (_template != null)
 124  
         {
 125  
             //VariableMapper orig = ctx.getVariableMapper();
 126  
             //if (_params != null)
 127  
             //{
 128  
             //    VariableMapper vm = new VariableMapperWrapper(orig);
 129  
             //    ctx.setVariableMapper(vm);
 130  
             //    for (int i = 0; i < _params.length; i++)
 131  
             //    {
 132  
             //        _params[i].apply(ctx, parent);
 133  
             //    }
 134  
             //}
 135  0
             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 136  0
             FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
 137  0
             actx.extendClient(this);
 138  0
             if (_params != null)
 139  
             {
 140  0
                 String uniqueId = fcc.generateUniqueComponentId();
 141  
                 //VariableMapper vm = new VariableMapperWrapper(orig);
 142  
                 //ctx.setVariableMapper(vm);
 143  0
                 for (int i = 0; i < _params.length; i++)
 144  
                 {
 145  0
                     _params[i].apply(ctx, parent, _params[i].getName(ctx), _params[i].getValue(ctx), uniqueId);
 146  
                 }
 147  
             }
 148  
 
 149  
             try
 150  
             {
 151  0
                 ctx.includeFacelet(parent, _template.getValue(ctx));
 152  
             }
 153  
             finally
 154  
             {
 155  0
                 actx.popExtendedClient(this);
 156  
                 //ctx.setVariableMapper(orig);
 157  0
             }
 158  0
         }
 159  
         else
 160  
         {
 161  0
             this.nextHandler.apply(ctx, parent);
 162  
         }
 163  0
     }
 164  
 
 165  
     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
 166  
             FaceletException, ELException
 167  
     {
 168  0
         if (name != null)
 169  
         {
 170  0
             if (_handlers == null)
 171  
             {
 172  0
                 return false;
 173  
             }
 174  
             
 175  0
             DefineHandler handler = _handlers.get(name);
 176  0
             if (handler != null)
 177  
             {
 178  0
                 handler.applyDefinition(ctx, parent);
 179  0
                 return true;
 180  
             }
 181  
             else
 182  
             {
 183  0
                 return false;
 184  
             }
 185  
         }
 186  
         else
 187  
         {
 188  0
             this.nextHandler.apply(ctx, parent);
 189  0
             return true;
 190  
         }
 191  
     }
 192  
 
 193  
 }