Coverage Report - org.apache.myfaces.view.facelets.tag.ui.DecorateHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
DecorateHandler
0%
0/73
0%
0/52
10.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.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  
 
 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  
 import org.apache.myfaces.view.facelets.TemplateClient;
 44  
 import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
 45  
 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
 46  
 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
 47  
 
 48  
 /**
 49  
  * The decorate tag acts the same as a composition tag, but it will not trim 
 50  
  * everything outside of it. This is useful in cases where you have a list of 
 51  
  * items in a document, which you would like to be decorated or framed.
 52  
  *  
 53  
  * The sum of it all is that you can take any element in the document and decorate 
 54  
  * it with some external logic as provided by the template.
 55  
  * 
 56  
  * TODO: REFACTOR - This class could easily use a common parent with CompositionHandler
 57  
  * 
 58  
  * @author Jacob Hookom
 59  
  * @version $Id$
 60  
  */
 61  
 @JSFFaceletTag(name="ui:decorate")
 62  
 public final class DecorateHandler extends TagHandler implements TemplateClient, ComponentContainerHandler
 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 ParamHandler[] _params;
 81  
 
 82  
     /**
 83  
      * @param config
 84  
      */
 85  
     public DecorateHandler(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<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class);
 101  0
         if (!params.isEmpty())
 102  
         {
 103  0
             int i = 0;
 104  0
             _params = new ParamHandler[params.size()];
 105  0
             for (ParamHandler handler : params)
 106  
             {
 107  0
                 _params[i++] = handler;
 108  0
             }
 109  0
         }
 110  
         else
 111  
         {
 112  0
             _params = null;
 113  
         }
 114  0
     }
 115  
 
 116  
     /*
 117  
      * (non-Javadoc)
 118  
      * 
 119  
      * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
 120  
      * javax.faces.component.UIComponent)
 121  
      */
 122  
     public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
 123  
             ELException
 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  
 
 136  0
         AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
 137  0
         actx.pushClient(this);
 138  0
         FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
 139  0
         String uniqueId = null;
 140  
         try
 141  
         {
 142  0
             if (!_template.isLiteral())
 143  
             {
 144  0
                 uniqueId = actx.generateUniqueFaceletTagId(
 145  
                     fcc.startComponentUniqueIdSection(), tagId);
 146  
             }
 147  0
             else if (_params != null)
 148  
             {
 149  0
                 uniqueId = actx.generateUniqueFaceletTagId(
 150  
                     fcc.generateUniqueComponentId(), tagId);
 151  
             }
 152  0
             if (_params != null)
 153  
             {
 154  
                 //VariableMapper vm = new VariableMapperWrapper(orig);
 155  
                 //ctx.setVariableMapper(vm);
 156  0
                 for (int i = 0; i < _params.length; i++)
 157  
                 {
 158  0
                     _params[i].apply(ctx, parent, _params[i].getName(ctx), _params[i].getValue(ctx), uniqueId);
 159  
                 }
 160  
             }
 161  
 
 162  
             String path;
 163  0
             boolean markInitialState = false;
 164  0
             if (!_template.isLiteral())
 165  
             {
 166  
                 //String uniqueId = fcc.startComponentUniqueIdSection();
 167  
                 //path = getTemplateValue(actx, fcc, parent, uniqueId);
 168  0
                 String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
 169  0
                 if (restoredPath != null)
 170  
                 {
 171  
                     // If is not restore view phase, the path value should be
 172  
                     // evaluated and if is not equals, trigger markInitialState stuff.
 173  0
                     if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
 174  
                     {
 175  0
                         path = this._template.getValue(ctx);
 176  0
                         if (path == null || path.length() == 0)
 177  
                         {
 178  
                             return;
 179  
                         }
 180  0
                         if (!path.equals(restoredPath))
 181  
                         {
 182  0
                             markInitialState = true;
 183  
                         }
 184  
                     }
 185  
                     else
 186  
                     {
 187  0
                         path = restoredPath;
 188  
                     }
 189  
                 }
 190  
                 else
 191  
                 {
 192  
                     //No state restored, calculate path
 193  0
                     path = this._template.getValue(ctx);
 194  
                 }
 195  0
                 ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
 196  0
             }
 197  
             else
 198  
             {
 199  0
                 path = _template.getValue(ctx);
 200  
             }
 201  
             try
 202  
             {
 203  0
                 boolean oldMarkInitialState = false;
 204  0
                 Boolean isBuildingInitialState = null;
 205  0
                 if (markInitialState)
 206  
                 {
 207  
                     //set markInitialState flag
 208  0
                     oldMarkInitialState = fcc.isMarkInitialState();
 209  0
                     fcc.setMarkInitialState(true);
 210  0
                     isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
 211  
                             StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
 212  
                 }
 213  
                 try
 214  
                 {
 215  0
                     ctx.includeFacelet(parent, path);
 216  
                 }
 217  
                 finally
 218  
                 {
 219  0
                     if (markInitialState)
 220  
                     {
 221  
                         //unset markInitialState flag
 222  0
                         if (isBuildingInitialState == null)
 223  
                         {
 224  0
                             ctx.getFacesContext().getAttributes().remove(
 225  
                                     StateManager.IS_BUILDING_INITIAL_STATE);
 226  
                         }
 227  
                         else
 228  
                         {
 229  0
                             ctx.getFacesContext().getAttributes().put(
 230  
                                     StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
 231  
                         }
 232  0
                         fcc.setMarkInitialState(oldMarkInitialState);
 233  
                     }
 234  
                 }
 235  
             }
 236  
             finally
 237  
             {
 238  
                 //ctx.setVariableMapper(orig);
 239  0
                 actx.popClient(this);
 240  0
             }
 241  
         }
 242  
         finally
 243  
         {
 244  0
             if (!_template.isLiteral())
 245  
             {
 246  0
                 fcc.endComponentUniqueIdSection();
 247  
             }
 248  
         }
 249  0
         if (!_template.isLiteral() && fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() &&
 250  
             !fcc.isRefreshingTransientBuild())
 251  
         {
 252  
             //Mark the parent component to be saved and restored fully.
 253  0
             ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
 254  
         }
 255  0
         if (!_template.isLiteral() && fcc.isDynamicComponentSection())
 256  
         {
 257  0
             ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
 258  
         }
 259  0
     }
 260  
 
 261  
     public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException,
 262  
             FaceletException, ELException
 263  
     {
 264  0
         if (name != null)
 265  
         {
 266  0
             DefineHandler handler = _handlers.get(name);
 267  0
             if (handler != null)
 268  
             {
 269  0
                 handler.applyDefinition(ctx, parent);
 270  0
                 return true;
 271  
             }
 272  
             else
 273  
             {
 274  0
                 return false;
 275  
             }
 276  
         }
 277  
         else
 278  
         {
 279  0
             this.nextHandler.apply(ctx, parent);
 280  0
             return true;
 281  
         }
 282  
     }
 283  
 }