package com.digi.config.util; import java.awt.Color; import java.awt.Font; import java.io.*; import java.util.*; import java.net.*; import java.applet.*; /** *
ConfigSettings reads and parses the content of a user defined * configuration file and provides a set of query functions to retrieve * these configuration information. *
The default configuration file pathname is config.ini in the * classpath. *
* Syntax : // **
* Syntax : [section_name] **
* Syntax : tag = value **
* Syntax : $([[section_name]:]tag) **
* @return ConfigSettings */ public static ConfigSettings getInstance() { if (theInstance==null) { theInstance=new ConfigSettings(); } return theInstance; } //===================================================================== // Instance Methods //===================================================================== /** * Constructor *
*/ private ConfigSettings() { // Load the ini file if it is available try { load_profile(); } catch (Exception e) { System.out.println("Error loading configuration profile: "); e.printStackTrace(); } addDefaultValueFinderListener(new DefaultConfigSettings()); } /** * This method adds a defaultvalue finder listener. *
* @param def the default value finder class */ public void addDefaultValueFinderListener(DefaultValueFinder def) { ivDefValFunction.addElement(def); } /** * Returns environment configuration file pathname. * @return the configuration file name */ public String getConfigFileName() { return configFileName; } /** * Returns value of specified ProfileProperty as a string. Returns null if section/tag is not found. * * @param section Section name in the profile * @param tag Tag name * @return Replacement text of the input tag */ public String getProfileProperty(String section, String tag) { return getProfileProperty(section,tag,null,null); } /** * Returns value of specified ProfileProperty as a string. Returns null if section/tag is not found. * Caller specifies both a primary and fallback set of section/tag pairs. The search order is: *
*/
public synchronized void save_profile() throws IOException {
// If ini file has not been created yet, determine where it should go
if (iniFileName==null) {
iniFileName=PathLocater.getDefaultSaveFileName(configFileName);
}
//System.out.println("Saving config settings to '"+iniFileName+"'");
PathLocater.createPathToFile(iniFileName);
PrintWriter pw=new PrintWriter(new FileWriter(iniFileName), true);
list(pw);
setChanged();
if (ivModSettings!=null)
// If there were no changes ivModSettings will be null
{
notifyObservers(ivModSettings);
ivModSettings.getSections().clear();
}
}
/**
* List properties, for debugging.
* @param out Output Stream
*/
public void list(PrintWriter out) {
//Time Stamp
out.println("// " +new Date());
//Print keys without section first
Vector2 noSection=(Vector2)ivSectionTable.get("");
if (noSection!=null) {
int cnt=noSection.size();
for (int idx=0; idx
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, a MissingResourceException is thrown.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, a MissingResourceException is thrown.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, false is returned.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, false is returned.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, a MissingResourceException is thrown.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
* The logic will first look for the value by searching the specified category in the
* ConfigSettings for the specified key. If either the category or the key cannot be found,
* the logic will perform the same search in the DefaultConfigSettings. If a value is still
* not found, a MissingResourceException is thrown.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return
*/
public static void save() throws IOException {
getInstance().save_profile();
}
}
true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static boolean getBoolean(String category, String key) {
return getInstance().getBooleanProfileProperty(category, key);
}
/**
* Gets boolean value of the specified key in the specified category. This method
* returns false if the value associated with the category/key is not actually a
* boolean value.
* true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static boolean getBoolean(String category, String key, String fallbackSection, String fallbackTag) {
return getInstance().getBooleanProfileProperty(category, key, fallbackSection, fallbackTag);
}
/**
* Sets the boolean value of the specified key in the specified category. If the key
* already exists in this category, the provided value will replace the existing value.
*
* @param category is the name of the setting category into which the key/value pair is
* inserted. If it does not exist, it will be created.
* @param key is the name of the key to the value within the category
* @param value is the boolean value to be inserted into the ConfigSettings
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
*/
public static void setBoolean(String category, String key, boolean value) {
getInstance().setBooleanProfileProperty(category, key, value);
}
/**
* Gets integer value of the specified key in the specified category.
* true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
* @throws NumberFormatException if the requested value is not actually an integer.
*/
public static int getInteger(String category, String key) {
return getInstance().getIntegerProfileProperty(category, key);
}
/**
* Gets integer value of the specified key in the specified category.
* true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
* @throws NumberFormatException if the requested value is not actually an integer.
*/
public static int getInteger(String category, String key, String fallbackSection, String fallbackTag) {
return getInstance().getIntegerProfileProperty(category, key, fallbackSection, fallbackTag);
}
/**
* Sets the integer value of the specified key in the specified category. If the key
* already exists in this category, the provided value will replace the existing value.
*
* @param category is the name of the setting category into which the key/value pair is
* inserted. If it does not exist, it will be created.
* @param key is the name of the key to the value within the category
* @param value is the integer value to be inserted into the ConfigSettings
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
*/
public static void setInteger(String category, String key, int value) {
getInstance().setIntegerProfileProperty(category, key, value);
}
/**
* Gets font value of the specified tag. This method
* returns null if the tag is not found.
* @param section Section name in the profile
* @param tag Tag name
* @return font
*/
public static Font getFont(String section, String tag) {
return getInstance().getFontProfileProperty(section,tag,null,null);
}
/**
* Gets font value of the specified tag. This method
* returns null if the tag is not found.
* @param section Section name in the profile
* @param tag Tag name
* @return font
*/
public static Font getFont(String section, String tag, String fallbackSection, String fallbackTag) {
return getInstance().getFontProfileProperty(section,tag,fallbackSection,fallbackTag);
}
/**
* Gets color value of the specified key in the specified category. This method
* returns false if the value associated with the category/key is not actually a
* color value.
* true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static Color getColor(String category, String key) {
return getInstance().getColorProfileProperty(category, key);
}
/**
* Gets color value of the specified key in the specified category. This method
* returns false if the value associated with the category/key is not actually a
* color value.
* true
if and only if the value associated with
* the category/key is true.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static Color getColor(String category, String key, String fallbackSection, String fallbackTag) {
return getInstance().getColorProfileProperty(category, key, fallbackSection, fallbackTag);
}
/**
* Sets the color value of the specified key in the specified category. If the key
* already exists in this category, the provided value will replace the existing value.
*
* @param category is the name of the setting category into which the key/value pair is
* inserted. If it does not exist, it will be created.
* @param key is the name of the key to the value within the category
* @param value is the color value to be inserted into the ConfigSettings
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
*/
public static void setColor(String category, String key, Color value) {
getInstance().setColorProfileProperty(category, key, value);
}
/**
* Gets value of the specified key within the specified category in the ConfigSettings.
* If a value is not found, the same search will be repeated in the DefaultConfigSettings.
* If a value is still not found, a MissingResourceException will be thrown. An exception
* to this search order occurs when the category = 'System'. In that case, only the System
* properties are searched.
*
* NOTE: the category and key parameters are case sensitive.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return the String associated with the specified category/key.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static String getProperty(String category, String key) {
if ((category==null)||(key==null)) {
throw new IllegalArgumentException("Category and Key are MANDATORY parameters");
}
String retValue=getInstance().getProfileProperty(category, key);
if (retValue==null) {
throw new MissingResourceException("Value not found for category: " +
category+" and key: " +key, category, key);
}
return retValue;
} //end getProperty
/**
* Gets value of the specified key within the specified category in the ConfigSettings.
* If a value is not found, the same search will be repeated in the DefaultConfigSettings.
* If a value is still not found, a MissingResourceException will be thrown. An exception
* to this search order occurs when the category = 'System'. In that case, only the System
* properties are searched.
*
* NOTE: the category and key parameters are case sensitive.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @return the String associated with the specified category/key.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static String getProperty(String category, String key, String fallbackSection, String fallbackTag) {
if ((category==null)||(key==null)) {
throw new IllegalArgumentException("Category and Key are MANDATORY parameters");
}
String retValue=getInstance().getProfileProperty(category, key, fallbackSection, fallbackTag);
if (retValue==null) {
throw new MissingResourceException("Value not found for category: " +
category+" and key: " +key, category, key);
}
return retValue;
} //end getProperty
/**
* Gets value of the specified key within the specified category in the ConfigSettings.
* If a value is not found, the same search will be repeated in the DefaultConfigSettings.
* If a value is still not found, the provided dftRetValue will be returned. An exception
* to this search order occurs when the category = 'System'. In that case, only the System
* properties are searched (but the provided dftRetValue is still returned if a value is
* not found in the System Properties).
*
* NOTE: the category and key parameters are case sensitive. It is suggested that callers
* always use the constants provided by this class when specifying the category.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @param dftRetValue is the default return String if a value for the specified category/key
* is not found in the configSettings.
* @return the String associated with the specified category/key. If one is not
* found in the configSettings, the provided default value will be returned.
*
* @throws IllegalArgumentException if the category or key values are either not
* provided or are empty Strings.
* @throws MissingResourceException if a value for the specified category/key is
* not found.
*/
public static String getProperty(String category, String key,
String dftRetValue) {
String retValue=null;
try {
retValue=getProperty(category, key);
} catch (MissingResourceException mre) {
retValue=dftRetValue;
}
return retValue;
}
/**
* Sets the provided value under the specified key within the specified category in the ConfigSettings.
* If the key/value already exists in the specified category, the value will be replaced. If the
* specified category does not already exist, it will be created.
*
* NOTE: the category and key parameters are case sensitive. It is strongly suggested that the
* constants for the categories provided by this class are used when specifying the category parameter.
*
* NOTE: values placed in the System
or CFG
categories
* will NOT be persisted between restarts of this application.
*
* @param category is the name of the setting category to search
* @param key is the name of the key to the value within the category
* @param value is the String value to be placed in the ConfigSettings
*
* @throws IllegalArgumentException if the category or key values are
* empty Strings.
* @throws NullPointerException if the category, key or value are null.
*/
public static void setProperty(String category, String key, String value) {
if ((category.length()==0)||(key.length()==0)) {
throw new IllegalArgumentException("Category and Key are MANDATORY parameters and must not be empty Strings");
}
getInstance().setProfileProperty(category, key, value);
} //end setProperty
/**
* This method saves the config settings to the ini file
*