org.apache.velocity.tools.view.tools
Class ParameterParser

java.lang.Object
  |
  +--org.apache.velocity.tools.view.tools.ParameterParser
All Implemented Interfaces:
ViewTool

public class ParameterParser
extends java.lang.Object
implements ViewTool

Utility class for easy parsing of ServletRequest parameters.

This class implements the ViewTool interface to allow it to be used as a request scoped tool.

It should be noted that this class is not thread-safe. As it is wholly dependent upon the current ServletRequest, therefore each ServletRequest should have its own instance. This tool should not be used in the session or application scopes of your webapp.

Version:
$Revision: 1.5 $ $Date: 2003/05/28 00:17:16 $
Author:
Nathan Bubna

Field Summary
private  javax.servlet.ServletRequest request
           
 
Constructor Summary
ParameterParser()
          Constructs a new instance
ParameterParser(javax.servlet.ServletRequest request)
          Constructs a new instance using the specified request.
 
Method Summary
 boolean exists(java.lang.String key)
          Convenience method for checking whether a certain parameter exists.
 java.lang.String get(java.lang.String key)
          Convenience method for use in Velocity templates.
 java.lang.Boolean getBoolean(java.lang.String key)
           
 boolean getBoolean(java.lang.String key, boolean alternate)
           
 java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean alternate)
           
 double getDouble(java.lang.String key, double alternate)
           
 double[] getDoubles(java.lang.String key)
           
 int getInt(java.lang.String key, int alternate)
           
 int[] getInts(java.lang.String key)
           
 java.lang.Number getNumber(java.lang.String key)
           
 java.lang.Number getNumber(java.lang.String key, java.lang.Number alternate)
           
 java.lang.Number[] getNumbers(java.lang.String key)
           
protected  javax.servlet.ServletRequest getRequest()
          Returns the current ServletRequest for this instance.
 java.lang.String getString(java.lang.String key)
           
 java.lang.String getString(java.lang.String key, java.lang.String alternate)
           
 java.lang.String[] getStrings(java.lang.String key)
           
 void init(java.lang.Object obj)
          Initializes this instance.
protected  java.lang.Number parseNumber(java.lang.String value)
          Converts a parameter value into a Number This is used as the base for all numeric parsing methods.
protected  void setRequest(javax.servlet.ServletRequest request)
          Sets the current ServletRequest
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

private javax.servlet.ServletRequest request
Constructor Detail

ParameterParser

public ParameterParser()
Constructs a new instance


ParameterParser

public ParameterParser(javax.servlet.ServletRequest request)
Constructs a new instance using the specified request.

Method Detail

init

public void init(java.lang.Object obj)
Initializes this instance.

Specified by:
init in interface ViewTool
Parameters:
obj - the current ViewContext or ServletRequest
Throws:
java.lang.IllegalArgumentException - if the param is not a ViewContext or ServletRequest

setRequest

protected void setRequest(javax.servlet.ServletRequest request)
Sets the current ServletRequest


getRequest

protected javax.servlet.ServletRequest getRequest()
Returns the current ServletRequest for this instance.

Returns:
the current ServletRequest
Throws:
java.lang.UnsupportedOperationException - if the request is null

exists

public boolean exists(java.lang.String key)
Convenience method for checking whether a certain parameter exists.

Parameters:
key - the parameter's key
Returns:
true if a parameter exists for the specified key; otherwise, returns false.

get

public java.lang.String get(java.lang.String key)
Convenience method for use in Velocity templates. This allows for easy "dot" access to parameters. e.g. $params.foo instead of $params.getString('foo')

Parameters:
key - the parameter's key
Returns:
parameter matching the specified key or null if there is no matching parameter

getString

public java.lang.String getString(java.lang.String key)
Parameters:
key - the parameter's key
Returns:
parameter matching the specified key or null if there is no matching parameter

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String alternate)
Returns:
parameter matching the specified key or the specified alternate String if there is no matching parameter

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key)
Returns:
a Boolean object for the specified key or null if no matching parameter is found

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean alternate)
Returns:
boolean value for the specified key or the alternate boolean is no value is found

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key,
                                    java.lang.Boolean alternate)
Returns:
a Boolean for the specified key or the specified alternate if no matching parameter is found

getNumber

public java.lang.Number getNumber(java.lang.String key)
Returns:
a Number for the specified key or null if no matching parameter is found

getNumber

public java.lang.Number getNumber(java.lang.String key,
                                  java.lang.Number alternate)
Returns:
a Number for the specified key or the specified alternate if no matching parameter is found

getInt

public int getInt(java.lang.String key,
                  int alternate)
Returns:
the int value for the specified key or the specified alternate value if no matching parameter is found

getDouble

public double getDouble(java.lang.String key,
                        double alternate)
Returns:
the double value for the specified key or the specified alternate value if no matching parameter is found

getStrings

public java.lang.String[] getStrings(java.lang.String key)
Returns:
an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist

getNumbers

public java.lang.Number[] getNumbers(java.lang.String key)
Returns:
an array of Number objects for the specified key or null if the parameter does not exist or the parameter does not contain Numbers.

getInts

public int[] getInts(java.lang.String key)
Returns:
an array of int values for the specified key or null if the parameter does not exist or the parameter does not contain ints.

getDoubles

public double[] getDoubles(java.lang.String key)
Returns:
an array of double values for the specified key or null if the parameter does not exist or the parameter does not contain doubles.

parseNumber

protected java.lang.Number parseNumber(java.lang.String value)
                                throws java.lang.NumberFormatException
Converts a parameter value into a Number This is used as the base for all numeric parsing methods. So, sub-classes can override to allow for customized number parsing. (e.g. to handle fractions, compound numbers, etc.)

Parameters:
value - the string to be parsed
Returns:
the value as a Number
java.lang.NumberFormatException


Copyright (c) 2003 Apache Software Foundation