Index: TagSupport.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/TagSupport.java,v retrieving revision 1.28 diff -u -r1.28 TagSupport.java --- TagSupport.java 30 Oct 2003 20:04:48 -0000 1.28 +++ TagSupport.java 1 Jul 2004 14:29:48 -0000 @@ -56,7 +56,7 @@ * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . - * + * * $Id: TagSupport.java,v 1.28 2003/10/30 20:04:48 dion Exp $ */ package org.apache.commons.jelly; @@ -71,7 +71,7 @@ import org.apache.commons.jelly.impl.ScriptBlock; import org.apache.commons.jelly.impl.TextScript; -/**

TagSupport an abstract base class which is useful to +/**

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

* * @author James Strachan @@ -79,29 +79,29 @@ */ public abstract class TagSupport implements Tag { - + /** the parent of this tag */ protected Tag parent; - /** the body of the tag */ + /** the body of the tag */ protected Script body; /** The current context */ protected Boolean shouldTrim; protected boolean hasTrimmed; - + protected JellyContext context; - /** - * Searches up the parent hierarchy from the given tag - * for a Tag of the given type + /** + * Searches up the parent hierarchy from the given tag + * for a Tag of the given type * * @param from the tag to start searching from * @param tagClass the type of the tag to find * @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 + // 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) { @@ -113,8 +113,8 @@ return null; } - /** - * Searches up the parent hierarchy from the given tag + /** + * 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 @@ -131,11 +131,11 @@ } from = from.getParent(); } - return null; + return null; } - /** - * Searches up the parent hierarchy from the given tag + /** + * 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 @@ -146,7 +146,7 @@ public static Tag findAncestorWithClass(Tag from, Class[] tagClasses) { return findAncestorWithClass(from,Arrays.asList(tagClasses)); } - + public TagSupport() { } @@ -155,13 +155,13 @@ } /** - * Sets whether whitespace inside this tag should be trimmed or not. + * Sets whether whitespace inside this tag should be trimmed or not. * Defaults to true so whitespace is trimmed */ public void setTrim(boolean shouldTrim) { if ( shouldTrim ) { this.shouldTrim = Boolean.TRUE; - } + } else { this.shouldTrim = Boolean.FALSE; } @@ -172,13 +172,13 @@ Tag parent = getParent(); if ( parent == null ) { return true; - } + } else { if ( parent instanceof TagSupport ) { TagSupport parentSupport = (TagSupport) parent; this.shouldTrim = ( parentSupport.isTrim() ? Boolean.TRUE : Boolean.FALSE ); - } + } else { this.shouldTrim = Boolean.TRUE; } @@ -187,17 +187,17 @@ return this.shouldTrim.booleanValue(); } - + /** @return the parent of this tag */ public Tag getParent() { return parent; } - + /** Sets the parent of this tag */ public void setParent(Tag parent) { this.parent = parent; } - + /** @return the body of the tag */ public Script getBody() { if (! hasTrimmed) { @@ -208,42 +208,42 @@ } return body; } - + /** Sets the body of the tag */ public void setBody(Script body) { this.body = body; this.hasTrimmed = false; } - + /** @return the context in which the tag will be run */ public JellyContext getContext() { return context; } - + /** Sets the context in which the tag will be run */ public void setContext(JellyContext context) throws JellyTagException { this.context = context; - } - + } + /** * Invokes the body of this tag using the given output */ public void invokeBody(XMLOutput output) throws JellyTagException { getBody().run(context, output); } - + // Implementation methods - //------------------------------------------------------------------------- - /** + //------------------------------------------------------------------------- + /** * 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. + + /** + * 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) */ @@ -251,14 +251,14 @@ return findAncestorWithClass(getParent(),parentClasses); } - /** - * Searches up the parent hierarchy for a Tag of one of the given types. + /** + * 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); } - + /** * Executes the body of the tag and returns the result as a String. * @@ -284,56 +284,57 @@ } - /** - * Find all text nodes inside the top level of this body and + /** + * Find all text nodes inside the top level of this body and * if they are just whitespace then remove them */ - protected void trimBody() { - - // #### should refactor this code into - // #### trimWhitespace() methods on the Script objects - - if ( body instanceof CompositeTextScriptBlock ) { - CompositeTextScriptBlock block = (CompositeTextScriptBlock) body; - List list = block.getScriptList(); - int size = list.size(); - if ( size > 0 ) { - Script script = (Script) list.get(0); - if ( script instanceof TextScript ) { - TextScript textScript = (TextScript) script; - textScript.trimStartWhitespace(); - } - if ( size > 1 ) { - script = (Script) list.get(size - 1); - if ( script instanceof TextScript ) { - TextScript textScript = (TextScript) script; - textScript.trimEndWhitespace(); - } - } - } - } - else - if ( body instanceof ScriptBlock ) { - ScriptBlock block = (ScriptBlock) body; - List list = block.getScriptList(); - for ( int i = list.size() - 1; i >= 0; i-- ) { - Script script = (Script) list.get(i); - if ( script instanceof TextScript ) { - TextScript textScript = (TextScript) script; - String text = textScript.getText(); - text = text.trim(); - if ( text.length() == 0 ) { - list.remove(i); - } - else { - textScript.setText(text); - } - } - } - } - else if ( body instanceof TextScript ) { - TextScript textScript = (TextScript) body; - textScript.trimWhitespace(); - } - } + protected void trimBody() { + synchronized(body) { + // #### should refactor this code into + // #### trimWhitespace() methods on the Script objects + + if ( body instanceof CompositeTextScriptBlock ) { + CompositeTextScriptBlock block = (CompositeTextScriptBlock) body; + List list = block.getScriptList(); + int size = list.size(); + if ( size > 0 ) { + Script script = (Script) list.get(0); + if ( script instanceof TextScript ) { + TextScript textScript = (TextScript) script; + textScript.trimStartWhitespace(); + } + if ( size > 1 ) { + script = (Script) list.get(size - 1); + if ( script instanceof TextScript ) { + TextScript textScript = (TextScript) script; + textScript.trimEndWhitespace(); + } + } + } + } + else + if ( body instanceof ScriptBlock ) { + ScriptBlock block = (ScriptBlock) body; + List list = block.getScriptList(); + for ( int i = list.size() - 1; i >= 0; i-- ) { + Script script = (Script) list.get(i); + if ( script instanceof TextScript ) { + TextScript textScript = (TextScript) script; + String text = textScript.getText(); + text = text.trim(); + if ( text.length() == 0 ) { + list.remove(i); + } + else { + textScript.setText(text); + } + } + } + } + else if ( body instanceof TextScript ) { + TextScript textScript = (TextScript) body; + textScript.trimWhitespace(); + } + } + } }