KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > util > ParamUtils


1 package com.Yasna.forum.util;
2
3 import javax.servlet.http.*;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.LinkedList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Calendar JavaDoc;
8
9 /**
10  * This class assists skin writers in getting parameters.
11  */

12 public class ParamUtils {
13
14     /**
15      * Gets a parameter as a string.
16      * @param request The HttpServletRequest object, known as "request" in a
17      * JSP page.
18      * @param paramName The name of the parameter you want to get
19      * @return The value of the parameter or null if the parameter was not
20      * found or if the parameter is a zero-length string.
21      */

22         public static String JavaDoc getParameter( HttpServletRequest request, String JavaDoc paramName ) {
23                 return getParameter( request, paramName, false );
24         }
25
26     /**
27      * Gets a parameter as a string.
28      * @param request The HttpServletRequest object, known as "request" in a
29      * JSP page.
30      * @param paramName The name of the parameter you want to get
31      * @param emptyStringsOK Return the parameter values even if it is an empty string.
32      * @return The value of the parameter or null if the parameter was not
33      * found.
34      */

35         public static String JavaDoc getParameter( HttpServletRequest request, String JavaDoc paramName, boolean emptyStringsOK ) {
36                 String JavaDoc temp = request.getParameter(paramName);
37         if( temp != null ) {
38             if( temp.equals("") && !emptyStringsOK ) {
39                 return null;
40             }
41             else {
42                 return temp;
43             }
44         }
45         else {
46             return null;
47         }
48     }
49         public static java.sql.Date JavaDoc getDateParameter( HttpServletRequest request, String JavaDoc paramName) {
50                 String JavaDoc temp = request.getParameter(paramName);
51                 if( temp != null && !temp.equals("") ) {
52                 SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
53
54                   try {
55                    return new java.sql.Date JavaDoc(df.parse(temp).getTime());
56                   }
57                   catch( Exception JavaDoc ignored ) {}
58                         return null;
59                 } else {
60                         return null;
61                 }
62         }
63
64     public static Calendar JavaDoc getCalendarParameter( HttpServletRequest request, String JavaDoc paramName) {
65             String JavaDoc temp = request.getParameter(paramName);
66             java.sql.Date JavaDoc tempdt=null;
67             if( temp != null && !temp.equals("") ) {
68             SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
69
70               try {
71                tempdt = new java.sql.Date JavaDoc(df.parse(temp).getTime());
72               }
73               catch( Exception JavaDoc ignored ) {}
74             } else {
75                     return null;
76             }
77             if (tempdt != null){
78                 Calendar JavaDoc tempcal = Calendar.getInstance();
79                 tempcal.setTime(tempdt);
80                 return tempcal;
81             } else {
82                 return null;
83             }
84     }
85
86
87     /**
88      * Gets a parameter as a boolean.
89      * @param request The HttpServletRequest object, known as "request" in a
90      * JSP page.
91      * @param paramName The name of the parameter you want to get
92      * @return True if the value of the parameter was "true", false otherwise.
93      */

94         public static boolean getBooleanParameter( HttpServletRequest request, String JavaDoc paramName ) {
95                 String JavaDoc temp = request.getParameter(paramName);
96                 if( temp != null && temp.equals("true") ) {
97                         return true;
98                 } else {
99                         return false;
100                 }
101         }
102
103     /**
104      * Gets a parameter as a int.
105      * @param request The HttpServletRequest object, known as "request" in a
106      * JSP page.
107      * @param paramName The name of the parameter you want to get
108      * @return The int value of the parameter specified or the default value if
109      * the parameter is not found.
110      */

111         public static int getIntParameter( HttpServletRequest request, String JavaDoc paramName, int defaultNum ) {
112                 String JavaDoc temp = request.getParameter(paramName);
113                 if( temp != null && !temp.equals("") ) {
114             int num = defaultNum;
115             try {
116                 num = Integer.parseInt(temp);
117             }
118             catch( Exception JavaDoc ignored ) {}
119                         return num;
120                 } else {
121                         return defaultNum;
122                 }
123         }
124     public static float getFloatParameter( HttpServletRequest request, String JavaDoc paramName, float defaultNum ) {
125         String JavaDoc temp = request.getParameter(paramName);
126         if( temp != null && !temp.equals("") ) {
127             float num = defaultNum;
128             try {
129                 num = Float.parseFloat(temp);
130             }
131             catch( Exception JavaDoc ignored ) {}
132             return num;
133         } else {
134             return defaultNum;
135         }
136     }
137     public static String JavaDoc[] getArrayParameter( HttpServletRequest request, String JavaDoc paramName) {
138             return request.getParameterValues(paramName);
139     }
140     public static int[] getIntArrayParameter( HttpServletRequest request, String JavaDoc paramName) {
141         String JavaDoc delim = ",";
142         String JavaDoc s = request.getParameter(paramName);
143         if (s == null) {
144             return null;
145         }
146         if (delim == null) {
147             return null;
148         }
149
150         List JavaDoc l = new LinkedList JavaDoc();
151         int pos = 0;
152         int delPos = 0;
153         while ((delPos = s.indexOf(delim, pos)) != -1) {
154             l.add(s.substring(pos, delPos));
155             pos = delPos + delim.length();
156         }
157         if (pos <= s.length()) {
158             // add rest of String
159
l.add(s.substring(pos));
160         }
161
162         String JavaDoc[] temp = (String JavaDoc[]) l.toArray(new String JavaDoc[l.size()]);
163         if (temp == null){
164             return null;
165         } else {
166             int[] tempInt = new int[temp.length];
167             for (int i=0; i< temp.length; i++) {
168                 tempInt[i] = Integer.parseInt(temp[i]);
169             }
170             return tempInt;
171         }
172     }
173
174     /**
175      * Gets a checkbox parameter value as a boolean.
176      * @param request The HttpServletRequest object, known as "request" in a
177      * JSP page.
178      * @param paramName The name of the parameter you want to get
179      * @return True if the value of the checkbox is "on", false otherwise.
180      */

181     public static boolean getCheckboxParameter( HttpServletRequest request, String JavaDoc paramName ) {
182         String JavaDoc temp = request.getParameter(paramName);
183         if( temp != null && temp.equals("on") ) {
184             return true;
185         } else {
186             return false;
187         }
188     }
189
190     /**
191      * Gets a parameter as a string.
192      * @param request The HttpServletRequest object, known as "request" in a
193      * JSP page.
194      * @param attribName The name of the parameter you want to get
195      * @return The value of the parameter or null if the parameter was not
196      * found or if the parameter is a zero-length string.
197      */

198         public static String JavaDoc getAttribute( HttpServletRequest request, String JavaDoc attribName ) {
199                 return getAttribute( request, attribName, false );
200         }
201
202     /**
203      * Gets a parameter as a string.
204      * @param request The HttpServletRequest object, known as "request" in a
205      * JSP page.
206      * @param attribName The name of the parameter you want to get
207      * @param emptyStringsOK Return the parameter values even if it is an empty string.
208      * @return The value of the parameter or null if the parameter was not
209      * found.
210      */

211         public static String JavaDoc getAttribute( HttpServletRequest request, String JavaDoc attribName, boolean emptyStringsOK ) {
212                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
213         if( temp != null ) {
214             if( temp.equals("") && !emptyStringsOK ) {
215                 return null;
216             }
217             else {
218                 return temp;
219             }
220         }
221         else {
222             return null;
223         }
224     }
225
226     /**
227      * Gets an attribute as a boolean.
228      * @param request The HttpServletRequest object, known as "request" in a
229      * JSP page.
230      * @param attribName The name of the attribute you want to get
231      * @return True if the value of the attribute is "true", false otherwise.
232      */

233         public static boolean getBooleanAttribute( HttpServletRequest request, String JavaDoc attribName ) {
234                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
235                 if( temp != null && temp.equals("true") ) {
236                         return true;
237                 } else {
238                         return false;
239                 }
240         }
241
242     /**
243      * Gets an attribute as a int.
244      * @param request The HttpServletRequest object, known as "request" in a
245      * JSP page.
246      * @param attribName The name of the attribute you want to get
247      * @return The int value of the attribute or the default value if the attribute is not
248      * found or is a zero length string.
249      */

250         public static int getIntAttribute( HttpServletRequest request, String JavaDoc attribName, int defaultNum ) {
251                 String JavaDoc temp = (String JavaDoc)request.getAttribute(attribName);
252                 if( temp != null && !temp.equals("") ) {
253             int num = defaultNum;
254             try {
255                 num = Integer.parseInt(temp);
256             }
257             catch( Exception JavaDoc ignored ) {}
258                         return num;
259                 } else {
260                         return defaultNum;
261                 }
262         }
263
264 }
265
Popular Tags