Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 12564 invoked from network); 12 Jun 2002 19:28:17 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 12 Jun 2002 19:28:17 -0000 Received: (qmail 24197 invoked by uid 97); 12 Jun 2002 19:28:20 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 24148 invoked by uid 97); 12 Jun 2002 19:28:19 -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 24137 invoked by uid 97); 12 Jun 2002 19:28:18 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) Date: 12 Jun 2002 19:28:08 -0000 Message-ID: <20020612192808.2739.qmail@icarus.apache.org> From: werken@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz example.jelly X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N werken 2002/06/12 12:28:08 Modified: jelly/src/java/org/apache/commons/jelly TagSupport.java jelly/src/java/org/apache/commons/jelly/tags/ant AntTagLibrary.java jelly/src/java/org/apache/commons/jelly/tags/core ForEachTag.java jelly/src/java/org/apache/commons/jelly/tags/werkz ProjectTag.java WerkzTagSupport.java jelly/src/test/org/apache/commons/jelly/werkz example.jelly Log: * Hacked in pertnear perfect whitespace trimming/handling. Any tag that inherits from TagSupport somewhere along the line now has the following properties: - A 'trim' attribute, taking 'true' or 'false' - If no 'trim' is set, then simply inherit from parent tag, or previous ancestor that has explicitly set a trim attribute. - TagSupport has a ctor that takes a boolean 'trim' parameter so that tags (or entire taglibs) may be marked as text-trimming or not. - The default response, if nothing set through ctor or trim attr is to *not trim*, which I think retains backwards compatibility. Revision Changes Path 1.8 +94 -6 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.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- TagSupport.java 25 May 2002 18:27:21 -0000 1.7 +++ TagSupport.java 12 Jun 2002 19:28:08 -0000 1.8 @@ -61,8 +61,12 @@ */ package org.apache.commons.jelly; +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.

@@ -79,6 +83,9 @@ /** the body of the tag */ protected Script body; /** The current context */ + + protected Boolean shouldTrim; + protected boolean hasTrimmed; protected JellyContext context; @@ -99,6 +106,44 @@ } return null; } + + public TagSupport() + { + } + + public TagSupport(boolean shouldTrim) + { + setTrim( shouldTrim ); + } + + public void setTrim(boolean shouldTrim) + { + if ( shouldTrim ) { + this.shouldTrim = Boolean.TRUE; + } else { + this.shouldTrim = Boolean.FALSE; + } + } + + public boolean getTrim() + { + if ( this.shouldTrim == null ) { + Tag parent = getParent(); + if ( parent == null ) { + return false; + } else { + if ( parent instanceof TagSupport ) { + TagSupport parentSupport = (TagSupport) parent; + + this.shouldTrim = ( parentSupport.getTrim() ? Boolean.TRUE : Boolean.FALSE ); + } else { + this.shouldTrim = Boolean.FALSE; + } + } + } + + return this.shouldTrim.booleanValue(); + } /** @return the parent of this tag */ public Tag getParent() { @@ -112,12 +157,19 @@ /** @return the body of the tag */ public Script getBody() { + if ( getTrim() + && + ! hasTrimmed ) + { + trimBody(); + } 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 */ @@ -149,7 +201,43 @@ protected String getBodyText() throws Exception { // XXX: could maybe optimise this later on by having a pool of buffers StringWriter writer = new StringWriter(); - body.run(context, XMLOutput.createXMLOutput(writer)); + getBody().run(context, XMLOutput.createXMLOutput(writer)); return writer.toString(); + } + + /** + * Find all text nodes inside the top level of this body and + * if they are just whitespace then remove them + */ + protected void trimBody() { // throws Exception { + // System.err.println( "trimBody() " + this.getClass().getName() ); + 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); + // System.err.println( "removeText(" + i + ")" ); + // } + // else { + // System.err.println( "setText(" + text + ")" ); + textScript.setText(text); + // } + } + } + } + else if ( body instanceof TextScript ) { + TextScript textScript = (TextScript) body; + String text = textScript.getText(); + text = text.trim(); + textScript.setText(text); + } + + this.hasTrimmed = true; } } 1.7 +2 -1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java Index: AntTagLibrary.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/ant/AntTagLibrary.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- AntTagLibrary.java 12 Jun 2002 06:07:29 -0000 1.6 +++ AntTagLibrary.java 12 Jun 2002 19:28:08 -0000 1.7 @@ -173,7 +173,8 @@ Task task = (Task) type.newInstance(); task.setProject(project); task.setTaskName(name); - Tag tag = new TaskTag( task ); + TaskTag tag = new TaskTag( task ); + tag.setTrim( true ); return TagScript.newInstance(tag); } 1.12 +6 -6 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ForEachTag.java Index: ForEachTag.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/ForEachTag.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- ForEachTag.java 4 Jun 2002 18:34:33 -0000 1.11 +++ ForEachTag.java 12 Jun 2002 19:28:08 -0000 1.12 @@ -145,6 +145,7 @@ if (indexVar != null) { context.setVariable(indexVar, new Integer(index)); } + // System.err.println( "body=" + getBody() ); getBody().run(context, output); // now we need to move to next index @@ -202,5 +203,4 @@ public void setStep(int step) { this.step = step; } - } 1.6 +2 -1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java Index: ProjectTag.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/ProjectTag.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ProjectTag.java 12 Jun 2002 14:38:57 -0000 1.5 +++ ProjectTag.java 12 Jun 2002 19:28:08 -0000 1.6 @@ -74,12 +74,13 @@ * @author James Strachan * @version $Revision$ */ -public class ProjectTag extends TagSupport { +public class ProjectTag extends WerkzTagSupport { /** the project */ private Project project; public ProjectTag() { + super( true ); } 1.4 +6 -0 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java Index: WerkzTagSupport.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/werkz/WerkzTagSupport.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- WerkzTagSupport.java 12 Jun 2002 14:00:40 -0000 1.3 +++ WerkzTagSupport.java 12 Jun 2002 19:28:08 -0000 1.4 @@ -72,8 +72,14 @@ */ public abstract class WerkzTagSupport extends TagSupport { + public WerkzTagSupport(boolean trim) { + super( trim ); + } + public WerkzTagSupport() { } + + // Implementation methods 1.5 +43 -42 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz/example.jelly Index: example.jelly =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/werkz/example.jelly,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- example.jelly 12 Jun 2002 17:09:41 -0000 1.4 +++ example.jelly 12 Jun 2002 19:28:08 -0000 1.5 @@ -1,42 +1,43 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - ------------------------------------- - jelly+werkz+ant-taskdefs - ------------------------------------- - - begin target [] - ------------------------------------- - - end target [] - ------------------------------------- - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + +--------------------------------------- + | jelly+werkz+ant-taskdefs + +--------------------------------------- + + + + | begin target [] + +---------------------------------------- + + + + | end target [] + +---------------------------------------- + + + + + + -- To unsubscribe, e-mail: For additional commands, e-mail: