Author: gvanmatre
Date: Fri Nov 10 20:37:07 2006
New Revision: 473639
URL: http://svn.apache.org/viewvc?view=rev&rev=473639
Log:
Allow setting the view root's properties within a Clay template (SHALE-331).
Added:
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
(with props)
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
(with props)
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
(with props)
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
(with props)
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
(with props)
Modified:
shale/framework/trunk/shale-clay/src/main/resources/META-INF/clay-config.xml
shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/component/chain/shale-clay-config.xml
Added:
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java?view=auto&rev=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
(added)
+++
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
Fri Nov 10 20:37:07 2006
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.component.chain;
+
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.faces.context.FacesContext;
+
+import org.apache.commons.chain.Context;
+import org.apache.shale.clay.config.beans.AttributeBean;
+import org.apache.shale.clay.config.beans.Attributes;
+import org.apache.shale.clay.config.beans.ComponentBean;
+
+/**
+ * <p>This Command assigns properties to an existing UIViewRoot.
+ * It can only update the renderKit and locale properties.
+ * There are two new properties on the view root in JSF 1.2,
+ * beforePhaseListener and afterPhaseListener. These two require
+ * MethodExpression that is introduced in JSP 2.1. We will have to wait
+ * until we can migrate to 1.2 before we can support these attributes. For
some reason,
+ * these are not wrappered like the ValueBinding is wrapperd by the
ValueExpression.
+ * </p>
+ */
+public class AssignViewRootCommand extends AbstractCommand {
+
+ /**
+ * <p>If the target <code>componentType</code> is "javax.faces.ViewRoot",
+ * assign the property overrides is present.</p>
+ * @param context commons chains context
+ * @return <code>true</code> if the current display element is for the
view root;
+ * Otherwise, return <code>false</code> to create/update a component.
+ * @exception Exception any error that might terminate processing
+ */
+ public boolean execute(Context context) throws Exception {
+ ClayContext clayContext = (ClayContext) context;
+ if (clayContext == null) {
+ throw new NullPointerException(getMessages()
+ .getMessage("clay.null.clayContext"));
+ }
+
+ ComponentBean displayElement = clayContext.getDisplayElement();
+ if (displayElement == null) {
+ throw new NullPointerException(getMessages()
+ .getMessage("clay.null.componentBean"));
+ }
+
+ if (!displayElement.getComponentType().equals("javax.faces.ViewRoot"))
{
+ return false;
+ }
+
+ FacesContext facesContext = clayContext.getFacesContext();
+ if (facesContext == null) {
+ throw new NullPointerException(getMessages()
+ .getMessage("clay.null.facesContext"));
+ }
+
+ // create a new scoped symbol table
+ Map symbolTable = new Attributes();
+ // inherit the parents symbols
+ symbolTable.putAll(clayContext.getSymbols());
+ // override config (XML, HTML) symbols
+ symbolTable.putAll(displayElement.getSymbols());
+ // push to context
+ clayContext.setSymbols(symbolTable);
+
+ // evaluate nested symbols; symbols having symbols as values
+ realizeSymbols(clayContext);
+
+ AttributeBean attr = null;
+ attr = displayElement.getAttribute("renderKitId");
+ if (attr != null && attr.getValue() != null) {
+ clayContext.setAttribute(attr);
+ String expr = replaceMnemonic(clayContext);
+ if (expr != null) {
+ if (isValueReference(expr)) {
+ getTagUtils().setValueBinding(facesContext.getViewRoot(),
"renderKitId", expr);
+ } else {
+ facesContext.getViewRoot().setRenderKitId(expr);
+ }
+ }
+ clayContext.setAttribute(null);
+ }
+ attr = displayElement.getAttribute("locale");
+ if (attr != null && attr.getValue() != null) {
+ clayContext.setAttribute(attr);
+ String expr = replaceMnemonic(clayContext);
+ if (expr != null) {
+ if (isValueReference(expr)) {
+ getTagUtils().setValueBinding(facesContext.getViewRoot(),
"locale", expr);
+ } else {
+ final int language = 0;
+ final int country = 1;
+ StringTokenizer tokenizer = new StringTokenizer(expr,
"-_");
+ String[] tokens = new String[2];
+ int i = 0;
+ while (tokenizer.hasMoreTokens()) {
+ tokens[i++] = tokenizer.nextToken();
+ }
+ Locale locale = null;
+ if (tokens[language] != null && tokens[country] != null) {
+ locale = new Locale(tokens[language], tokens[country]);
+ } else if (tokens[language] != null) {
+ locale = new Locale(tokens[language]);
+ }
+ if (locale != null) {
+ facesContext.getViewRoot().setLocale(locale);
+ }
+ }
+ }
+ clayContext.setAttribute(null);
+ }
+ // Two new properties on the view root in JSF 1.2 beforePhaseListener
and afterPhaseListener.
+ // These two require MethodExpression that is introduced in JSP 2.1.
We will have to wait
+ // until we can migrate to 1.2 before we can support these attributes.
For some reason,
+ // these are not wrappered like the ValueBinding is wrapperd by the
ValueExpression.
+
+ // assign any children of the display element to the parent
+ // we return "true" indicating the component already exists
+ // and the "createComponent" chain will stop and return back to the
+ // "addComponent" chain
+ clayContext.setChild(clayContext.getParent());
+
+ return true;
+ }
+
+}
Propchange:
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-clay/src/main/java/org/apache/shale/clay/component/chain/AssignViewRootCommand.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified:
shale/framework/trunk/shale-clay/src/main/resources/META-INF/clay-config.xml
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/resources/META-INF/clay-config.xml?view=diff&rev=473639&r1=473638&r2=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/main/resources/META-INF/clay-config.xml
(original)
+++
shale/framework/trunk/shale-clay/src/main/resources/META-INF/clay-config.xml
Fri Nov 10 20:37:07 2006
@@ -25,6 +25,21 @@
"http://shale.apache.org/dtds/clay-config_1_0.dtd">
<view>
+ <component jsfid="view" componentType="javax.faces.ViewRoot">
+ <attributes>
+ <set name="renderKitId" bindingType="VB" />
+ <!--
+ Can not support until full JSF 1.2 support
+ Requires MethodExpression type
+
+ <set name="beforePhaseListener" bindingType="VB" />
+ <set name="afterPhaseListener" bindingType="VB" />
+ -->
+ <set name="locale" bindingType="VB"/>
+ </attributes>
+ </component>
+ <component jsfid="f:view" extends="view"/>
+
<component jsfid="converter" componentType="override">
<description>Abstract component definition.</description>
<attributes>
Modified:
shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/component/chain/shale-clay-config.xml
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/component/chain/shale-clay-config.xml?view=diff&rev=473639&r1=473638&r2=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/component/chain/shale-clay-config.xml
(original)
+++
shale/framework/trunk/shale-clay/src/main/resources/org/apache/shale/clay/component/chain/shale-clay-config.xml
Fri Nov 10 20:37:07 2006
@@ -47,7 +47,7 @@
<chain name="createComponent">
-
+ <command
className="org.apache.shale.clay.component.chain.AssignViewRootCommand"/>
<command
className="org.apache.shale.clay.component.chain.CreateComponentCommand"/>
<command
className="org.apache.shale.clay.component.chain.AssignPropertiesCommand"/>
<command
className="org.apache.shale.clay.component.chain.AssignValidatorsCommand"/>
Added:
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java?view=auto&rev=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
(added)
+++
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
Fri Nov 10 20:37:07 2006
@@ -0,0 +1,135 @@
+package org.apache.shale.clay.config;
+
+import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.Locale;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.ResponseWriter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.shale.clay.component.Clay;
+
+public class AssignViewRootTestCase extends AbstractTestCaseConfig {
+
+ // Construct a new instance of this test case.
+ public AssignViewRootTestCase(String name) {
+ super(name);
+ }
+
+ // Return the tests included in this test case.
+ public static Test suite() {
+ return (new TestSuite(AssignViewRootTestCase.class));
+ }
+
+ private Clay clay = null;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ loadConfigFiles(null, null);
+
+ }
+
+ public void testAssign1() throws Exception {
+
+ buildSubtree("/org/apache/shale/clay/config/viewroot1.html");
+
+ String renderKitId = facesContext.getViewRoot().getRenderKitId();
+ assertEquals("renderKitId", "MY_KIT1", renderKitId);
+
+ Locale locale = facesContext.getViewRoot().getLocale();
+ assertEquals("locale", "ja_JP", locale.toString());
+
+ UIComponent input = findComponent(clay, "input1");
+ assertNotNull("child 1 assigned", input);
+
+ input = findComponent(clay, "input2");
+ assertNotNull("child 2 assigned", input);
+
+ }
+
+ public void testAssign2() throws Exception {
+
+ buildSubtree("/org/apache/shale/clay/config/viewroot2.html");
+
+ String renderKitId = facesContext.getViewRoot().getRenderKitId();
+ assertEquals("renderKitId", "MY_KIT2", renderKitId);
+
+ Locale locale = facesContext.getViewRoot().getLocale();
+ assertEquals("locale", "pt_PT", locale.toString());
+
+ UIComponent input = findComponent(clay, "input1");
+ assertNotNull("child 1 assigned", input);
+
+ input = findComponent(clay, "input2");
+ assertNotNull("child 2 assigned", input);
+
+ }
+
+ public void testAssign3() throws Exception {
+
+ buildSubtree("/org/apache/shale/clay/config/viewroot3.html");
+
+ String renderKitId = facesContext.getViewRoot().getRenderKitId();
+ assertEquals("renderKitId", "MY_KIT3", renderKitId);
+
+ Locale locale = facesContext.getViewRoot().getLocale();
+ assertEquals("locale", "de", locale.toString());
+
+ UIComponent input = findComponent(clay, "input1");
+ assertNotNull("child 1 assigned", input);
+
+ input = findComponent(clay, "input2");
+ assertNotNull("child 2 assigned", input);
+
+ }
+
+ private void buildSubtree(String jsfid) throws Exception {
+ clay = (Clay)
application.createComponent("org.apache.shale.clay.component.Clay");
+ clay.setId("test");
+ clay.setJsfid(jsfid);
+ clay.setManagedBeanName("test");
+
+ // builds a buffer to write the page to
+ StringWriter writer = new StringWriter();
+ // create a buffered response writer
+ ResponseWriter buffResponsewriter = facesContext.getRenderKit()
+ .createResponseWriter(writer, null,
+ response.getCharacterEncoding());
+ // push buffered writer to the faces context
+ facesContext.setResponseWriter(buffResponsewriter);
+ // start a document
+ buffResponsewriter.startDocument();
+
+ // build subtree
+ clay.encodeBegin(facesContext);
+
+ }
+
+ private UIComponent findComponent(UIComponent parent, String id) {
+ if (parent == null) {
+ return null;
+ }
+
+ if (parent.getId() != null && parent.getId().equals(id)) {
+ return parent;
+ } else {
+ Iterator ci = parent.getChildren().iterator();
+ while (ci.hasNext()) {
+ UIComponent child = (UIComponent) ci.next();
+ UIComponent target = findComponent(child, id);
+ if (target != null) {
+ return target;
+ }
+ }
+ }
+
+ return null;
+ }
+
+
+
+}
Propchange:
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-clay/src/test/java/org/apache/shale/clay/config/AssignViewRootTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html?view=auto&rev=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
(added)
+++
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
Fri Nov 10 20:37:07 2006
@@ -0,0 +1,24 @@
+<!-- ### clay:remove ### -->
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to you 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.
+-->
+<!-- ### /clay:remove ### -->
+<html>
+ <span id="view" jsfid="f:view" locale="ja_JP" renderKitId="MY_KIT1">
+ <input type="text" id="input1">
+ <input type="text" id="input2">
+ </span>
+</html>
\ No newline at end of file
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot1.html
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html?view=auto&rev=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
(added)
+++
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
Fri Nov 10 20:37:07 2006
@@ -0,0 +1,27 @@
+<!-- ### clay:remove ### -->
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to you 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.
+-->
+<!-- ### /clay:remove ### -->
+<html xmlns:f="http://java.sun.com/jsf/core"
+ xmlns:h="http://java.sun.com/jsf/html">
+
+ <f:view locale="pt_PT" renderKitId="MY_KIT2">
+ <h:inputText id="input1"/>
+ <h:inputText id="input2"/>
+ </f:view>
+
+</html>
\ No newline at end of file
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot2.html
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
URL:
http://svn.apache.org/viewvc/shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html?view=auto&rev=473639
==============================================================================
---
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
(added)
+++
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
Fri Nov 10 20:37:07 2006
@@ -0,0 +1,24 @@
+<!-- ### clay:remove ### -->
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to you 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.
+-->
+<!-- ### /clay:remove ### -->
+<html xmlns:f="http://java.sun.com/jsf/core"
+ xmlns:h="http://java.sun.com/jsf/html">
+ <f:view locale="de" renderKitId="MY_KIT3"/>
+ <h:inputText id="input1"/>
+ <h:inputText id="input2"/>
+</html>
\ No newline at end of file
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
shale/framework/trunk/shale-clay/src/test/resources/org/apache/shale/clay/config/viewroot3.html
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL