com.googlecode.fascinator.common
Class JsonConfig

java.lang.Object
  extended by com.googlecode.fascinator.common.JsonConfig

public class JsonConfig
extends Object

Manages JSON configuration. Configuration values are read from a specified JSON and if not found, the system-wide JSON will also be searched.

Author:
Oliver Lucido

Constructor Summary
JsonConfig()
          Creates a config with only the system settings
JsonConfig(File jsonFile)
          Creates a JSON configuration from the specified file
JsonConfig(InputStream jsonIn)
          Creates a JSON configuration from the specified input stream
JsonConfig(String jsonString)
          Create a JSON configuration from the specified input string
 
Method Summary
static File backupSystemFile()
          Performs a backup on the system-wide configuration file from the default config dir if it exists.
 String get(String path)
          Gets the value of the specified node
 String get(String path, String defaultValue)
          Gets the value of the specified node, with a specified default if the not was not found
 Map<String,JsonConfigHelper> getJsonMap(String path)
          Gets the JSON Map of the specified node
 List<Object> getList(String path)
          Gets values of the specified node as a list.
 Map<String,Object> getMap(String path)
          Gets a map of the child nodes of the specified node
 Map<String,Object> getMapWithChild(String path)
          Gets a map of the child (and the 2nd level children) nodes of the specified node
static File getSystemFile()
          Gets the system-wide configuration file from the default config dir.
 boolean isConfigured()
          Tests whether or not the system-config has been properly configured.
 boolean isOutdated()
          To check if configuration file is outdated
 void set(String path, String value, boolean system)
          Sets the value of the specified node.
 void store(Writer writer)
          Serialises the current state of the JSON configuration to the specified writer.
 void store(Writer writer, boolean pretty)
          Serialises the current state of the JSON configuration to the specified writer.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonConfig

public JsonConfig()
           throws IOException
Creates a config with only the system settings

Throws:
IOException - if these was an error parsing or reading the system JSON file

JsonConfig

public JsonConfig(File jsonFile)
           throws IOException
Creates a JSON configuration from the specified file

Parameters:
jsonFile - a JSON file
Throws:
IOException - if there was an error parsing or reading the file

JsonConfig

public JsonConfig(InputStream jsonIn)
           throws IOException
Creates a JSON configuration from the specified input stream

Parameters:
jsonIn - a JSON stream
Throws:
IOException - if there was an error parsing or reading the stream

JsonConfig

public JsonConfig(String jsonString)
           throws IOException
Create a JSON configuration from the specified input string

Parameters:
jsonString -
Throws:
IOException
Method Detail

get

public String get(String path)
Gets the value of the specified node

Parameters:
path - XPath to node
Returns:
node value or null if not found

get

public String get(String path,
                  String defaultValue)
Gets the value of the specified node, with a specified default if the not was not found

Parameters:
path - XPath to node
defaultValue - value to return if the node was not found
Returns:
node value or defaultValue if not found

getList

public List<Object> getList(String path)
Gets values of the specified node as a list. Use this method for JSON arrays.

Parameters:
path - XPath to node
Returns:
value list, possibly empty

getMap

public Map<String,Object> getMap(String path)
Gets a map of the child nodes of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

getMapWithChild

public Map<String,Object> getMapWithChild(String path)
Gets a map of the child (and the 2nd level children) nodes of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

set

public void set(String path,
                String value,
                boolean system)
Sets the value of the specified node. If the node doesn't exist it is created. The value can be set in the system configuration if necessary.

Parameters:
path - XPath to node
value - value to set
system - set the value in the system configuration

store

public void store(Writer writer)
           throws IOException
Serialises the current state of the JSON configuration to the specified writer. By default this doesn't use a pretty printer.

Parameters:
writer - a writer
Throws:
IOException - if there was an error writing the configuration

store

public void store(Writer writer,
                  boolean pretty)
           throws IOException
Serialises the current state of the JSON configuration to the specified writer. The output can be set to be pretty printed if required.

Parameters:
writer - a writer
pretty - use pretty printer
Throws:
IOException - if there was an error writing the configuration

backupSystemFile

public static File backupSystemFile()
                             throws IOException
Performs a backup on the system-wide configuration file from the default config dir if it exists. Returns a reference to the backed up file.

Returns:
the backed up system JSON file
Throws:
IOException - if there was an error reading or writing either file

getSystemFile

public static File getSystemFile()
                          throws IOException
Gets the system-wide configuration file from the default config dir. If the file doesn't exist, a default is copied to the config dir.

Returns:
the system JSON file
Throws:
IOException - if there was an error reading or writing the system configuration file

getJsonMap

public Map<String,JsonConfigHelper> getJsonMap(String path)
Gets the JSON Map of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

toString

public String toString()
Overrides:
toString in class Object
Returns:
The full JSON string

isConfigured

public boolean isConfigured()
Tests whether or not the system-config has been properly configured.

Returns:
true if configured, false if still using defaults

isOutdated

public boolean isOutdated()
To check if configuration file is outdated

Returns:
true if outdated, false otherwise


Copyright © 2009-2013. All Rights Reserved.