Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 16215 invoked from network); 10 Dec 2002 01:30:34 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 10 Dec 2002 01:30:34 -0000 Received: (qmail 8257 invoked by uid 97); 10 Dec 2002 01:31:45 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 8217 invoked by uid 97); 10 Dec 2002 01:31:45 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 8176 invoked by uid 97); 10 Dec 2002 01:31:44 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 10 Dec 2002 01:30:26 -0000 Message-ID: <20021210013026.66424.qmail@icarus.apache.org> From: rwaldhoff@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly TagSupport.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N 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) 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; - /**

TagSupport an abstract base class which is useful to * inherit from if developing your own tag.

* @@ -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: For additional commands, e-mail: