KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > random > RandomStrg


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
17 package org.apache.taglibs.random;
18
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.NoSuchProviderException JavaDoc;
21 import java.security.SecureRandom JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Random JavaDoc;
25
26 import javax.servlet.jsp.JspException JavaDoc;
27
28 /**
29  * RandomStrg class will produce a variable set of random characters.
30  *
31  * @author Rich Catlett
32  *
33  * @version 1.0
34  *
35  */

36
37 public class RandomStrg {
38
39     /**
40      * generated password
41      */

42     private String JavaDoc randomstr;
43     /**
44      * flag determines if all chars ar to be used
45      */

46     private boolean allchars = false;
47     /**
48      * length of random string defaults to 8
49      */

50     private Integer JavaDoc length = new Integer JavaDoc(8);
51     /**
52      * list of all generated strings, list is stored at the application level
53      */

54     private HashMap JavaDoc hmap;
55     /**
56      * ArrayList for the lowerbound of the char sets
57      */

58     private ArrayList JavaDoc lower = null;
59     /**
60      * ArrayList for the upperbound of the char sets
61      */

62     private ArrayList JavaDoc upper = null;
63     /**
64      * Array for the set of chars that aren't part of a range
65      */

66     private char[] single = null;
67     /**
68      * counter for position in the array single
69      */

70     private int singlecount = 0;
71     /**
72      * boolean flat that tells the random char generator if there is a list of
73      * single chars
74      */

75     private boolean singles = false;
76     /**
77      * the algorithm to use for a SecureRandom object
78      */

79     private String JavaDoc algorithm = null;
80     /**
81      * the provider package to check for the algorithm
82      */

83     private String JavaDoc provider = null;
84     /**
85      * boolean value that marks if a Random or SecureRandom object is to be used,
86      * default value of false says that a Random object will be used
87      */

88     private boolean secure = false;
89     /**
90      * random object that could be used
91      */

92     private Random JavaDoc random = null;
93     /**
94      * SecureRandom object that could be used
95      */

96     private SecureRandom JavaDoc secrandom = null;
97
98     /**
99      * nethod determines if a Random or SecureRandom object is to be used to
100      * generate the random number
101      *
102      */

103     private final float getFloat() {
104     if (random == null)
105         return secrandom.nextFloat();
106     else
107         return random.nextFloat();
108     }
109
110     /**
111      * generate the Random object that will be used for this random number
112      * generator
113      *
114      */

115     public final void generateRandomObject() throws JspException JavaDoc {
116
117     // check to see if the object is a SecureRandom object
118
if (secure) {
119         try {
120         // get an instance of a SecureRandom object
121
if (provider != null)
122             // search for algorithm in package provider
123
random = SecureRandom.getInstance(algorithm, provider);
124         else
125             random = SecureRandom.getInstance(algorithm);
126         } catch (NoSuchAlgorithmException JavaDoc ne) {
127         throw new JspException JavaDoc(ne.getMessage());
128         } catch (NoSuchProviderException JavaDoc pe) {
129         throw new JspException JavaDoc(pe.getMessage());
130         }
131     } else
132         random = new Random JavaDoc();
133     }
134
135     /**
136      * generate the random string
137      *
138      */

139     private final void generaterandom() {
140
141     // use all chars in the string
142
if (allchars)
143         for (int i = 0; i < length.intValue(); i++)
144         randomstr = randomstr + new Character JavaDoc((char)((int) 34 +
145                      ((int)(getFloat() * 93)))).toString();
146     else if (singles) {
147         // check if there are single chars to be included
148

149         if (upper.size() == 3) {
150         // check for the number of ranges max 3 uppercase lowercase digits
151

152         // build the random string
153
for (int i = 0; i < length.intValue(); i++) {
154             // you have four groups to choose a random number from, to make
155
// the choice a little more random select a number out of 100
156

157             // get a random number even or odd
158
if (((int) (getFloat() * 100)) % 2 == 0) {
159
160             // the number was even get another number even or odd
161
if (((int) (getFloat() * 100)) % 2 == 0)
162                 // choose a random char from the single char group
163
randomstr = randomstr + randomSingle().toString();
164             else
165                 // get a random char from the first range
166
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(2),
167                         (Character JavaDoc)upper.get(2)).toString();
168             } else {
169             // the number was odd
170

171             if (((int) (getFloat() * 100)) % 2 == 0)
172                 // choose a random char from the second range
173
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(1),
174                            (Character JavaDoc)upper.get(1)).toString();
175             else
176                 // choose a random char from the third range
177
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
178                            (Character JavaDoc)upper.get(0)).toString();
179             }
180         }
181         } else if (upper.size() == 2) {
182         // single chars are to be included choose a random char from
183
// two different ranges
184

185         // build the random char from single chars and two ranges
186
for (int i = 0; i < length.intValue(); i++) {
187             // select the single chars or a range to get each random char
188
// from
189

190             if (((int)(getFloat() * 100)) % 2 == 0) {
191
192             // get random char from the single chars
193
randomstr = randomstr + randomSingle().toString();
194             } else if (((int) (getFloat() * 100)) % 2 == 0) {
195
196             // get the random char from the first range
197
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(1),
198                            (Character JavaDoc)upper.get(1)).toString();
199             } else {
200
201             // get the random char from the second range
202
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
203                            (Character JavaDoc)upper.get(0)).toString();
204             }
205         }
206         } else if (upper.size() == 1) {
207
208         // build the random string from single chars and one range
209
for (int i = 0; i < length.intValue(); i++) {
210             if (((int) getFloat() * 100) % 2 == 0)
211             // get a random single char
212
randomstr = randomstr + randomSingle().toString();
213             else
214             // get a random char from the range
215
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
216                            (Character JavaDoc)upper.get(0)).toString();
217         }
218         } else {
219         // build the rand string from single chars
220
for (int i = 0; i < length.intValue(); i++)
221             randomstr = randomstr + randomSingle().toString();
222         }
223     } else {
224
225         // no single chars are to be included in the random string
226
if (upper.size() == 3) {
227
228         // build random strng from three ranges
229
for (int i = 0; i < length.intValue(); i++) {
230
231             if (((int) (getFloat() * 100)) % 2 == 0) {
232
233             // get random char from first range
234
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(2),
235                            (Character JavaDoc)upper.get(2)).toString();
236             } else if (((int) (getFloat() * 100)) % 2 == 0) {
237
238             // get random char form second range
239
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(1),
240                            (Character JavaDoc)upper.get(1)).toString();
241             } else {
242
243             // get random char from third range
244
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
245                            (Character JavaDoc)upper.get(0)).toString();
246             }
247         }
248         } else if (upper.size() == 2) {
249
250         // build random string from two ranges
251
for (int i = 0; i < length.intValue(); i++) {
252             if (((int) (getFloat() * 100)) % 2 == 0)
253             // get random char from first range
254
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(1),
255                            (Character JavaDoc)upper.get(1)).toString();
256             else
257             // get random char from second range
258
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
259                          (Character JavaDoc)upper.get(0)).toString();
260         }
261         } else
262
263         // build random string
264
for (int i = 0; i < length.intValue(); i++)
265             // get random char from only range
266
randomstr = randomstr + randomChar((Character JavaDoc)lower.get(0),
267                            (Character JavaDoc)upper.get(0)).toString();
268     }
269     }
270
271     /**
272      * generate a random char from the single char list
273      *
274      * @returns - a randomly selscted character from the single char list
275      *
276      */

277     private final Character JavaDoc randomSingle() {
278
279     return (new Character JavaDoc(single[(int)((getFloat() * singlecount) - 1)]));
280     }
281
282     /**
283      * generate a random character
284      *
285      * @param lower lower bound from which to get a random char
286      * @param upper upper bound from which to get a random char
287      *
288      * @returns - a randomly generated character
289      *
290      */

291     private final Character JavaDoc randomChar(Character JavaDoc lower, Character JavaDoc upper) {
292     int tempval;
293     char low = lower.charValue();
294     char up = upper.charValue();
295
296     // get a random number in the range lowlow - lowup
297
tempval = (int)((int)low + (getFloat() * ((int)(up - low))));
298
299     // return the random char
300
return (new Character JavaDoc((char) tempval));
301     }
302
303     /**
304      * get the randomly created string for use with the
305      * &lt;jsp:getProperty name=<i>"id"</i> property="randomstr"/&gt;
306      *
307      * @return - randomly created string
308      *
309      */

310     public final String JavaDoc getRandom() {
311
312     randomstr = new String JavaDoc();
313
314     generaterandom(); // generate the first random string
315

316     if (hmap != null) {
317
318         while (hmap.containsKey(randomstr)) {
319         // random string has already been created generate a different one
320
generaterandom();
321         }
322
323         hmap.put(randomstr, null); // add the new random string
324
}
325
326     return randomstr;
327     }
328
329     /**
330      * set the ranges from which to choose the characters for the random string
331      *
332      * @param low set of lower ranges
333      * @param up set of upper ranges
334      *
335      */

336     public final void setRanges(ArrayList JavaDoc low, ArrayList JavaDoc up) {
337     lower = low;
338     upper = up;
339     }
340
341
342     /**
343      * set the hashmap that is used to check the uniqueness of random strings
344      *
345      * @param map hashmap whose keys are used to insure uniqueness of random strgs
346      *
347      */

348     public final void setHmap(HashMap JavaDoc map) {
349     hmap = map;
350     }
351
352     /**
353      * set the length of the random string
354      *
355      * @param value length of the random string
356      *
357      */

358     public final void setLength(Integer JavaDoc value) {
359     length = value;
360     }
361
362     /**
363      * set the algorithm name
364      *
365      * @param value name of the algorithm to use for a SecureRandom object
366      *
367      */

368     public final void setAlgorithm(String JavaDoc value) {
369     algorithm = value;
370     secure = true; // a SecureRandom object is to be used
371
}
372
373     /**
374      * set the provider name
375      *
376      * @param value name of the package to check for the algorithm
377      *
378      */

379     public final void setProvider(String JavaDoc value) {
380     provider = value;
381     }
382
383     /**
384      * set the allchars flag
385      *
386      * @param value boolean value of the allchars flag
387      *
388      */

389     public final void setAllchars(boolean value) {
390     allchars = value;
391     }
392
393     /**
394      * set the array of single chars to choose from for this random string and the
395      * number of chars in the array
396      *
397      * @param chars the array of single chars
398      * @param value the number of single chars
399      *
400      */

401     public final void setSingle(char[] chars, int value) {
402     single = chars; // set the array of chars
403
singlecount = value; // set the number of chars in array single
404
singles = true; // set flag that single chars are in use
405
}
406 }
407
Popular Tags