001: package com.Yasna.forum.util;
002:
003: import javax.servlet.http.*;
004: import java.text.SimpleDateFormat;
005: import java.util.LinkedList;
006: import java.util.List;
007: import java.util.Calendar;
008:
009: /**
010: * This class assists skin writers in getting parameters.
011: */
012: public class ParamUtils {
013:
014: /**
015: * Gets a parameter as a string.
016: * @param request The HttpServletRequest object, known as "request" in a
017: * JSP page.
018: * @param paramName The name of the parameter you want to get
019: * @return The value of the parameter or null if the parameter was not
020: * found or if the parameter is a zero-length string.
021: */
022: public static String getParameter(HttpServletRequest request,
023: String paramName) {
024: return getParameter(request, paramName, false);
025: }
026:
027: /**
028: * Gets a parameter as a string.
029: * @param request The HttpServletRequest object, known as "request" in a
030: * JSP page.
031: * @param paramName The name of the parameter you want to get
032: * @param emptyStringsOK Return the parameter values even if it is an empty string.
033: * @return The value of the parameter or null if the parameter was not
034: * found.
035: */
036: public static String getParameter(HttpServletRequest request,
037: String paramName, boolean emptyStringsOK) {
038: String temp = request.getParameter(paramName);
039: if (temp != null) {
040: if (temp.equals("") && !emptyStringsOK) {
041: return null;
042: } else {
043: return temp;
044: }
045: } else {
046: return null;
047: }
048: }
049:
050: public static java.sql.Date getDateParameter(
051: HttpServletRequest request, String paramName) {
052: String temp = request.getParameter(paramName);
053: if (temp != null && !temp.equals("")) {
054: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
055:
056: try {
057: return new java.sql.Date(df.parse(temp).getTime());
058: } catch (Exception ignored) {
059: }
060: return null;
061: } else {
062: return null;
063: }
064: }
065:
066: public static Calendar getCalendarParameter(
067: HttpServletRequest request, String paramName) {
068: String temp = request.getParameter(paramName);
069: java.sql.Date tempdt = null;
070: if (temp != null && !temp.equals("")) {
071: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
072:
073: try {
074: tempdt = new java.sql.Date(df.parse(temp).getTime());
075: } catch (Exception ignored) {
076: }
077: } else {
078: return null;
079: }
080: if (tempdt != null) {
081: Calendar tempcal = Calendar.getInstance();
082: tempcal.setTime(tempdt);
083: return tempcal;
084: } else {
085: return null;
086: }
087: }
088:
089: /**
090: * Gets a parameter as a boolean.
091: * @param request The HttpServletRequest object, known as "request" in a
092: * JSP page.
093: * @param paramName The name of the parameter you want to get
094: * @return True if the value of the parameter was "true", false otherwise.
095: */
096: public static boolean getBooleanParameter(
097: HttpServletRequest request, String paramName) {
098: String temp = request.getParameter(paramName);
099: if (temp != null && temp.equals("true")) {
100: return true;
101: } else {
102: return false;
103: }
104: }
105:
106: /**
107: * Gets a parameter as a int.
108: * @param request The HttpServletRequest object, known as "request" in a
109: * JSP page.
110: * @param paramName The name of the parameter you want to get
111: * @return The int value of the parameter specified or the default value if
112: * the parameter is not found.
113: */
114: public static int getIntParameter(HttpServletRequest request,
115: String paramName, int defaultNum) {
116: String temp = request.getParameter(paramName);
117: if (temp != null && !temp.equals("")) {
118: int num = defaultNum;
119: try {
120: num = Integer.parseInt(temp);
121: } catch (Exception ignored) {
122: }
123: return num;
124: } else {
125: return defaultNum;
126: }
127: }
128:
129: public static float getFloatParameter(HttpServletRequest request,
130: String paramName, float defaultNum) {
131: String temp = request.getParameter(paramName);
132: if (temp != null && !temp.equals("")) {
133: float num = defaultNum;
134: try {
135: num = Float.parseFloat(temp);
136: } catch (Exception ignored) {
137: }
138: return num;
139: } else {
140: return defaultNum;
141: }
142: }
143:
144: public static String[] getArrayParameter(
145: HttpServletRequest request, String paramName) {
146: return request.getParameterValues(paramName);
147: }
148:
149: public static int[] getIntArrayParameter(
150: HttpServletRequest request, String paramName) {
151: String delim = ",";
152: String s = request.getParameter(paramName);
153: if (s == null) {
154: return null;
155: }
156: if (delim == null) {
157: return null;
158: }
159:
160: List l = new LinkedList();
161: int pos = 0;
162: int delPos = 0;
163: while ((delPos = s.indexOf(delim, pos)) != -1) {
164: l.add(s.substring(pos, delPos));
165: pos = delPos + delim.length();
166: }
167: if (pos <= s.length()) {
168: // add rest of String
169: l.add(s.substring(pos));
170: }
171:
172: String[] temp = (String[]) l.toArray(new String[l.size()]);
173: if (temp == null) {
174: return null;
175: } else {
176: int[] tempInt = new int[temp.length];
177: for (int i = 0; i < temp.length; i++) {
178: tempInt[i] = Integer.parseInt(temp[i]);
179: }
180: return tempInt;
181: }
182: }
183:
184: /**
185: * Gets a checkbox parameter value as a boolean.
186: * @param request The HttpServletRequest object, known as "request" in a
187: * JSP page.
188: * @param paramName The name of the parameter you want to get
189: * @return True if the value of the checkbox is "on", false otherwise.
190: */
191: public static boolean getCheckboxParameter(
192: HttpServletRequest request, String paramName) {
193: String temp = request.getParameter(paramName);
194: if (temp != null && temp.equals("on")) {
195: return true;
196: } else {
197: return false;
198: }
199: }
200:
201: /**
202: * Gets a parameter as a string.
203: * @param request The HttpServletRequest object, known as "request" in a
204: * JSP page.
205: * @param attribName The name of the parameter you want to get
206: * @return The value of the parameter or null if the parameter was not
207: * found or if the parameter is a zero-length string.
208: */
209: public static String getAttribute(HttpServletRequest request,
210: String attribName) {
211: return getAttribute(request, attribName, false);
212: }
213:
214: /**
215: * Gets a parameter as a string.
216: * @param request The HttpServletRequest object, known as "request" in a
217: * JSP page.
218: * @param attribName The name of the parameter you want to get
219: * @param emptyStringsOK Return the parameter values even if it is an empty string.
220: * @return The value of the parameter or null if the parameter was not
221: * found.
222: */
223: public static String getAttribute(HttpServletRequest request,
224: String attribName, boolean emptyStringsOK) {
225: String temp = (String) request.getAttribute(attribName);
226: if (temp != null) {
227: if (temp.equals("") && !emptyStringsOK) {
228: return null;
229: } else {
230: return temp;
231: }
232: } else {
233: return null;
234: }
235: }
236:
237: /**
238: * Gets an attribute as a boolean.
239: * @param request The HttpServletRequest object, known as "request" in a
240: * JSP page.
241: * @param attribName The name of the attribute you want to get
242: * @return True if the value of the attribute is "true", false otherwise.
243: */
244: public static boolean getBooleanAttribute(
245: HttpServletRequest request, String attribName) {
246: String temp = (String) request.getAttribute(attribName);
247: if (temp != null && temp.equals("true")) {
248: return true;
249: } else {
250: return false;
251: }
252: }
253:
254: /**
255: * Gets an attribute as a int.
256: * @param request The HttpServletRequest object, known as "request" in a
257: * JSP page.
258: * @param attribName The name of the attribute you want to get
259: * @return The int value of the attribute or the default value if the attribute is not
260: * found or is a zero length string.
261: */
262: public static int getIntAttribute(HttpServletRequest request,
263: String attribName, int defaultNum) {
264: String temp = (String) request.getAttribute(attribName);
265: if (temp != null && !temp.equals("")) {
266: int num = defaultNum;
267: try {
268: num = Integer.parseInt(temp);
269: } catch (Exception ignored) {
270: }
271: return num;
272: } else {
273: return defaultNum;
274: }
275: }
276:
277: }
|