001 import java.io.*;
002 import javax.servlet.*;
003
004 public class ParameterParser {
005
006 private ServletRequest req;
007
008 public ParameterParser(ServletRequest request) {
009 this.req = request;
010 }
011
012 public String getStringParameter(String name) throws ParameterNotFoundException {
013
014 String value = req.getParameter(name);
015 if (value == null)
016 throw new ParameterNotFoundException(name + " not found");
017 else if (value.length() == 0)
018 throw new ParameterNotFoundException(name + " was empty");
019 else return value;
020 }
021
022 public String getStringParameter(String name, String def) {
023 try { return getStringParameter(name); }
024 catch (Exception e) { return def; }
025 }
026
027 public boolean getBooleanParameter(String name) throws ParameterNotFoundException {
028 return new Boolean(getStringParameter(name)).booleanValue();
029 }
030
031 public boolean getBooleanParameter(String name, boolean def) {
032 try { return getBooleanParameter(name); }
033 catch (Exception e) { return def; }
034 }
035
036 public byte getByteParameter(String name)
037 throws ParameterNotFoundException, NumberFormatException {
038 return Byte.parseByte(getStringParameter(name));
039 }
040
041 public byte getByteParameter(String name, byte def) {
042 try { return getByteParameter(name); }
043 catch (Exception e) { return def; }
044 }
045
046 public char getCharParameter(String name) throws ParameterNotFoundException {
047
048 String param = getStringParameter(name);
049 if (param.length() == 0)
050 throw new ParameterNotFoundException(name + " is empty string");
051 else
052 return (param.charAt(0));
053 }
054
055 public char getCharParameter(String name, char def) {
056 try { return getCharParameter(name); }
057 catch (Exception e) { return def; }
058 }
059
060 public double getDoubleParameter(String name)
061 throws ParameterNotFoundException, NumberFormatException {
062 return new Double(getStringParameter(name)).doubleValue();
063 }
064
065 public double getDoubleParameter(String name, double def) {
066 try { return getDoubleParameter(name); }
067 catch (Exception e) { return def; }
068 }
069
070 public float getFloatParameter(String name)
071 throws ParameterNotFoundException, NumberFormatException {
072 return new Float(getStringParameter(name)).floatValue();
073 }
074
075 public float getFloatParameter(String name, float def) {
076 try { return getFloatParameter(name); }
077 catch (Exception e) { return def; }
078 }
079
080 public int getIntParameter(String name)
081 throws ParameterNotFoundException, NumberFormatException {
082 return Integer.parseInt(getStringParameter(name));
083 }
084
085 public int getIntParameter(String name, int def) {
086 try { return getIntParameter(name); }
087 catch (Exception e) { return def; }
088 }
089
090 public long getLongParameter(String name)
091 throws ParameterNotFoundException, NumberFormatException {
092 return Long.parseLong(getStringParameter(name));
093 }
094
095 public long getLongParameter(String name, long def) {
096 try { return getLongParameter(name); }
097 catch (Exception e) { return def; }
098 }
099
100 public short getShortParameter(String name)
101 throws ParameterNotFoundException, NumberFormatException {
102 return Short.parseShort(getStringParameter(name));
103 }
104
105 public short getShortParameter(String name, short def) {
106 try { return getShortParameter(name); }
107 catch (Exception e) { return def; }
108 }
109 }
|