Class ZConfig
public class ZConfig extends Object
An application configuration file is a text file that contains mapping of property names (the keys) to property values. Each line in the file has the format:
[property name]property value
This class has methods for reading configuration files into a map object, retrieving and interpreting configuration properties, modifying properties, updating configuration files and some other tools.
Note that a configuration map allows only one value per property. If the configuration file contains multiple definitions of the same property, then when loading the file into a configuration map the latter in the file will step over the earlier. Also lines that do not have the structure of a configuration property will be ignored.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classZConfig.PropertyRepresents one mapping of property name -> property value.static classZConfig.PropertySheetRepresents all the properties defined in a configuration file as anArrayList. -
Constructor Summary
Constructors Constructor Description ZConfig()Class constructor. -
Method Summary
Modifier and Type Method Description intgetIntProperty(String prop)Returns the value to which the specified property is mapped converted to an integer.StringgetProperty(String prop)Returns the value to which the specified property is mapped.ZConfig.PropertySheetgetPropertySheet()Returns a list that contains the content of the configuration file.booleaninit(String pathname)Initializes the object with a file pathname and the default character set (UTF-8).booleaninit(String pathname, String charset)Initializes the object with a file pathname and the specified character set.booleanisPropertySet(String... props)Returns true if at least one property from a given list is set on.voidprocessAllProperties()Performs custom processing for each property included in the configuration map.protected voidprocessProperty(String prop, String value)Processes the given property.booleanreload()Reloads the properties from the file used to initialize the object.voidsaveConfiguration()Saves the loaded configuration map in the configuration file.voidsavePropertySheet(ZConfig.PropertySheet properties)Uses a list of properties to rewrite the configuration file and reloads the running application configuration.voidsetProperties(String assignments)Sets values in a list of properties.voidsetProperty(String prop, String value)Sets a property value or remove it.
-
Constructor Details
-
ZConfig
public ZConfig()Class constructor.
-
-
Method Details
-
init
Initializes the object with a file pathname and the default character set (UTF-8).The method opens the specified file that is supposed to contain UTF-8 text, reads its content, parses the configuration properties into the map managed by this object, then closes the file.
- Parameters:
pathname- the system-dependent file pathname.- Returns:
- true if the file exists and the object has been initialized successfully, false otherwise.
- See Also:
init(pathname, charset),getPropertySheet()
-
init
Initializes the object with a file pathname and the specified character set.The method opens the specified file that is supposed to contain text in the specified character set, reads its content, parses the configuration properties into the map managed by this object, then closes the file.
- Parameters:
pathname- the system-dependent file pathname.charset- the character set used for the text in the file.- Returns:
- true if the file exists and the object has been initialized successfully, false otherwise.
- See Also:
init(pathname)
-
reload
public boolean reload()Reloads the properties from the file used to initialize the object.Call this method if the configuration file was modified externally and you want to reload it into the map managed by this object.
- Returns:
- true if the file used for initializing the object exists and its content has been reloaded successfully, false otherwise.
- See Also:
init(pathname),init(pathname, charset)
-
getProperty
Returns the value to which the specified property is mapped.Returns the value to which the specified property is mapped, or null if the configuration contains no mapping for the property.
- Parameters:
prop- the property whose associated value is to be returned.- Returns:
- the value to which the specified property is mapped, or null if the configuration contains no mapping for the property.
- See Also:
getIntProperty()
-
getIntProperty
Returns the value to which the specified property is mapped converted to an integer.Returns the value to which the specified property is mapped converted to an integer, or zero if the property is not an integer number or the configuration contains no mapping for the property.
- Parameters:
prop- the property whose associated value is to be returned.- Returns:
- the value to which the specified property is mapped converted to an integer, or zero if the property is not an integer number or the configuration contains no mapping for the property.
- See Also:
getProperty()
-
isPropertySet
Returns true if at least one property from a given list is set on.A property is considered to be set on if it exists in the configuration map and its value is neither an empty string nor does it start with the letters "no", case insensitive. This method accepts as parameters a list of property names and returns true if and only if at least one of the properties is set on.
- Parameters:
props- zero or more property names.- Returns:
- true if and only if at least one of the specified properties is set on.
-
setProperty
Sets a property value or remove it.If
valueis not null the method updates the value of the property if it exists and will add it if it does not. Ifvalueisnullthe property is deleted if it exists.- Parameters:
prop- the property to be set.value- the value to set. If null then the property is removed if it exists.- See Also:
setProperties()
-
setProperties
Sets values in a list of properties.The
assignmentsparameter is a list assignmentsproperty=valueconcatenated by a semicolon, i.e.:property1=value1;property2=value2;...The method sets each of the properties in the list to the specified value.
- Parameters:
assignments- a list of properties in the format property1=value1;property2=value2;...- See Also:
setProperty()
-
saveConfiguration
public final void saveConfiguration()Saves the loaded configuration map in the configuration file.Saves the current configuration map image in the file provided in the call to
init(). The order of the properties in the saved file is not guaranteed, and if the original file contains multiple definitions of the same property, only the last one is stored in the newly written file. Lines that do not have the configuration structure are written to the new file. -
getPropertySheet
Returns a list that contains the content of the configuration file.This method loads the configuration file into a
PropertySheetobject that lists all the properties contained in the configuration file, in the order they appear in the file. If there are multiple definitions of the same property, they are copied to the returned object as they appear in the file.You can use this method in conjunction with
savePropertySheet()in order to edit the file without shuffling its content.- Returns:
- a list that contains the content of the configuration file.
- Throws:
IOException- See Also:
savePropertySheet()
-
savePropertySheet
public void savePropertySheet(ZConfig.PropertySheet properties) throws FileNotFoundException, UnsupportedEncodingExceptionUses a list of properties to rewrite the configuration file and reloads the running application configuration.This method gets a
PropertySheetobject that contains a list of properties and saves them in the configuration file, in the order they appear in the list, rewriting over the file's previous content. If the list contains multiple definitions of the same property, they are all written to the file as they appear in the list.Besides writing to the file, the method reloads the property map of the running application with the values contained in the list. If the list contains multiple definitions of the same property, the value of the latter steps over the earlier.
You can use this method in conjunction with
getPropertySheet()in order to edit the file without shuffling its content.- Parameters:
properties- a list of properties to store in the file.- Throws:
FileNotFoundExceptionUnsupportedEncodingException- See Also:
getPropertySheet()
-
processAllProperties
public void processAllProperties()Performs custom processing for each property included in the configuration map.This method activates
processProperty()for each property included in the configuration map. You can override that method in order to perform custom processing for each property.- See Also:
processProperty()
-
processProperty
Processes the given property.This method is activated from
processAllProperties(). Override it to perform custom processing on the given property.- Parameters:
prop- name of the property to be processed.value- value of the property to be processed.- See Also:
processAllProperties()
-