com.jalios.util
Class JProperties

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<String,String>
          extended by com.jalios.util.JProperties
All Implemented Interfaces:
Serializable, Cloneable, Map<String,String>
Direct Known Subclasses:
LangProperties

public class JProperties
extends Hashtable<String,String>

This class, based on Properties manage properties file in UTF-8 encoding. Its save updateAndSave(File) methods keep layout of previous properties file.

Version:
$Revision: 31334 $
See Also:
Serialized Form

Field Summary
protected  Set<JPropertiesListener> listenerSet
           
protected  boolean propertyNameAsDefaultValue
           
static String REVISION
           
 
Constructor Summary
JProperties()
           
 
Method Summary
 void addProperties(JProperties prop)
          Add every property of the given JProperties to this JProperties.
 void addProperties(Properties prop)
          Add every property of the given Properties to this JProperties.
 void addPropertiesListener(JPropertiesListener l)
          Adds a listener for any change of this JProperties.
protected static String[] buildArrayFromString(String value, String[] defaultValue)
          Convert a string to a String[] using the following formats: "@value1value2...", where delim is a one character delimiter. "value1value2...", where delim is a character from " \t\n\r\f". Or return the default value if no values were found.
 void clearProperties()
          Clear all properties.
 boolean containsProperty(String name)
          Tests if this JProperties contains the given property key/name.
 void firePropertiesChange()
          Triggers a propertie change to notify all listener of this JProperties.
 boolean getBooleanProperty(String name, boolean defaultValue)
          Gets the value of a property as a boolean given its key/name.
 double getDoubleProperty(String name, double defaultValue)
          Gets the value of a property as a double value given its key/name.
 float getFloatProperty(String name, float defaultValue)
          Gets the value of a property as a float value given its key/name.
 int[] getIntArrayProperty(String name, int[] defaultValue)
          Gets the value of a property as a int array.
 int getIntegerProperty(String name, int defaultValue)
          Gets the value of a property as an int value given its key/name.
protected  Set<JPropertiesListener> getListenerSet()
          Retrieve set of all listeners for this JProperties.
 long getLongProperty(String name, long defaultValue)
          Gets the value of a property as a long value given its key/name.
 JProperties getProperties(String prefix)
          Returns a new property set of properties with key matching a given prefix.
 String getProperty(String name)
          Gets value of a property.
 String getProperty(String name, String defaultValue)
          Gets the value of a property given its key/name.
 String[] getStringArrayProperty(String name, String[] defaultValue)
          Gets the value of a property as a String array.
 Map<String,Object> getSubProperties(String prefix)
          Returns a map of properties built from those properties and having a given prefix.
Example:
prop:
foo.bar.case1.x: value of x for case1
foo.bar.case1.y: value of y for case1
foo.bar.case2.x: value of x for case2
foo.bar.case2.y: value of y for case2
foo.bar.case2.z.t: value of z for case2
 void load(File file)
          Reads a property file (key and element pairs) from the given File.
 void load(File file, String prefix)
          Reads a property file (key and element pairs) from the given File.
 void removePropertiesListener(JPropertiesListener l)
          Removes a listener for any change of this JProperties.
 Object setProperty(String name, String value)
          Sets value of a property.
 void updateAndSave(File file)
          Updates and save properties.
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values

propertyNameAsDefaultValue

protected boolean propertyNameAsDefaultValue

listenerSet

protected Set<JPropertiesListener> listenerSet
Constructor Detail

JProperties

public JProperties()
Method Detail

getListenerSet

protected Set<JPropertiesListener> getListenerSet()
Retrieve set of all listeners for this JProperties.

See Also:
addPropertiesListener(JPropertiesListener), removePropertiesListener(JPropertiesListener)

addPropertiesListener

public void addPropertiesListener(JPropertiesListener l)
Adds a listener for any change of this JProperties.

Parameters:
l - the listener to add
See Also:
removePropertiesListener(JPropertiesListener)

removePropertiesListener

public void removePropertiesListener(JPropertiesListener l)
Removes a listener for any change of this JProperties.

Parameters:
l - the listener to remove
See Also:
addPropertiesListener(JPropertiesListener)

firePropertiesChange

public void firePropertiesChange()
Triggers a propertie change to notify all listener of this JProperties.

See Also:
addPropertiesListener(JPropertiesListener)

containsProperty

public boolean containsProperty(String name)
Tests if this JProperties contains the given property key/name.

Parameters:
name - the key/name of the property to check
Returns:
true if the specified object is a key in this hashtable; false otherwise.

getProperty

public String getProperty(String name)
Gets value of a property.

Parameters:
name - the key/name of the property to retrieve
Returns:
the property value

getProperty

public String getProperty(String name,
                          String defaultValue)
Gets the value of a property given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null.
Returns:
the value of a property given its key/name.

setProperty

public Object setProperty(String name,
                          String value)
Sets value of a property.

Parameters:
name - the key/name of the property to retrieve
value - the new value to assign.
Returns:
the previous value of the specified key/name

getBooleanProperty

public boolean getBooleanProperty(String name,
                                  boolean defaultValue)
Gets the value of a property as a boolean given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null or is neither "false" nor "true" (case sensitive).
Returns:
the boolean value of the property

getIntegerProperty

public int getIntegerProperty(String name,
                              int defaultValue)
Gets the value of a property as an int value given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null or if Integer.parseInt() throws exception during String to int conversion.
Returns:
the int value of the property

getLongProperty

public long getLongProperty(String name,
                            long defaultValue)
Gets the value of a property as a long value given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null or if Long.parseLong() throws exception during String to long conversion.
Returns:
the long value of the property

getFloatProperty

public float getFloatProperty(String name,
                              float defaultValue)
Gets the value of a property as a float value given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null or if Float.parseFloat() throws exception during String to float conversion.
Returns:
the float value of the property

getDoubleProperty

public double getDoubleProperty(String name,
                                double defaultValue)
Gets the value of a property as a double value given its key/name.

Parameters:
name - the key/name of the property to retrieve
defaultValue - the default value to use if the existing value is null or if Double.parseDouble() throws exception during String to double conversion.
Returns:
the double value of the property

getStringArrayProperty

public String[] getStringArrayProperty(String name,
                                       String[] defaultValue)
Gets the value of a property as a String array.
The property value should be in one of the following forms:
  • "@value1value2...", where delim is a one character delimiter.
  • "value1value2...", where delim is a character from " \t\n\r\f".
  • Parameters:
    name - the key/name of the property to retrieve
    defaultValue - the default value to use if the existing value is null or no values could be found in the conversion.
    Returns:
    a String array

    buildArrayFromString

    protected static String[] buildArrayFromString(String value,
                                                   String[] defaultValue)
    Convert a string to a String[] using the following formats:
  • "@value1value2...", where delim is a one character delimiter.
  • "value1value2...", where delim is a character from " \t\n\r\f".
  • Or return the default value if no values were found.


    getIntArrayProperty

    public int[] getIntArrayProperty(String name,
                                     int[] defaultValue)
    Gets the value of a property as a int array. Uses same format of getStringArrayProperty(String, String[]).

    Parameters:
    name - the key/name of the property to retrieve
    defaultValue - the default value to use if the existing value is null or no values could be found in the conversion.
    Returns:
    an int array
    See Also:
    getStringArrayProperty(String, String[])

    addProperties

    public void addProperties(Properties prop)
    Add every property of the given Properties to this JProperties.

    Parameters:
    prop - the properties to be added

    addProperties

    public void addProperties(JProperties prop)
    Add every property of the given JProperties to this JProperties.

    Parameters:
    prop - the jproperties to be added

    getProperties

    public JProperties getProperties(String prefix)
    Returns a new property set of properties with key matching a given prefix.

    Parameters:
    prefix - the prefix the properties must match to be added to the returned JProperties instance, if null, all properties are added.
    Returns:
    a new property set of properties with key matching a given prefix.

    getSubProperties

    public Map<String,Object> getSubProperties(String prefix)
    Returns a map of properties built from those properties and having a given prefix.
    Example:
    prop:
    foo.bar.case1.x: value of x for case1
    foo.bar.case1.y: value of y for case1
    foo.bar.case2.x: value of x for case2
    foo.bar.case2.y: value of y for case2
    foo.bar.case2.z.t: value of z for case2

    prop.getSubProperties("foo.bar.") returns a Map with the keys case1 and case2.

    A JProperties is bound to case1 and it contains the properties:
    x: value of x for case1
    y: value of y for case1

    A JProperties is bound to case2 and it contains the properties:
    x: value of x for case2
    y: value of y for case2
    z.t: value of z for case2

    Parameters:
    prefix - the prefix
    Returns:
    a map with string as the key and either String or JProperties as the value.
    Since:
    jcms-5.7.2

    clearProperties

    public void clearProperties()
    Clear all properties.


    load

    public void load(File file)
              throws IOException
    Reads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.

    Parameters:
    file - the file from which to read properties.
    Throws:
    IOException - if an error occurred when reading from the input stream.
    IllegalArgumentException - if the input stream contains a malformed Unicode escape sequence.
    See Also:
    Properties.load(java.io.InputStream)

    load

    public void load(File file,
                     String prefix)
              throws IOException
    Reads a property file (key and element pairs) from the given File. The file encoding is assumed to be UTF-8.

    Parameters:
    file - the file from which to read properties.
    prefix - a prefix to use add to all properties' name. if null, no prefix is used.
    Throws:
    IOException - if an error occurred when reading from the input stream.
    IllegalArgumentException - if the input stream contains a malformed Unicode escape sequence.
    See Also:
    Properties.load(java.io.InputStream)

    updateAndSave

    public void updateAndSave(File file)
                       throws IOException
    Updates and save properties. The original layout is preserved for existing properties and comments New properties are appended.

    Parameters:
    file - the properties File to read and update.
    Throws:
    IOException


    Copyright © 2001-2010 Jalios SA. All Rights Reserved.