Coverage Report - org.apache.myfaces.view.facelets.tag.ui.LegacyDecorateHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LegacyDecorateHandler
0%
0/74
0%
0/48
10
 
 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  
 import javax.el.ELException;
 28  
 import javax.el.VariableMapper;
 29  
 import javax.faces.FacesException;
 30  
 import javax.faces.application.StateManager;
 31  
 import javax.faces.component.UIComponent;
 32  
 import javax.faces.event.PhaseId;
 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.TagHandler;
 38  
 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
 39  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 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.TagHandlerUtils;
 43  
 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
 44  
 
 45  
 /**
 46  
  * The decorate tag acts the same as a composition tag, but it will not trim 
 47  
  * everything outside of it. This is useful in cases where you have a list of 
 48  
  * items in a document, which you would like to be decorated or framed.
 49  
  *  
 50  
  * The sum of it all is that you can take any element in the document and decorate 
 51  
  * it with some external logic as provided by the template.
 52  
  * 
 53  
  * NOTE: This implementation is provided for compatibility reasons and
 54  
  * it is considered faulty. It is enabled using
 55  
  * org.apache.myfaces.STRICT_JSF_2_FACELETS_COMPATIBILITY web config param.
 56  
  * Don't use it if EL expression caching is enabled.
 57  
  * 
 58  
  * @author Jacob Hookom
 59  
  * @version $Id: DecorateHandler.java,v 1.16 2008/07/13 19:01:41 rlubke Exp $
 60  
  */
 61  
 //@JSFFaceletTag(name="ui:decorate")
 62  
 public final class LegacyDecorateHandler extends TagHandler implements TemplateClient
 63  
 {
 64  
 
 65  
     //private static final Logger log = Logger.getLogger("facelets.tag.ui.decorate");
 66  0
     private static final Logger log = Logger.getLogger(DecorateHandler.class.getName());
 67  
 
 68  
     /**
 69  
      * The resolvable URI of the template to use. The content within the decorate tag 
 70  
      * will be used in populating the template specified.
 71  
      */
 72  
     //@JSFFaceletAttribute(
 73  
     //        name="template",
 74  
     //        className="javax.el.ValueExpression",
 75  
     //        deferredValueType="java.lang.String")
 76  
     private final TagAttribute _template;
 77  
 
 78  
     private final Map<String, DefineHandler> _handlers;
 79  
 
 80  
     private final LegacyParamHandler[] _params;
 81  
 
 82  
     /**
 83  
      * @param config
 84  
      */
 85  
     public LegacyDecorateHandler(TagConfig config)
 86  
     {
 87  0
         super(config);
 88  0
         _template = getRequiredAttribute("template");
 89  0
         _handlers = new HashMap<String, DefineHandler>();
 90  
 
 91  0
         for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class))
 92  
         {
 93  0
             _handlers.put(handler.getName(), handler);
 94  0
             if (log.isLoggable(Level.FINE))
 95  
             {
 96  0
                 log.fine(tag + " found Define[" + handler.getName() + "]");
 97  
             }
 98  0
         }
 99  
 
 100  0
         Collection<LegacyParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, 
 101  
                 LegacyParamHandler.class);
 102  0
         if (!params.isEmpty())
 103  
         {
 104  0
             int i = 0;
 105  0
             _params = new LegacyParamHandler[params.size()];
 106  0
             for (LegacyParamHandler handler : params)
 107  
             {
 108  0
                 _params[i++] = handler;
 109  0
             }
 110  0
         }
 111  
         else
 112  
         {
 113  0
             _params = null;
 114  
         }
 115  0
     }
 116  
 
 117  
     /*
 118  
      * (non-Javadoc)
 119  
      * 
 120  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
 121  
      * javax.faces.component.UIComponent)
 122  
      */
 123  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 124  
             ELException
 125  
     {
 126  0
         VariableMapper orig = ctx.getVariableMapper();
 127  0
         AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 128  0
         actx.pushClient(this);
 129  
 
 130  0
         if (_params != null)
 131  
         {
 132  0
             VariableMapper vm = new VariableMapperWrapper(orig);
 133  0
             ctx.setVariableMapper(vm);
 134  0
             for (int i = 0; i < _params.length; i++)
 135  
             {
 136  0
                 _params[i].apply(ctx, parent);
 137  
             }
 138  
         }
 139  
 
 140  0
         FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
 141  
         String path;
 142  0
         boolean markInitialState = false;
 143  0
         if (!_template.isLiteral())
 144  
         {
 145  0
             String uniqueId = actx.generateUniqueFaceletTagId(fcc.startComponentUniqueIdSection(), tagId);
 146  
             //path = getTemplateValue(actx, fcc, parent, uniqueId);
 147  0
             String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
 148  0
             if (restoredPath != null)
 149  
             {
 150  
                 // If is not restore view phase, the path value should be
 151  
                 // evaluated and if is not equals, trigger markInitialState stuff.
 152  0
                 if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
 153  
                 {
 154  0
                     path = this._template.getValue(ctx);
 155  0
                     if (path == null || path.length() == 0)
 156  
                     {
 157  0
                         return;
 158  
                     }
 159  0
                     if (!path.equals(restoredPath))
 160  
                     {
 161  0
                         markInitialState = true;
 162  
                     }
 163  
                 }
 164  
                 else
 165  
                 {
 166  0
                     path = restoredPath;
 167  
                 }
 168  
             }
 169  
             else
 170  
             {
 171  
                 //No state restored, calculate path
 172  0
                 path = this._template.getValue(ctx);
 173  
             }
 174  0
             ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
 175  0
         }
 176  
         else
 177  
         {
 178  0
             path = _template.getValue(ctx);
 179  
         }
 180  
         try
 181  
         {
 182  
             try
 183  
             {
 184  0
                 boolean oldMarkInitialState = false;
 185  0
                 Boolean isBuildingInitialState = null;
 186  0
                 if (markInitialState)
 187  
                 {
 188  
                     //set markInitialState flag
 189  0
                     oldMarkInitialState = fcc.isMarkInitialState();
 190  0
                     fcc.setMarkInitialState(true);
 191  0
                     isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
 192  
                             StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
 193  
                 }
 194  
                 try
 195  
                 {
 196  0
                     ctx.includeFacelet(parent, path);
 197  
                 }
 198  
                 finally
 199  
                 {
 200  0
                     if (markInitialState)
 201  
                     {
 202  
                         //unset markInitialState flag
 203  0
                         if (isBuildingInitialState == null)
 204  
                         {
 205  0
                             ctx.getFacesContext().getAttributes().remove(
 206  
                                     StateManager.IS_BUILDING_INITIAL_STATE);
 207  
                         }
 208  
                         else
 209  
                         {
 210  0
                             ctx.getFacesContext().getAttributes().put(
 211  
                                     StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
 212  
                         }
 213  0
                         fcc.setMarkInitialState(oldMarkInitialState);
 214  
                     }
 215  
                 }
 216  
             }
 217  
             finally
 218  
             {
 219  0
                 ctx.setVariableMapper(orig);
 220  0
                 actx.popClient(this);
 221  0
             }
 222  
         }
 223  
         finally
 224  
         {
 225  0
             if (!_template.isLiteral())
 226  
             {
 227  0
                 fcc.endComponentUniqueIdSection();
 228  
             }
 229  
         }
 230  0
         if (!_template.isLiteral() && fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() &&
 231  
             !fcc.isRefreshingTransientBuild())
 232  
         {
 233  
             //Mark the parent component to be saved and restored fully.
 234  0
             ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
 235  
         }
 236  0
         if (!_template.isLiteral() && fcc.isDynamicComponentSection())
 237  
         {
 238  0
             ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
 239  
         }
 240  0
     }
 241  
 
 242  
     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
 243  
             FaceletException, ELException
 244  
     {
 245  0
         if (name != null)
 246  
         {
 247  0
             DefineHandler handler = _handlers.get(name);
 248  0
             if (handler != null)
 249  
             {
 250  0
                 handler.applyDefinition(ctx, parent);
 251  0
                 return true;
 252  
             }
 253  
             else
 254  
             {
 255  0
                 return false;
 256  
             }
 257  
         }
 258  
         else
 259  
         {
 260  0
             this.nextHandler.apply(ctx, parent);
 261  0
             return true;
 262  
         }
 263  
     }
 264  
 }