jd.util
Class Config

java.lang.Object
  extended byjd.util.Config

public class Config
extends Object

Config represents a set of key-value pairs. Each key and its corresponding value is a string. Config has several methods to access the value and convert it to another simple type (e.g. integer, double, Date, ...). The value of a key can be another config object thus allowing arbitrary nested levels.

A Config object can be loaded from and saved to a file.


Constructor Summary
Config()
           
 
Method Summary
 boolean containsKey(String key)
          Test if the given string is a key of this Config.
 String get(String key)
          Get the value of a key.
 String get(String key, String defaultValue)
          Get the value of a key.
 boolean getBoolean(String key)
          Return true if the value to which the key is mapped is "true", false otherwise.
 boolean getBoolean(String key, boolean defaultValue)
          Return true if the value to which the key is mapped is "true", false otherwise.
 File getDirectory(String key)
          Return the value of a key as a File.
 double getDouble(String key)
          Return the value of a key as a double.
 double getDouble(String key, double defaultValue)
          Return the value of a key as a double.
 File getFile(String parameter)
          Return the value of a key as a File.
 File getFile(String parameter, File defaultFile)
          Return the value of a key as a File.
 int getInt(String key)
          Return the value of a key as an integer.
 int getInt(String key, int defaultValue)
          Return the value of a key as an integer.
 String[] getItemList(String keyPrefix)
           
 void getItemList(String keyPrefix, Vector v)
           
 String[] getList(String key, String delimiter)
          Return the value of a key splitted into an string array.
 void getList(String key, String delimiter, Vector list)
          Return the value of a key splitted into an string array.
 long getLong(String key)
          Return the value of a key as a long.
 long getLong(String key, long defaultValue)
          Return the value to which the key is mapped as a long.
 String getPath(String key)
          Return the value to which the key is mapped.
 String getPath(String parameter, boolean startsWithSeparator, boolean endsWithSeparator)
          Return the value to which the key is mapped.
 File getSourceFile()
           
 Config getSubConfig(String key)
          Return a sub config.
 Config getSubConfig(String key, boolean createNonExistentKey)
          Return a sub config.
 Config getSubConfig(String key, Config defaultConfig)
          Return a sub config.
 Enumeration keys()
          Return an enumeration of all keys.
 void read(File file)
          Reads the Config values from the file.
 void read(InputStream in)
          Read the Config values from the Reader.
 void read(Reader reader)
          Read the Config values from the Reader.
 void read(String fileName)
          Read the Config from the file.
 void read(String name, ClassLoader loader)
          Read the Config from a resource.
 Object remove(String key)
          Remove a key from the config.
 void removeAll()
          Remove all values from the config.
 Config removeSubConfig(String key)
          Remove a subconfig.
 void set(String key, String value)
          Map the key to the value.
 void setBoolean(String key, boolean value)
          Map the key to the boolean value.
 void setDouble(String key, double value)
          Map the key to the double value.
 void setFile(String key, File value)
          Set the file value for a key.
 void setInt(String key, int value)
          Map the key to the int value.
 void setLong(String key, long value)
          Map the key to the long value.
 int size()
          Return the number of values not counting any subconfigs.
 int size(boolean recursive)
          Return the number of values.
 Enumeration values()
          Return an enumeration of all keys.
 void write()
          Write the Config to the file from that it was previously read.
 void write(File file)
          Write the Config to the file.
 void write(String fileName)
          Write the Config to the file.
 void write(Writer writer)
          Write the Config to the PrintWriter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Config

public Config()
Method Detail

get

public String get(String key)
           throws IllegalArgumentException
Get the value of a key.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

get

public String get(String key,
                  String defaultValue)
Get the value of a key. If the key is not mapped to any value, the default value is returned.


set

public void set(String key,
                String value)
Map the key to the value.


getInt

public int getInt(String key)
           throws IllegalArgumentException,
                  NumberFormatException
Return the value of a key as an integer.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.
NumberFormatException - thrown if the value cannot be converted to an integer

getInt

public int getInt(String key,
                  int defaultValue)
           throws NumberFormatException
Return the value of a key as an integer. If the key is not mapped to any value, the default value is returned.

Throws:
NumberFormatException - thrown if the value cannot be converted to an integer

setInt

public void setInt(String key,
                   int value)
Map the key to the int value.


getLong

public long getLong(String key)
             throws IllegalArgumentException,
                    NumberFormatException
Return the value of a key as a long.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.
NumberFormatException - thrown if the value cannot be converted to a long

getLong

public long getLong(String key,
                    long defaultValue)
             throws NumberFormatException
Return the value to which the key is mapped as a long. If the key is not mapped to any value, the default value is returned.

Throws:
NumberFormatException - thrown if the value cannot be converted to a long

setLong

public void setLong(String key,
                    long value)
Map the key to the long value.


getDouble

public double getDouble(String key)
                 throws IllegalArgumentException,
                        NumberFormatException
Return the value of a key as a double.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.
NumberFormatException - thrown if the value cannot be converted to a double

getDouble

public double getDouble(String key,
                        double defaultValue)
                 throws NumberFormatException
Return the value of a key as a double. If the key is not mapped to any value, the default value is returned.

Throws:
NumberFormatException - thrown if the value cannot be converted to a double

setDouble

public void setDouble(String key,
                      double value)
Map the key to the double value.


getBoolean

public boolean getBoolean(String key)
                   throws IllegalArgumentException
Return true if the value to which the key is mapped is "true", false otherwise.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

getBoolean

public boolean getBoolean(String key,
                          boolean defaultValue)
Return true if the value to which the key is mapped is "true", false otherwise. If the key is not mapped to any value, the default value is returned.


setBoolean

public void setBoolean(String key,
                       boolean value)
Map the key to the boolean value.


getSubConfig

public Config getSubConfig(String key)
Return a sub config.


getSubConfig

public Config getSubConfig(String key,
                           boolean createNonExistentKey)
Return a sub config.


getSubConfig

public Config getSubConfig(String key,
                           Config defaultConfig)
Return a sub config.


getList

public String[] getList(String key,
                        String delimiter)
                 throws IllegalArgumentException
Return the value of a key splitted into an string array.

Parameters:
key - the key
delimiter - the delimiter for separating the substrings
Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

getList

public void getList(String key,
                    String delimiter,
                    Vector list)
Return the value of a key splitted into an string array.

Parameters:
key - the key
delimiter - the delimiter for separating the substrings

getItemList

public String[] getItemList(String keyPrefix)

getItemList

public void getItemList(String keyPrefix,
                        Vector v)

getPath

public String getPath(String key)
               throws IllegalArgumentException
Return the value to which the key is mapped. The value is interpreted as a path string and ends with a slash.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

getPath

public String getPath(String parameter,
                      boolean startsWithSeparator,
                      boolean endsWithSeparator)
               throws IllegalArgumentException
Return the value to which the key is mapped. The value is interpreted as a path string and the beginning and end of the string contains a directory separator according to the provided flags.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

getFile

public File getFile(String parameter)
             throws IllegalArgumentException
Return the value of a key as a File.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

getFile

public File getFile(String parameter,
                    File defaultFile)
Return the value of a key as a File. If the key is not mapped to any value, the default value is returned.


setFile

public void setFile(String key,
                    File value)
Set the file value for a key.


getDirectory

public File getDirectory(String key)
                  throws IllegalArgumentException
Return the value of a key as a File.

Throws:
IllegalArgumentException - thrown if the key is not mapped to any value.

size

public int size()
Return the number of values not counting any subconfigs.


size

public int size(boolean recursive)
Return the number of values.

Parameters:
recursive - recurse into subconfigs?

containsKey

public boolean containsKey(String key)
Test if the given string is a key of this Config.


remove

public Object remove(String key)
Remove a key from the config.

Parameters:
key - the key
Returns:
the value of the key or null if no value with the key exists

removeSubConfig

public Config removeSubConfig(String key)
Remove a subconfig.


removeAll

public void removeAll()
Remove all values from the config.


keys

public Enumeration keys()
Return an enumeration of all keys.


values

public Enumeration values()
Return an enumeration of all keys.


getSourceFile

public File getSourceFile()

read

public void read(String fileName)
          throws IOException
Read the Config from the file.

Parameters:
fileName - the file name
Throws:
IOException - thrown if an I/O error occurs

read

public void read(String name,
                 ClassLoader loader)
          throws IOException
Read the Config from a resource.

Parameters:
name - the resource name
loader - a class loader or null if the config is a system resource
Throws:
IOException - thrown if an I/O error occurs

read

public void read(File file)
          throws IOException
Reads the Config values from the file.

Throws:
IOException - thrown if an I/O error occurs

read

public void read(InputStream in)
          throws IOException
Read the Config values from the Reader.

Throws:
IOException - thrown if an I/O error occurs

read

public void read(Reader reader)
          throws IOException
Read the Config values from the Reader.

Throws:
IOException - thrown if an I/O error occurs

write

public void write()
           throws IOException
Write the Config to the file from that it was previously read.

Throws:
IOException - if a IO Error occurs.

write

public void write(String fileName)
           throws IOException
Write the Config to the file.

Throws:
IOException - if a IO Error occurs.

write

public void write(File file)
           throws IOException
Write the Config to the file.

Throws:
IOException - if a IO Error occurs.

write

public void write(Writer writer)
           throws IOException
Write the Config to the PrintWriter.

Throws:
IOException - if a IO Error occurs.