KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.lang.StringUtils;
19 import org.apache.commons.lang.math.NumberUtils;
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 /**
23  * Replace a specified substring with another string.
24  * A number of times may be specified to say how many times
25  * to replace the substring.
26  *
27  * <dl>
28  * <dt>replace</dt><dd>
29  * Value to find to replace.
30  * Required.
31  * </dd>
32  * <dt>with</dt><dd>
33  * Value to replace with.
34  * Required.
35  * </dd>
36  * <dt>count</dt><dd>
37  * Number of times to replace.
38  * Default is all matches.
39  * </dd>
40  * <dt>newlineToken</dt><dd>
41  * Token to use as a newline.
42  * This gets around the pain of haivng a literal newline in the jsp.
43  * By default this is ignored.
44  * </dd>
45  * <dt>carriageToken</dt><dd>
46  * Token to use as a carriage return.
47  * This gets around the pain of haivng a literal carriage return
48  * in the jsp.
49  * By default this is ignored.
50  * </dd>
51  * </dl>
52  *
53  * @author bayard@generationjava.com
54  */

55 public class ReplaceTag extends StringTagSupport {
56
57     private String JavaDoc count;
58     private String JavaDoc replace;
59     private String JavaDoc with;
60     private String JavaDoc newlineToken;
61     private String JavaDoc carriageToken;
62
63     public ReplaceTag() {
64         super();
65     }
66
67     /**
68      * Get the replace property
69      *
70      * @return String property
71      */

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

81     public void setReplace(String JavaDoc replace) {
82         this.replace = replace;
83     }
84
85     /**
86      * Get the newlineToken property
87      *
88      * @return String property
89      */

90     public String JavaDoc getNewlineToken() {
91         return this.newlineToken;
92     }
93
94     /**
95      * Set the newlineToken property
96      *
97      * @param newlineToken String property
98      */

99     public void setNewlineToken(String JavaDoc newlineToken) {
100         this.newlineToken = newlineToken;
101     }
102
103     /**
104      * Get the carriageToken property
105      *
106      * @return String property
107      */

108     public String JavaDoc getCarriageToken() {
109         return this.carriageToken;
110     }
111
112     /**
113      * Set the carriageToken property
114      *
115      * @param carriageToken String property
116      */

117     public void setCarriageToken(String JavaDoc carriageToken) {
118         this.carriageToken = carriageToken;
119     }
120
121
122     /**
123      * Get the with property
124      *
125      * @return String property
126      */

127     public String JavaDoc getWith() {
128         return this.with;
129     }
130
131     /**
132      * Set the with property
133      *
134      * @param with String property
135      */

136     public void setWith(String JavaDoc with) {
137         this.with = with;
138     }
139
140
141     /**
142      * Get the count property
143      *
144      * @return String property
145      */

146     public String JavaDoc getCount() {
147         return this.count;
148     }
149
150     /**
151      * Set the count property
152      *
153      * @param count String property
154      */

155     public void setCount(String JavaDoc count) {
156         this.count = count;
157     }
158
159
160
161     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
162         String JavaDoc repl = replace;
163         String JavaDoc wit = with;
164         if(this.carriageToken != null) {
165             repl = StringUtils.replace(replace, this.carriageToken, "\r");
166             wit = StringUtils.replace(with, this.carriageToken, "\r");
167         }
168         if(this.newlineToken != null) {
169             repl = StringUtils.replace(replace, this.newlineToken, "\n");
170             wit = StringUtils.replace(with, this.newlineToken, "\n");
171         }
172         int ct = -1;
173         if(count != null) {
174             ct = NumberUtils.stringToInt(count);
175         }
176         return StringUtils.replace(text, repl, wit, ct);
177     }
178
179     public void initAttributes() {
180
181         this.replace = null;
182
183         this.with = null;
184
185         this.count = null;
186
187         this.newlineToken = null;
188         this.carriageToken = null;
189
190     }
191
192 }
193
Popular Tags