You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/06/14 01:37:44 UTC

svn commit: r1135339 - in /myfaces/core/trunk: api/src/main/java/javax/faces/component/ impl/src/main/java/org/apache/myfaces/view/facelets/el/ impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/ impl/src/test/resources/org/apache/myface...

Author: lu4242
Date: Mon Jun 13 23:37:44 2011
New Revision: 1135339

URL: http://svn.apache.org/viewvc?rev=1135339&view=rev
Log:
MYFACES-3173 #{cc} inside f:event is not resolved correctly

Added:
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/resources/testComposite/simpleFEvent.xhtml
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleFEvent.xhtml
Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeTestComponent.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java?rev=1135339&r1=1135338&r2=1135339&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/UIComponentBase.java Mon Jun 13 23:37:44 2011
@@ -218,7 +218,15 @@ public abstract class UIComponentBase ex
                 // This prevents skip components when processing
                 do 
                 {
-                    _publishPostAddToViewEvent(context, child);
+                    child.pushComponentToEL(context, child);
+                    try
+                    {
+                        _publishPostAddToViewEvent(context, child);
+                    }
+                    finally
+                    {
+                        child.popComponentFromEL(context);
+                    }
                     currentChild = child;
                 }
                 while ((i < children.size()) &&
@@ -230,7 +238,15 @@ public abstract class UIComponentBase ex
         {
             for (UIComponent child : component.getFacets().values())
             {
-                _publishPostAddToViewEvent(context, child);
+                child.pushComponentToEL(context, child);
+                try
+                {
+                    _publishPostAddToViewEvent(context, child);
+                }
+                finally
+                {
+                    child.popComponentFromEL(context);
+                }
             }
         }        
     }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java?rev=1135339&r1=1135338&r2=1135339&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/el/CompositeComponentELUtils.java Mon Jun 13 23:37:44 2011
@@ -18,7 +18,6 @@
  */
 package org.apache.myfaces.view.facelets.el;
 
-import java.util.LinkedList;
 import java.util.regex.Pattern;
 
 import javax.faces.component.UIComponent;
@@ -38,7 +37,7 @@ public final class CompositeComponentELU
      * The key under which the component stack is stored in the FacesContext.
      * ATTENTION: this constant is duplicate in UIComponent.
      */
-    public static final String COMPONENT_STACK = "componentStack:" + UIComponent.class.getName();
+    //public static final String COMPONENT_STACK = "componentStack:" + UIComponent.class.getName();
     
     /**
      * The key under which the current composite component is stored in the attribute
@@ -85,62 +84,118 @@ public final class CompositeComponentELU
     }
     
     /**
-     * Trys to find a composite component on the composite component stack
+     * Try to find a composite component on the composite component stack
      * and using UIComponent.getCurrentCompositeComponent() based on the 
      * location of the facelet page that generated the composite component.
      * @param facesContext
      * @param location
      * @return
      */
-    public static UIComponent getCompositeComponentBasedOnLocation(FacesContext facesContext, 
-            Location location)
+    public static UIComponent getCompositeComponentBasedOnLocation(final FacesContext facesContext, 
+            final Location location)
     {
-        // look on the component stack
-        LinkedList<UIComponent> componentStack = getComponentStack(facesContext);
-        if (componentStack != null && !componentStack.isEmpty())
+        //1 Use getCurrentComponent and getCurrentCompositeComponent to look on the component stack
+        UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
+        
+        if (currentComponent == null)
+        {
+            // Cannot found any component, because we don't have any reference!
+            return null;
+        }
+        
+        UIComponent currentCompositeComponent = UIComponent.getCurrentCompositeComponent(facesContext);
+        
+        //1.1 Use getCurrentCompositeComponent first!
+        if (currentCompositeComponent != null)
+        {
+            Location componentLocation = (Location) currentCompositeComponent.getAttributes().get(LOCATION_KEY);
+            if (componentLocation != null 
+                    && componentLocation.getPath().equals(location.getPath()))
+            {
+                return currentCompositeComponent;
+            }
+        }
+        
+        //2. Look on the stack using a recursive algorithm.
+        UIComponent matchingCompositeComponent = lookForCompositeComponentOnStack(facesContext, location, currentComponent);
+        
+        if (matchingCompositeComponent != null)
+        {
+            return matchingCompositeComponent;
+        }
+        
+        //2. Try to find it using UIComponent.getCurrentCompositeComponent(). 
+        // This one will look the direct parent hierarchy of the component,
+        // to see if the composite component can be found.
+        if (currentCompositeComponent != null)
         {
-            // try to find the right composite component
-            for (UIComponent component : componentStack)
+            currentComponent = currentCompositeComponent;
+        }
+        else
+        {
+            //Try to find the composite component looking directly the parent
+            //ancestor of the current component
+            //currentComponent = UIComponent.getCurrentComponent(facesContext);
+            boolean found = false;
+            while (currentComponent != null && !found)
             {
-                if (UIComponent.isCompositeComponent(component))
+                if (UIComponent.isCompositeComponent(currentComponent))
                 {
-                    Location componentLocation = (Location) component.getAttributes().get(LOCATION_KEY);
-                    if (componentLocation != null 
-                            && componentLocation.getPath().equals(location.getPath()))
-                    {
-                        return component;
-                    }
+                    found = true;
+                }
+                else
+                {
+                    currentComponent = currentComponent.getParent();
                 }
             }
         }
         
-        // try to find it using UIComponent.getCurrentCompositeComponent()
-        UIComponent component = UIComponent.getCurrentCompositeComponent(facesContext);
-        while (component != null)
+        //if currentComponent != null means we have a composite component that we can check
+        //Use UIComponent.getCompositeComponentParent() to traverse here.
+        while (currentComponent != null)
         {
-            Location componentLocation = (Location) component.getAttributes().get(LOCATION_KEY);
+            Location componentLocation = (Location) currentComponent.getAttributes().get(LOCATION_KEY);
             if (componentLocation != null 
                     && componentLocation.getPath().equals(location.getPath()))
             {
-                return component;
+                return currentComponent;
             }
             // get the composite component's parent
-            component = UIComponent.getCompositeComponentParent(component);
+            currentComponent = UIComponent.getCompositeComponentParent(currentComponent);
         }
         
         // not found
         return null;
     }
     
-    /**
-     * Gets the current component stack from the FacesContext.
-     * @param facesContext
-     * @return
-     */
-    @SuppressWarnings("unchecked")
-    public static LinkedList<UIComponent> getComponentStack(FacesContext facesContext)
+    private static UIComponent lookForCompositeComponentOnStack(final FacesContext facesContext, final Location location, UIComponent currentComponent)
     {
-        return (LinkedList<UIComponent>) facesContext.getAttributes().get(COMPONENT_STACK);
+        if (UIComponent.isCompositeComponent(currentComponent))
+        {
+            Location componentLocation = (Location) currentComponent.getAttributes().get(LOCATION_KEY);
+            if (componentLocation != null 
+                    && componentLocation.getPath().equals(location.getPath()))
+            {
+                return currentComponent;
+            }
+        }
+        currentComponent.popComponentFromEL(facesContext);
+        try
+        {
+            UIComponent c = UIComponent.getCurrentComponent(facesContext);
+            if (c != null)
+            {
+                return lookForCompositeComponentOnStack( facesContext, location, c);
+            }
+            else
+            {
+                return null;
+            }
+        }
+        finally
+        {
+            currentComponent.pushComponentToEL(facesContext, currentComponent);
+        }
     }
     
     /**

Modified: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java?rev=1135339&r1=1135338&r2=1135339&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java (original)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java Mon Jun 13 23:37:44 2011
@@ -34,6 +34,8 @@ import javax.faces.component.html.HtmlCo
 import javax.faces.component.html.HtmlGraphicImage;
 import javax.faces.component.html.HtmlOutputText;
 
+import org.apache.myfaces.config.NamedEventManager;
+import org.apache.myfaces.config.RuntimeConfig;
 import org.apache.myfaces.test.mock.MockResponseWriter;
 import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
 import org.apache.myfaces.test.utils.HtmlRenderedAttr;
@@ -45,6 +47,22 @@ import org.junit.Test;
 public class CompositeComponentTestCase extends FaceletTestCase
 {
 
+    @Override
+    protected void setupComponents() throws Exception
+    {
+        super.setupComponents();
+        application.addComponent(CompositeTestComponent.class.getName(), 
+                CompositeTestComponent.class.getName());
+    }
+    
+    @Override
+    protected void setUpExternalContext() throws Exception
+    {
+        super.setUpExternalContext();
+        
+        RuntimeConfig.getCurrentInstance(externalContext).setNamedEventManager(new NamedEventManager());
+    }
+
     /**
      * Test if a child component inside composite component template is
      * rendered.
@@ -640,6 +658,41 @@ public class CompositeComponentTestCase 
     }
     
     @Test
+    public void testSimpleFEvent() throws Exception
+    {
+        HelloWorld helloWorld = new HelloWorld(); 
+        
+        facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
+                helloWorld);
+        
+        UIViewRoot root = facesContext.getViewRoot();
+        vdl.buildView(facesContext, root, "testSimpleFEvent.xhtml");
+        
+        UIComponent panelGroup1 = root.findComponent("testGroup1");
+        Assert.assertNotNull(panelGroup1);
+        CompositeTestComponent compositeComponent1 = (CompositeTestComponent) panelGroup1.getChildren().get(0);
+        Assert.assertNotNull(compositeComponent1);
+        
+        Assert.assertTrue("postAddToViewCallback should be called", (Boolean) compositeComponent1.getAttributes().get("postAddToViewCallback"));
+        
+        /*
+        StringWriter sw = new StringWriter();
+        MockResponseWriter mrw = new MockResponseWriter(sw);
+        facesContext.setResponseWriter(mrw);
+
+        compositeComponent1.encodeAll(facesContext);
+
+        sw.flush();
+        
+        String resp = sw.toString();
+
+        Assert.assertTrue(resp.contains("HELLO"));
+        Assert.assertTrue(resp.contains("WORLD"));
+        */
+        
+    }
+
+    @Test
     public void testSimpleThisResourceReference() throws Exception
     {
         UIViewRoot root = facesContext.getViewRoot();

Modified: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeTestComponent.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeTestComponent.java?rev=1135339&r1=1135338&r2=1135339&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeTestComponent.java (original)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeTestComponent.java Mon Jun 13 23:37:44 2011
@@ -19,6 +19,7 @@
 package org.apache.myfaces.view.facelets.tag.composite;
 
 import javax.faces.component.UINamingContainer;
+import javax.faces.event.ComponentSystemEvent;
 
 public class CompositeTestComponent extends UINamingContainer
 {
@@ -33,6 +34,11 @@ public class CompositeTestComponent exte
         getStateHelper().put(PropertyKeys.javaProperty, javaProperty);
     }
     
+    public void postAddToViewCallback(ComponentSystemEvent event)
+    {
+        getAttributes().put("postAddToViewCallback", true);
+    }
+    
     protected enum PropertyKeys
     {
         javaProperty

Added: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/resources/testComposite/simpleFEvent.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/resources/testComposite/simpleFEvent.xhtml?rev=1135339&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/resources/testComposite/simpleFEvent.xhtml (added)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/resources/testComposite/simpleFEvent.xhtml Mon Jun 13 23:37:44 2011
@@ -0,0 +1,30 @@
+<!--
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Id: defineInclude.xml 804043 2009-08-13 22:08:44Z lu4242 $
+-->
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:h="http://java.sun.com/jsf/html"
+	xmlns:f="http://java.sun.com/jsf/core"
+	xmlns:composite="http://java.sun.com/jsf/composite">
+<head>
+</head>
+<body>
+<composite:interface  componentType="org.apache.myfaces.view.facelets.tag.composite.CompositeTestComponent">
+</composite:interface>
+<composite:implementation>
+    <f:event type="javax.faces.event.PostAddToViewEvent" listener="#{cc.postAddToViewCallback}"/>
+</composite:implementation>
+</body>
+</html>

Added: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleFEvent.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleFEvent.xhtml?rev=1135339&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleFEvent.xhtml (added)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleFEvent.xhtml Mon Jun 13 23:37:44 2011
@@ -0,0 +1,29 @@
+<!--
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ $Id: defineInclude.xml 804043 2009-08-13 22:08:44Z lu4242 $
+-->
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:h="http://java.sun.com/jsf/html"
+	xmlns:f="http://java.sun.com/jsf/core"
+	xmlns:testComposite="http://java.sun.com/jsf/composite/testComposite">
+<head>
+</head>
+<body>
+<h:panelGroup id="testGroup1">
+<testComposite:simpleFEvent>
+</testComposite:simpleFEvent>
+</h:panelGroup>
+</body>
+</html>