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 2013/08/13 01:07:55 UTC

svn commit: r1513291 - in /myfaces/core/trunk: impl/src/main/java/org/apache/myfaces/view/facelets/compiler/ impl/src/main/java/org/apache/myfaces/view/facelets/tag/ impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/ impl/src/test/resou...

Author: lu4242
Date: Mon Aug 12 23:07:54 2013
New Revision: 1513291

URL: http://svn.apache.org/r1513291
Log:
MYFACES-3750 Allow to reference composite components directly from facelets taglib file using <resource-id> 

Added:
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testComponentFromResourceId.xhtml
      - copied, changed from r1509781, myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleComposite.xhtml
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/AbstractTagLibrary.java
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/view/facelets/tag/composite/CompositeComponentTestCase.java
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/test-facelet.taglib.xml
    myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ResourceValidationUtils.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java?rev=1513291&r1=1513290&r2=1513291&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java Mon Aug 12 23:07:54 2013
@@ -317,6 +317,13 @@ public final class TagLibraryConfig
             ParameterCheck.notNull("type", type);
             this.addTagHandler(name, type);
         }
+        
+        public void putComponentFromResourceId(String name, String resourceId)
+        {
+            ParameterCheck.notNull("name", name);
+            ParameterCheck.notNull("resourceId", resourceId);
+            this.addComponentFromResourceId(name, resourceId);
+        }
 
         public void putComponent(String name, String componentType, String rendererType)
         {
@@ -441,6 +448,8 @@ public final class TagLibraryConfig
         
         private String compositeLibraryName;
         
+        private String resourceId;
+        
         public LibraryHandler(FacesContext facesContext, URL source)
         {
             this.source = source;
@@ -514,6 +523,10 @@ public final class TagLibraryConfig
                 {
                     //Not used
                 }                
+                else if ("resource-id".equals(qName))
+                {
+                    this.resourceId = this.captureBuffer();
+                }
                 else
                 {
                     // Make sure there we've seen a namespace element
@@ -545,6 +558,10 @@ public final class TagLibraryConfig
                             impl.putComponent(this.tagName, this.componentType, this.rendererType, this.handlerClass);
                             this.handlerClass = null;
                         }
+                        else if (this.resourceId != null)
+                        {
+                            impl.putComponentFromResourceId(this.tagName, this.resourceId);
+                        }
                         else
                         {
                             impl.putComponent(this.tagName, this.componentType, this.rendererType);

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/AbstractTagLibrary.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/AbstractTagLibrary.java?rev=1513291&r1=1513290&r2=1513291&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/AbstractTagLibrary.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/tag/AbstractTagLibrary.java Mon Aug 12 23:07:54 2013
@@ -27,6 +27,9 @@ import java.util.Map;
 
 import javax.el.ELException;
 import javax.faces.FacesException;
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.faces.context.FacesContext;
 import javax.faces.view.facelets.BehaviorConfig;
 import javax.faces.view.facelets.BehaviorHandler;
 import javax.faces.view.facelets.ComponentConfig;
@@ -40,6 +43,8 @@ import javax.faces.view.facelets.TagConf
 import javax.faces.view.facelets.TagHandler;
 import javax.faces.view.facelets.ValidatorConfig;
 import javax.faces.view.facelets.ValidatorHandler;
+import org.apache.myfaces.view.facelets.tag.composite.CompositeComponentResourceTagHandler;
+import org.apache.myfaces.view.facelets.tag.composite.CompositeResouceWrapper;
 
 /**
  * Base class for defining TagLibraries in Java
@@ -171,6 +176,11 @@ public abstract class AbstractTagLibrary
     {
         _factories.put(name, new UserComponentHandlerFactory(componentType, rendererType, handlerType));
     }
+    
+    protected final void addComponentFromResourceId(String name, String resourceId)
+    {
+        _factories.put(name, new UserComponentFromResourceIdHandlerFactory(resourceId));
+    }
 
     /**
      * Add a ConvertHandler for the specified converterId
@@ -744,4 +754,36 @@ public abstract class AbstractTagLibrary
             }
         }
     }
+    
+    private static class UserComponentFromResourceIdHandlerFactory implements TagHandlerFactory
+    {
+        protected final String resourceId;
+        
+        public UserComponentFromResourceIdHandlerFactory(String resourceId)
+        {
+            this.resourceId = resourceId;
+        }
+
+        public TagHandler createHandler(TagConfig cfg) throws FacesException, ELException
+        {
+            FacesContext facesContext = FacesContext.getCurrentInstance();
+            ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
+            Resource compositeComponentResourceWrapped = resourceHandler.createResourceFromId(resourceId);
+            if (compositeComponentResourceWrapped != null)
+            {
+                Resource compositeComponentResource
+                        = new CompositeResouceWrapper(compositeComponentResourceWrapped);
+                ComponentConfig componentConfig = new ComponentConfigWrapper(cfg,
+                        "javax.faces.NamingContainer", null);
+
+                return new CompositeComponentResourceTagHandler(componentConfig,
+                                                                compositeComponentResource);
+            }
+            else
+            {
+                throw new FaceletException(
+                    "Error Instantiating Component from <resource-id> declaration: " + this.resourceId);
+            }
+        }
+    }
 }

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=1513291&r1=1513290&r2=1513291&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 Aug 12 23:07:54 2013
@@ -860,5 +860,32 @@ public class CompositeComponentTestCase 
         
         Assert.assertTrue(result.contains(resource.getRequestPath()));
     }
+    
+    @Test
+    public void testComponentFromResourceId() throws Exception
+    {
+        UIViewRoot root = facesContext.getViewRoot();
+        vdl.buildView(facesContext, root, "testComponentFromResourceId.xhtml");
+
+        UIComponent panelGroup = root.findComponent("testGroup");
+        Assert.assertNotNull(panelGroup);
+        UINamingContainer compositeComponent = (UINamingContainer) panelGroup.getChildren().get(0);
+        Assert.assertNotNull(compositeComponent);
+        UIOutput text = (UIOutput) compositeComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME).findComponent("text");
+        Assert.assertNotNull(text);
+        
+        StringWriter sw = new StringWriter();
+        MockResponseWriter mrw = new MockResponseWriter(sw);
+        facesContext.setResponseWriter(mrw);
+        
+        compositeComponent.encodeAll(facesContext);
+        sw.flush();
+        
+        HtmlRenderedAttr[] attrs = new HtmlRenderedAttr[]{
+                new HtmlRenderedAttr("value")
+        };
+            
+        HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, sw.toString());
+    }
 
 }

Modified: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/test-facelet.taglib.xml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/test-facelet.taglib.xml?rev=1513291&r1=1513290&r2=1513291&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/test-facelet.taglib.xml (original)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/test-facelet.taglib.xml Mon Aug 12 23:07:54 2013
@@ -36,5 +36,12 @@
         <tag-name>testUserTag5</tag-name>
         <source>testCompositeInsertChildren5UserTagHandler.xhtml</source>
     </tag>
+    
+    <tag>
+        <tag-name>testComponentFromResourceId</tag-name>
+        <component>
+            <resource-id>testComposite/simpleComposite.xhtml</resource-id>
+        </component>
+    </tag>
 
 </facelet-taglib>
\ No newline at end of file

Copied: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testComponentFromResourceId.xhtml (from r1509781, myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleComposite.xhtml)
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testComponentFromResourceId.xhtml?p2=myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testComponentFromResourceId.xhtml&p1=myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleComposite.xhtml&r1=1509781&r2=1513291&rev=1513291&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testSimpleComposite.xhtml (original)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/view/facelets/tag/composite/testComponentFromResourceId.xhtml Mon Aug 12 23:07:54 2013
@@ -17,13 +17,13 @@
 <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">
+	xmlns:testComponent="http://myfaces.apache.org/testComponent">
 <head>
 </head>
 <body>
 <h:panelGroup id="testGroup">
-<testComposite:simpleComposite>
-</testComposite:simpleComposite>
+<testComponent:testComponentFromResourceId>
+</testComponent:testComponentFromResourceId>
 </h:panelGroup>
 </body>
 </html>
\ No newline at end of file

Modified: myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ResourceValidationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ResourceValidationUtils.java?rev=1513291&r1=1513290&r2=1513291&view=diff
==============================================================================
--- myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ResourceValidationUtils.java (original)
+++ myfaces/core/trunk/shared/src/main/java/org/apache/myfaces/shared/resource/ResourceValidationUtils.java Mon Aug 12 23:07:54 2013
@@ -40,7 +40,7 @@ public class ResourceValidationUtils
         // Follow the same rules as for resourceName, but check resourceId does not
         // start with '/'
         return validateResourceName(resourceId, true) && 
-            resourceId.length() > 0 && !(resourceId.charAt(0) != '/');
+            resourceId.length() > 0 && resourceId.charAt(0) != '/';
     }
     
     public static boolean isValidViewResource(String resourceId)