001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.impl;
017:
018: import org.apache.commons.jelly.JellyContext;
019: import org.apache.commons.jelly.JellyTagException;
020: import org.apache.commons.jelly.Script;
021: import org.apache.commons.jelly.XMLOutput;
022:
023: import org.xml.sax.SAXException;
024:
025: /** <p><code>TextScript</code> outputs some static text.</p>
026: *
027: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
028: * @version $Revision: 155420 $
029: */
030: public class TextScript implements Script {
031:
032: /** the text output by this script */
033: private String text;
034:
035: public TextScript() {
036: }
037:
038: public TextScript(String text) {
039: this .text = text;
040: }
041:
042: public String toString() {
043: return super .toString() + "[text=" + text + "]";
044: }
045:
046: /**
047: * Trims whitespace from the start and end of the text in this script
048: */
049: public void trimWhitespace() {
050: this .text = text.trim();
051: }
052:
053: /**
054: * Trims whitespace from the start of the text
055: */
056: public void trimStartWhitespace() {
057: int index = 0;
058: for (int length = text.length(); index < length; index++) {
059: char ch = text.charAt(index);
060: if (!Character.isWhitespace(ch)) {
061: break;
062: }
063: }
064: if (index > 0) {
065: this .text = text.substring(index);
066: }
067: }
068:
069: /**
070: * Trims whitespace from the end of the text
071: */
072: public void trimEndWhitespace() {
073: int index = text.length();
074: while (--index >= 0) {
075: char ch = text.charAt(index);
076: if (!Character.isWhitespace(ch)) {
077: break;
078: }
079: }
080: index++;
081: if (index < text.length()) {
082: this .text = text.substring(0, index);
083: }
084: }
085:
086: /** @return the text output by this script */
087: public String getText() {
088: return text;
089: }
090:
091: /** Sets the text output by this script */
092: public void setText(String text) {
093: this .text = text;
094: }
095:
096: // Script interface
097: //-------------------------------------------------------------------------
098: public Script compile() {
099: return this ;
100: }
101:
102: /** Evaluates the body of a tag */
103: public void run(JellyContext context, XMLOutput output)
104: throws JellyTagException {
105: if (text != null) {
106: try {
107: output.write(text);
108: } catch (SAXException e) {
109: throw new JellyTagException(
110: "could not write to XMLOutput", e);
111: }
112: }
113: }
114: }
|