001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025:
026: package org.nemesis.forum.webapp.front;
027:
028: import javax.servlet.http.*;
029:
030: /**
031: * This class assists skin writers in getting parameters.
032: */
033: public class ParamUtils {
034:
035: /**
036: * Gets a parameter as a string.
037: * @param request The HttpServletRequest object, known as "request" in a
038: * JSP page.
039: * @param paramName The name of the parameter you want to get
040: * @return The value of the parameter or null if the parameter was not
041: * found or if the parameter is a zero-length string.
042: */
043: public static String getParameter(HttpServletRequest request,
044: String paramName) {
045: return getParameter(request, paramName, false);
046: }
047:
048: /**
049: * Gets a parameter as a string.
050: * @param request The HttpServletRequest object, known as "request" in a
051: * JSP page.
052: * @param paramName The name of the parameter you want to get
053: * @param emptyStringsOK Return the parameter values even if it is an empty string.
054: * @return The value of the parameter or null if the parameter was not
055: * found.
056: */
057: public static String getParameter(HttpServletRequest request,
058: String paramName, boolean emptyStringsOK) {
059: String temp = request.getParameter(paramName);
060: if (temp != null) {
061: if (temp.equals("") && !emptyStringsOK) {
062: return null;
063: } else {
064: return temp;
065: }
066: } else {
067: return null;
068: }
069: }
070:
071: /**
072: * Gets a parameter as a boolean.
073: * @param request The HttpServletRequest object, known as "request" in a
074: * JSP page.
075: * @param paramName The name of the parameter you want to get
076: * @return True if the value of the parameter was "true", false otherwise.
077: */
078: public static boolean getBooleanParameter(
079: HttpServletRequest request, String paramName) {
080: String temp = request.getParameter(paramName);
081: if (temp != null && temp.equals("true")) {
082: return true;
083: } else {
084: return false;
085: }
086: }
087:
088: /**
089: * Gets a parameter as a int.
090: * @param request The HttpServletRequest object, known as "request" in a
091: * JSP page.
092: * @param paramName The name of the parameter you want to get
093: * @return The int value of the parameter specified or the default value if
094: * the parameter is not found.
095: */
096: public static int getIntParameter(HttpServletRequest request,
097: String paramName, int defaultNum) {
098: String temp = request.getParameter(paramName);
099: if (temp != null && !temp.equals("")) {
100: int num = defaultNum;
101: try {
102: num = Integer.parseInt(temp);
103: } catch (Exception ignored) {
104: }
105: return num;
106: } else {
107: return defaultNum;
108: }
109: }
110:
111: /**
112: * Gets a checkbox parameter value as a boolean.
113: * @param request The HttpServletRequest object, known as "request" in a
114: * JSP page.
115: * @param paramName The name of the parameter you want to get
116: * @return True if the value of the checkbox is "on", false otherwise.
117: */
118: public static boolean getCheckboxParameter(
119: HttpServletRequest request, String paramName) {
120: String temp = request.getParameter(paramName);
121: if (temp != null && temp.equals("on")) {
122: return true;
123: } else {
124: return false;
125: }
126: }
127:
128: /**
129: * Gets a parameter as a string.
130: * @param request The HttpServletRequest object, known as "request" in a
131: * JSP page.
132: * @param attribName The name of the parameter you want to get
133: * @return The value of the parameter or null if the parameter was not
134: * found or if the parameter is a zero-length string.
135: */
136: public static String getAttribute(HttpServletRequest request,
137: String attribName) {
138: return getAttribute(request, attribName, false);
139: }
140:
141: /**
142: * Gets a parameter as a string.
143: * @param request The HttpServletRequest object, known as "request" in a
144: * JSP page.
145: * @param attribName The name of the parameter you want to get
146: * @param emptyStringsOK Return the parameter values even if it is an empty string.
147: * @return The value of the parameter or null if the parameter was not
148: * found.
149: */
150: public static String getAttribute(HttpServletRequest request,
151: String attribName, boolean emptyStringsOK) {
152: String temp = (String) request.getAttribute(attribName);
153: if (temp != null) {
154: if (temp.equals("") && !emptyStringsOK) {
155: return null;
156: } else {
157: return temp;
158: }
159: } else {
160: return null;
161: }
162: }
163:
164: /**
165: * Gets an attribute as a boolean.
166: * @param request The HttpServletRequest object, known as "request" in a
167: * JSP page.
168: * @param attribName The name of the attribute you want to get
169: * @return True if the value of the attribute is "true", false otherwise.
170: */
171: public static boolean getBooleanAttribute(
172: HttpServletRequest request, String attribName) {
173: String temp = (String) request.getAttribute(attribName);
174: if (temp != null && temp.equals("true")) {
175: return true;
176: } else {
177: return false;
178: }
179: }
180:
181: /**
182: * Gets an attribute as a int.
183: * @param request The HttpServletRequest object, known as "request" in a
184: * JSP page.
185: * @param attribName The name of the attribute you want to get
186: * @return The int value of the attribute or the default value if the attribute is not
187: * found or is a zero length string.
188: */
189: public static int getIntAttribute(HttpServletRequest request,
190: String attribName, int defaultNum) {
191: String temp = (String) request.getAttribute(attribName);
192: if (temp != null && !temp.equals("")) {
193: int num = defaultNum;
194: try {
195: num = Integer.parseInt(temp);
196: } catch (Exception ignored) {
197: }
198: return num;
199: } else {
200: return defaultNum;
201: }
202: }
203:
204: }
|