From 3bd9c276dab79a8dbff0394c41e4cab6e6fa443c Mon Sep 17 00:00:00 2001 From: shaper Date: Wed, 13 Feb 2002 19:35:50 +0000 Subject: [PATCH] Added getIntParameter(). git-svn-id: https://samskivert.googlecode.com/svn/trunk@574 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../servlet/util/ParameterUtil.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java b/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java index c7b6b67f..bee47836 100644 --- a/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java +++ b/projects/samskivert/src/java/com/samskivert/servlet/util/ParameterUtil.java @@ -1,5 +1,5 @@ // -// $Id: ParameterUtil.java,v 1.1 2001/10/31 09:44:22 mdb Exp $ +// $Id: ParameterUtil.java,v 1.2 2002/02/13 19:35:50 shaper Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -60,9 +60,35 @@ public class ParameterUtil public static int requireIntParameter ( HttpServletRequest req, String name, String invalidDataMessage) throws DataValidationException + { + return parseIntParameter( + getParameter(req, name, false), invalidDataMessage); + + } + + /** + * Fetches the supplied parameter from the request and converts it to + * an integer. If the parameter does not exist, defval is + * returned. If the parameter is not a well-formed integer, a data + * validation exception is thrown with the supplied message. + */ + public static int getIntParameter ( + HttpServletRequest req, String name, int defval, + String invalidDataMessage) + throws DataValidationException { String value = getParameter(req, name, false); - try { + if (StringUtil.blank(value)) { + return defval; + } + return parseIntParameter(value, invalidDataMessage); + } + + protected static int parseIntParameter ( + String value, String invalidDataMessage) + throws DataValidationException + { + try { return Integer.parseInt(value); } catch (NumberFormatException nfe) { throw new DataValidationException(invalidDataMessage);