You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2009/10/06 15:27:14 UTC
svn commit: r822265 - in /tiles/framework/trunk/tiles-core/src:
main/java/org/apache/tiles/definition/pattern/PatternUtil.java
test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java
Author: apetrelli
Date: Tue Oct 6 13:27:14 2009
New Revision: 822265
URL: http://svn.apache.org/viewvc?rev=822265&view=rev
Log:
TILES-474
Added wildcards support in attributes children of list attributes.
Modified:
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java
tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java
Modified: tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java?rev=822265&r1=822264&r2=822265&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java (original)
+++ tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/definition/pattern/PatternUtil.java Tue Oct 6 13:27:14 2009
@@ -22,12 +22,14 @@
package org.apache.tiles.definition.pattern;
import java.text.MessageFormat;
+import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.tiles.Attribute;
import org.apache.tiles.Definition;
import org.apache.tiles.Expression;
+import org.apache.tiles.ListAttribute;
/**
* Utilities for pattern matching and substitution.
@@ -107,7 +109,28 @@
*/
private static Attribute replaceVarsInAttribute(Attribute attr,
Object... vars) {
- Attribute nuattr = new Attribute();
+ Attribute nuattr;
+ if (attr instanceof ListAttribute) {
+ nuattr = replaceVarsInListAttribute((ListAttribute) attr, vars);
+ } else {
+ nuattr = replaceVarsInSimpleAttribute(attr, vars);
+ }
+ return nuattr;
+ }
+
+ /**
+ * Replaces variables into a simple (not list) attribute.
+ *
+ * @param attr The attribute to be used as a basis, containing placeholders
+ * for variables.
+ * @param vars The variables to replace.
+ * @return A new instance of an attribute, whose properties have been
+ * replaced with variables' values.
+ */
+ private static Attribute replaceVarsInSimpleAttribute(Attribute attr,
+ Object... vars) {
+ Attribute nuattr;
+ nuattr = new Attribute();
nuattr.setRole(replace(attr.getRole(), vars));
nuattr.setRenderer(attr.getRenderer());
@@ -125,6 +148,36 @@
}
/**
+ * Replaces variables into a list attribute.
+ *
+ * @param listAttr The attribute to be used as a basis, containing attributes
+ * that may contain placeholders for variables.
+ * @param vars The variables to replace.
+ * @return A new instance of an attribute, whose properties have been
+ * replaced with variables' values.
+ */
+ @SuppressWarnings("unchecked")
+ private static Attribute replaceVarsInListAttribute(ListAttribute listAttr,
+ Object... vars) {
+ Attribute nuattr;
+ ListAttribute nuListAttr = new ListAttribute();
+ nuListAttr.setInherit(listAttr.isInherit());
+ List<Object> nuItems = (List<Object>) nuListAttr.getValue();
+ for (Object item : (List<Object>) listAttr.getValue()) {
+ if (item instanceof Attribute) {
+ Attribute child = (Attribute) item;
+ child = replaceVarsInAttribute(child, vars);
+ nuItems.add(child);
+ } else {
+ // Seems improbable, but APIs permit it.
+ nuItems.add(item);
+ }
+ }
+ nuattr = nuListAttr;
+ return nuattr;
+ }
+
+ /**
* Replaces a string with placeholders using values of a variable map.
*
* @param st The string to replace.
Modified: tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java?rev=822265&r1=822264&r2=822265&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java (original)
+++ tiles/framework/trunk/tiles-core/src/test/java/org/apache/tiles/definition/pattern/PatternUtilTest.java Tue Oct 6 13:27:14 2009
@@ -24,10 +24,12 @@
import static org.junit.Assert.*;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.apache.tiles.Attribute;
import org.apache.tiles.Definition;
+import org.apache.tiles.ListAttribute;
import org.junit.Test;
/**
@@ -39,6 +41,11 @@
public class PatternUtilTest {
/**
+ * The size of the list in the main list attribute.
+ */
+ private static final int LIST_ATTRIBUTE_SIZE = 3;
+
+ /**
* Test method for
* {@link PatternUtil#replacePlaceholders(Definition, String, Object[])}.
*/
@@ -100,4 +107,46 @@
attribute = nudef.getAttribute("attrib2");
assertEquals("valuevalue2value3", attribute.getValue());
}
+
+ /**
+ * Test method for
+ * {@link PatternUtil#replacePlaceholders(Definition, String, Object[])}.
+ */
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testReplacePlaceholdersListAttribute() {
+ Map<String, Attribute> attributes = new HashMap<String, Attribute>();
+ ListAttribute listAttribute = new ListAttribute();
+ ListAttribute internalListAttribute = new ListAttribute();
+ listAttribute.setInherit(true);
+ attributes.put("myList", listAttribute);
+ listAttribute.add(new Attribute("value{2}"));
+ listAttribute.add(new Attribute("value{2}{3}"));
+ listAttribute.add(internalListAttribute);
+ internalListAttribute.add(new Attribute("secondvalue{2}"));
+ internalListAttribute.add(new Attribute("secondvalue{2}{3}"));
+ Definition definition = new Definition("definitionName", new Attribute(
+ "template{1}"), attributes);
+ Definition nudef = PatternUtil.replacePlaceholders(definition, "nudef",
+ "value0", "value1", "value2", "value3");
+ assertEquals("nudef", nudef.getName());
+ Attribute attribute = nudef.getTemplateAttribute();
+ assertEquals("templatevalue1", attribute.getValue());
+ ListAttribute nuListAttribute = (ListAttribute) nudef.getAttribute("myList");
+ assertTrue(nuListAttribute.isInherit());
+ List<Attribute> list = (List<Attribute>) nuListAttribute.getValue();
+ assertEquals(LIST_ATTRIBUTE_SIZE, list.size());
+ attribute = list.get(0);
+ assertEquals("valuevalue2", attribute.getValue());
+ attribute = list.get(1);
+ assertEquals("valuevalue2value3", attribute.getValue());
+ ListAttribute evaluatedListAttribute = (ListAttribute) list.get(2);
+ assertFalse(evaluatedListAttribute.isInherit());
+ list = (List<Attribute>) evaluatedListAttribute.getValue();
+ assertEquals(2, list.size());
+ attribute = list.get(0);
+ assertEquals("secondvaluevalue2", attribute.getValue());
+ attribute = list.get(1);
+ assertEquals("secondvaluevalue2value3", attribute.getValue());
+ }
}