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.util;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.Script;
20  import org.apache.commons.jelly.impl.CompositeTextScriptBlock;
21  import org.apache.commons.jelly.impl.ScriptBlock;
22  import org.apache.commons.jelly.impl.TextScript;
23  import org.apache.commons.jelly.impl.WeakReferenceWrapperScript;
24  
25  /*** Contains static methods to help tag developers.
26   * @author Hans Gilde
27   *
28   */
29  public class TagUtils {
30      private TagUtils() {
31          
32      }
33  
34      /*** Trims the whitespace from a script and its children.
35       * 
36       */
37      public static void trimScript(Script body) {
38          synchronized(body) {
39              if (body instanceof WeakReferenceWrapperScript) {
40                  WeakReferenceWrapperScript wrrs = (WeakReferenceWrapperScript) body;
41                  try {
42                      wrrs.trimWhitespace();
43                  } catch (JellyTagException e) {
44                      //TODO handle this exception once the Tag interface allows JellyTagException to be thrown
45                      return;
46                  }
47              } else if ( body instanceof CompositeTextScriptBlock ) {
48                  CompositeTextScriptBlock block = (CompositeTextScriptBlock) body;
49                  block.trimWhitespace();
50              }
51              else
52              if ( body instanceof ScriptBlock ) {
53                  ScriptBlock block = (ScriptBlock) body;
54                  block.trimWhitespace();
55              }
56              else if ( body instanceof TextScript ) {
57                  TextScript textScript = (TextScript) body;
58                  textScript.trimWhitespace();
59              }
60          }
61      }
62  
63  }