Coverage Report - org.apache.myfaces.view.facelets.tag.LegacyUserTagHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LegacyUserTagHandler
0%
0/51
0%
0/18
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  
 
 20  
 package org.apache.myfaces.view.facelets.tag;
 21  
 
 22  
 import java.io.FileNotFoundException;
 23  
 import java.io.IOException;
 24  
 import java.net.URL;
 25  
 import java.util.Collection;
 26  
 import java.util.HashMap;
 27  
 import java.util.Map;
 28  
 import javax.el.ELException;
 29  
 import javax.el.ValueExpression;
 30  
 import javax.el.VariableMapper;
 31  
 import javax.faces.FacesException;
 32  
 import javax.faces.component.UIComponent;
 33  
 import javax.faces.view.facelets.FaceletContext;
 34  
 import javax.faces.view.facelets.FaceletException;
 35  
 import javax.faces.view.facelets.TagAttribute;
 36  
 import javax.faces.view.facelets.TagConfig;
 37  
 import javax.faces.view.facelets.TagException;
 38  
 import javax.faces.view.facelets.TagHandler;
 39  
 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
 40  
 import org.apache.myfaces.view.facelets.TemplateClient;
 41  
 import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
 42  
 import org.apache.myfaces.view.facelets.tag.ui.DefineHandler;
 43  
 
 44  
 /**
 45  
  * A Tag that is specified in a FaceletFile. Takes all attributes specified and sets them on the FaceletContext before
 46  
  * including the targeted Facelet file.
 47  
  * 
 48  
  * @author Jacob Hookom
 49  
  * @version $Id: UserTagHandler.java,v 1.12 2008/07/13 19:01:35 rlubke Exp $
 50  
  */
 51  
 final class LegacyUserTagHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
 52  
 {
 53  
 
 54  
     protected final TagAttribute[] _vars;
 55  
 
 56  
     protected final URL _location;
 57  
 
 58  
     protected final Map<String, DefineHandler> _handlers;
 59  
 
 60  
     /**
 61  
      * @param config
 62  
      */
 63  
     public LegacyUserTagHandler(TagConfig config, URL location)
 64  
     {
 65  0
         super(config);
 66  0
         this._vars = this.tag.getAttributes().getAll();
 67  0
         this._location = location;
 68  
         
 69  0
         Collection<DefineHandler> defines = TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class);
 70  0
         if (defines.isEmpty())
 71  
         {
 72  0
             _handlers = null;
 73  
         }
 74  
         else
 75  
         {
 76  0
             _handlers = new HashMap<String, DefineHandler>();
 77  0
             for (DefineHandler handler : defines)
 78  
             {
 79  0
                 _handlers.put(handler.getName(), handler);
 80  0
             }
 81  
         }
 82  0
     }
 83  
 
 84  
     /**
 85  
      * Iterate over all TagAttributes and set them on the FaceletContext's VariableMapper, then include the target
 86  
      * Facelet. Finally, replace the old VariableMapper.
 87  
      * 
 88  
      * @see TagAttribute#getValueExpression(FaceletContext, Class)
 89  
      * @see VariableMapper
 90  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
 91  
      */
 92  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 93  
             ELException
 94  
     {
 95  0
         VariableMapper orig = ctx.getVariableMapper();
 96  
 
 97  0
         AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 98  
         // eval include
 99  
         try
 100  
         {
 101  0
             String[] names = null;
 102  0
             ValueExpression[] values = null;
 103  0
             if (this._vars.length > 0)
 104  
             {
 105  0
                 names = new String[_vars.length];
 106  0
                 values = new ValueExpression[_vars.length];
 107  0
                 for (int i = 0; i < _vars.length; i++)
 108  
                 {
 109  0
                     names[i] = _vars[i].getLocalName();
 110  0
                     values[i] = _vars[i].getValueExpression(ctx, Object.class);
 111  
                 }
 112  
             }
 113  
             //actx.pushTemplateContext(new TemplateContextImpl());
 114  0
             actx.pushClient(this);
 115  
             // setup a variable map
 116  0
             if (this._vars.length > 0)
 117  
             {
 118  0
                 VariableMapper varMapper = new VariableMapperWrapper(orig);
 119  0
                 for (int i = 0; i < this._vars.length; i++)
 120  
                 {
 121  0
                     varMapper.setVariable(names[i], values[i]);
 122  
                 }
 123  0
                 ctx.setVariableMapper(varMapper);
 124  
             }
 125  0
             actx.getTemplateContext().setAllowCacheELExpressions(false);
 126  
 
 127  0
             ctx.includeFacelet(parent, this._location);
 128  
         }
 129  0
         catch (FileNotFoundException e)
 130  
         {
 131  0
             throw new TagException(this.tag, e.getMessage());
 132  
         }
 133  
         finally
 134  
         {
 135  
 
 136  
             // make sure we undo our changes
 137  0
             actx.popClient(this);
 138  
             //actx.popTemplateContext();
 139  0
             ctx.setVariableMapper(orig);
 140  0
         }
 141  0
     }
 142  
 
 143  
     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
 144  
             FaceletException, ELException
 145  
     {
 146  0
         if (name != null)
 147  
         {
 148  0
             if (this._handlers == null)
 149  
             {
 150  0
                 return false;
 151  
             }
 152  0
             DefineHandler handler = (DefineHandler) this._handlers.get(name);
 153  0
             if (handler != null)
 154  
             {
 155  0
                 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 156  
                 //TemplateContext itc = actx.popTemplateContext();
 157  
                 try
 158  
                 {
 159  0
                     handler.applyDefinition(ctx, parent);
 160  
                 }
 161  
                 finally
 162  0
                 {
 163  
                     //actx.pushTemplateContext(itc);
 164  0
                 }
 165  0
                 return true;
 166  
             }
 167  
             else
 168  
             {
 169  0
                 return false;
 170  
             }
 171  
         }
 172  
         else
 173  
         {
 174  0
             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 175  
             //TemplateContext itc = actx.popTemplateContext();
 176  
             try
 177  
             {
 178  0
                 this.nextHandler.apply(ctx, parent);
 179  
             }
 180  
             finally
 181  0
             {
 182  
                 //actx.pushTemplateContext(itc);
 183  0
             }
 184  0
             return true;
 185  
         }
 186  
     }
 187  
 
 188  
 }