/**
* Author: $Author: gus $
* Last modified: $Date: 2001/01/09 16:12:04 $
* Revision number: $Revision: 1.2 $
**/
package aurora.servlet;
import java.io.*;
import java.sql.*;
import java.net.*;
import java.util.*;
import aurora.Spect;
import javax.servlet.*;
import javax.servlet.http.*;
/**
A utility class for getting values out of a request. The HttpServletRequest is
pretty limited in what you can get out of it. And to top it off, different
browsers handle how they send parameters differently. If a form value is blank,
some browsers send and empty string ("") as the parameter, and some browsers just
don't send the parameter. To fix this, Params takes any parameters that have an
empty string and removes them.
It also has special functions that will return int's, booleans, trimed strings,
and a method to provide a default value if the parameter is not present.
@author August Mueller
*/
public class Params
{
/** Used to store the request parameters */
private Hashtable params = new Hashtable();
/**
Constructor for creating the Params object.
Iterates through all the parameter names in the request object and sets them in params.
*/
public Params (HttpServletRequest request) {
Enumeration enum = request.getParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String values = request.getParameter(name);
if (values != null) {
if (!"".equals(values.trim()))
setP (name, values);
}
}
}
/**
returns an enumeration of parameter names.
*/
public Enumeration getPNames() {
return params.keys();
}
/** get a parameter value and trim it before returning
@param parameter the name of the parameter you want to get.
*/
public String getPt(String parameter) {
String s = getP(parameter);
return s == null ? "" : s.trim();
}
/**
Check and see if a value is present for the name.
@param parameter the name of the parameter you want to test.
*/
public boolean getPb(String parameter) {
return getP(parameter) != null;
}
/**
Set a parameter. Unlike HttpServletRequest, you can change the values
progmaticly if you wish.
@param parameter the name of the parameter
@param value the value of the parameter
*/
public void setP(String parameter, String value) {
params.put(parameter, value);
}
/**
Remove a parameter from the list.
@param parameter the name of the parameter you wish to remove
*/
public void removeP(String parameter) {
params.remove(parameter);
}
/**
Get a parameter value.
Returns null if parameter name is not present.
@param parameter the name of the parameter you want to get.
*/
public String getP(String parameter) {
return (String)params.get(parameter);
}
/**
Get a parameter value.
Returns the default_value passed if parameter name is not present.
@param parameter the name of the parameter you want to get.
@param default_value the value you want returned if the parameter value isn't present.
*/
public String getP(String parameter, String default_value) {
String s = (String)params.get(parameter);
return s == null ? default_value : s;
}
/**
Get a parameter value and trim it before returning.
Returns the default_value passed if parameter name is not present.
@param parameter the name of the parameter you want to get.
@param default_value the value you want returned if the parameter value isn't present.
*/
public String getPt(String parameter, String default_value) {
return getP(parameter, default_value).trim();
}
/**
Get a parameter value as an integer.
-1 is returned if the value is not present or cannot be cast into an int.
@param parameter the name of the parameter you want to get.
*/
public int getPi(String parameter) {
int returnable = -1;
try {
returnable = Integer.parseInt((String)params.get(parameter));
}
catch(NumberFormatException e) {
returnable = -1;
}
return returnable;
}
/**
Get a parameter value as an integer.
default_value is returned if the value is not present or cannot be cast into an int.
@param parameter the name of the parameter you want to get.
@param default_value the default value.
*/
public int getPi(String parameter, int default_value) {
int returnable = default_value;
try {
returnable = Integer.parseInt((String)params.get(parameter));
}
catch(NumberFormatException e) {
returnable = default_value;
}
return returnable;
}
public void match(Object o) {
match(o, false);
}
public void match(Object o, boolean trim) {
try {
Enumeration e = params.keys();
while ( e.hasMoreElements() ) {
String name = (String) e.nextElement();
String value = (String) params.get(name);
if (trim)
value = value.trim();
Spect.setIt(o, name, value);
}
}
catch(Exception e) {
e.printStackTrace(System.out);
}
}
public Enumeration keys() {
return params.keys();
}
public String toString() {
StringBuffer sb = new StringBuffer();
Enumeration keys = keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
sb.append(key).append(":").append(getP(key)).append("\n");
}
return sb.toString();
}
public Hashtable getHashtable() {
return params;
}
}