KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 /**
28  * RandomStrg tag will create a random string generator accessiable by the
29  * <b>jsp:getProperty</b> tag..
30  *
31  * &lt;tag&gt;
32  * &lt;name&gt;random&lt;/name&gt;
33  * &lt;tagclass&gt;org.apache.taglibs.random.RandomTag&lt;/tagclass&gt;
34  * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
35  * &lt;info&gt;Creates an variable length random string generator&lt;/info&gt;
36  *
37  * &lt;attribute&gt;
38  * &lt;name&gt;id&lt;/name&gt;
39  * &lt;required&gt;true&lt;/required&gt;
40  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
41  * &lt;/attribute&gt;
42  * &lt;attribute&gt;
43  * &lt;name&gt;length&lt;/name&gt;
44  * &lt;required&gt;false&lt;/required&gt;
45  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
46  * &lt;/attribute&gt;
47  * &lt;attribute&gt;
48  * &lt;name&gt;map&lt;/name&gt;
49  * &lt;required&gt;false&lt;/required&gt;
50  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
51  * &lt;/attribute&gt;
52  * &lt;attribute&gt;
53  * &lt;name&gt;charset&lt;/name&gt;
54  * &lt;required&gt;false&lt;/required&gt;
55  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
56  * &lt;/attribute&gt;
57  * &lt;attribute&gt;
58  * &lt;name&gt;algorithm&lt;/name&gt;
59  * &lt;required&gt;false&lt;/required&gt;
60  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
61  * &lt;/attribute&gt;
62  * &lt;attribute&gt;
63  * &lt;name&gt;provider&lt;/name&gt;
64  * &lt;required&gt;false&lt;/required&gt;
65  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
66  * &lt;/attribute&gt;
67  * &lt;/tag&gt;
68  *
69  * @author Rich Catlett
70  *
71  * @version 1.0
72  *
73  */

74
75 public class RandomStrgTag extends TagSupport JavaDoc {
76
77     /**
78      * flag determines if all chars ar to be used
79      */

80     private boolean allchars = false;
81     /**
82      * length of random string defaults to 8
83      */

84     private Integer JavaDoc length = new Integer JavaDoc(8);
85     /**
86      * list of all generated strings, list is stored at the application level
87      */

88     private HashMap JavaDoc hmap;
89     /**
90      * Hashmap to check keys against to see if they have already been used
91      */

92     private String JavaDoc map = null;
93     /**
94      * ArrayList for the lowerbound of the char sets
95      */

96     private ArrayList JavaDoc lower = null;
97     /**
98      * ArrayList for the upperbound of the char sets
99      */

100     private ArrayList JavaDoc upper = null;
101     /**
102      * ArrayL for the set of chars that aren't part of a range
103      */

104     private char[] single = null;
105     /**
106      * counter for position in the array single
107      */

108     private int singlecount = 0;
109     /**
110      * the algorithm to use for a SecureRandom object
111      */

112     private String JavaDoc algorithm = null;
113     /**
114      * the provider package to check for the algorithm
115      */

116     private String JavaDoc provider = null;
117     /**
118      * the charset to use for generating the random string
119      */

120     private String JavaDoc charset = null;
121
122     /**
123      * implementation of method from the Tag interface that tells the JSP what
124      * to do upon encountering the start tag for this tag set
125      *
126      * @return SKIP_BODY - integer value telling the JSP engine to not evaluate
127      * the body of this tag
128      *
129      * @throws JspException thrown when error occurs in processing the body of
130      * this method
131      *
132      */

133     public final int doStartTag() throws JspException JavaDoc {
134
135     // create the class that will become the script variable available through
136
// the jsp:getProperty tag
137
RandomStrg random = new RandomStrg();
138
139     // set the default for the charset to a-ZA-Z0-9
140
if (charset != null) {
141             generateCharset(charset);
142         } else {
143         generateCharset("a-zA-Z0-9");
144         }
145
146     if (map != null) {
147         try {
148
149         // check if the list is to be used if so get list
150
if ((hmap = (HashMap JavaDoc) pageContext.findAttribute(map)) == null)
151             // named hashmap does not exist throw error out to the author
152
throw new JspException JavaDoc("A hashmap does not exist in any " +
153                        "scope under the name " + map);
154         } catch (ClassCastException JavaDoc cce) {
155         throw new JspException JavaDoc("The named attribute exists but it is not" +
156                        " a hashmap.");
157         }
158
159         random.setHmap(hmap); // set the Hashmap in the script variable
160
}
161
162     // if ranges exist set them in the script variable
163
if (lower != null)
164         random.setRanges(lower, upper);
165
166     // if a set of single chars exists set that in the script variable
167
if (single != null)
168         random.setSingle(single, singlecount);
169
170     random.setLength(length); // set the length of the random string
171

172     random.setAllchars(allchars); // set the allchars flag
173

174     // check to see if algorithm and provider have been set and set them in the
175
// randomnum
176
if (algorithm != null)
177         random.setAlgorithm(algorithm);
178     if (provider != null)
179         random.setProvider(provider);
180
181     // generate the random object to be used either a Random or SecureRandom
182
random.generateRandomObject();
183
184     // place the script variable in the page scope
185
pageContext.setAttribute(id, random, PageContext.PAGE_SCOPE);
186
187     return SKIP_BODY;
188     }
189
190     /**
191      * set the name of the map
192      *
193      * @param value name of the hashmap to search for on the server that contains
194      * the keys to compare the random strings to
195      */

196     public final void setMap(String JavaDoc value) {
197     map = value;
198     }
199
200     /**
201      * set the length of the password
202      *
203      * @param value length of the random string to be generated
204      */

205     public final void setLength(String JavaDoc value) {
206     try {
207         length = new Integer JavaDoc(value);
208     } catch (NumberFormatException JavaDoc ne) {
209         pageContext.getServletContext().log("length attribute could not be" +
210                    " turned into an Integer default value was used");
211     }
212     }
213
214     /**
215      * set the algorithm name
216      *
217      * @param value name of the algorithm to use for a SecureRandom object
218      *
219      */

220     public final void setAlgorithm(String JavaDoc value) {
221     algorithm = value;
222     }
223
224     /**
225      * set the provider name
226      *
227      * @param value name of the package to check for the algorithm
228      *
229      */

230     public final void setProvider(String JavaDoc value) {
231     provider = value;
232     }
233
234     /**
235      * set the range of characters to use
236      *
237      * @param value the range of characters to use could be any char from a-z, A-Z
238      * 0-9 or ! @ # $ % ^ & * ( ) _ \- + = [ ] { } \ | ; : ' " , . /
239      * < > ?
240      *
241      */

242     public final void setCharset(String JavaDoc value) {
243         charset = value;
244     }
245
246     private void generateCharset(String JavaDoc value) {
247     // values tells the method whether or not to check for single chars
248
boolean more = true;
249
250         // Reset private variables
251
allchars = false;
252         single = null;
253         singlecount = 0;
254
255     // create the arraylists to hold the upper and lower bounds for the char
256
// ranges
257
lower = new ArrayList JavaDoc(3);
258     upper = new ArrayList JavaDoc(3);
259
260     // user has chosen to use all possible characters in the random string
261
if (value.compareTo("all") == 0) {
262         allchars = true; // set allchars flag
263
// all chars are to be used so there are no single chars to sort
264
// through
265
more = false;
266     } else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\')) {
267         // run through the ranges at most 3
268
while (more && (value.charAt(1) == '-')){
269
270         // check to make sure that the dash is not the single char
271
if (value.charAt(0) == '\\')
272             break;
273         else {
274             // add upper and lower ranges to there list
275
lower.add(new Character JavaDoc(value.charAt(0)));
276             upper.add(new Character JavaDoc(value.charAt(2)));
277         }
278
279         // check to see if there is more to the charset
280
if (value.length() <= 3)
281             more = false;
282         else
283             // create a new string so that the next range if there is one
284
// starts it
285
value = value.substring(3);
286         }
287     }
288
289     // if more = false there are no single chars in the charset
290
if (more) {
291
292         single = new char[30]; // create single
293

294         // create a set of tokens from the string of single chars
295
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value);
296
297         while (tokens.hasMoreTokens()) {
298         // get the next token from the string
299
String JavaDoc token = tokens.nextToken();
300
301         if (token.length() > 1)
302             // char is a - add it to the list
303
single[singlecount++] = '-';
304
305         // add the current char to the list
306
single[singlecount++] = token.charAt(0);
307         }
308     }
309     }
310 }
311
Popular Tags