You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shale.apache.org by gv...@apache.org on 2006/10/09 04:59:49 UTC

svn commit: r454260 - in /shale/framework/trunk/shale-clay/src: main/java/org/apache/shale/clay/config/ main/java/org/apache/shale/clay/parser/builder/ main/java/org/apache/shale/clay/utils/ main/resources/org/apache/shale/clay/parser/builder/chain/ te...

Author: gvanmatre
Date: Sun Oct  8 19:59:48 2006
New Revision: 454260

URL: http://svn.apache.org/viewvc?view=rev&rev=454260
Log:
Added a pluggable extension so that alternate clay builders can be registered by xml name space.

Added:
    shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java   (with props)
    shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns-config.xml
    shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html   (with props)
    shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml   (with props)
Modified:
    shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/config/Globals.java
    shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/BuilderFactory.java
    shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/JsfDefaultBuilder.java
    shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/utils/PluggableLookupCommand.java
    shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/parser/builder/chain/shale-builder-config.xml

Modified: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/config/Globals.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/config/Globals.java?view=diff&rev=454260&r1=454259&r2=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/config/Globals.java (original)
+++ shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/config/Globals.java Sun Oct  8 19:59:48 2006
@@ -258,5 +258,18 @@
      * in request scope.</p>
      */
     public static final String CLAY_SEQUENCE_GENERATOR = "org.apache.shale.clay.chain.SequenceGenerator";
+
+    /**
+     * <p>The request scope key that will be populated with the unknow namespace when parsing an
+     * HTML document.  Custom builders can be registed by the namespace uri by the commons chains.
+     * <br/><br/>For example:<br/>
+     * &lt;catalog name="clayCustomization" &gt;<br/>
+     * &nbsp;&lt;chain name="http://www.acme.com/jsf/mywidgets"&gt;<br/>
+     * &nbsp;&nbsp;&lt;command  className="org.apache.shale.clay.parser.builder.chain.JsfDefaultBuilderRule"
+     *  prefix="w"/&gt;<br/>
+     * &nbsp;&lt;/chain&gt;<br/>
+     *
+     */
+    public static final String CLAY_CUSTOM_BUILDER_XMLNS = "XMLNS";
 }
 

Modified: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/BuilderFactory.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/BuilderFactory.java?view=diff&rev=454260&r1=454259&r2=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/BuilderFactory.java (original)
+++ shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/BuilderFactory.java Sun Oct  8 19:59:48 2006
@@ -21,6 +21,8 @@
 
 import java.net.URL;
 
+import javax.faces.context.FacesContext;
+
 import org.apache.commons.chain.Catalog;
 import org.apache.commons.chain.CatalogFactory;
 import org.apache.commons.chain.Command;
@@ -114,6 +116,12 @@
                 if (uri != null) {
                     command = catalog.getCommand(uri);
                     if (command == null) {
+                        FacesContext facesContext = FacesContext.getCurrentInstance();
+                        if (facesContext != null) {
+                            facesContext.getExternalContext().getRequestMap()
+                            .put(Globals.CLAY_CUSTOM_BUILDER_XMLNS, uri);
+                        }
+
                         command = catalog.getCommand(Globals.FIND_UNKNOWN_BUILDER_COMMAND_NAME);
                     }
                 } else {

Modified: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/JsfDefaultBuilder.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/JsfDefaultBuilder.java?view=diff&rev=454260&r1=454259&r2=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/JsfDefaultBuilder.java (original)
+++ shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/parser/builder/JsfDefaultBuilder.java Sun Oct  8 19:59:48 2006
@@ -429,9 +429,14 @@
             //override the node prefix with the one
             //assigned to the uri.  the prefix will
             //match the config beans
-            if (getPrefix() != null) {
+
+            String prefix = getPrefix();
+            if (prefix == null) {
+                prefix = node.getQname();
+            }
+            if (prefix != null) {
                 jsfid.insert(0, ':');
-                jsfid.insert(0, getPrefix());
+                jsfid.insert(0, prefix);
             }
         }
 

Modified: shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/utils/PluggableLookupCommand.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/utils/PluggableLookupCommand.java?view=diff&rev=454260&r1=454259&r2=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/utils/PluggableLookupCommand.java (original)
+++ shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/utils/PluggableLookupCommand.java Sun Oct  8 19:59:48 2006
@@ -19,7 +19,12 @@
  */
 package org.apache.shale.clay.utils;
 
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+import org.apache.commons.chain.Catalog;
 import org.apache.commons.chain.CatalogFactory;
+import org.apache.commons.chain.Command;
 import org.apache.commons.chain.Context;
 import org.apache.commons.chain.generic.LookupCommand;
 
@@ -29,7 +34,9 @@
  * <code>optional</code> property in the super class is <code>true</code>.
  * If the catalog is not found and the command is not optional, it returns a
  * <code>false</code> value.  Otherwise, if the catalog exists, the super
- * implementation is invoked.</p>
+ * implementation is invoked.  The name of the command can be a value
+ * binding expression.  The value is evaluated if it contains an expression
+ * and the resolved command is invoked.</p>
  *
  */
 public class PluggableLookupCommand extends LookupCommand {
@@ -51,6 +58,14 @@
     }
 
     /**
+     * @return <code>Catalog</code> for the <code>catalogName</code>
+     */
+    private Catalog getCatalog() {
+        CatalogFactory catalogFactory = CatalogFactory.getInstance();
+        return catalogFactory.getCatalog(getCatalogName());
+    }
+
+    /**
      * <p>Adds an additional check to determine if the catalog name is loaded.
      * If loaded, the super implementation is invoked.  Otherwise, the chain
      * continues if the command is optional.</p>
@@ -65,7 +80,35 @@
            return !isOptional();
         }
 
+        if (isValueReference(getName())) {
+            // command is a binding expression; evaluate and the find the target command
+            FacesContext facesContext = FacesContext.getCurrentInstance();
+            ValueBinding vb = facesContext.getApplication().createValueBinding(getName());
+            String targetCommand = (String) vb.getValue(facesContext);
+
+            if (targetCommand != null) {
+               Command command = getCatalog().getCommand(targetCommand);
+               return command.execute(context);
+            }
+        }
         return super.execute(context);
+    }
+
+    /**
+     * @param value the command name to test for a binding expression
+     * @return <code>true</code> if the <code>value</code> is a binding expression
+     */
+    private boolean isValueReference(String value) {
+        if (value == null) {
+            return false;
+        }
+
+        if ((value.indexOf("#{") > -1)
+                && (value.indexOf("}") > -1)) {
+            return true;
+        }
+
+        return false;
     }
 
 }

Modified: shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/parser/builder/chain/shale-builder-config.xml
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/parser/builder/chain/shale-builder-config.xml?view=diff&rev=454260&r1=454259&r2=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/parser/builder/chain/shale-builder-config.xml (original)
+++ shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/parser/builder/chain/shale-builder-config.xml Sun Oct  8 19:59:48 2006
@@ -35,7 +35,12 @@
     <command  className="org.apache.shale.clay.parser.builder.chain.DefaultBuilderRule"/>		
   </chain>
 
-  <chain           name="unknown-namespace">
+  <chain           name="unknown-namespace">
+    <command className   = "org.apache.shale.clay.utils.PluggableLookupCommand"
+             catalogName = "clayCustomization"
+             name        = "#{requestScope.XMLNS}"
+             optional    = "true"/>
+  
     <command  className="org.apache.shale.clay.parser.builder.chain.DefaultBuilderRule"/>
   </chain>
 

Added: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java?view=auto&rev=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java (added)
+++ shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java Sun Oct  8 19:59:48 2006
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * 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.
+ */
+package org.apache.shale.clay.config;
+
+import java.util.Iterator;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.chain.web.ChainListener;
+import org.apache.shale.clay.config.beans.AttributeBean;
+import org.apache.shale.clay.config.beans.ComponentBean;
+
+// tests registering builders in an alternate namespace
+public class ConfigAltNsTestCase extends AbstractTestCaseConfig {
+
+    // Construct a new instance of this test case.
+    public ConfigAltNsTestCase(String name) {
+        super(name);
+    }
+
+    // Return the tests included in this test case.
+    public static Test suite() {
+
+        return (new TestSuite(ConfigAltNsTestCase.class));
+
+    }
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+    }
+    
+    public void testLoadBuilderNamespace() throws Exception {
+    	
+    	//contains a widget in the xmlns:acme="http://www.acme.org/widgets" namespace
+        loadConfigFiles("/org/apache/shale/clay/config/altns-config.xml", null);
+        
+        // simulate invoking the common chains context listener
+        servletContext.addInitParameter("org.apache.commons.chain.CONFIG_WEB_RESOURCE", "/org/apache/shale/clay/config/chain-config.xml");        
+        ServletContextListener chainsListener = new ChainListener();
+        ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
+        chainsListener.contextInitialized(servletContextEvent);
+        
+        // find the config bean definition in the commons XML config file
+        ComponentBean configBean = standardConfigBean.getElement("acme:city");
+        assertNotNull("acme:city exists", configBean);
+        assertEquals("id", "city", configBean.getId());
+        
+        AttributeBean size = configBean.getAttribute("size");
+        assertNotNull("size", size);
+        assertEquals("size", "20", size.getValue());
+        
+        AttributeBean value = configBean.getAttribute("value");
+        assertNotNull("value", value);
+        assertEquals("value", "#{@managed-bean-name.city}", value.getValue());
+        
+        AttributeBean maxlength = configBean.getAttribute("maxlength");
+        assertNotNull("maxlength", maxlength);
+        assertEquals("maxlength", "30", maxlength.getValue());
+        
+        AttributeBean required = configBean.getAttribute("required");
+        assertNotNull("required", required);
+        assertEquals("required", "true", required.getValue());
+       
+
+        // load a template that points/extends the acme:city widget
+        ComponentBean templateRoot = htmlTemplateConfigBean.getElement("/org/apache/shale/clay/config/altns.html");
+        assertNotNull("/org/apache/shale/clay/config/altns.html",  templateRoot);
+        
+        // config bean after extend by the html template
+        ComponentBean cityConfigBean = findForId(configBean.getId(), templateRoot);
+        assertNotNull("city from html template", cityConfigBean);
+        
+        // inherit from acme:city
+        assertEquals("id", "city", cityConfigBean.getId());
+        
+        // html override
+        size = cityConfigBean.getAttribute("size");
+        assertNotNull("size", size);
+        assertEquals("size", "10", size.getValue());
+        
+        // inherit from acme:city
+        value = cityConfigBean.getAttribute("value");
+        assertNotNull("value", value);
+        assertEquals("value", "#{@managed-bean-name.city}", value.getValue());
+        
+        // html override
+        maxlength = cityConfigBean.getAttribute("maxlength");
+        assertNotNull("maxlength", maxlength);
+        assertEquals("maxlength", "100", maxlength.getValue());
+        
+        // html override
+        required = cityConfigBean.getAttribute("required");
+        assertNotNull("required", required);
+        assertEquals("required", "false", required.getValue());
+        
+        // just because we can
+        chainsListener.contextDestroyed(servletContextEvent);
+    }
+    
+    private ComponentBean findForId(String id, ComponentBean root) {
+        if (root.getId() != null && root.getId().equals(id)) {
+           return root;
+        }
+        Iterator ci = root.getChildren().iterator();
+        while (ci.hasNext()) {
+           ComponentBean child = (ComponentBean) ci.next();
+           ComponentBean target = findForId(id, child);
+           if (target != null) {
+              return target;
+           }
+        }
+        
+        return null;
+    }
+
+}

Propchange: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/ConfigAltNsTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns-config.xml
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns-config.xml?view=auto&rev=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns-config.xml (added)
+++ shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns-config.xml Sun Oct  8 19:59:48 2006
@@ -0,0 +1,18 @@
+<?xml version='1.0' encoding="UTF-8"?>
+
+  <!DOCTYPE view PUBLIC
+      "-//Apache Software Foundation//DTD Shale Clay View Configuration 1.0//EN"
+      "http://shale.apache.org/dtds/clay-config_1_0.dtd">
+
+<view>
+		
+	<component jsfid="acme:city" extends="inputText" id="city"> 
+		   <attributes>
+		      <set name="value" value="#{@managed-bean-name.city}" />		
+			  <set name="size" value="20" />
+			  <set name="maxlength" value="30" />
+			  <set name="required" value="true" />
+		   </attributes>
+	</component>
+	
+</view>

Added: shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html?view=auto&rev=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html (added)
+++ shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html Sun Oct  8 19:59:48 2006
@@ -0,0 +1,3 @@
+<form xmlns:a="http://www.acme.org/widgets">
+   <a:city size="10" maxlength="100" required="false" />
+</form>
\ No newline at end of file

Propchange: shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/altns.html
------------------------------------------------------------------------------
    svn:eol-style = native

Added: shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml
URL: http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml?view=auto&rev=454260
==============================================================================
--- shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml (added)
+++ shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml Sun Oct  8 19:59:48 2006
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+
+ Copyright 2004-2005 The Apache Software Foundation.
+ 
+ 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.
+
+-->
+
+<catalogs>
+
+  <!-- Define Clay builder/namespace override -->
+  <catalog               name="clayCustomization">
+    <chain               name="http://www.acme.org/widgets">
+       <command  className="org.apache.shale.clay.parser.builder.chain.JsfDefaultBuilderRule" prefix="acme"/>
+    </chain>
+  </catalog>
+</catalogs>

Propchange: shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/chain-config.xml
------------------------------------------------------------------------------
    svn:eol-style = native