org.deegree.commons.utils.kvp
Class KVPUtils

java.lang.Object
  extended by org.deegree.commons.utils.kvp.KVPUtils

public class KVPUtils
extends java.lang.Object

This class contains convenience methods for working with key-value pair maps (e.g. from OGC KVP requests).

Version:
$Revision: 26022 $, $Date: 2010-08-23 16:59:38 +0200 (Mo, 23. Aug 2010) $
Author:
Markus Schneider, Oliver Tonnhofer, last edited by: $Author: aschmitz $

Constructor Summary
KVPUtils()
           
 
Method Summary
static boolean getBoolean(java.util.Map<java.lang.String,java.lang.String> kvpParams, java.lang.String paramName, boolean defaultValue)
          Returns the specified parameter from a KVP map as a boolean value.
static java.lang.String getDefault(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key, java.lang.String defaultValue)
          Returns the value of the key or the default value if the kvp map doesn't contain the key.
static double getDefaultDouble(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key, double defaultValue)
           
static int getInt(java.util.Map<java.lang.String,java.lang.String> kvpParams, java.lang.String paramName, int defaultValue)
          Returns the specified parameter from a KVP map as an integer value.
static java.util.Map<java.lang.String,java.lang.String> getNormalizedKVPMap(java.lang.String queryString, java.lang.String encoding)
           
static java.lang.String getRequired(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key)
          Returns the value of the key.
static double getRequiredDouble(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key)
           
static int getRequiredInt(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key)
           
static java.util.Map<java.lang.String,java.lang.String> readFileIntoMap(java.net.URL url)
          Reads a text file with KVP content into a map.
static java.util.List<java.lang.String> splitAll(java.util.Map<java.lang.String,java.lang.String> param, java.lang.String key)
          Return a list with all values for a key.
static java.lang.String toQueryString(java.util.Map<java.lang.String,java.lang.String> map)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KVPUtils

public KVPUtils()
Method Detail

getRequired

public static java.lang.String getRequired(java.util.Map<java.lang.String,java.lang.String> param,
                                           java.lang.String key)
                                    throws MissingParameterException
Returns the value of the key. Throws an exception if the kvp map doesn't contain the key.

Parameters:
param - the key-value map
key -
Returns:
the value
Throws:
MissingParameterException - if the kvp map doesn't contain the key

getRequiredInt

public static int getRequiredInt(java.util.Map<java.lang.String,java.lang.String> param,
                                 java.lang.String key)
                          throws MissingParameterException,
                                 InvalidParameterValueException
Parameters:
param -
key -
Returns:
the parsed value as an integer
Throws:
MissingParameterException - if the kvp map doesn't contain the key
InvalidParameterValueException - if the value is not an integer

getRequiredDouble

public static double getRequiredDouble(java.util.Map<java.lang.String,java.lang.String> param,
                                       java.lang.String key)
                                throws MissingParameterException,
                                       InvalidParameterValueException
Parameters:
param -
key -
Returns:
the parsed value as a double
Throws:
MissingParameterException - if the kvp map doesn't contain the key
InvalidParameterValueException - if the value is not a double

getDefaultDouble

public static double getDefaultDouble(java.util.Map<java.lang.String,java.lang.String> param,
                                      java.lang.String key,
                                      double defaultValue)
Parameters:
param -
key -
defaultValue - to be used if missing.
Returns:
the parsed value as a double or if the given key was not found or the value was not a number, the default value.

getDefault

public static java.lang.String getDefault(java.util.Map<java.lang.String,java.lang.String> param,
                                          java.lang.String key,
                                          java.lang.String defaultValue)
Returns the value of the key or the default value if the kvp map doesn't contain the key.

Parameters:
param - the key-value map
key -
defaultValue -
Returns:
the value

splitAll

public static java.util.List<java.lang.String> splitAll(java.util.Map<java.lang.String,java.lang.String> param,
                                                        java.lang.String key)
Return a list with all values for a key. Values are splitted if they contain multiple comma delimited values.

Parameters:
param - the key-value map
key -
Returns:
a list with all values

getBoolean

public static boolean getBoolean(java.util.Map<java.lang.String,java.lang.String> kvpParams,
                                 java.lang.String paramName,
                                 boolean defaultValue)
                          throws InvalidParameterValueException
Returns the specified parameter from a KVP map as a boolean value.

Parameters:
kvpParams - KVP map
paramName - name of the parameter
defaultValue - returned when the specified parameter is not present in the map (=null)
Returns:
the specified parameter value as a boolean value
Throws:
InvalidParameterValueException - if the parameter value is neither null nor "true" nor "false"

getInt

public static int getInt(java.util.Map<java.lang.String,java.lang.String> kvpParams,
                         java.lang.String paramName,
                         int defaultValue)
                  throws InvalidParameterValueException
Returns the specified parameter from a KVP map as an integer value.

Parameters:
kvpParams - KVP map
paramName - name of the parameter
defaultValue - returned when the specified parameter is not present in the map (=null)
Returns:
the specified parameter value as an integer value
Throws:
InvalidParameterValueException - if the parameter value does not denote an integer

readFileIntoMap

public static java.util.Map<java.lang.String,java.lang.String> readFileIntoMap(java.net.URL url)
                                                                        throws java.io.IOException
Reads a text file with KVP content into a map.

Example contents:

 SERVICE=WFS
 VERSION=1.1.0
 REQUEST=DescribeFeatureType
 TYPENAME=TreesA_1M
 
What this method does:

Parameters:
url - url of the text file
Returns:
map with the contents of the file, keys are uppercased
Throws:
java.io.IOException - if the the file cannot be loaded

getNormalizedKVPMap

public static java.util.Map<java.lang.String,java.lang.String> getNormalizedKVPMap(java.lang.String queryString,
                                                                                   java.lang.String encoding)
                                                                            throws java.io.UnsupportedEncodingException
Parameters:
queryString -
encoding - used for decoding the URL-decoded characters, may be null (turns on guessing, don't do it)
Returns:
a map with the query string's kvps parsed (uppercase keys)
Throws:
java.io.UnsupportedEncodingException

toQueryString

public static java.lang.String toQueryString(java.util.Map<java.lang.String,java.lang.String> map)
Parameters:
map -
Returns:
an UTF-8 URL encoded query string from the kvps in the map


Copyright © 2011. All Rights Reserved.