KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22
23 /**
24  * The RandomNumTag will set up a random number generator which can be accessed by
25  * the <b>jsp:getProperty</b> tag.
26  *
27  * &lt;tag&gt;
28  * &lt;name&gt;randomnum&lt;/name&gt;
29  * &lt;tagclass&gt;org.apache.taglibs.random.RandomNumTag&lt;/tagclass&gt;
30  * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
31  * &lt;info&gt;Creates a variable length random number generator&lt;/info&gt;
32  *
33  * &lt;attribute&gt;
34  * &lt;name&gt;id&lt;/name&gt;
35  * &lt;required&gt;true&lt;/required&gt;
36  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
37  * &lt;/attribute&gt;
38  * &lt;attribute&gt;
39  * &lt;name&gt;range&lt;/name&gt;
40  * &lt;required&gt;false&lt;/required&gt;
41  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
42  * &lt;/attribute&gt;
43  * &lt;attribute&gt;
44  * &lt;name&gt;algorithm&lt;/name&gt;
45  * &lt;required&gt;false&lt;/required&gt;
46  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
47  * &lt;/attribute&gt;
48  * &lt;attribute&gt;
49  * &lt;name&gt;provider&lt;/name&gt;
50  * &lt;required&gt;false&lt;/required&gt;
51  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
52  * &lt;/attribute&gt;
53  * &lt;/tag&gt;
54  *
55  * @author Rich Catlett
56  *
57  * @version 1.0
58  *
59  */

60
61 public class RandomNumTag extends TagSupport JavaDoc {
62
63     /**
64      * upper bound in which to search for a number defaults to 9
65      */

66     private long upper = 100;
67     /**
68      * lower bound in which to search for a number defaults to 0
69      */

70     private long lower = 0;
71     /**
72      * the algorithm to use for a SecureRandom object
73      */

74     private String JavaDoc algorithm = null;
75     /**
76      * the provider package to check for the algorithm
77      */

78     private String JavaDoc provider = null;
79
80     /**
81      * implementation of method from the Tag interface that tells the JSP what
82      * to do upon encountering the start tag for this tag set
83      *
84      * @return SKIP_BODY - integer value telling the JSP engine to not evaluate
85      * the body of this tag
86      *
87      * @throws JspException thrown when error occurs in processing the body of
88      * this method
89      *
90      */

91     public final int doStartTag() throws JspException JavaDoc {
92
93     RandomNum random = new RandomNum();
94
95     // set the range in the randomnum class
96
random.setRange(lower, upper);
97
98     // check to see if algorithm and provider have been set and set them in the
99
// randomnum
100
if (algorithm != null)
101         random.setAlgorithm(algorithm);
102     if (provider != null)
103         random.setProvider(provider);
104
105     // generate the random object to be used either a Random or SecureRandom
106
random.generateRandomObject();
107
108     // place random into page scope so that it will be available as a
109
// script variable
110
pageContext.setAttribute(id, random, PageContext.PAGE_SCOPE);
111
112     return SKIP_BODY;
113     }
114
115     /**
116      * set the Range
117      *
118      * @param value String value that determines range from which the random
119      * number will be chosen
120      *
121      * @throws JspException if the lowerbound is greater than the upperbound
122      *
123      */

124     public final void setRange(String JavaDoc value) throws JspException JavaDoc {
125     try {
126       upper = new Integer JavaDoc(value.substring(value.indexOf('-') + 1)).longValue();
127     } catch (NumberFormatException JavaDoc nfe) {
128         pageContext.getServletContext().log("upper attribute could not be" +
129                    " turned into an Integer default value was used");
130     }
131     try {
132       lower = new Integer JavaDoc(value.substring(0, value.indexOf('-'))).longValue();
133     } catch (NumberFormatException JavaDoc nfe) {
134         pageContext.getServletContext().log("lower attribute could not be" +
135                    " turned into an Integer default value was used");
136     }
137
138     // check that the range is possible i.e. the upperbound is not lower than
139
// the lowerbound
140
if (upper < lower)
141         throw new JspException JavaDoc("You can't have a range where the lowerbound" +
142                    " is higher than the upperbound.");
143     }
144
145     /**
146      * set the algorithm name
147      *
148      * @param value name of the algorithm to use for a SecureRandom object
149      *
150      */

151     public final void setAlgorithm(String JavaDoc value) {
152     algorithm = value;
153     }
154
155     /**
156      * set the provider name
157      *
158      * @param value name of the package to check for the algorithm
159      *
160      */

161     public final void setProvider(String JavaDoc value) {
162     provider = value;
163     }
164 }
165
Popular Tags