KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > ParamUtils


1 /**
2  * $RCSfile: ParamUtils.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2004/12/27 02:54:13 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15
16 /**
17  * Assists JSP writers in getting parameters and attributes.
18  */

19 public class ParamUtils {
20
21     /**
22      * Returns a parameter as a string.
23      *
24      * @param request the HttpServletRequest object, known as "request" in a
25      * JSP page.
26      * @param name the name of the parameter you want to get
27      * @return the value of the parameter or null if the parameter was not
28      * found or if the parameter is a zero-length string.
29      */

30     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc name) {
31         return getParameter(request, name, false);
32     }
33
34     /**
35      * Returns a parameter as a string.
36      *
37      * @param request the HttpServletRequest object, known as "request" in a
38      * JSP page.
39      * @param name the name of the parameter you want to get
40      * @param emptyStringsOK teturn the parameter values even if it is an empty string.
41      * @return the value of the parameter or null if the parameter was not
42      * found.
43      */

44     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc name,
45             boolean emptyStringsOK)
46     {
47         String JavaDoc temp = request.getParameter(name);
48         if (temp != null) {
49             if (temp.equals("") && !emptyStringsOK) {
50                 return null;
51             }
52             else {
53                 return temp;
54             }
55         }
56         else {
57             return null;
58         }
59     }
60
61     /**
62      * Returns a list of parameters of the same name
63      *
64      * @param request an HttpServletRequest object.
65      * @return an array of non-null, non-blank strings of the same name. This
66      * method will return an empty array if no parameters were found.
67      */

68     public static String JavaDoc[] getParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
69         if (name == null) {
70             return new String JavaDoc[0];
71         }
72         String JavaDoc[] paramValues = request.getParameterValues(name);
73         if (paramValues == null || paramValues.length == 0) {
74             return new String JavaDoc[0];
75         }
76         else {
77             java.util.List JavaDoc values = new java.util.ArrayList JavaDoc(paramValues.length);
78             for (int i = 0; i < paramValues.length; i++) {
79                 if (paramValues[i] != null && !"".equals(paramValues[i])) {
80                     values.add(paramValues[i]);
81                 }
82             }
83             return (String JavaDoc[])values.toArray(new String JavaDoc[]{});
84         }
85     }
86
87     /**
88      * Returns a parameter as a boolean.
89      *
90      * @param request the HttpServletRequest object, known as "request" in a
91      * JSP page.
92      * @param name the name of the parameter you want to get
93      * @return true if the value of the parameter was "true", false otherwise.
94      */

95     public static boolean getBooleanParameter(HttpServletRequest JavaDoc request, String JavaDoc name) {
96         return getBooleanParameter(request, name, false);
97     }
98
99     /**
100      * Returns a parameter as a boolean.
101      *
102      * @param request the HttpServletRequest object, known as "request" in a
103      * JSP page.
104      * @param name the name of the parameter you want to get
105      * @return true if the value of the parameter was "true", false otherwise.
106      */

107     public static boolean getBooleanParameter(HttpServletRequest JavaDoc request,
108             String JavaDoc name, boolean defaultVal)
109     {
110         String JavaDoc temp = request.getParameter(name);
111         if ("true".equals(temp) || "on".equals(temp)) {
112             return true;
113         }
114         else if ("false".equals(temp) || "off".equals(temp)) {
115             return false;
116         }
117         else {
118             return defaultVal;
119         }
120     }
121
122     /**
123      * Returns a parameter as an int.
124      *
125      * @param request the HttpServletRequest object, known as "request" in a
126      * JSP page.
127      * @param name the name of the parameter you want to get
128      * @return the int value of the parameter specified or the default value if
129      * the parameter is not found.
130      */

131     public static int getIntParameter(HttpServletRequest JavaDoc request,
132                                       String JavaDoc name, int defaultNum) {
133         String JavaDoc temp = request.getParameter(name);
134         if (temp != null && !temp.equals("")) {
135             int num = defaultNum;
136             try {
137                 num = Integer.parseInt(temp);
138             }
139             catch (Exception JavaDoc ignored) {
140             }
141             return num;
142         }
143         else {
144             return defaultNum;
145         }
146     }
147
148     /**
149      * Returns a list of int parameters.
150      *
151      * @param request the HttpServletRequest object, known as "request" in a
152      * JSP page.
153      * @param name the name of the parameter you want to get
154      * @param defaultNum the default value of a parameter, if the parameter
155      * can't be converted into an int.
156      */

157     public static int[] getIntParameters(HttpServletRequest JavaDoc request,
158                                          String JavaDoc name, int defaultNum) {
159         String JavaDoc[] paramValues = request.getParameterValues(name);
160         if (paramValues == null || paramValues.length == 0) {
161             return new int[0];
162         }
163         int[] values = new int[paramValues.length];
164         for (int i = 0; i < paramValues.length; i++) {
165             try {
166                 values[i] = Integer.parseInt(paramValues[i]);
167             }
168             catch (Exception JavaDoc e) {
169                 values[i] = defaultNum;
170             }
171         }
172         return values;
173     }
174
175     /**
176      * Returns a parameter as a double.
177      *
178      * @param request the HttpServletRequest object, known as "request" in a
179      * JSP page.
180      * @param name the name of the parameter you want to get
181      * @return the double value of the parameter specified or the default value
182      * if the parameter is not found.
183      */

184     public static double getDoubleParameter(HttpServletRequest JavaDoc request, String JavaDoc name, double defaultNum) {
185         String JavaDoc temp = request.getParameter(name);
186         if (temp != null && !temp.equals("")) {
187             double num = defaultNum;
188             try {
189                 num = Double.parseDouble(temp);
190             }
191             catch (Exception JavaDoc ignored) {
192             }
193             return num;
194         }
195         else {
196             return defaultNum;
197         }
198     }
199
200     /**
201      * Returns a parameter as a long.
202      *
203      * @param request the HttpServletRequest object, known as "request" in a
204      * JSP page.
205      * @param name the name of the parameter you want to get
206      * @return the long value of the parameter specified or the default value if
207      * the parameter is not found.
208      */

209     public static long getLongParameter(HttpServletRequest JavaDoc request, String JavaDoc name, long defaultNum) {
210         String JavaDoc temp = request.getParameter(name);
211         if (temp != null && !temp.equals("")) {
212             long num = defaultNum;
213             try {
214                 num = Long.parseLong(temp);
215             }
216             catch (Exception JavaDoc ignored) {
217             }
218             return num;
219         }
220         else {
221             return defaultNum;
222         }
223     }
224
225     /**
226      * Returns a list of long parameters.
227      *
228      * @param request the HttpServletRequest object, known as "request" in a
229      * JSP page.
230      * @param name the name of the parameter you want to get
231      * @param defaultNum the default value of a parameter, if the parameter
232      * can't be converted into a long.
233      */

234     public static long[] getLongParameters(HttpServletRequest JavaDoc request, String JavaDoc name,
235             long defaultNum)
236     {
237         String JavaDoc[] paramValues = request.getParameterValues(name);
238         if (paramValues == null || paramValues.length == 0) {
239             return new long[0];
240         }
241         long[] values = new long[paramValues.length];
242         for (int i = 0; i < paramValues.length; i++) {
243             try {
244                 values[i] = Long.parseLong(paramValues[i]);
245             }
246             catch (Exception JavaDoc e) {
247                 values[i] = defaultNum;
248             }
249         }
250         return values;
251     }
252
253     /**
254      * Returns an attribute as a string.
255      *
256      * @param request the HttpServletRequest object, known as "request" in a JSP page.
257      * @param name the name of the parameter you want to get
258      * @return the value of the parameter or null if the parameter was not
259      * found or if the parameter is a zero-length string.
260      */

261     public static String JavaDoc getAttribute(HttpServletRequest JavaDoc request, String JavaDoc name) {
262         return getAttribute(request, name, false);
263     }
264
265     /**
266      * Returns an attribute as a string.
267      *
268      * @param request the HttpServletRequest object, known as "request" in a JSP page.
269      * @param name the name of the parameter you want to get.
270      * @param emptyStringsOK return the parameter values even if it is an empty string.
271      * @return the value of the parameter or null if the parameter was not
272      * found.
273      */

274     public static String JavaDoc getAttribute(HttpServletRequest JavaDoc request, String JavaDoc name,
275             boolean emptyStringsOK)
276     {
277         String JavaDoc temp = (String JavaDoc)request.getAttribute(name);
278         if (temp != null) {
279             if (temp.equals("") && !emptyStringsOK) {
280                 return null;
281             }
282             else {
283                 return temp;
284             }
285         }
286         else {
287             return null;
288         }
289     }
290
291     /**
292      * Returns an attribute as a boolean.
293      *
294      * @param request the HttpServletRequest object, known as "request" in a JSP page.
295      * @param name the name of the attribute you want to get.
296      * @return true if the value of the attribute is "true", false otherwise.
297      */

298     public static boolean getBooleanAttribute(HttpServletRequest JavaDoc request, String JavaDoc name) {
299         String JavaDoc temp = (String JavaDoc)request.getAttribute(name);
300         if (temp != null && temp.equals("true")) {
301             return true;
302         }
303         else {
304             return false;
305         }
306     }
307
308     /**
309      * Returns an attribute as a int.
310      *
311      * @param request the HttpServletRequest object, known as "request" in a JSP page.
312      * @param name the name of the attribute you want to get.
313      * @return the int value of the attribute or the default value if the
314      * attribute is not found or is a zero length string.
315      */

316     public static int getIntAttribute(HttpServletRequest JavaDoc request, String JavaDoc name, int defaultNum) {
317         String JavaDoc temp = (String JavaDoc)request.getAttribute(name);
318         if (temp != null && !temp.equals("")) {
319             int num = defaultNum;
320             try {
321                 num = Integer.parseInt(temp);
322             }
323             catch (Exception JavaDoc ignored) {
324             }
325             return num;
326         }
327         else {
328             return defaultNum;
329         }
330     }
331
332     /**
333      * Returns an attribute as a long.
334      *
335      * @param request the HttpServletRequest object, known as "request" in a JSP page.
336      * @param name the name of the attribute you want to get.
337      * @return the long value of the attribute or the default value if the
338      * attribute is not found or is a zero length string.
339      */

340     public static long getLongAttribute(HttpServletRequest JavaDoc request, String JavaDoc name, long defaultNum) {
341         String JavaDoc temp = (String JavaDoc)request.getAttribute(name);
342         if (temp != null && !temp.equals("")) {
343             long num = defaultNum;
344             try {
345                 num = Long.parseLong(temp);
346             }
347             catch (Exception JavaDoc ignored) {
348             }
349             return num;
350         }
351         else {
352             return defaultNum;
353         }
354     }
355 }
Popular Tags