Coverage Report - org.apache.myfaces.view.facelets.tag.composite.CreateDynamicCompositeComponentListener
 
Classes in this File Line Coverage Branch Coverage Complexity
CreateDynamicCompositeComponentListener
0%
0/80
0%
0/32
3.714
 
 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.io.IOException;
 22  
 import java.util.Map;
 23  
 import javax.faces.FacesException;
 24  
 import javax.faces.component.StateHolder;
 25  
 import javax.faces.component.UIComponent;
 26  
 import javax.faces.context.FacesContext;
 27  
 import javax.faces.event.ComponentSystemEvent;
 28  
 import javax.faces.event.ComponentSystemEventListener;
 29  
 import javax.faces.view.facelets.Facelet;
 30  
 import org.apache.myfaces.config.RuntimeConfig;
 31  
 import org.apache.myfaces.view.facelets.AbstractFacelet;
 32  
 import org.apache.myfaces.view.facelets.DynamicComponentRefreshTransientBuildEvent;
 33  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 34  
 import org.apache.myfaces.view.facelets.FaceletDynamicComponentRefreshTransientBuildEvent;
 35  
 import org.apache.myfaces.view.facelets.FaceletFactory;
 36  
 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
 37  
 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageBase;
 38  
 import org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener;
 39  
 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
 40  
 
 41  
 /**
 42  
  * This listener must be attached to PostAddToViewEvent, so when composite component is
 43  
  * added to the view, the algorithm that create the composite component content is executed.
 44  
  *
 45  
  * @author lu4242
 46  
  */
 47  
 public class CreateDynamicCompositeComponentListener 
 48  
     implements ComponentSystemEventListener, StateHolder
 49  
 {
 50  
     private String taglibURI;
 51  
     private String tagName;
 52  
     private Map<String,Object> attributes;
 53  
     private String baseKey;
 54  
 
 55  
     public CreateDynamicCompositeComponentListener(String taglibURI, String tagName, 
 56  
         Map<String, Object> attributes, String baseKey)
 57  0
     {
 58  0
         this.taglibURI = taglibURI;
 59  0
         this.tagName = tagName;
 60  0
         this.attributes = attributes;
 61  0
         this.baseKey = baseKey;
 62  0
     }
 63  
 
 64  
     public CreateDynamicCompositeComponentListener()
 65  0
     {
 66  0
     }
 67  
     
 68  
     public void processEvent(ComponentSystemEvent event)
 69  
     {
 70  0
         FacesContext facesContext = FacesContext.getCurrentInstance();
 71  
         
 72  0
         FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) 
 73  
             facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(
 74  
                 facesContext, facesContext.getViewRoot().getViewId());
 75  
         
 76  
         Facelet componentFacelet;
 77  0
         FaceletFactory faceletFactory = vdl.getFaceletFactory();
 78  
             
 79  0
         FaceletFactory.setInstance(faceletFactory);
 80  
         try
 81  
         {
 82  0
             componentFacelet
 83  
                     = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
 84  
         }
 85  
         finally
 86  
         {
 87  0
             FaceletFactory.setInstance(null);
 88  0
         }
 89  
         
 90  0
         UIComponent component = event.getComponent(); 
 91  
         // The execution of this listener activated another call to PostAddToViewEvent, because
 92  
         // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
 93  
         // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
 94  
         // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
 95  
         // have any side effect (counts as a refresh).
 96  0
         Integer step = (Integer) component.getAttributes().get(
 97  
             CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW); 
 98  0
         if (step != null && step.intValue() == 0)
 99  
         {
 100  0
             component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
 101  
         }
 102  
         else
 103  
         {
 104  0
             return;
 105  
         }
 106  
         try
 107  
         {
 108  0
             facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD,
 109  
                 Boolean.TRUE);
 110  
             
 111  
             // Detect the relationship between parent and child, to ensure the component is properly created
 112  
             // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a 
 113  
             // dynamic component, we need to do it here before apply the handler
 114  0
             UIComponent parent = component.getParent();
 115  0
             String facetName = null;
 116  0
             if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component))
 117  
             {
 118  0
                 facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
 119  
             }
 120  
             
 121  
             
 122  
             try
 123  
             {
 124  0
                 if (facetName != null)
 125  
                 {
 126  0
                     parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY, 
 127  
                             facetName);
 128  
                 }
 129  
                 // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
 130  
                 // find the component. Then we reset it to exclude it from facelets refresh algorithm.
 131  0
                 String markId = (String) component.getAttributes().get("oam.vf.GEN_MARK_ID");
 132  0
                 if (markId == null)
 133  
                 {
 134  0
                     ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
 135  
                         facesContext, component, baseKey);
 136  
                 }
 137  
                 else
 138  
                 {
 139  
                     try
 140  
                     {
 141  0
                         component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
 142  0
                         ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
 143  
                             facesContext, component.getParent(), baseKey);
 144  
                     }
 145  
                     finally
 146  
                     {
 147  0
                         component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
 148  0
                     }
 149  
                 }
 150  
                 
 151  0
                 if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext))
 152  
                 {
 153  0
                     FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
 154  0
                     if (fcc == null)
 155  
                     {
 156  0
                         FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
 157  0
                         FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
 158  0
                         component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new 
 159  
                             RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
 160  0
                         component.getAttributes().put(
 161  
                             DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
 162  
 
 163  
                     }
 164  
                     else
 165  
                     {
 166  0
                         component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new 
 167  
                             RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
 168  
                     }
 169  
                 }
 170  
             }
 171  
             finally
 172  
             {
 173  0
                 if (facetName != null)
 174  
                 {
 175  0
                     parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY);
 176  
                 }
 177  
             }
 178  
         }
 179  0
         catch (IOException e)
 180  
         {
 181  0
             throw new FacesException(e);
 182  
         }
 183  
         finally
 184  
         {
 185  0
             facesContext.getAttributes().remove(
 186  
                 FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
 187  0
         }
 188  0
     }
 189  
 
 190  
     public Object saveState(FacesContext context)
 191  
     {
 192  0
         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
 193  
             context.getExternalContext());
 194  0
         Object[] values = new Object[4];
 195  0
         Integer tagId = runtimeConfig.getIdByNamespace().get(taglibURI);
 196  0
         if (tagId != null)
 197  
         {
 198  0
             values[0] = tagId;
 199  
         }
 200  0
         else if (taglibURI.startsWith(CompositeResourceLibrary.NAMESPACE_PREFIX))
 201  
         {
 202  0
             values[0] = new Object[]{0, taglibURI.substring(35)};
 203  
         }
 204  0
         else if(taglibURI.startsWith(CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX))
 205  
         {
 206  0
             values[0] = new Object[]{1, taglibURI.substring(34)};
 207  
         }
 208  
         else
 209  
         {
 210  0
             values[0] = taglibURI;
 211  
         }
 212  0
         values[1] = tagName;
 213  0
         values[2] = attributes;
 214  0
         values[3] = baseKey;
 215  0
         return values;
 216  
     }
 217  
 
 218  
     public void restoreState(FacesContext context, Object state)
 219  
     {
 220  0
         Object[] values = (Object[]) state;
 221  0
         if (values[0] instanceof String)
 222  
         {
 223  0
             taglibURI = (String) values[0];
 224  
         }
 225  0
         else if (values[0] instanceof Integer)
 226  
         {
 227  0
             RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
 228  
                 context.getExternalContext());
 229  0
             taglibURI = runtimeConfig.getNamespaceById().get((Integer)values[0]);
 230  0
         }
 231  0
         else if (values[0] instanceof Object[])
 232  
         {
 233  0
             Object[] def = (Object[])values[0];
 234  0
             String ns = ( ((Integer)def[0]).intValue() == 0) ? 
 235  
                 CompositeResourceLibrary.NAMESPACE_PREFIX :
 236  
                 CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX;
 237  0
             taglibURI = ns + (String)(((Object[])values[0])[1]);
 238  
         }
 239  0
         tagName = (String)values[1];
 240  0
         attributes = (Map<String,Object>) values[2];
 241  0
         baseKey = (String)values[3];
 242  0
     }
 243  
 
 244  
     public boolean isTransient()
 245  
     {
 246  0
         return false;
 247  
     }
 248  
 
 249  
     public void setTransient(boolean newTransientValue)
 250  
     {
 251  0
     }
 252  
 
 253  
 }