eu.semaine.util
Class SEMAINEUtils

java.lang.Object
  extended by eu.semaine.util.SEMAINEUtils

public class SEMAINEUtils
extends java.lang.Object


Field Summary
static java.lang.String LOGPREFIX
           
 
Constructor Summary
SEMAINEUtils()
           
 
Method Summary
static java.io.File getConfigFile(java.lang.String configSystemProperty)
          Get a config file as defined in the given system property.
static java.io.File getConfigFile(java.lang.String configSystemProperty, java.lang.String defaultValue)
          Get a config file as defined in the given system property, using the given default value if the property is not set.
static org.apache.log4j.Logger getLogger(java.lang.Class clazz)
          Provide a Logger object whose name is built from SEMAINEUtils.LOGPREFIX and the name of the given class as a suffix.
static org.apache.log4j.Logger getLogger(java.lang.String nameSuffix)
          Provide a Logger object whose name is built from SEMAINEUtils.LOGPREFIX and the given nameSuffix.
static long getLongProperty(java.lang.String propertyName, long defaultValue)
          Try to get the system property with the given name as a long value.
static java.lang.String getReaderAsString(java.io.Reader reader)
           
static java.lang.String getStreamAsString(java.io.InputStream inputStream, java.lang.String encoding)
           
static boolean isLog4jConfigured()
          Returns true if it appears that log4j have been previously configured.
static java.lang.String locale2xmllang(java.util.Locale locale)
          Convert a locale into a string that is conform with XML's xml:lang attribute.
static float parseFloat(java.lang.String valueString, float defaultValue)
          Convert the given string into a float value, if possible.
static long parseLong(java.lang.String valueString, long defaultValue)
          Convert the given string into a long value, if possible.
static void setupLog4j()
          Set up the log4j logging system using a default configuration.
static java.util.Locale string2locale(java.lang.String localeString)
           
static java.util.Map<java.lang.String,java.lang.String> string2map(java.lang.String s, boolean keysFirst)
          For a given string containing lines of key-value pairs, construct a map of key-value pairs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGPREFIX

public static final java.lang.String LOGPREFIX
See Also:
Constant Field Values
Constructor Detail

SEMAINEUtils

public SEMAINEUtils()
Method Detail

isLog4jConfigured

public static boolean isLog4jConfigured()
Returns true if it appears that log4j have been previously configured. This code checks to see if there are any appenders defined for log4j which is the definitive way to tell if log4j is already initialized


setupLog4j

public static void setupLog4j()
Set up the log4j logging system using a default configuration.


getLogger

public static org.apache.log4j.Logger getLogger(java.lang.String nameSuffix)
Provide a Logger object whose name is built from SEMAINEUtils.LOGPREFIX and the given nameSuffix.

Parameters:
nameSuffix - the suffix to use for the logger name.
Returns:

getLogger

public static org.apache.log4j.Logger getLogger(java.lang.Class clazz)
Provide a Logger object whose name is built from SEMAINEUtils.LOGPREFIX and the name of the given class as a suffix.

Parameters:
the - class whose simple name to use asthe suffix for the logger name.
Returns:

getConfigFile

public static java.io.File getConfigFile(java.lang.String configSystemProperty)
Get a config file as defined in the given system property. If the setting in the system property is a relative path, then this path is interpreted as relative to the location of the system config file.

Parameters:
configSystemProperty - the name of a config setting containing the filename and path of the config file to load, e.g. "semaine.character-config".
Returns:
a file object representing the requested config file, or null if the config file could not be found.

getConfigFile

public static java.io.File getConfigFile(java.lang.String configSystemProperty,
                                         java.lang.String defaultValue)
Get a config file as defined in the given system property, using the given default value if the property is not set. If the setting in the system property is a relative path, then this path is interpreted as relative to the location of the system config file.

Parameters:
configSystemProperty - the name of a config setting containing the filename and path of the config file to load, e.g. "semaine.character-config".
Returns:
a file object representing the requested config file, or null if the config file could not be found.

getStreamAsString

public static java.lang.String getStreamAsString(java.io.InputStream inputStream,
                                                 java.lang.String encoding)
                                          throws java.io.IOException
Throws:
java.io.IOException

getReaderAsString

public static java.lang.String getReaderAsString(java.io.Reader reader)
                                          throws java.io.IOException
Throws:
java.io.IOException

string2locale

public static java.util.Locale string2locale(java.lang.String localeString)

locale2xmllang

public static java.lang.String locale2xmllang(java.util.Locale locale)
Convert a locale into a string that is conform with XML's xml:lang attribute. Basically it is language-COUNTRY, e.g. en-US.

Parameters:
locale - a locale, must not be null
Returns:
Throws:
java.lang.IllegalArgumentException - if locale is null

string2map

public static java.util.Map<java.lang.String,java.lang.String> string2map(java.lang.String s,
                                                                          boolean keysFirst)
For a given string containing lines of key-value pairs, construct a map of key-value pairs. This assumes that the String contains whitespace-separated key-value pairs as follows: key1 value1 key2 value2 Leading or trailing whitespace is ignored. If a line contains only a single field, it is treated as a key that maps to the empty string. Empty lines are ignored.

Parameters:
s -
keysFirst - if true, the first item in a line is the key, and the rest is the value; if false, the first item in the line is the value and the rest is the key.
Returns:

parseFloat

public static float parseFloat(java.lang.String valueString,
                               float defaultValue)
Convert the given string into a float value, if possible.

Parameters:
valueString - string to be converted into a float.
defaultValue - value to use if the string cannot be converted into a float.
Returns:
the best approximation that can be made for the float value given in valueString. If valueString cannot be converted into any float, returns defaultValue.

parseLong

public static long parseLong(java.lang.String valueString,
                             long defaultValue)
Convert the given string into a long value, if possible.

Parameters:
valueString - string to be converted into a long.
defaultValue - value to use if the string cannot be converted into a long.
Returns:
the best approximation that can be made for the long value given in valueString. If valueString cannot be converted into any long, returns defaultValue.

getLongProperty

public static long getLongProperty(java.lang.String propertyName,
                                   long defaultValue)
Try to get the system property with the given name as a long value. If the system property is not defined, or cannot be interpreted as a long, return defaultvalue.

Parameters:
propertyName -
defaultValue -
Returns:
the property as a long or the default value