1 5 package com.oreilly.servlet; 6 7 import java.io.*; 8 import java.util.*; 9 import javax.servlet.*; 10 11 63 public class ParameterParser { 64 65 private ServletRequest req; 66 private String encoding; 67 68 74 public ParameterParser(ServletRequest req) { 75 this.req = req; 76 } 77 78 87 public void setCharacterEncoding(String encoding) 88 throws UnsupportedEncodingException { 89 new String ("".getBytes("8859_1"), encoding); 91 this.encoding = encoding; 93 } 94 95 103 public String getStringParameter(String name) 104 throws ParameterNotFoundException { 105 String [] values = req.getParameterValues(name); 106 if (values == null) { 107 throw new ParameterNotFoundException(name + " not found"); 108 } 109 else if (values[0].length() == 0) { 110 throw new ParameterNotFoundException(name + " was empty"); 111 } 112 else { 113 if (encoding == null) { 114 return values[0]; 115 } 116 else { 117 try { 118 return new String (values[0].getBytes("8859_1"), encoding); 119 } 120 catch (UnsupportedEncodingException e) { 121 return values[0]; } 123 } 124 } 125 } 126 127 136 public String getStringParameter(String name, String def) { 137 try { return getStringParameter(name); } 138 catch (Exception e) { return def; } 139 } 140 141 152 public boolean getBooleanParameter(String name) 153 throws ParameterNotFoundException, NumberFormatException { 154 String value = getStringParameter(name).toLowerCase(); 155 if ((value.equalsIgnoreCase("true")) || 156 (value.equalsIgnoreCase("on")) || 157 (value.equalsIgnoreCase("yes"))) { 158 return true; 159 } 160 else if ((value.equalsIgnoreCase("false")) || 161 (value.equalsIgnoreCase("off")) || 162 (value.equalsIgnoreCase("no"))) { 163 return false; 164 } 165 else { 166 throw new NumberFormatException ("Parameter " + name + " value " + value + 167 " is not a boolean"); 168 } 169 } 170 171 179 public boolean getBooleanParameter(String name, boolean def) { 180 try { return getBooleanParameter(name); } 181 catch (Exception e) { return def; } 182 } 183 184 193 public byte getByteParameter(String name) 194 throws ParameterNotFoundException, NumberFormatException { 195 return Byte.parseByte(getStringParameter(name)); 196 } 197 198 207 public byte getByteParameter(String name, byte def) { 208 try { return getByteParameter(name); } 209 catch (Exception e) { return def; } 210 } 211 212 220 public char getCharParameter(String name) 221 throws ParameterNotFoundException { 222 String param = getStringParameter(name); 223 if (param.length() == 0) 224 throw new ParameterNotFoundException(name + " is empty string"); 225 else 226 return (param.charAt(0)); 227 } 228 229 237 public char getCharParameter(String name, char def) { 238 try { return getCharParameter(name); } 239 catch (Exception e) { return def; } 240 } 241 242 251 public double getDoubleParameter(String name) 252 throws ParameterNotFoundException, NumberFormatException { 253 return new Double (getStringParameter(name)).doubleValue(); 254 } 255 256 264 public double getDoubleParameter(String name, double def) { 265 try { return getDoubleParameter(name); } 266 catch (Exception e) { return def; } 267 } 268 269 278 public float getFloatParameter(String name) 279 throws ParameterNotFoundException, NumberFormatException { 280 return new Float (getStringParameter(name)).floatValue(); 281 } 282 283 291 public float getFloatParameter(String name, float def) { 292 try { return getFloatParameter(name); } 293 catch (Exception e) { return def; } 294 } 295 296 305 public int getIntParameter(String name) 306 throws ParameterNotFoundException, NumberFormatException { 307 return Integer.parseInt(getStringParameter(name)); 308 } 309 310 318 public int getIntParameter(String name, int def) { 319 try { return getIntParameter(name); } 320 catch (Exception e) { return def; } 321 } 322 323 332 public long getLongParameter(String name) 333 throws ParameterNotFoundException, NumberFormatException { 334 return Long.parseLong(getStringParameter(name)); 335 } 336 337 345 public long getLongParameter(String name, long def) { 346 try { return getLongParameter(name); } 347 catch (Exception e) { return def; } 348 } 349 350 359 public short getShortParameter(String name) 360 throws ParameterNotFoundException, NumberFormatException { 361 return Short.parseShort(getStringParameter(name)); 362 } 363 364 372 public short getShortParameter(String name, short def) { 373 try { return getShortParameter(name); } 374 catch (Exception e) { return def; } 375 } 376 377 384 public String [] getMissingParameters(String [] required) { 385 Vector missing = new Vector(); 386 for (int i = 0; i < required.length; i++) { 387 String val = getStringParameter(required[i], null); 388 if (val == null) { 389 missing.addElement(required[i]); 390 } 391 } 392 if (missing.size() == 0) { 393 return null; 394 } 395 else { 396 String [] ret = new String [missing.size()]; 397 missing.copyInto(ret); 398 return ret; 399 } 400 } 401 } 402 | Popular Tags |