public class LayoutPreservingProperties extends Properties
A Properties collection which preserves comments and whitespace present in the input stream from which it was loaded.
The class defers the usual work of the java.util.Properties class to there, but it also keeps track of the contents of the input stream from which it was loaded (if applicable), so that it can write out the properties in as close a form as possible to the input.
If no changes occur to property values, the output should be the same
as the input, except for the leading date stamp, as normal for a
properties file. Properties added are appended to the file. Properties
whose values are changed are changed in place. Properties that are
removed are excised. If the removeComments
flag is set,
then the comments immediately preceding the property are also removed.
If a second set of properties is loaded into an existing set, the lines of the second set are added to the end. Note however, that if a property already stored is present in a stream subsequently loaded, then that property is removed before the new value is set. For example, consider the file
# the first line alpha=one # the second line beta=two
This file is loaded, and then the following is also loaded into the
same LayoutPreservingProperties
object
# association beta=band # and finally gamma=rays
The resulting collection sequence of logical lines depends on whether
or not removeComments
was set at the time the second stream
is loaded. If it is set, then the resulting list of lines is
# the first line alpha=one # association beta=band # and finally gamma=rays
If the flag is not set, then the comment "the second line" is retained,
although the key-value pair beta=two
is removed.
defaults
Constructor and Description |
---|
LayoutPreservingProperties()
Create a new, empty, Properties collection, with no defaults.
|
LayoutPreservingProperties(Properties defaults)
Create a new, empty, Properties collection, with the specified defaults.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this hashtable so that it contains no keys.
|
Object |
clone()
Creates a shallow copy of this hashtable.
|
boolean |
isRemoveComments()
Returns
true if comments are removed along with
properties, or false otherwise. |
void |
listLines(PrintStream out)
Echo the lines of the properties (including blanks and comments) to the
stream.
|
void |
load(InputStream inStream)
Reads a property list (key and element pairs) from the input byte stream.
|
Object |
put(Object key,
Object value)
Maps the specified
key to the specified value in this hashtable. |
Object |
remove(Object key)
Removes the key (and its corresponding value) from this hashtable.
|
void |
saveAs(File dest)
Save the properties to a file.
|
Object |
setProperty(String key,
String value)
Calls the Hashtable method
put . |
void |
setRemoveComments(boolean val)
Sets the behaviour for comments accompanying properties that
are being removed.
|
void |
store(OutputStream out,
String header)
Writes this property list (key and element pairs) in this
Properties table to the output stream in a
format suitable for loading into a Properties table using the load(InputStream) method. |
getProperty, getProperty, list, list, load, propertyNames, store, stringPropertyNames
public LayoutPreservingProperties()
public LayoutPreservingProperties(Properties defaults)
defaults
- the default property valuespublic void clear()
Hashtable
public Object clone()
Hashtable
public boolean isRemoveComments()
true
if comments are removed along with
properties, or false
otherwise. If
true
, then when a property is removed, the comment
preceding it in the original file is removed also.true
if leading comments are removed when
a property is removed; false
otherwisepublic void listLines(PrintStream out)
out
- the stream to write topublic void load(InputStream inStream) throws IOException
Properties
load(Reader)
and is assumed to use the ISO
8859-1 character encoding; that is each byte is one Latin1 character. Characters not in Latin1, and certain
special characters, are represented in keys and elements using Unicode escapes as defined in section 3.3 of
The Java™ Language Specification.
The specified stream remains open after this method returns.
load
in class Properties
inStream
- the input stream.IOException
- if an error occurred when reading from the input stream.public Object put(Object key, Object value) throws NullPointerException
Hashtable
key
to the specified value
in this hashtable.
Neither the key nor the value can be null
.
The value can be retrieved by calling the get
method with a key that is equal to the
original key.
put
in interface Map<Object,Object>
put
in class Hashtable<Object,Object>
key
- the hashtable keyvalue
- the valuenull
if it did
not have oneNullPointerException
- if the key or value is null
Object.equals(Object)
,
Hashtable.get(Object)
public Object remove(Object key)
Hashtable
public void saveAs(File dest) throws IOException
dest
- the file to write toIOException
public Object setProperty(String key, String value) throws NullPointerException
Properties
put
. Provided for parallelism with the getProperty method.
Enforces use of strings for property keys and values. The value returned is the result of the Hashtable
call to put
.setProperty
in class Properties
key
- the key to be placed into this property list.value
- the value corresponding to key.null
if it did not have one.NullPointerException
Properties.getProperty(java.lang.String)
public void setRemoveComments(boolean val)
true
, then when a property
is removed, the comment preceding it in the original file is
removed also.val
- true
if leading comments are to be
removed when a property is removed; false
otherwisepublic void store(OutputStream out, String header) throws IOException
Properties
Properties
table to the output stream in a
format suitable for loading into a Properties
table using the load(InputStream)
method.
Properties from the defaults table of this Properties
table (if any) are not written out by this
method.
This method outputs the comments, properties keys and values in the same format as specified in
store(Writer)
, with the following differences:
\u
xxxx for their appropriate
unicode hexadecimal value xxxx.
\u0020
and characters greater than \u007E
in property keys or
values are written as \u
xxxx for the appropriate hexadecimal value xxxx.
After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.
store
in class Properties
out
- an output stream.header
- a description of the property list.IOException
- if writing this property list to the specified output stream throws an IOException.