KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > string > WordWrapTag


1 /*
2  * Copyright 1999,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.taglibs.string;
17
18 import javax.servlet.jsp.JspException JavaDoc;
19 import org.apache.taglibs.string.util.StringW;
20 import org.apache.commons.lang.math.NumberUtils;
21
22 /**
23  * Word-wrap a String. This involves formatting a long
24  * String to fit within a certain character width of page.
25  * A delimiter may be passed in to put at the end of each
26  * line and a splitting character can be specified for when
27  * a word has to be cut in half.
28  *
29  * <dl>
30  * <dt>delimiter</dt><dd>
31  * Character to put between each line.
32  * Default is a newline character.
33  * </dd>
34  * <dt>width</dt><dd>
35  * Width to word wrap to.
36  * Default is 80.
37  * </dd>
38  * <dt>split</dt><dd>
39  * Character to use when a word has to be split.
40  * Default is a - character.
41  * </dd>
42  * <dt>delimiterInside</dt><dd>
43  * Flag indicating if the delimiter should be included in chunk before length reaches width.
44  * Default is true.
45  * </dd>
46  * </dl>
47  *
48  * @author bayard@generationjava.com
49  */

50 public class WordWrapTag extends StringTagSupport {
51
52     private String JavaDoc delimiter;
53     private String JavaDoc width;
54     private String JavaDoc split;
55     private boolean delimiterInside;
56
57     public WordWrapTag() {
58         super();
59     }
60
61     /**
62      * Get the width property
63      *
64      * @return String property
65      */

66     public String JavaDoc getWidth() {
67         return this.width;
68     }
69
70     /**
71      * Set the width property
72      *
73      * @param width String property
74      */

75     public void setWidth(String JavaDoc width) {
76         this.width = width;
77     }
78
79
80     /**
81      * Get the delimiter property
82      *
83      * @return String property
84      */

85     public String JavaDoc getDelimiter() {
86         return this.delimiter;
87     }
88
89     /**
90      * Set the delimiter property
91      *
92      * @param delimiter String property
93      */

94     public void setDelimiter(String JavaDoc delimiter) {
95         this.delimiter = delimiter;
96     }
97
98
99     /**
100      * Get the split property
101      *
102      * @return String property
103      */

104     public String JavaDoc getSplit() {
105         return this.split;
106     }
107
108     /**
109      * Set the split property
110      *
111      * @param split String property
112      */

113     public void setSplit(String JavaDoc split) {
114         this.split = split;
115     }
116
117     /**
118      * Get the delimiterInside property
119      *
120      * @return delimiterInside property
121      */

122     public boolean getDelimiterInside() {
123         return this.delimiterInside;
124     }
125
126     /**
127      * Set the delimiterInside property
128      *
129      * @param delimiterInside property
130      */

131     public void setDelimiterInside(boolean delimiterInside) {
132         this.delimiterInside = delimiterInside;
133     }
134
135     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
136         return StringW.wordWrap(text, NumberUtils.stringToInt(width), delimiter, split, delimiterInside );
137     }
138
139     public void initAttributes() {
140
141         this.width = "80";
142
143         this.delimiter = "\n";
144
145         this.split = "-";
146
147         this.delimiterInside = true;
148     }
149
150 }
151
Popular Tags