You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2002/12/10 02:30:26 UTC
cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly TagSupport.java
rwaldhoff 2002/12/09 17:30:26
Modified: jelly/src/java/org/apache/commons/jelly TagSupport.java
Log:
add findAncestorWithClass([Tag],Class[]) and
findAncestorWithClass([Tag],Collection<Class>) methods
for finding an ancestor that matches at least one of the set
Revision Changes Path
1.20 +66 -10 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java
Index: TagSupport.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/TagSupport.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- TagSupport.java 30 Oct 2002 19:16:26 -0000 1.19
+++ TagSupport.java 10 Dec 2002 01:30:26 -0000 1.20
@@ -61,14 +61,16 @@
*/
package org.apache.commons.jelly;
+import java.io.StringWriter;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
import org.apache.commons.jelly.impl.CompositeTextScriptBlock;
import org.apache.commons.jelly.impl.ScriptBlock;
import org.apache.commons.jelly.impl.TextScript;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.List;
-
/** <p><code>TagSupport</code> an abstract base class which is useful to
* inherit from if developing your own tag.</p>
*
@@ -99,6 +101,9 @@
* @return the tag of the given type or null if it could not be found
*/
public static Tag findAncestorWithClass(Tag from, Class tagClass) {
+ // we could implement this as
+ // return findAncestorWithClass(from,Collections.singleton(tagClass));
+ // but this is so simple let's save the object creation for now
while (from != null) {
if (tagClass.isInstance(from)) {
return from;
@@ -108,6 +113,40 @@
return null;
}
+ /**
+ * Searches up the parent hierarchy from the given tag
+ * for a Tag matching one or more of given types.
+ *
+ * @param from the tag to start searching from
+ * @param tagClasses a Collection of Class types that might match
+ * @return the tag of the given type or null if it could not be found
+ */
+ public static Tag findAncestorWithClass(Tag from, Collection tagClasses) {
+ while (from != null) {
+ for(Iterator iter = tagClasses.iterator();iter.hasNext();) {
+ Class klass = (Class)(iter.next());
+ if (klass.isInstance(from)) {
+ return from;
+ }
+ }
+ from = from.getParent();
+ }
+ return null;
+ }
+
+ /**
+ * Searches up the parent hierarchy from the given tag
+ * for a Tag matching one or more of given types.
+ *
+ * @param from the tag to start searching from
+ * @param tagClasses an array of types that might match
+ * @return the tag of the given type or null if it could not be found
+ * @see #findAncestorWithClass(Tag,Collection)
+ */
+ public static Tag findAncestorWithClass(Tag from, Class[] tagClasses) {
+ return findAncestorWithClass(from,Arrays.asList(tagClasses));
+ }
+
public TagSupport() {
}
@@ -196,11 +235,28 @@
// Implementation methods
//-------------------------------------------------------------------------
/**
- * Searches up the parent hierarchy for a Tag of the given type
+ * Searches up the parent hierarchy for a Tag of the given type.
* @return the tag of the given type or null if it could not be found
*/
protected Tag findAncestorWithClass(Class parentClass) {
return findAncestorWithClass(getParent(), parentClass);
+ }
+
+ /**
+ * Searches up the parent hierarchy for a Tag of one of the given types.
+ * @return the tag of the given type or null if it could not be found
+ * @see #findAncestorWithClass(Collection)
+ */
+ protected Tag findAncestorWithClass(Class[] parentClasses) {
+ return findAncestorWithClass(getParent(),parentClasses);
+ }
+
+ /**
+ * Searches up the parent hierarchy for a Tag of one of the given types.
+ * @return the tag of the given type or null if it could not be found
+ */
+ protected Tag findAncestorWithClass(Collection parentClasses) {
+ return findAncestorWithClass(getParent(),parentClasses);
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>