org.x4juli.global.helper
Class OptionConverter

java.lang.Object
  extended byorg.x4juli.global.helper.OptionConverter

public final class OptionConverter
extends java.lang.Object

A convenience class to convert property values to specific types.

Logging API as a whole was originally done for Apache log4j. Juli is a port of main parts of that to complete the Java Logging APIs. All credits for initial idea, design, implementation, documentation belong to the log4j crew. This file was originally published by Ceki Gülcü, Simon Kitching, Anders Kristensen, Avy Sharell. Please use exclusively the appropriate mailing lists for questions, remarks and contribution.

Since:
0.5
Author:
Boris Unckel

Field Summary
(package private) static java.lang.String DELIM_START
          Constant for delimiter start.
(package private) static int DELIM_START_LEN
          Constant for delimiter start length.
(package private) static char DELIM_STOP
          Constant for delimiter stop.
(package private) static int DELIM_STOP_LEN
          Constant for delimiter stop length.
 
Constructor Summary
private OptionConverter()
          No instanciation wanted.
 
Method Summary
static java.lang.String[] concatanateArrays(java.lang.String[] l, java.lang.String[] r)
          Concatanate two String arrays.
static java.lang.String convertSpecialChars(java.lang.String s)
          White- and Controlchar conversion.
static java.lang.String[] extractDefaultReplacement(java.lang.String key)
          Splits up a given String into in String[2] Array.
static java.lang.String findAndSubst(java.lang.String key, java.util.Properties props)
          Find the value corresponding to key in props.
private static java.util.logging.Logger getLogger()
           
static java.lang.String getSystemProperty(java.lang.String key, java.lang.String def)
          Very similar to System.getProperty except that the SecurityException is hidden.
static java.lang.Object instantiateByClassName(java.lang.String className, java.lang.Class superClass, java.lang.Object defaultValue)
          Instantiate an object given a class name.
static java.lang.Object instantiateByKey(java.util.Properties props, java.lang.String key, java.lang.Class superClass, java.lang.Object defaultValue)
          Instantiate an object by given keys.
static java.lang.String stripDuplicateBackslashes(java.lang.String src)
          Replaces double backslashes.
static java.lang.String substVars(java.lang.String val, java.util.Properties props)
          Perform variable substitution in string val from the values of keys found the properties passed as parameter or in the system propeties.
static boolean toBoolean(java.lang.String value, boolean dEfault)
          Converts a String to an boolean.
static long toFileSize(java.lang.String value, long dEfault)
          Parsing a string with given KB, MB, or GB filesize.
static int toInt(java.lang.String value, int dEfault)
          Converts a string to an int.
static java.util.logging.Level toLevel(java.lang.String value, java.util.logging.Level defaultValue)
          Converts a standard or custom priority level to a Level object.
static long toLong(java.lang.String value, long dEfault)
          Converts a String to a long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELIM_START

static final java.lang.String DELIM_START
Constant for delimiter start.
Value is "${".

See Also:
Constant Field Values

DELIM_STOP

static final char DELIM_STOP
Constant for delimiter stop.
Value is 125.

See Also:
Constant Field Values

DELIM_START_LEN

static final int DELIM_START_LEN
Constant for delimiter start length.
Value is 2.

See Also:
Constant Field Values

DELIM_STOP_LEN

static final int DELIM_STOP_LEN
Constant for delimiter stop length.
Value is 1.

See Also:
Constant Field Values
Constructor Detail

OptionConverter

private OptionConverter()
No instanciation wanted.

Method Detail

concatanateArrays

public static java.lang.String[] concatanateArrays(java.lang.String[] l,
                                                   java.lang.String[] r)
Concatanate two String arrays. Fast due to use of System.arraycopy.

Parameters:
l - first array
r - second arry
Returns:
concated array

convertSpecialChars

public static java.lang.String convertSpecialChars(java.lang.String s)
White- and Controlchar conversion.

Parameters:
s - the string to convert
Returns:
the string with converted chars

getSystemProperty

public static java.lang.String getSystemProperty(java.lang.String key,
                                                 java.lang.String def)
Very similar to System.getProperty except that the SecurityException is hidden.

Parameters:
key - The key to search for.
def - The default value to return.
Returns:
the string value of the system property, or the default value if there is no property with that key.
Since:
0.5

instantiateByKey

public static java.lang.Object instantiateByKey(java.util.Properties props,
                                                java.lang.String key,
                                                java.lang.Class superClass,
                                                java.lang.Object defaultValue)
Instantiate an object by given keys.

Parameters:
props - to look in and replace.
key - to search for.
superClass - of the object.
defaultValue - default Object, if instantion fails.
Returns:
the instantiated Object.
Since:
0.5

toBoolean

public static boolean toBoolean(java.lang.String value,
                                boolean dEfault)
Converts a String to an boolean.

If value is "true", then true is returned. If value is "false", then true is returned. Otherwise, default is returned.

Case of value is unimportant.

Parameters:
value - the value
dEfault - returned if parsing fails
Since:
0.5

toInt

public static int toInt(java.lang.String value,
                        int dEfault)
Converts a string to an int.

Parameters:
value -
dEfault -
Returns:
the parsed int, on error the default
Since:
0.5

toLong

public static long toLong(java.lang.String value,
                          long dEfault)
Converts a String to a long.

Parameters:
value -
dEfault -
Returns:
the parsed long, on error the default
Since:
0.5

toFileSize

public static long toFileSize(java.lang.String value,
                              long dEfault)
Parsing a string with given KB, MB, or GB filesize.

Parameters:
value - the string with the size (i.e. 9KB, 10MB, 1GB...)
dEfault - the value returned if parsing fails
Returns:
the parsed filesize
Since:
0.5

findAndSubst

public static java.lang.String findAndSubst(java.lang.String key,
                                            java.util.Properties props)
Find the value corresponding to key in props. Then perform variable substitution on the found value.

Since:
0.5

instantiateByClassName

public static java.lang.Object instantiateByClassName(java.lang.String className,
                                                      java.lang.Class superClass,
                                                      java.lang.Object defaultValue)
Instantiate an object given a class name. Check that the className is a subclass of superClass. If that test fails or the object could not be instantiated, then defaultValue is returned.

Parameters:
className - The fully qualified class name of the object to instantiate.
superClass - The class to which the new object should belong.
defaultValue - The object to return in case of non-fulfillment
Since:
0.5

substVars

public static java.lang.String substVars(java.lang.String val,
                                         java.util.Properties props)
Perform variable substitution in string val from the values of keys found the properties passed as parameter or in the system propeties.

The variable substitution delimeters are ${ and }.

For example, if the properties parameter contains a property "key1" set as "value1", then the call

 String s = OptionConverter.substituteVars("Value of key is ${key1}.");
 
will set the variable s to "Value of key is value1.".

If no value could be found for the specified key, then the system properties are searched, if the value could not be found there, then substitution defaults to the empty string.

For example, if system propeties contains no value for the key "inexistentKey", then the call

 String s = OptionConverter.subsVars("Value of inexistentKey is [${inexistentKey}]");
 
will set s to "Value of inexistentKey is []".

Nevertheless, it is possible to specify a default substitution value using the ":-" operator. For example, the call

 String s = OptionConverter.subsVars("Value of key is [${key2:-val2}]");
 
will set s to "Value of key is [val2]" even if the "key2" property is unset.

An IllegalArgumentException is thrown if val contains a start delimeter "${" which is not balanced by a stop delimeter "}".

Parameters:
val - The string on which variable substitution is performed.
Throws:
java.lang.IllegalArgumentException - if val is malformed.
Since:
0.5

extractDefaultReplacement

public static java.lang.String[] extractDefaultReplacement(java.lang.String key)
Splits up a given String into in String[2] Array.

Parameters:
key - format must be FIRST:-SECOND
Returns:
the splited array, in case of parse error the key
Since:
0.5

stripDuplicateBackslashes

public static java.lang.String stripDuplicateBackslashes(java.lang.String src)
Replaces double backslashes. (Except the leading doubles in UNC's) with single backslashes for compatibility with existing path specifications that were working around use of OptionConverter.convertSpecialChars in XML configuration files.

Parameters:
src - source string
Returns:
source string with double backslashes replaced
Since:
0.5

toLevel

public static java.util.logging.Level toLevel(java.lang.String value,
                                              java.util.logging.Level defaultValue)
Converts a standard or custom priority level to a Level object.

If value is of form "level#classname", then the specified class' toLevel method is called to process the specified level string; if no '#' character is present, then the default Level class is used to process the level value.

As a special case, if the value parameter is equal to the string "NULL", then the value null will be returned.

If any error occurs while converting the value to a level, the defaultValue parameter, which may be null, is returned.

Case of value is insignificant for the level level, but is significant for the class name part, if present.

Since:
0.5

getLogger

private static java.util.logging.Logger getLogger()
Returns:
the internal logger for this class
Since:
0.5


${license_html}