001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.jelly.JellyContext;
023: import org.apache.commons.jelly.JellyException;
024: import org.apache.commons.jelly.JellyTagException;
025: import org.apache.commons.jelly.Script;
026: import org.apache.commons.jelly.XMLOutput;
027:
028: /** <p><code>ScriptBlock</code> a block of scripts.</p>
029: *
030: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
031: * @version $Revision: 155420 $
032: */
033: public class ScriptBlock implements Script {
034:
035: /** The list of scripts */
036: private List list = new ArrayList();
037:
038: /**
039: * Create a new instance.
040: */
041: public ScriptBlock() {
042: }
043:
044: /**
045: * @see Object#toString()
046: */
047: public String toString() {
048: return super .toString() + "[scripts=" + list + "]";
049: }
050:
051: /** Add a new script to the end of this block */
052: public void addScript(Script script) {
053: list.add(script);
054: }
055:
056: /** Removes a script from this block */
057: public void removeScript(Script script) {
058: list.remove(script);
059: }
060:
061: /**
062: * Gets the child scripts that make up this block. This list is live
063: * so that it can be modified if requried
064: */
065: public List getScriptList() {
066: return list;
067: }
068:
069: // Script interface
070: //-------------------------------------------------------------------------
071: public Script compile() throws JellyException {
072: int size = list.size();
073: if (size == 1) {
074: Script script = (Script) list.get(0);
075: return script.compile();
076: }
077: // now compile children
078: for (int i = 0; i < size; i++) {
079: Script script = (Script) list.get(i);
080: list.set(i, script.compile());
081: }
082: return this ;
083: }
084:
085: /** Evaluates the body of a tag */
086: public void run(JellyContext context, XMLOutput output)
087: throws JellyTagException {
088: /*
089: for (int i = 0, size = scripts.length; i < size; i++) {
090: Script script = scripts[i];
091: script.run(context, output);
092: }
093: */
094: for (Iterator iter = list.iterator(); iter.hasNext();) {
095: Script script = (Script) iter.next();
096: script.run(context, output);
097: }
098: }
099:
100: /**
101: * Trim the body of the script.
102: * In this case, trim all elements, removing any that are empty text.
103: */
104: public void trimWhitespace() {
105: List list = getScriptList();
106: for (int i = list.size() - 1; i >= 0; i--) {
107: Script script = (Script) list.get(i);
108: if (script instanceof TextScript) {
109: TextScript textScript = (TextScript) script;
110: String text = textScript.getText();
111: text = text.trim();
112: if (text.length() == 0) {
113: list.remove(i);
114: } else {
115: textScript.setText(text);
116: }
117: }
118: }
119: }
120: }
|