1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }