KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Gets 'n' characters from the inside of a string.
24  *
25  * <dl>
26  * <dt>count</dt><dd>
27  * Size of characters to get.
28  * Required.
29  * </dd>
30  * <dt>start</dt><dd>
31  * Index to start from.
32  * Default is to use the central point - count/2.
33  * </dd>
34  * </dl>
35  *
36  * @author bayard@generationjava.com
37  */

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

52     public String JavaDoc getCount() {
53         return this.count;
54     }
55
56     /**
57      * Set the count property
58      *
59      * @param count String property
60      */

61     public void setCount(String JavaDoc count) {
62         this.count = count;
63     }
64
65     /**
66      * Get the start property
67      *
68      * @return String property
69      */

70     public String JavaDoc getStart() {
71         return this.start;
72     }
73
74     /**
75      * Set the start property
76      *
77      * @param start String property
78      */

79     public void setStart(String JavaDoc start) {
80         this.start = start;
81     }
82
83
84     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
85         int st = NumberUtils.stringToInt(start);
86         int ct = NumberUtils.stringToInt(count);
87         if(st == -1) {
88             st = (text.length() - ct)/2;
89         }
90         return StringUtils.mid(text, st, ct);
91     }
92
93     public void initAttributes() {
94         this.count = "0";
95         this.start = "-1";
96     }
97
98
99 }
100
Popular Tags