KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A more intelligent substring. It attempts to cut off a string after
24  * a space, following predefined or user-supplied lower and upper limits,
25  * useful for making short descriptions from long text. Can also strip
26  * HTML, or if not, intelligently close any tags that were left open.
27  * It adds on a user-defined ending.
28  *
29  * <dl>
30  * <dt>lower</dt><dd>
31  * Minimum length to truncate at.
32  * Required.
33  * </dd>
34  * <dt>upper</dt><dd>
35  * Maximum length to truncate at.
36  * Required.
37  * </dd>
38  * <dt>upper</dt><dd>
39  * String to append to end of truncated string.
40  * </dd>
41  * </dl>
42  *
43  * @author timster@mac.com
44  */

45 public class TruncateNicelyTag extends StringTagSupport {
46
47     private String JavaDoc stripMarkup;
48     private String JavaDoc lower;
49     private String JavaDoc upper;
50     private String JavaDoc appendToEnd;
51
52     public TruncateNicelyTag() {
53         super();
54     }
55
56     /**
57      * Get the lower property
58      * @return String lower property
59      */

60     public String JavaDoc getLower() {
61         return this.lower;
62     }
63
64     /**
65      * Set the upper property
66      * @param lower String property
67      */

68     public void setLower(String JavaDoc l) {
69         this.lower = l;
70     }
71
72     /**
73      * Get the upper property
74      * @return String upper property
75      */

76     public String JavaDoc getUpper() {
77         return this.upper;
78     }
79
80     /**
81      * Set the upper property
82      * @param upper String property
83      */

84     public void setUpper(String JavaDoc u) {
85         this.upper = u;
86     }
87
88     public String JavaDoc getAppendToEnd() {
89         return this.appendToEnd;
90     }
91
92     public void setAppendToEnd(String JavaDoc s) {
93         this.appendToEnd = s;
94     }
95
96     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
97                     
98         int l = NumberUtils.stringToInt(lower);
99         int u = NumberUtils.stringToInt(upper);
100     
101         return StringW.truncateNicely(text, l, u, this.appendToEnd);
102     }
103
104     public void initAttributes() {
105
106         this.lower = "10";
107         this.upper = "-1";
108         this.appendToEnd = "...";
109
110     }
111
112 }
113
Popular Tags