KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.lang.StringUtils;
20 import org.apache.commons.lang.math.NumberUtils;
21
22 /**
23  * Get a specified substring from a larger String based
24  * on the start index and end-before index in the larger
25  * String.
26  *
27  * <dl>
28  * <dt>start</dt><dd>
29  * Start index.
30  * Required.
31  * </dd>
32  * <dt>end</dt><dd>
33  * Index to end before.
34  * Required.
35  * </dd>
36  * </dl>
37  *
38  * @author bayard@generationjava.com
39  */

40 public class SubstringTag extends StringTagSupport {
41
42     private String JavaDoc end;
43     private String JavaDoc start;
44
45     public SubstringTag() {
46         super();
47     }
48
49     /**
50      * Get the start property
51      *
52      * @return String property
53      */

54     public String JavaDoc getStart() {
55         return this.start;
56     }
57
58     /**
59      * Set the start property
60      *
61      * @param start String property
62      */

63     public void setStart(String JavaDoc start) {
64         this.start = start;
65     }
66
67
68     /**
69      * Get the end property
70      *
71      * @return String property
72      */

73     public String JavaDoc getEnd() {
74         return this.end;
75     }
76
77     /**
78      * Set the end property
79      *
80      * @param end String property
81      */

82     public void setEnd(String JavaDoc end) {
83         this.end = end;
84     }
85
86
87
88     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
89         if(end == null) {
90             return StringUtils.substring(text, NumberUtils.stringToInt(start) );
91         } else {
92             return StringUtils.substring(text, NumberUtils.stringToInt(start), NumberUtils.stringToInt(end) );
93         }
94     }
95
96     public void initAttributes() {
97
98         this.start = "0";
99
100         this.end = null;
101
102     }
103
104
105 }
106
Popular Tags