View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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  /*** <p><code>ScriptBlock</code> a block of scripts.</p>
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155420 $
32    */
33  public class ScriptBlock implements Script {
34  
35      /*** The list of scripts */
36      private List list = new ArrayList();
37  
38      /***
39       * Create a new instance.
40       */
41      public ScriptBlock() {
42      }
43  
44      /***
45       * @see Object#toString()
46       */
47      public String toString() {
48          return super.toString() + "[scripts=" + list + "]";
49      }
50  
51      /*** Add a new script to the end of this block */
52      public void addScript(Script script) {
53          list.add(script);
54      }
55  
56      /*** Removes a script from this block */
57      public void removeScript(Script script) {
58          list.remove(script);
59      }
60  
61      /***
62       * Gets the child scripts that make up this block. This list is live
63       * so that it can be modified if requried
64       */
65      public List getScriptList() {
66          return list;
67      }
68  
69      // Script interface
70      //-------------------------------------------------------------------------
71      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          // now compile children
78          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      /*** Evaluates the body of a tag */
86      public void run(JellyContext context, XMLOutput output) throws JellyTagException {
87  /*
88          for (int i = 0, size = scripts.length; i < size; i++) {
89              Script script = scripts[i];
90              script.run(context, output);
91          }
92  */
93          for (Iterator iter = list.iterator(); iter.hasNext(); ) {
94              Script script = (Script) iter.next();
95              script.run(context, output);
96          }
97      }
98      
99      /***
100      * Trim the body of the script.
101      * In this case, trim all elements, removing any that are empty text.
102      */
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                 else {
115                     textScript.setText(text);
116                 }
117             }
118         }
119     }
120 }