public class PropertiesList
extends java.lang.Object
PropertiesList
class represents a set of
application-specific, persistent properties.
A PropertiesList
can contain another properties list as its
"defaults". The default property list is searched when the property is not
found in the original (parent) properties list.
The PropertiesList
methods allow you to can construct
property lists in the following ways:
iniFileSpec = new String("myIniFile.ini"); propList = new PropertiesList(); propList.load(iniFileSpec);
iniFileSpec = new String("myIniFile.ini"); defaultSpec = new String("defaults.ini"); propList = new PropertiesList(iniFileSpec, defaultSpec);or
iniFileSpec = new String("myIniFile.ini"); defaultSpec = new String("defaults.ini"); propList = new PropertiesList(); propList.load(iniFileSpec); propList.setDefaults(defaultSpec);
personalProps = new String("individual.ini"); siteProps = new String("site.ini"); companyProps = new String("company.ini"); personalPropList = new PropertiesList(); sitePropList = new PropertiesList(); companyPropList = new PropertiesList(); personalPropList.load(personalProps); sitePropList.load(siteProps); companyPropList.load(companyProps); personalPropList.setDefaults(sitePropList); sitePropList.setDefaults(companyPropList);
Modifier and Type | Class and Description |
---|---|
static class |
PropertiesList.ParsingMode |
Constructor and Description |
---|
PropertiesList(java.io.InputStream propsFile,
java.io.InputStream defaultsFile,
PropertiesList.ParsingMode pMode) |
PropertiesList(java.io.InputStream defaultsFile,
PropertiesList.ParsingMode pMode) |
PropertiesList(PropertiesList.ParsingMode pMode)
Creates an empty
PropertiesList with no defaults values. |
PropertiesList(java.lang.String defaultsFile,
PropertiesList.ParsingMode pMode)
Creates an empty
PropertiesList with the specified defaults. |
PropertiesList(java.lang.String propsFile,
java.lang.String defaultsFile,
PropertiesList.ParsingMode pMode)
Creates a
PropertiesList with the specified defaults. |
Modifier and Type | Method and Description |
---|---|
void |
addSectionStringValue(java.lang.String sectionName,
java.lang.String itemRoot,
java.lang.String value)
Adds a new item value to string section.
|
void |
addSectionStringValue(java.lang.String sectionName,
java.lang.String countKey,
java.lang.String itemRoot,
java.lang.String value)
Adds a new item value to string section.
|
void |
clearSection(java.lang.String sectionName)
Removes all properties from a section.
|
void |
clearSectionStringValues(java.lang.String sectionName,
java.lang.String itemRoot)
Clears the contents of a section created with the
putSectionStringValues and removes the
the referenced sections. |
void |
clearSectionStringValues(java.lang.String sectionName,
java.lang.String countKey,
java.lang.String itemRoot)
Clears the contents of a section created with the
putSectionStringValues and removes the
the referenced sections. |
PropertiesList |
getDefaults()
Returns the default
PropertiesList |
protected java.lang.String |
getIllegalValueMsg(java.lang.String section,
java.lang.String item,
java.lang.String val) |
boolean |
getPropertyBoolean(java.lang.String sectionName,
java.lang.String itemName)
Returns a property's
boolean value. |
boolean |
getPropertyBoolean(java.lang.String sectionName,
java.lang.String itemName,
boolean defValue)
Returns a property's
boolean value or the provided default. |
double |
getPropertyDouble(java.lang.String sectionName,
java.lang.String itemName)
Returns a property's
double value. |
double |
getPropertyDouble(java.lang.String sectionName,
java.lang.String itemName,
double defValue)
Returns a property's
double value or the provided default. |
int |
getPropertyInt(java.lang.String sectionName,
java.lang.String itemName)
Returns a property's
int value. |
int |
getPropertyInt(java.lang.String sectionName,
java.lang.String itemName,
int defValue)
Returns a property's
int value or the provided default. |
long |
getPropertyLong(java.lang.String sectionName,
java.lang.String itemName)
Returns a property's
long value. |
long |
getPropertyLong(java.lang.String sectionName,
java.lang.String itemName,
long defValue)
Returns a property's
long value or the provided default. |
java.lang.String |
getPropertyString(java.lang.String sectionName,
java.lang.String itemName)
Returns a property's
String value. |
java.lang.String |
getPropertyString(java.lang.String sectionName,
java.lang.String itemName,
java.lang.String defValue)
Returns a property's
String value or the provided default. |
java.util.Set<java.lang.String> |
getSectionItemNames(java.lang.String sectionName)
Return the names of all items under the given section.
|
java.util.Set<java.lang.String> |
getSectionNames() |
java.util.Set<java.lang.String> |
getSectionNames(boolean includeDefaults) |
java.lang.String[] |
getSectionStringValues(java.lang.String sectionName,
java.lang.String itemRoot)
Returns the string values from a section.
|
java.lang.String[] |
getSectionStringValues(java.lang.String sectionName,
java.lang.String countKey,
java.lang.String itemRoot)
Returns the string values from a section.
|
boolean |
hasSection(java.lang.String sectionName)
Determines whether this PropertiesList contains a particular
section.
|
boolean |
isEmpty()
Returns whether this PropertiesList, and its default
PropertiesLists contains any values.
|
boolean |
isExistingProperty(java.lang.String sectionName,
java.lang.String itemName)
Returns whether the property of given name exists or not.
|
boolean |
isValueInSection(java.lang.String sectionName,
java.lang.String itemRoot,
java.lang.String value) |
boolean |
isValueInSection(java.lang.String sectionName,
java.lang.String countKey,
java.lang.String itemRoot,
java.lang.String value) |
PropertiesList |
load(java.io.InputStream is) |
PropertiesList |
load(java.lang.String propFileName)
Loads an ini file into a PropertiesList.
|
PropertiesList |
merge() |
void |
putPropertyBoolean(java.lang.String sectionName,
java.lang.String itemName,
boolean itemValue)
Adds or replaces a property and its
boolean value in the property list. |
void |
putPropertyDouble(java.lang.String sectionName,
java.lang.String itemName,
double itemValue)
Adds or replaces a property and its
double value in the property list. |
void |
putPropertyInt(java.lang.String sectionName,
java.lang.String itemName,
int itemValue)
Adds or replaces a property and its
int value in the property list. |
void |
putPropertyLong(java.lang.String sectionName,
java.lang.String itemName,
long itemValue)
Adds or replaces a property and its
long value in the property list. |
void |
putPropertyString(java.lang.String sectionName,
java.lang.String itemName,
java.lang.String itemValue)
Adds or replaces a property and its
String value in the property list. |
void |
putSectionStringValues(java.lang.String sectionName,
java.lang.String itemRoot,
java.lang.String[] values)
Adds multiple string values to a section.
|
void |
putSectionStringValues(java.lang.String sectionName,
java.lang.String countKey,
java.lang.String itemRoot,
java.lang.String[] values)
Adds multiple string values to a section.
|
void |
removeAll()
Empties this PropertiesList of all items.
|
static java.lang.String |
removeComment(java.lang.String data,
java.lang.String commentChar) |
void |
removeProperty(java.lang.String sectionName,
java.lang.String itemName)
Removes the property.
|
void |
removeSection(java.lang.String sectionName)
Removes the whole section (and all the properties that belong to the
section).
|
void |
save()
Writes the current properties list to the list's source file.
|
void |
save(java.lang.String fileName)
Writes the current property values to a file.
|
void |
setDefaults(PropertiesList defaults)
Makes an existing default properties list the default properties for this
PropertiesList . |
void |
setDefaults(java.lang.String defaultsFile)
Creates a default properties list and makes it the default
properties for this
PropertiesList . |
java.lang.String |
toString()
Returns a string representation of a properties list and its defaults, if any,
recursively.
|
public PropertiesList(PropertiesList.ParsingMode pMode)
PropertiesList
with no defaults values.
After creating the PropertiesList
, you can load application-specific
values using load
and the
putProperty*
methods, and you can assign default
property values by calling setDefaults
load(java.lang.String)
,
setDefaults(org.assistments.util.PropertiesList)
,
putPropertyString(java.lang.String, java.lang.String, java.lang.String)
,
putPropertyBoolean(java.lang.String, java.lang.String, boolean)
,
putPropertyInt(java.lang.String, java.lang.String, int)
,
putPropertyLong(java.lang.String, java.lang.String, long)
public PropertiesList(java.lang.String defaultsFile, PropertiesList.ParsingMode pMode) throws java.util.NoSuchElementException, java.io.IOException
PropertiesList
with the specified defaults.
After creating the PropertiesList
, you can load application-specific
values using load
and
the putProperty*
methods.
defaultsFile
- Full path of file containing the default properties.java.util.NoSuchElementException
- if defaultsFile was not foundjava.io.IOException
- if an error occurred reading defaultsFileload(java.lang.String)
,
putPropertyBoolean(java.lang.String, java.lang.String, boolean)
,
putPropertyInt(java.lang.String, java.lang.String, int)
,
putPropertyLong(java.lang.String, java.lang.String, long)
,
putPropertyString(java.lang.String, java.lang.String, java.lang.String)
public PropertiesList(java.io.InputStream defaultsFile, PropertiesList.ParsingMode pMode) throws java.util.NoSuchElementException, java.io.IOException
java.util.NoSuchElementException
java.io.IOException
public PropertiesList(java.lang.String propsFile, java.lang.String defaultsFile, PropertiesList.ParsingMode pMode) throws java.util.NoSuchElementException, java.io.IOException
PropertiesList
with the specified defaults.propsFile
- Full path of file containing the properties to load.defaultsFile
- Full path of file containing the default properties.java.util.NoSuchElementException
- if propsFile or
defaultsFile were not foundjava.io.IOException
- if an error occurred reading propsFile
or defaultsFilepublic PropertiesList(java.io.InputStream propsFile, java.io.InputStream defaultsFile, PropertiesList.ParsingMode pMode) throws java.util.NoSuchElementException, java.io.IOException
java.util.NoSuchElementException
java.io.IOException
public void setDefaults(PropertiesList defaults)
PropertiesList
.defaults
- PropertiesList
to use for this
properties list.public void setDefaults(java.lang.String defaultsFile) throws java.util.NoSuchElementException, java.io.IOException
PropertiesList
.defaultsFile
- Full path of file containing the default properties.java.util.NoSuchElementException
- if defaultsFile was not foundjava.io.IOException
- if an error occurred reading defaultsFilepublic PropertiesList getDefaults()
PropertiesList
PropertiesList
for this properties
list, or null
if the current object has no default
properties.public PropertiesList load(java.lang.String propFileName) throws java.util.NoSuchElementException, java.io.EOFException, java.io.IOException
propFileName
- ini file to loadisRequired
- if true propFileName must exist
and contain at least one section and item.
java.util.NoSuchElementException
- if propFileName was not foundjava.io.IOException
- if an error occurred reading propFileNamejava.io.EOFException
- if the file contained no itemsload(java.lang.String)
public PropertiesList load(java.io.InputStream is) throws java.util.NoSuchElementException, java.io.IOException, java.io.EOFException
java.util.NoSuchElementException
java.io.IOException
java.io.EOFException
public void putPropertyBoolean(java.lang.String sectionName, java.lang.String itemName, boolean itemValue)
boolean
value in the property list.
This does not modify the default properties list or any of its defaults.
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)itemValue
- Property value to add (or replace): true
or
false
getPropertyBoolean(java.lang.String, java.lang.String)
public void putPropertyDouble(java.lang.String sectionName, java.lang.String itemName, double itemValue)
double
value in the property list.
This does not modify the default properties list or any of its defaults.
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)itemValue
- Property value to add (or replace)getPropertyLong(java.lang.String, java.lang.String)
public double getPropertyDouble(java.lang.String sectionName, java.lang.String itemName) throws java.util.NoSuchElementException, java.lang.NumberFormatException
double
value.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyDouble logs and throws the exception.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.java.util.NoSuchElementException
- the item was not foundjava.lang.NumberFormatException
- the value of the item was
not a longpublic double getPropertyDouble(java.lang.String sectionName, java.lang.String itemName, double defValue)
double
value or the provided default.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyDouble logs the exception before returning the provided default.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.defValue
- The default value when a valid item value is not
found.public boolean getPropertyBoolean(java.lang.String sectionName, java.lang.String itemName) throws java.util.NoSuchElementException, java.lang.IllegalArgumentException
boolean
value.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyBoolean logs and throws the exception.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.java.util.NoSuchElementException
- the item was not foundjava.lang.IllegalArgumentException
- the value of the item was
not a booleanputPropertyBoolean(java.lang.String, java.lang.String, boolean)
public boolean getPropertyBoolean(java.lang.String sectionName, java.lang.String itemName, boolean defValue)
boolean
value or the provided default.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyBoolean logs the exception before returning the provided default.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.defValue
- The default value when a valid item value is not
found.public void putPropertyLong(java.lang.String sectionName, java.lang.String itemName, long itemValue)
long
value in the property list.
This does not modify the default properties list or any of its defaults.
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)itemValue
- Property value to add (or replace)getPropertyLong(java.lang.String, java.lang.String)
public long getPropertyLong(java.lang.String sectionName, java.lang.String itemName) throws java.util.NoSuchElementException, java.lang.NumberFormatException
long
value.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyLong logs and throws the exception.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.java.util.NoSuchElementException
- the item was not foundjava.lang.NumberFormatException
- the value of the item was
not a longpublic long getPropertyLong(java.lang.String sectionName, java.lang.String itemName, long defValue)
long
value or the provided default.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyLong logs the exception before returning the provided default.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.defValue
- The default value when a valid item value is not
found.public void putPropertyInt(java.lang.String sectionName, java.lang.String itemName, int itemValue)
int
value in the property list.
This does not modify the default properties list or any of its defaults.
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)itemValue
- Property value to add (or replace)getPropertyInt(java.lang.String, java.lang.String)
public int getPropertyInt(java.lang.String sectionName, java.lang.String itemName) throws java.util.NoSuchElementException, java.lang.NumberFormatException
int
value.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyInt logs and throws the exception.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.java.util.NoSuchElementException
- the item was not foundjava.lang.NumberFormatException
- the value of the item was
not a intpublic int getPropertyInt(java.lang.String sectionName, java.lang.String itemName, int defValue)
int
value or the provided default.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyInt logs the exception before returning the provided default.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.defValue
- The default value when a valid item value is not
found.public void putPropertyString(java.lang.String sectionName, java.lang.String itemName, java.lang.String itemValue)
String
value in the property list.
This does not modify the default properties list or any of its defaults.
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)itemValue
- Property value to add (or replace)getPropertyString(java.lang.String, java.lang.String)
public java.lang.String getPropertyString(java.lang.String sectionName, java.lang.String itemName) throws java.util.NoSuchElementException
String
value.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
sectionName
- The properties section name.itemName
- The property name within section sectionName
java.util.NoSuchElementException
- if the sectionName,
itemName pair is not found.
If the item is not found, getPropertyString logs and throws an exception.
putPropertyString(java.lang.String, java.lang.String, java.lang.String)
public java.lang.String getPropertyString(java.lang.String sectionName, java.lang.String itemName, java.lang.String defValue)
String
value or the provided default.
If the property is not found in this property list, the default property list and its defaults are searched recursively.
If the item is not found or its value is illegal, getPropertyString logs the exception before returning the provided default.
sectionName
- The properties section name.itemName
- The property name within section
sectionName
.defValue
- The default value when a valid item value is not
found.public java.lang.String[] getSectionStringValues(java.lang.String sectionName, java.lang.String itemRoot)
This method can be used for returning all of the string values in a section when the section contains a count item and each string item name is of the form: itemRoot<n>, where n goes from 1 to count.
sectionName
- section name to processitemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)getSectionStringValues(java.lang.String, java.lang.String, java.lang.String)
public java.lang.String[] getSectionStringValues(java.lang.String sectionName, java.lang.String countKey, java.lang.String itemRoot)
This method can be used for returning all of the string values in a section when the section contains a countKey item and each string item name is of the form: itemRoot<n>, where n goes from 1 to countKey.
sectionName
- section name to processcountKey
- key name of item holding total number of items in
the section.itemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)getSectionStringValues(java.lang.String, java.lang.String)
public void putSectionStringValues(java.lang.String sectionName, java.lang.String itemRoot, java.lang.String[] values)
This method can be used for adding multiple string values to a section. The resulting section contains a count item and a string item name of the form: itemRoot<n>, where n goes from 1 to count.
sectionName
- section name to processitemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)values
- item values to addputSectionStringValues
,
addSectionStringValue
,
addSectionStringValue
public void putSectionStringValues(java.lang.String sectionName, java.lang.String countKey, java.lang.String itemRoot, java.lang.String[] values)
This method can be used for adding multiple string values to a section. The resulting section contains a countKey item and a string item name of the form: itemRoot<n>, where n goes from 1 to count.
sectionName
- section name to processcountKey
- key name of item holding total number of items in
the section.itemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)values
- item values to addputSectionStringValues
,
addSectionStringValue
,
addSectionStringValue
public void addSectionStringValue(java.lang.String sectionName, java.lang.String itemRoot, java.lang.String value)
This is identical to calling the
addSectionStringValue
method
with a countKey
value of "count".
sectionName
- section name to processitemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)value
- item value to addaddSectionStringValue
,
putSectionStringValues
,
putSectionStringValues
public void addSectionStringValue(java.lang.String sectionName, java.lang.String countKey, java.lang.String itemRoot, java.lang.String value)
This method can be used for adding a string value to an existing
section (or creating the section if it does not already exist).
addSectionStringValue
automatically increments
the countKey item and adds a string item name of the form:
itemRoot<n>,
where n is the current countKey value.
sectionName
- section name to processcountKey
- key name of item holding total number of items in
the section.itemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)value
- item value to addaddSectionStringValue
,
putSectionStringValues
,
putSectionStringValues
public boolean isValueInSection(java.lang.String sectionName, java.lang.String itemRoot, java.lang.String value)
public boolean isValueInSection(java.lang.String sectionName, java.lang.String countKey, java.lang.String itemRoot, java.lang.String value)
public boolean isExistingProperty(java.lang.String sectionName, java.lang.String itemName)
If the property is not found in this property list, the default property
list and its defaults are searched recursively. This method returns
true
if the property is found in any one of the lists
searched. Otherwise, returns false
.
sectionName
- The properties section name.itemName
- The property name within section sectionName
public void removeSection(java.lang.String sectionName)
sectionName
- Name of the section to be removedpublic void removeProperty(java.lang.String sectionName, java.lang.String itemName)
sectionName
- Name of section for adding (or replacing a property value).itemName
- Property to add (or replace)getPropertyString(java.lang.String, java.lang.String)
public void clearSection(java.lang.String sectionName)
sectionName
- Name of the section to clear of all properties.public void clearSectionStringValues(java.lang.String sectionName, java.lang.String itemRoot)
putSectionStringValues
and removes the
the referenced sections.sectionName
- section added by calling
putSectionStringValues
itemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)clearSectionStringValues
,
putSectionStringValues
,
putSectionStringValues
public void clearSectionStringValues(java.lang.String sectionName, java.lang.String countKey, java.lang.String itemRoot)
putSectionStringValues
and removes the
the referenced sections.sectionName
- section added by calling
putSectionStringValues
countKey
- key name of item holding total number of items in
the sectionName
itemRoot
- common portion of each item name (ex: a root
of name yields item names of name1,
name2, name3, etc.)clearSectionStringValues
,
putSectionStringValues
,
putSectionStringValues
public void save() throws java.io.IOException
java.io.IOException
- If a source file is not foundpublic void save(java.lang.String fileName) throws java.io.IOException
fileName
- File specification.java.io.IOException
- If fileName is not foundpublic java.lang.String toString()
toString
in class java.lang.Object
public static java.lang.String removeComment(java.lang.String data, java.lang.String commentChar)
public boolean isEmpty()
public void removeAll()
The result is equivalent to calling the PropertiesList() constructor.
#PropertiesList()
protected java.lang.String getIllegalValueMsg(java.lang.String section, java.lang.String item, java.lang.String val)
public java.util.Set<java.lang.String> getSectionNames()
public java.util.Set<java.lang.String> getSectionNames(boolean includeDefaults)
public java.util.Set<java.lang.String> getSectionItemNames(java.lang.String sectionName)
sectionName
- name of sectionpublic PropertiesList merge()
public boolean hasSection(java.lang.String sectionName)
sectionName
- section to look for.