org.apache.stanbol.enhancer.servicesapi.helper
Class ConfigUtils

java.lang.Object
  extended by org.apache.stanbol.enhancer.servicesapi.helper.ConfigUtils

public final class ConfigUtils
extends java.lang.Object

Utility Class with methods allowing to parse line based configurations formatted like:

   <key1>;<parm1>=<value1>,<value2>;<parm2>=<value1>...
   <key2>;<state>
   <key3>;<state>=true
   <key4>;<parm1>=<value1>;<parm1>=<value2>
 
Rules:

This is intended to be used to parse richer configurations form OSGI configuration files.

Author:
Rupert Westenthaler

Method Summary
static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParameters(java.lang.String[] elements, int start)
          Utility that parses 'key=value,value2' parameters from the parsed array.
static boolean getState(java.util.Map<java.lang.String,java.util.List<java.lang.String>> parameters, java.lang.String key)
          Parses the boolean value form the values for the parsed key.
static java.lang.String getValue(java.util.Map<java.lang.String,java.util.List<java.lang.String>> parameters, java.lang.String key)
          Getter for the first value of a given key
static java.lang.String guessRdfFormat(java.lang.String extension)
          Guesses the RDF format based on the provided file extension.
static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfig(java.lang.Iterable<java.lang.String> configuration)
          Parses configurations formatted like
static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfig(java.util.Iterator<java.lang.String> confIterator)
          Parses configurations formatted like
static java.util.Map.Entry<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfigEntry(java.lang.String line)
          Returns the "key, parameter" entry parsed form the parsed configuration line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

guessRdfFormat

public static java.lang.String guessRdfFormat(java.lang.String extension)
Guesses the RDF format based on the provided file extension. Parsing null will return the default format.

Parameters:
extension - the extension or null to ask for the default
Returns:
the mime type or null if the parsed extension is not known.

parseConfig

public static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfig(java.lang.Iterable<java.lang.String> configuration)
Parses configurations formatted like
   <key1>;<parm1>=<value1>,<value2>;<parm2>=<value1>...
   <key2>;<state>
   <key3>;<state>=true
   <key4>;<parm1>=<value1>;<parm1>=<value2>
 
Rules:

Parameters:
configuration - The configuration
Throws:
java.lang.IllegalArgumentException - on any syntax error in the parsed configuration

parseConfig

public static java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfig(java.util.Iterator<java.lang.String> confIterator)
Parses configurations formatted like
   <key1>;<parm1>=<value1>,<value2>;<parm2>=<value1>...
   <key2>;<state>
   <key3>;<state>=true
   <key4>;<parm1>=<value1>;<parm1>=<value2>
 
Rules:

Parameters:
confIterator - The Iterator over the lines of the configuration.
Throws:
java.lang.IllegalArgumentException - on any syntax error in the parsed configuration

parseConfigEntry

public static java.util.Map.Entry<java.lang.String,java.util.Map<java.lang.String,java.util.List<java.lang.String>>> parseConfigEntry(java.lang.String line)
Returns the "key, parameter" entry parsed form the parsed configuration line.

This method is useful if the caller need to preserve the oder of multi line configurations and therefore can not use the parseConfig methods.

Parameters:
line - a configuration line
Returns:
the key, parameter entry

getState

public static boolean getState(java.util.Map<java.lang.String,java.util.List<java.lang.String>> parameters,
                               java.lang.String key)
Parses the boolean value form the values for the parsed key. The state is assumed as true if the key is present and the value is either an empty list of the first element of the list evaluates to Boolean.parseBoolean(String) == true.

Parameters:
parameters - the parameter
key - the key
Returns:
the boolean state of the requested key

getValue

public static java.lang.String getValue(java.util.Map<java.lang.String,java.util.List<java.lang.String>> parameters,
                                        java.lang.String key)
Getter for the first value of a given key

Parameters:
parameters - the parameters
key - the key
Returns:
the first value or null if the key is not present or the list is empty.

getParameters

public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> getParameters(java.lang.String[] elements,
                                                                                             int start)
Utility that parses 'key=value,value2' parameters from the parsed array. If key appear multiple time values are appended.

Parameters:
elements - the elements
start - the start position within the parsed array to start parsing
Returns:
the parsed parameters. If no value is provided for a key the value of entries will be NOT null but en empty list. The returned Map provides read and write access.
Throws:
IllegalArgumentExeption - if elements are illegal formatted (e.g. if the start with an '='


Copyright © 2010-2012 The Apache Software Foundation. All Rights Reserved.