You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by dr...@apache.org on 2010/11/10 22:41:50 UTC
svn commit: r1033730 -
/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java
Author: drobiazko
Date: Wed Nov 10 21:41:50 2010
New Revision: 1033730
URL: http://svn.apache.org/viewvc?rev=1033730&view=rev
Log:
TAP5-1342: Provide convenient methods for Element and Document to find elements
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java?rev=1033730&r1=1033729&r2=1033730&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/dom/Element.java Wed Nov 10 21:41:50 2010
@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.apache.tapestry5.func.Predicate;
import org.apache.tapestry5.internal.TapestryInternalUtils;
import org.apache.tapestry5.internal.util.PrintOutCollector;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -410,15 +411,56 @@ public final class Element extends Node
}
/**
- * Tries to find an element under this element (including itself) whose id is specified. Performs a width-first
+ * Tries to find an element under this element (including itself) whose id is specified.
+ * Performs a width-first
* search of the document tree.
- *
- * @param id the value of the id attribute of the element being looked for
+ *
+ * @param id
+ * the value of the id attribute of the element being looked for
+ * @return the element if found. null if not found.
+ */
+ public Element getElementById(final String id)
+ {
+ return getElementByAttributeValue("id", id);
+ }
+
+ /**
+ * Tries to find an element under this element (including itself) whose given attribute has a given value.
+ *
+ * @since 5.2.3
+ *
+ * @param attributeName the name of the attribute of the element being looked for
+ * @param attributeValue
+ * the value of the attribute of the element being looked for
+ * @return the element if found. null if not found.
+ */
+ public Element getElementByAttributeValue(final String attributeName, final String attributeValue)
+ {
+ assert attributeName != null;
+ assert attributeValue != null;
+
+ return getElement(new Predicate<Element>()
+ {
+
+ @Override
+ public boolean accept(Element e)
+ {
+ String elementId = e.getAttribute(attributeName);
+ return attributeValue.equals(elementId);
+ }
+ });
+ }
+
+ /**
+ * Tries to find an element under this element (including itself) accepted by the given predicate.
+ *
+ * @since 5.2.3
+ *
+ * @param predicate Predicate to accept the element
* @return the element if found. null if not found.
*/
- public Element getElementById(String id)
+ public Element getElement(Predicate<Element> predicate)
{
- assert id != null;
LinkedList<Element> queue = CollectionFactory.newLinkedList();
queue.add(this);
@@ -427,9 +469,7 @@ public final class Element extends Node
{
Element e = queue.removeFirst();
- String elementId = e.getAttribute("id");
-
- if (id.equals(elementId)) return e;
+ if (predicate.accept(e)) return e;
for (Element child : e.childElements())
{