| java.lang.Object com.Ostermiller.util.UberProperties
UberProperties | public class UberProperties (Code) | | The Properties class represents a persistent set of properties. The
Properties can be saved to a stream or loaded from a stream. Each key and
its corresponding value in the property list is a string.
More information about this class is available from ostermiller.org.
A property list can contain another property list as its "defaults"; this
second property list is searched if the property key is not found in the
original property list.
When saving properties to a stream or loading them from a stream, the ISO
8859-1 character encoding is used. For characters that cannot be directly
represented in this encoding, Unicode escapes are used; however, only a
single 'u' character is allowed in an escape sequence. The native2ascii tool
can be used to convert property files to and from other character encodings.
Unlike the java.util.Properties, UberProperties does not inherit from
java.util.Hashtable, so Objects other than strings cannot be stored in it.
Also, comments from a files are preserved, and there can be several
properties for a given name.
This class is not synchronized, so it should not be used in a
multi-threaded environment without external synchronization.
The file format that UberProperties uses is as follows:
The file is assumed to be using the ISO 8859-1 character encoding. All of the
comment lines (starting with a '#' or '!') at the beginning of the file before the
first line that is not a comment, are the comment associated with the file.
After that, each comment will be associated with the next property. If there
is more than one property with the same name, the first comment will be the
only one that is loaded.
Every property occupies one line of the input stream. Each line is terminated
by a line terminator (\n or \r or \r\n).
A line that contains only whitespace or whose first non-whitespace character
is an ASCII # or ! is ignored (thus, # or ! indicate comment lines).
Every line other than a blank line or a comment line describes one property
to be added to the table (except that if a line ends with \, then the
following line, if it exists, is treated as a continuation line,
as described below). The key consists of all the characters in the line
starting with the first non-whitespace character and up to, but not
including, the first ASCII =, :, or whitespace character. All of the key
termination characters may be included in the key by preceding them with a \.
Any whitespace after the key is skipped; if the first non-whitespace
character after the key is = or :, then it is ignored and any whitespace
characters after it are also skipped. All remaining characters on the line
become part of the associated element string. Within the element string, the
ASCII escape sequences \t, \n, \r, \\, \", \', \ (a backslash and a space),
and \\uxxxx are recognized and converted to single characters. Moreover, if
the last character on the line is \, then the next line is treated as a
continuation of the current line; the \ and line terminator are simply
discarded, and any leading whitespace characters on the continuation line are
also discarded and are not part of the element string.
As an example, each of the following four lines specifies the key "Truth"
and the associated element value "Beauty":
Truth = Beauty
Truth:Beauty
Truth :Beauty
As another example, the following three lines specify a single property:
fruits apple, banana, pear, \
cantaloupe, watermelon, \
kiwi, mango
The key is "fruits" and the associated element is:
"apple, banana, pear, cantaloupe, watermelon, kiwi, mango"
Note that a space appears before each \ so that a space will appear after
each comma in the final result; the \, line terminator, and leading
whitespace on the continuation line are merely discarded and are not replaced
by one or more other characters.
As a third example, the line:
cheeses
specifies that the key is "cheeses" and the associated element is the empty
string.
author: Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities since: ostermillerutils 1.00.00 |
Method Summary | |
public void | addProperties(String name, String[] values, String comment) Adds the values to the list of properties with the
given name. | public void | addProperties(String name, String[] values) Adds the values to the list of properties with the
given name. | public void | addProperty(String name, String value, String comment) Adds a value to the list of properties with the
given name. | public void | addProperty(String name, String value) Adds a value to the list of properties with the
given name. | public boolean | contains(String name) Test to see if a property with the given name exists.
Parameters: name - the name of the property. | public String | getComment(String name) Get the comment associated with this property.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. | public String | getComment() Get the comment associated with this set of properties. | public String[] | getProperties(String name) Get the values for a property.
Properties returned in the same order in which
they were added.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. | public String[] | getProperties(String name, String[] defaultValues) Get the values for a property. | public String | getProperty(String name) Get the first property with the given name.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. | public String | getProperty(String name, String defaultValue) Get the first property with the given name. | public int | getPropertyNameCount() Get the number of unique names for properties stored
in this UberProperties. | public void | load(String[] userFile, String systemResource) Load these properties from a user file with default properties
from a system resource.
Example:
load(
new String(){".java","tld","company","package","component.properties"}
"tld/company/package/component.properties",
)
This will load the properties file relative to the classpath as the
defaults and the file <%userhome%>/.java/tld/company/package/component.properties
if the file exists. | public void | load(InputStream in, boolean add) Add the properties from the input stream to this
UberProperties. | public void | load(InputStream in) Add the properties from the input stream to this
UberProperties. | public String[] | propertyNames() Returns an enumeration of all the keys in this property list, including
distinct keys in the default property list if a key of the same name has
not already been found from the main properties list. | public boolean | remove(String name) Remove any property with the given name.
Parameters: name - the name of the property. | public void | save(String[] userFile) Save these properties from a user file. | public void | save(OutputStream out) Save these properties to the given stream. | public void | setComment(String comment) Set the comment associated with this set of properties. | public void | setProperties(String name, String[] values) Replaces all properties of the given name with
properties with the given values. | public void | setProperties(String name, String[] values, String comment) Replaces all properties of the given name with
properties with the given values. | public void | setProperty(String name, String value) Replaces all properties of the given name with
a single property with the given value. | public void | setProperty(String name, String value, String comment) Replaces all properties of the given name with
a single property with the given value. | public String | toString() Save these properties to a string. |
UberProperties | public UberProperties()(Code) | | Creates an empty property list with no default values.
since: ostermillerutils 1.00.00 |
UberProperties | public UberProperties(UberProperties defaults)(Code) | | Creates an empty property list with the specified defaults.
Parameters: defaults - the defaults. throws: NullPointerException - if defaults is null. since: ostermillerutils 1.00.00 |
addProperties | public void addProperties(String name, String[] values, String comment)(Code) | | Adds the values to the list of properties with the
given name.
Parameters: name - the name of the property. Parameters: values - the values for the property. Parameters: comment - the comment for the property, or null to remove it. throws: NullPointerException - if name is null. throws: NullPointerException - if values is null. since: ostermillerutils 1.00.00 |
addProperties | public void addProperties(String name, String[] values)(Code) | | Adds the values to the list of properties with the
given name.
Parameters: name - the name of the property. Parameters: values - the values for the property. throws: NullPointerException - if name is null. throws: NullPointerException - if values is null. since: ostermillerutils 1.00.00 |
addProperty | public void addProperty(String name, String value, String comment)(Code) | | Adds a value to the list of properties with the
given name.
Parameters: name - the name of the property. Parameters: value - the values for the property, or null to remove. Parameters: comment - the comment for the property, or null to remove it. throws: NullPointerException - if name is null. throws: NullPointerException - if value is null. since: ostermillerutils 1.00.00 |
addProperty | public void addProperty(String name, String value)(Code) | | Adds a value to the list of properties with the
given name.
Parameters: name - the name of the property. Parameters: value - the values for the property. throws: NullPointerException - if name is null. throws: NullPointerException - if value is null. since: ostermillerutils 1.00.00 |
contains | public boolean contains(String name)(Code) | | Test to see if a property with the given name exists.
Parameters: name - the name of the property. true if the property existed and was removed, false if it did not exist. throws: NullPointerException - if name is null. since: ostermillerutils 1.00.00 |
getComment | public String getComment(String name)(Code) | | Get the comment associated with this property.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. If no default is found, null is returned.
Parameters: name - Parameter name the comment for this property, or null if there is no comment or the property does not exist. since: ostermillerutils 1.00.00 |
getComment | public String getComment()(Code) | | Get the comment associated with this set of properties.
comment for entire set of properties, or null if there is no comment. since: ostermillerutils 1.00.00 |
getProperties | public String[] getProperties(String name)(Code) | | Get the values for a property.
Properties returned in the same order in which
they were added.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. If no default is found, null is returned.
Parameters: name - Parameter name all the values associated with the given key, or null if the property does not exist. since: ostermillerutils 1.00.00 |
getProperties | public String[] getProperties(String name, String[] defaultValues)(Code) | | Get the values for a property.
Properties returned in the same order in which
they were added.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default
UberProperties is consulted, otherwise, the supplied
defaults are used.
Parameters: name - Parameter name Parameters: defaultValues - Values to use when property not present all the values associated with the given key, or null if the property does not exist. since: ostermillerutils 1.00.00 |
getProperty | public String getProperty(String name)(Code) | | Get the first property with the given name.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default is
used. If no default is found, null is returned.
Parameters: name - Parameter name the first value of this property, or null if the property does not exist. since: ostermillerutils 1.00.00 |
getProperty | public String getProperty(String name, String defaultValue)(Code) | | Get the first property with the given name.
If the property is not specified in this UberProperties
but it is in the default UberProperties, the default
UberProperties is consulted, otherwise, the supplied
default is used.
Parameters: name - Parameter name Parameters: defaultValue - Value to use when property not present the first value of this property. since: ostermillerutils 1.00.00 |
getPropertyNameCount | public int getPropertyNameCount()(Code) | | Get the number of unique names for properties stored
in this UberProperties.
number of names. since: ostermillerutils 1.00.00 |
load | public void load(String[] userFile, String systemResource) throws IOException(Code) | | Load these properties from a user file with default properties
from a system resource.
Example:
load(
new String(){".java","tld","company","package","component.properties"}
"tld/company/package/component.properties",
)
This will load the properties file relative to the classpath as the
defaults and the file <%userhome%>/.java/tld/company/package/component.properties
if the file exists. The .java directory is recommended as it is a common,
possibly hidden, directory in the users home directory commonly used by
Java programs.
This method is meant to be used with the save(String systemResource) method
which will save modified properties back to the user directory.
Parameters: userFile - array of Strings representing a path and file name relative to the user home directory. Parameters: systemResource - name relative to classpath of default properties, or null to ignore. throws: IOException - if an error occurs when reading. throws: NullPointerException - if userFile is null. throws: IllegalArgumentException - if userFile is empty. since: ostermillerutils 1.00.00 |
load | public void load(InputStream in, boolean add) throws IOException(Code) | | Add the properties from the input stream to this
UberProperties.
Parameters: in - InputStream containing properties. Parameters: add - whether parameters should add to parameters with the same name or replace them. throws: IOException - if an error occurs when reading. since: ostermillerutils 1.00.00 |
load | public void load(InputStream in) throws IOException(Code) | | Add the properties from the input stream to this
UberProperties.
Properties that are found replace any properties that
were there before.
Parameters: in - InputStream containing properties. throws: IOException - if an error occurs when reading. since: ostermillerutils 1.00.00 |
propertyNames | public String[] propertyNames()(Code) | | Returns an enumeration of all the keys in this property list, including
distinct keys in the default property list if a key of the same name has
not already been found from the main properties list.
an enumeration of all the keys in this property list, including the keys in the default property list. since: ostermillerutils 1.00.00 |
remove | public boolean remove(String name)(Code) | | Remove any property with the given name.
Parameters: name - the name of the property. true if the property existed and was removed, false if it did not exist. throws: NullPointerException - if name is null. since: ostermillerutils 1.00.00 |
save | public void save(String[] userFile) throws IOException(Code) | | Save these properties from a user file.
Example:
save(
new String(){"tld","company","package","component.properties"}
)
This will save the properties file relative to the user directory:
<%userhome%>/tld/company/package/component.properties
Directories will be created as needed.
Parameters: userFile - array of Strings representing a path and file name relative to the user home directory. throws: IOException - if an error occurs when reading. throws: NullPointerException - if userFile is null. throws: IllegalArgumentException - if userFile is empty. since: ostermillerutils 1.00.00 |
save | public void save(OutputStream out) throws IOException(Code) | | Save these properties to the given stream.
Parameters: out - OutputStream to which these properties should be written. throws: IOException - if an error occurs when writing. since: ostermillerutils 1.00.00 |
setComment | public void setComment(String comment)(Code) | | Set the comment associated with this set of properties.
Parameters: comment - the comment for entire set of properties, or null to clear. since: ostermillerutils 1.00.00 |
setProperties | public void setProperties(String name, String[] values)(Code) | | Replaces all properties of the given name with
properties with the given values.
Parameters: name - the name of the property. Parameters: values - for the property. throws: NullPointerException - if name is null. throws: NullPointerException - if values is null. throws: IllegalArgumentException - if values is empty. since: ostermillerutils 1.00.00 |
setProperties | public void setProperties(String name, String[] values, String comment)(Code) | | Replaces all properties of the given name with
properties with the given values.
Parameters: name - the name of the property. Parameters: values - value of the property. Parameters: comment - the comment for the property, or null to remove it. throws: NullPointerException - if name is null. throws: NullPointerException - if values is null. throws: IllegalArgumentException - if values is empty. since: ostermillerutils 1.00.00 |
setProperty | public void setProperty(String name, String value)(Code) | | Replaces all properties of the given name with
a single property with the given value.
Parameters: name - the name of the property. Parameters: value - the value of the property, or null to remove it. throws: NullPointerException - if name is null. since: ostermillerutils 1.00.00 |
setProperty | public void setProperty(String name, String value, String comment)(Code) | | Replaces all properties of the given name with
a single property with the given value.
Parameters: name - the name of the property. Parameters: value - the value of the property or null to remove it. Parameters: comment - the comment for the property, or null to remove it. throws: NullPointerException - if name is null. throws: NullPointerException - if comment is null. since: ostermillerutils 1.00.00 |
toString | public String toString()(Code) | | Save these properties to a string.
Serialized String version of these properties. since: ostermillerutils 1.02.23 |
|
|