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 org.apache.commons.jelly.JellyContext;
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.Script;
21  import org.apache.commons.jelly.XMLOutput;
22  
23  import org.xml.sax.SAXException;
24  
25  /*** <p><code>TextScript</code> outputs some static text.</p>
26    *
27    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28    * @version $Revision: 155420 $
29    */
30  public class TextScript implements Script {
31  
32      /*** the text output by this script */
33      private String text;
34  
35      public TextScript() {
36      }
37  
38      public TextScript(String text) {
39          this.text = text;
40      }
41  
42      public String toString() {
43          return super.toString() + "[text=" + text + "]";
44      }
45  
46      /***
47       * Trims whitespace from the start and end of the text in this script
48       */
49      public void trimWhitespace() {
50          this.text = text.trim();
51      }
52  
53      /***
54       * Trims whitespace from the start of the text
55       */
56      public void trimStartWhitespace() {
57          int index = 0;
58          for ( int length = text.length(); index < length; index++ ) {
59              char ch = text.charAt(index);
60              if (!Character.isWhitespace(ch)) {
61                  break;
62              }
63          }
64          if ( index > 0 ) {
65              this.text = text.substring(index);
66          }
67      }
68  
69      /***
70       * Trims whitespace from the end of the text
71       */
72      public void trimEndWhitespace() {
73          int index = text.length();
74          while (--index >= 0) {
75              char ch = text.charAt(index);
76              if (!Character.isWhitespace(ch)) {
77                  break;
78              }
79          }
80          index++;
81          if ( index < text.length() ) {
82              this.text = text.substring(0,index);
83          }
84      }
85  
86      /*** @return the text output by this script */
87      public String getText() {
88          return text;
89      }
90  
91      /*** Sets the text output by this script */
92      public void setText(String text) {
93          this.text = text;
94      }
95  
96      // Script interface
97      //-------------------------------------------------------------------------
98      public Script compile() {
99          return this;
100     }
101 
102     /*** Evaluates the body of a tag */
103     public void run(JellyContext context, XMLOutput output) throws JellyTagException {
104         if ( text != null ) {
105             try {
106               output.write(text);
107             } catch (SAXException e) {
108                 throw new JellyTagException("could not write to XMLOutput",e);
109             }
110         }
111     }
112 }