dion 2004/10/25 12:33:12
Modified: jelly/src/java/org/apache/commons/jelly/impl
ScriptBlock.java CompositeTextScriptBlock.java
jelly/src/java/org/apache/commons/jelly TagSupport.java
Log:
Fix for Jelly-151.
Revision Changes Path
1.16 +29 -1
jakarta-commons/jelly/src/java/org/apache/commons/jelly/impl/ScriptBlock.java
Index: ScriptBlock.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/impl/ScriptBlock.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- ScriptBlock.java 9 Sep 2004 12:26:22 -0000 1.15
+++ ScriptBlock.java 25 Oct 2004 19:33:12 -0000 1.16
@@ -35,9 +35,15 @@
/** The list of scripts */
private List list = new ArrayList();
+ /**
+ * Create a new instance.
+ */
public ScriptBlock() {
}
+ /**
+ * @see Object#toString()
+ */
public String toString() {
return super.toString() + "[scripts=" + list + "]";
}
@@ -87,6 +93,28 @@
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Script script = (Script) iter.next();
script.run(context, output);
+ }
+ }
+
+ /**
+ * Trim the body of the script.
+ * In this case, trim all elements, removing any that are empty text.
+ */
+ public void trimWhitespace() {
+ List list = 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);
+ }
+ }
}
}
}
1.7 +34 -0
jakarta-commons/jelly/src/java/org/apache/commons/jelly/impl/CompositeTextScriptBlock.java
Index: CompositeTextScriptBlock.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/impl/CompositeTextScriptBlock.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- CompositeTextScriptBlock.java 9 Sep 2004 12:26:22 -0000 1.6
+++ CompositeTextScriptBlock.java 25 Oct 2004 19:33:12 -0000 1.7
@@ -15,6 +15,10 @@
*/
package org.apache.commons.jelly.impl;
+import java.util.List;
+
+import org.apache.commons.jelly.Script;
+
/**
* <p><code>CompositeTextScriptBlock</code> represents a text body of a
@@ -26,6 +30,36 @@
*/
public class CompositeTextScriptBlock extends ScriptBlock {
+ /**
+ * Create an instance.
+ */
public CompositeTextScriptBlock() {
}
+
+
+ /**
+ * Trim the body of the script.
+ * In this case, trim the whitespace from the start of the first element
+ * and from the end of the last element.
+ */
+ public void trimWhitespace() {
+ List list = 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();
+ }
+ }
+ }
+ }
+
+
}
1.34 +3 -36
jakarta-commons/jelly/src/java/org/apache/commons/jelly/TagSupport.java
Index: TagSupport.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/TagSupport.java,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- TagSupport.java 9 Sep 2004 12:25:40 -0000 1.33
+++ TagSupport.java 25 Oct 2004 19:33:12 -0000 1.34
@@ -19,7 +19,6 @@
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;
@@ -245,46 +244,14 @@
*/
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();
- }
- }
- }
+ block.trimWhitespace();
}
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);
- }
- }
- }
+ block.trimWhitespace();
}
else if ( body instanceof TextScript ) {
TextScript textScript = (TextScript) body;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]