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

java.lang.Object
  extended byorg.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.

 Template example(s):
   $pp.foo                ->  bar
   $pp.getNumber('baz')   ->  12.6
   $pp.getInt('baz')      ->  12
   $pp.getNumbers('foo')  ->  [12.6]

 Toolbox configuration:
 <tool>
   <key>pp</key>
   <scope>request</scope>
   <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
 </tool>
 

When used as a view tool, this should only be used in the request scope. This class is, however, quite useful in your application's controller, filter, or action code as well as in templates.

Version:
$Revision: 1.8.2.1 $ $Date: 2004/05/17 17:09:46 $
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.

Parameters:
request - the ServletRequest to be parsed
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

Parameters:
request - the ServletRequest to be parsed

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)
Parameters:
key - the desired parameter's key
alternate - The alternate value
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)
Parameters:
key - the desired parameter's 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)
Parameters:
key - the desired parameter's key
alternate - The alternate boolean value
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)
Parameters:
key - the desired parameter's key
alternate - the alternate Boolean
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)
Parameters:
key - the desired parameter's 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)
Parameters:
key - the desired parameter's key
alternate - The alternate Number
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)
Parameters:
key - the desired parameter's key
alternate - The alternate int value
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)
Parameters:
key - the desired parameter's key
alternate - The alternate double value
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)
Parameters:
key - the key for the desired parameter
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)
Parameters:
key - the key for the desired parameter
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)
Parameters:
key - the key for the desired parameter
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)
Parameters:
key - the key for the desired parameter
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
Throws:
java.lang.NumberFormatException


Copyright (c) 2003 Apache Software Foundation