KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > impl > TextScript


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 JavaDoc;
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 JavaDoc text;
34
35     public TextScript() {
36     }
37
38     public TextScript(String JavaDoc text) {
39         this.text = text;
40     }
41
42     public String JavaDoc toString() {
43         return super.toString() + "[text=" + text + "]";
44     }
45
46     /**
47      * Trims whitespace if this is ignorable whitespace.
48      */

49     public void trimWhitespace() {
50         if(text.trim().length()==0)
51             this.text = "";
52     }
53
54     /** @return the text output by this script */
55     public String JavaDoc getText() {
56         return text;
57     }
58
59     /** Sets the text output by this script */
60     public void setText(String JavaDoc text) {
61         this.text = text;
62     }
63
64     // Script interface
65
//-------------------------------------------------------------------------
66
public Script compile() {
67         return this;
68     }
69
70     /** Evaluates the body of a tag */
71     public void run(JellyContext context, XMLOutput output) throws JellyTagException {
72         if ( text != null ) {
73             try {
74               output.write(text);
75             } catch (SAXException JavaDoc e) {
76                 throw new JellyTagException("could not write to XMLOutput",e);
77             }
78         }
79     }
80 }
81
Popular Tags