KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Left pad a string with a particular delimiter to a
24  * specified width. The width includes the size of
25  * the string.
26  *
27  * <dl>
28  * <dt>delimiter</dt><dd>
29  * String to pad the larger String with.
30  * Default is a space character.
31  * </dd>
32  * <dt>width</dt><dd>
33  * Size of larger String.
34  * Required.
35  * </dd>
36  * </dl>
37  *
38  * @author bayard@generationjava.com
39  */

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

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

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

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

82     public void setWidth(String JavaDoc width) {
83         this.width = width;
84     }
85
86
87
88     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
89         return StringUtils.leftPad(text, NumberUtils.stringToInt(width), delimiter);
90     }
91
92     public void initAttributes() {
93
94         this.delimiter = " ";
95
96         this.width = "0";
97
98     }
99
100 }
101
Popular Tags