KickJava   Java API By Example, From Geeks To Geeks.

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


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.RandomStringUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.commons.lang.math.NumberUtils;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23
24
25 // TODO: Decide if set should be a CharSet.
26
/// Note: There is a lot of tied logic between this class and
27
/// the RandomStringUtils random methods. Understand how all the
28
/// overloading works in RandomStringUtils before tackling this
29
/// class.
30

31 /**
32  * Create a random string. It can create an ascii string,
33  * a numeric string, an alphanumeric string, and a unicode
34  * string. The size of the string may be specified, as can a
35  * start and end character. Lastly, a set of characters to use
36  * may be passed in.
37  *
38  * <dl>
39  * <dt>count</dt><dd>
40  * Number of characters to generate.
41  * Required.
42  * </dd>
43  * <dt>start</dt><dd>
44  * Start of range of characters to generate from.
45  * Linked to type attribute.
46  * </dd>
47  * <dt>end</dt><dd>
48  * End of range of characters to generate from.
49  * Linked to type attribute.
50  * </dd>
51  * <dt>type</dt><dd>
52  * Type of random-string. One of: numeric | alphanumeric | alphabet | unicode.
53  * Default value is to unicode.
54  * </dd>
55  * </dl>
56  *
57  * @author bayard@generationjava.com
58  */

59 public class RandomStringTag extends StringTagSupport {
60     static public String JavaDoc NUMERIC = "numeric";
61     static public String JavaDoc ALPHANUMERIC = "alphanumeric";
62     static public String JavaDoc ALPHABET = "alphabet";
63     static public String JavaDoc UNICODE = "unicode";
64     private String JavaDoc count;
65     private String JavaDoc start;
66     private String JavaDoc end;
67     private String JavaDoc type;
68
69     public RandomStringTag() {
70         super();
71     }
72
73     /**
74      * Get the count property
75      *
76      * @return String property
77      */

78     public String JavaDoc getCount() {
79         return this.count;
80     }
81
82     /**
83      * Set the count property
84      *
85      * @param with String property
86      */

87     public void setCount(String JavaDoc count) {
88         this.count = count;
89     }
90
91     /**
92      * Get the start property
93      *
94      * @return String property
95      */

96     public String JavaDoc getStart() {
97         return this.start;
98     }
99
100     /**
101      * Set the start property
102      *
103      * @param start String property
104      */

105     public void setStart(String JavaDoc start) {
106         this.start = start;
107     }
108
109     /**
110      * Get the end property
111      *
112      * @return String property
113      */

114     public String JavaDoc getEnd() {
115         return this.end;
116     }
117
118     /**
119      * Set the end property
120      *
121      * @param end String property
122      */

123     public void setEnd(String JavaDoc end) {
124         this.end = end;
125     }
126
127     /**
128      * Get the type property
129      *
130      * @return String property
131      */

132     public String JavaDoc getType() {
133         return this.type;
134     }
135
136     /**
137      * Set the type property
138      *
139      * @param with String property
140      */

141     public void setType(String JavaDoc type) {
142         this.type = type;
143     }
144
145     public String JavaDoc changeString(String JavaDoc text) throws JspException JavaDoc {
146         int st = 0; // real start
147
int ed = 0; // real end
148
int ct = 0; // real count
149
boolean letters = false;
150         boolean numbers = false;
151
152         letters = ALPHABET.equals(type);
153         numbers = NUMERIC.equals(type);
154
155         if (ALPHANUMERIC.equals(type)) {
156             letters = true;
157             numbers = true;
158         }
159
160         char[] setChrs = null;
161
162         if (!StringUtils.isBlank(text)) {
163             setChrs = text.toCharArray();
164         }
165
166         if (!StringUtils.isEmpty(start)) {
167             try {
168                 if (NumberUtils.isNumber(start)) {
169                     st = NumberUtils.createNumber(start).intValue();
170                 } else {
171                     st = (int) start.charAt(0);
172                 }
173             } catch (NumberFormatException JavaDoc nfe) {
174             } catch (NullPointerException JavaDoc npe) { // easiest way to code it
175
}
176         }
177
178         if (!StringUtils.isEmpty(end)) {
179             try {
180                 if (NumberUtils.isNumber(end)) {
181                     ed = NumberUtils.createNumber(end).intValue();
182                 } else {
183                     ed = (int) end.charAt(0);
184                 }
185             } catch (NumberFormatException JavaDoc nfe) {
186                 nfe.printStackTrace();
187             } catch (NullPointerException JavaDoc npe) { // easiest way to code it
188
npe.printStackTrace();
189             }
190         }
191
192         // if a set of chars is passed in, we need to be very cautions with its boundaries
193
// NOTE: these checkings should be done by RandomStringUtils
194
if ( (setChrs != null) ) {
195             if ( st > setChrs.length-1 ) {
196                 st =0;
197             }
198             if ( ed == 0 || ed > setChrs.length-1 ) {
199                 ed = setChrs.length-1;
200             }
201
202         }
203         
204         try {
205             ct = NumberUtils.createNumber(count).intValue();
206         } catch (NumberFormatException JavaDoc nfe) {
207             nfe.printStackTrace();
208         } catch (NullPointerException JavaDoc npe) { // easiest way to code it
209
npe.printStackTrace();
210         }
211
212         String JavaDoc randomString = null;
213         try {
214             randomString = RandomStringUtils.random(ct, st, ed, letters, numbers, setChrs);
215         } catch ( Exception JavaDoc exc ) {
216             // in the worst case scenario, be nice and return a random String....
217
if ( letters && numbers ) {
218                 randomString = RandomStringUtils.randomAlphanumeric( ct );
219             } else if ( letters ) {
220                 randomString = RandomStringUtils.randomNumeric( ct );
221             } else if ( numbers ) {
222                 randomString = RandomStringUtils.randomNumeric( ct );
223             } else {
224                 randomString = RandomStringUtils.random( ct );
225             }
226             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
227             msg.append( "Exception on RandomStringTag: " + exc );
228             msg.append("\nCT: " + ct);
229             msg.append("\nST: " + st);
230             msg.append("\nED: " + ed);
231             msg.append("\nletters: " + letters);
232             msg.append("\nnumbers: " + numbers);
233             msg.append("\nset: " + (setChrs == null ? "null" : ""+setChrs.length + " characters") );
234             msg.append("\nreturning " + randomString );
235             System.err.println( msg.toString());
236         }
237         
238         // TODO: RandomString is not working if body is set...
239
/*
240         System.err.println(">>>" + randomString + "<<<" );
241         */

242         return randomString;
243     }
244
245     public void initAttributes() {
246         this.count = null;
247         this.start = null;
248         this.end = null;
249         this.type = UNICODE;
250     }
251
252 }
253
Popular Tags