1 16 package org.apache.commons.jelly.impl; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.apache.commons.jelly.JellyContext; 23 import org.apache.commons.jelly.JellyException; 24 import org.apache.commons.jelly.JellyTagException; 25 import org.apache.commons.jelly.Script; 26 import org.apache.commons.jelly.XMLOutput; 27 28 33 public class ScriptBlock implements Script { 34 35 36 private List list = new ArrayList (); 37 38 41 public ScriptBlock() { 42 } 43 44 47 public String toString() { 48 return super.toString() + "[scripts=" + list + "]"; 49 } 50 51 52 public void addScript(Script script) { 53 list.add(script); 54 } 55 56 57 public void removeScript(Script script) { 58 list.remove(script); 59 } 60 61 65 public List getScriptList() { 66 return list; 67 } 68 69 public Script compile() throws JellyException { 72 int size = list.size(); 73 if (size == 1) { 74 Script script = (Script) list.get(0); 75 return script.compile(); 76 } 77 for (int i = 0; i < size; i++) { 79 Script script = (Script) list.get(i); 80 list.set(i, script.compile()); 81 } 82 return this; 83 } 84 85 86 public void run(JellyContext context, XMLOutput output) throws JellyTagException { 87 93 for (Iterator iter = list.iterator(); iter.hasNext(); ) { 94 Script script = (Script) iter.next(); 95 script.run(context, output); 96 } 97 } 98 99 103 public void trimWhitespace() { 104 List list = getScriptList(); 105 for ( int i = list.size() - 1; i >= 0; i-- ) { 106 Script script = (Script) list.get(i); 107 if ( script instanceof TextScript ) { 108 TextScript textScript = (TextScript) script; 109 String text = textScript.getText(); 110 text = text.trim(); 111 if ( text.length() == 0 ) { 112 list.remove(i); 113 } 114 } 115 } 116 } 117 } 118 | Popular Tags |