Author: nbelaevski
Date: 2011-01-19 13:17:40 -0500 (Wed, 19 Jan 2011)
New Revision: 21098
Added:
trunk/core/impl/src/main/java/org/richfaces/context/ClientIdFunctionEvaluator.java
Removed:
trunk/core/impl/src/main/java/org/richfaces/context/ClientIdWalker.java
Modified:
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolver.java
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolverNode.java
trunk/core/impl/src/main/java/org/richfaces/context/RowsFunctionContextCallback.java
trunk/core/impl/src/test/java/org/richfaces/context/ComponentIdResolverTest.java
Log:
https://issues.jboss.org/browse/RF-10110
Copied: trunk/core/impl/src/main/java/org/richfaces/context/ClientIdFunctionEvaluator.java
(from rev 21073, trunk/core/impl/src/main/java/org/richfaces/context/ClientIdWalker.java)
===================================================================
--- trunk/core/impl/src/main/java/org/richfaces/context/ClientIdFunctionEvaluator.java
(rev 0)
+++
trunk/core/impl/src/main/java/org/richfaces/context/ClientIdFunctionEvaluator.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.context;
+
+import static org.richfaces.util.Util.NamingContainerDataHolder.SEPARATOR_CHAR_JOINER;
+
+import java.text.MessageFormat;
+import java.util.Collection;
+import java.util.Collections;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.context.IdParser.Node;
+
+import com.google.common.collect.Lists;
+
+class ClientIdFunctionEvaluator {
+
+ private FacesContext context;
+
+ private UIComponent functionTarget;
+
+ private Node[] parsedId;
+
+ private Collection<String> resolvedIds = Lists.newArrayListWithCapacity(1);
+
+ public ClientIdFunctionEvaluator(FacesContext context, Node[] parsedId) {
+ super();
+ this.context = context;
+ this.parsedId = parsedId;
+ }
+
+ private void walk(UIComponent component, String baseId, int nodeIdx) {
+
+ boolean isLastNode = (nodeIdx == parsedId.length - 1);
+
+ Node node = parsedId[nodeIdx];
+
+ Collection<String> directSubtreeIds;
+ UIComponent childComponent;
+
+ if (node.getFunction() == null) {
+ directSubtreeIds = Collections.singleton(node.getImage());
+ childComponent = component;
+ } else {
+ directSubtreeIds = evaluateFunction(component, baseId, node);
+ //functionTarget is set inside evaluateFunction(...) call!
+ childComponent = functionTarget;
+ }
+
+ for (String directSubtreeId : directSubtreeIds) {
+ String clientId = SEPARATOR_CHAR_JOINER.join(baseId, directSubtreeId);
+
+ if (isLastNode) {
+ resolvedIds.add(clientId);
+ } else {
+ walk(childComponent, clientId, nodeIdx + 1);
+ }
+ }
+ }
+
+ private Collection<String> evaluateFunction(UIComponent component, String
baseId, Node node) {
+ Collection<String> directSubtreeIds;
+ String function = node.getFunction();
+ String image = node.getImage();
+
+ if (!"rows".equals(function)) {
+ throw new IllegalArgumentException(MessageFormat.format("Function {0} is
not supported", function));
+ }
+
+ RowsFunctionContextCallback rowsFunctionCallback = new
RowsFunctionContextCallback(image);
+
+ if (!component.invokeOnComponent(context, baseId, rowsFunctionCallback)) {
+ throw new IllegalStateException(MessageFormat.format("Failed to visit
{0}", baseId));
+ }
+
+ functionTarget = rowsFunctionCallback.getComponent();
+ directSubtreeIds = rowsFunctionCallback.getConvertedKeys();
+ return directSubtreeIds;
+ }
+
+ public Collection<String> evaluate(UIComponent component) {
+ walk(component, null, 0);
+ return resolvedIds;
+ }
+}
\ No newline at end of file
Deleted: trunk/core/impl/src/main/java/org/richfaces/context/ClientIdWalker.java
===================================================================
--- trunk/core/impl/src/main/java/org/richfaces/context/ClientIdWalker.java 2011-01-19
18:17:35 UTC (rev 21097)
+++ trunk/core/impl/src/main/java/org/richfaces/context/ClientIdWalker.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -1,106 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.context;
-
-import static org.richfaces.util.Util.NamingContainerDataHolder.SEPARATOR_CHAR_JOINER;
-
-import java.text.MessageFormat;
-import java.util.Collection;
-import java.util.Collections;
-
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-
-import org.richfaces.context.IdParser.Node;
-
-import com.google.common.collect.Lists;
-
-class ClientIdWalker {
-
- private Node[] parsedId;
-
- private UIComponent functionTarget;
-
- private Collection<String> resolvedIds = Lists.newArrayList();
-
- public ClientIdWalker(Node[] parsedId) {
- super();
- this.parsedId = parsedId;
- }
-
- private void walk(FacesContext facesContext, UIComponent component, String baseId,
int nodeIdx) {
-
- boolean isLastNode = (nodeIdx == parsedId.length - 1);
-
- Node node = parsedId[nodeIdx];
-
- Collection<String> directSubtreeIds;
- UIComponent childComponent;
-
- if (node.getFunction() == null) {
- directSubtreeIds = Collections.singleton(node.getImage());
- childComponent = component;
- } else {
- directSubtreeIds = evaluateFunction(facesContext, component, baseId, node);
- //functionTarget is set inside evaluateFunction(...) call!
- childComponent = functionTarget;
- }
-
- for (String directSubtreeId : directSubtreeIds) {
- String clientId = SEPARATOR_CHAR_JOINER.join(baseId, directSubtreeId);
-
- if (isLastNode) {
- resolvedIds.add(clientId);
- } else {
- walk(facesContext, childComponent, clientId, nodeIdx + 1);
- }
- }
- }
-
- private Collection<String> evaluateFunction(FacesContext facesContext,
UIComponent component, String baseId, Node node) {
- Collection<String> directSubtreeIds;
- String function = node.getFunction();
- String image = node.getImage();
-
- if (!"rows".equals(function)) {
- throw new IllegalArgumentException(MessageFormat.format("Function {0} is
not supported", function));
- }
-
- RowsFunctionContextCallback rowsFunctionCallback = new
RowsFunctionContextCallback(image);
-
- if (!component.invokeOnComponent(facesContext, baseId, rowsFunctionCallback)) {
- throw new IllegalStateException(MessageFormat.format("Failed to visit
{0}", baseId));
- }
-
- functionTarget = rowsFunctionCallback.getComponent();
- directSubtreeIds = rowsFunctionCallback.getConvertedKeys();
- return directSubtreeIds;
- }
-
- public void walk(FacesContext facesContext) {
- walk(facesContext, facesContext.getViewRoot(), null, 0);
- }
-
- public Collection<String> getResolvedIds() {
- return resolvedIds;
- }
-}
\ No newline at end of file
Modified: trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolver.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolver.java 2011-01-19
18:17:35 UTC (rev 21097)
+++
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolver.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -26,6 +26,7 @@
import static org.richfaces.util.Util.NamingContainerDataHolder.SEPARATOR_CHAR_JOINER;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -150,22 +151,41 @@
static void setMetaComponentSubstitutions(Map<String, String> substitutionsMap)
{
metaComponentSubstitutions = substitutionsMap;
}
+
+ private boolean hasFunctionNodes(Node[] nodes) {
+ for (Node node : nodes) {
+ if (node.getFunction() != null) {
+ return true;
+ }
+ }
+
+ return false;
+ }
- private String computeClientId(FacesContext context,
+ private Collection<String> computeClientIds(FacesContext context,
UIComponent topMatchComponent, String id) {
- UIComponent container = findContainer(topMatchComponent.getParent());
-
- String containerClientId = null;
-
- if (container instanceof NamingContainer) {
- containerClientId = container.getContainerClientId(context);
+ String clientId;
+
+ int idx = id.indexOf(SEPARATOR_CHAR);
+ if (idx < 0) {
+ int metaSepIdx = id.indexOf(META_COMPONENT_SEPARATOR_CHAR);
+ if (metaSepIdx > 0) {
+ clientId = topMatchComponent.getClientId(facesContext) +
id.substring(metaSepIdx);
+ } else {
+ clientId = topMatchComponent.getClientId(facesContext);
+ }
+ } else {
+ clientId =
SEPARATOR_CHAR_JOINER.join(topMatchComponent.getClientId(facesContext), id.substring(idx +
1));
}
-
- if (containerClientId != null && containerClientId.length() != 0) {
- return SEPARATOR_CHAR_JOINER.join(containerClientId, id);
+
+ Node[] nodes = IdParser.parse(clientId);
+ if (hasFunctionNodes(nodes)) {
+ ClientIdFunctionEvaluator evaluator = new ClientIdFunctionEvaluator(context,
nodes);
+
+ return evaluator.evaluate(topMatchComponent);
} else {
- return id;
+ return Collections.singleton(clientId);
}
}
@@ -335,8 +355,7 @@
}
resolvedIds.add(resolvedId);
} else {
- String computedId = computeClientId(facesContext, topMatch,
fullId);
- resolvedIds.add(computedId);
+ resolvedIds.addAll(computeClientIds(facesContext, topMatch,
fullId));
}
}
}
Modified:
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolverNode.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolverNode.java 2011-01-19
18:17:35 UTC (rev 21097)
+++
trunk/core/impl/src/main/java/org/richfaces/context/ComponentIdResolverNode.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -82,7 +82,7 @@
public void addFullId(String fullId) {
if (fullIds == null) {
- fullIds = new HashSet<String>();
+ fullIds = new HashSet<String>(2);
}
fullIds.add(fullId);
Modified:
trunk/core/impl/src/main/java/org/richfaces/context/RowsFunctionContextCallback.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/context/RowsFunctionContextCallback.java 2011-01-19
18:17:35 UTC (rev 21097)
+++
trunk/core/impl/src/main/java/org/richfaces/context/RowsFunctionContextCallback.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -49,6 +49,10 @@
Collection<?> keys = (Collection<?>)
context.getApplication().evaluateExpressionGet(context, "#{" + image +
"}", Object.class);
+ if (keys == null) {
+ return;
+ }
+
for (Object key : keys) {
String convertedKey;
Modified:
trunk/core/impl/src/test/java/org/richfaces/context/ComponentIdResolverTest.java
===================================================================
---
trunk/core/impl/src/test/java/org/richfaces/context/ComponentIdResolverTest.java 2011-01-19
18:17:35 UTC (rev 21097)
+++
trunk/core/impl/src/test/java/org/richfaces/context/ComponentIdResolverTest.java 2011-01-19
18:17:40 UTC (rev 21098)
@@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@@ -82,6 +83,8 @@
facesContext = FacesContext.getCurrentInstance();
viewRoot = facesContext.getViewRoot();
+ facesContext.getExternalContext().getRequestMap().put("one",
Arrays.asList(1));
+
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
ViewDeclarationLanguage vdl =
viewHandler.getViewDeclarationLanguage(facesContext, viewRoot.getViewId());
vdl.buildView(facesContext, viewRoot);
@@ -144,38 +147,38 @@
}
@Test
- public void testFindByWildcardId() throws Exception {
+ public void testFindByRowsId() throws Exception {
ComponentIdResolver resolver = createComponentIdResolver();
- resolver.addId("table:@rows(inputKeys):input");
+ resolver.addId("table:@rows(one):input");
resolver.resolve(evaluateComponentExpression("#{testBean.table}"));
Set<String> resolvedIds = resolver.getResolvedIds();
- assertEquals(asSet("form:table:@rows(inputKeys):input"), resolvedIds);
+ assertEquals(asSet("form:table:1:input"), resolvedIds);
}
@Test
public void testFindByMetaComponentId() throws Exception {
ComponentIdResolver resolver = createComponentIdResolver();
resolver.addId("input@text");
- resolver.addId("table:@rows(headerKeys):header@head");
- resolver.addId("table:@rows(footerKeys):header@footer");
+ resolver.addId("table:@rows(one):header@head");
+ resolver.addId("table:@rows(one):header@footer");
resolver.resolve(viewRoot);
Set<String> resolvedIds = resolver.getResolvedIds();
- assertEquals(asSet("form:table:@rows(footerKeys):header@footer",
"form:table:@rows(headerKeys):header@head", "form:table:input@text"),
resolvedIds);
+ assertEquals(asSet("form:table:1:header@footer",
"form:table:1:header@head", "form:table:input@text"), resolvedIds);
}
@Test
public void testFindWithNoParentContainer() throws Exception {
ComponentIdResolver resolver = createComponentIdResolver();
- resolver.addId("form:table:[*]:column");
+ resolver.addId("form:table:@rows(one):column");
resolver.resolve(evaluateComponentExpression("#{testBean.table}"));
Set<String> resolvedIds = resolver.getResolvedIds();
- assertEquals(asSet("form:table:[*]:column"), resolvedIds);
+ assertEquals(asSet("form:table:1:column"), resolvedIds);
}
@Test
@@ -231,12 +234,12 @@
ComponentIdResolver resolver = createComponentIdResolver();
resolver.addId(":form:table:input");
resolver.addId(":form:table:column@head");
- resolver.addId(":form:table:[1]:column");
+ resolver.addId(":form:table:@rows(one):column");
resolver.resolve(viewRoot);
Set<String> resolvedIds = resolver.getResolvedIds();
- assertEquals(asSet("form:table:input",
"form:table:column@head", "form:table:[1]:column"), resolvedIds);
+ assertEquals(asSet("form:table:input",
"form:table:column@head", "form:table:1:column"), resolvedIds);
}
@Test