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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | LayoutPreservingProperties.Blank
A blank line of the input stream. |
| private class | LayoutPreservingProperties.Comment
A comment line of the input stream. |
| private abstract static class | LayoutPreservingProperties.LogicalLine
A logical line of the properties input stream. |
| private static class | LayoutPreservingProperties.Pair
A key-value pair from the input stream. |
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private Map | keyedPairLines
Position in the |
| private List | logicalLines
Logical lines have escaping and line continuation taken care of. |
| private boolean | removeComments
Flag to indicate that, when we remove a property from the file, we also want to remove the comments that precede it. |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public | |
| public | LayoutPreservingProperties(final Properties
the default property values defaults)Create a new, empty, Properties collection, with the specified defaults. |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Overrides java. Implements java. |
| public Object | |
| private String | Returns: the escaped stringthe string to escape s, final boolean if escapeAllSpaces)true the method escapes
all the spaces, if false, it escapes only the
leading whitespaceEscape the string |
| private String | Returns: the escaped stringthe string to escape s)Escape the string |
| private String | Returns: the unicode escape sequencethe character to encode ch)Return the unicode escape sequence for a character, in the form \uNNNN. |
| private String | Returns: the escaped stringthe string to escape s)Escape the string |
| private void | innerSetProperty(String
the key of the property to be stored key, String the value to be stored value)Store a new key-value pair, or add a new one. |
| public boolean | Returns: true if leading comments are removed when
a property is removed; false otherwiseReturns |
| public void | listLines(final PrintStream
the stream to write to out)Echo the lines of the properties (including blanks and comments) to the stream. |
| public void | load(final InputStream
the input stream. inStream)Overrides java. |
| public Object | put(final Object
the hashtable key key, final Object the value value)Overrides java. Implements java. key to the specified
value in this hashtable.
|
| private String | Returns: the first line without any line separator, leaves the reader positioned after the first line separatorReads the first line and determines the EOL-style of the file (relies on the style to be consistent, of course). |
| private String | readLines(final InputStream
the stream from which to read the data is)Reads a properties file into an internally maintained collection of logical lines (possibly spanning physical lines), which make up the comments, blank lines and properties of the file. |
| public Object | remove(final Object
the key that needs to be removed key)Overrides java. Implements java. |
| private void | removeCommentsEndingAt(int
the line number to which the comments lead pos)Remove the comments in the leading up the |
| private boolean | Returns: true if the line is to be continued,
false otherwisethe contents of the line to examine s)Returns |
| public void | |
| public Object | setProperty(final String
the key to be placed into this property list. key, final String the value corresponding to value)key.Overrides java. Hashtable method put.
|
| public void | setRemoveComments(final boolean
true if leading comments are to be
removed when a property is removed; false
otherwiseSets the behaviour for comments accompanying properties that are being removed. |
| public void | store(final OutputStream
an output stream. out, final String a description of the property list. header)Overrides java. Properties table to the output stream in a format suitable
for loading into a Properties table using the
load(InputStream) method.
|
| private String | Returns: the unescaped stringthe string to unescape (coming from the source file) s)Unescape the string according to the rules for a Properties file, as laid out in the docs for java.util.Properties. |
| private char | Returns: the character extractedthe character array containing the unicode character code ch, final int i)Retrieve the unicode character whose code is listed at position
|
| eol | back to summary |
|---|---|
| private String eol | |
| keyedPairLines | back to summary |
|---|---|
| private Map<String, Integer> keyedPairLines Position in the | |
| logicalLines | back to summary |
|---|---|
| private List<LayoutPreservingProperties. Logical lines have escaping and line continuation taken care of. Comments and blank lines are logical lines; they are not removed. | |
| removeComments | back to summary |
|---|---|
| private boolean removeComments Flag to indicate that, when we remove a property from the file, we also want to remove the comments that precede it. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| LayoutPreservingProperties | back to summary |
|---|---|
| public LayoutPreservingProperties() Create a new, empty, Properties collection, with no defaults. | |
| LayoutPreservingProperties | back to summary |
|---|---|
| public LayoutPreservingProperties(final Properties defaults) Create a new, empty, Properties collection, with the specified defaults.
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Clears this hashtable so that it contains no keys.
| |
| clone | back to summary |
|---|---|
| public Object clone() Overrides java. Doc from java. Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation. | |
| escape | back to summary |
|---|---|
| private String escape(final String s, final boolean escapeAllSpaces) Escape the string | |
| escapeName | back to summary |
|---|---|
| private String escapeName(final String s) Escape the string | |
| escapeUnicode | back to summary |
|---|---|
| private String escapeUnicode(final char ch) Return the unicode escape sequence for a character, in the form \uNNNN.
| |
| escapeValue | back to summary |
|---|---|
| private String escapeValue(final String s) Escape the string | |
| innerSetProperty | back to summary |
|---|---|
| private void innerSetProperty(String key, String value) Store a new key-value pair, or add a new one. The normal
functionality is taken care of by the superclass in the call to
| |
| isRemoveComments | back to summary |
|---|---|
| public boolean isRemoveComments() Returns
| |
| listLines | back to summary |
|---|---|
| public void listLines(final PrintStream out) Echo the lines of the properties (including blanks and comments) to the stream.
| |
| load | back to summary |
|---|---|
| public void load(final InputStream inStream) throws IOException Overrides java. Doc from java. Reads a property list (key and element pairs) from the input
byte stream. The input stream is in a simple line-oriented
format as specified in
The specified stream remains open after this method returns.
| |
| put | back to summary |
|---|---|
| public Object put(final Object key, final Object value) throws NullPointerException Overrides java. Implements java. Doc from java. Maps the specified
The value can be retrieved by calling the
| |
| readFirstLine | back to summary |
|---|---|
| private String readFirstLine(final PushbackReader r) throws IOException Reads the first line and determines the EOL-style of the file (relies on the style to be consistent, of course). Sets LS as a side-effect.
| |
| readLines | back to summary |
|---|---|
| private String readLines(final InputStream is) throws IOException Reads a properties file into an internally maintained collection of logical lines (possibly spanning physical lines), which make up the comments, blank lines and properties of the file.
| |
| remove | back to summary |
|---|---|
| public Object remove(final Object key) Overrides java. Implements java. Doc from java. Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable. | |
| removeCommentsEndingAt | back to summary |
|---|---|
| private void removeCommentsEndingAt(int pos) Remove the comments in the leading up the
| |
| requiresContinuation | back to summary |
|---|---|
| private boolean requiresContinuation(final String s) Returns
| |
| saveAs | back to summary |
|---|---|
| public void saveAs(final File dest) throws IOException Save the properties to a file.
| |
| setProperty | back to summary |
|---|---|
| public Object setProperty(final String key, final String value) throws NullPointerException Overrides java. Doc from java. Calls the | |
| setRemoveComments | back to summary |
|---|---|
| public void setRemoveComments(final boolean val) Sets the behaviour for comments accompanying properties that
are being removed. If
| |
| store | back to summary |
|---|---|
| public void store(final OutputStream out, final String header) throws IOException Overrides java. Doc from java. Writes this property list (key and element pairs) in this
Properties from the defaults table of this
This method outputs the comments, properties keys and values in
the same format as specified in
After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.
| |
| unescape | back to summary |
|---|---|
| private String unescape(final String s) Unescape the string according to the rules for a Properties file, as laid out in the docs for java.util.Properties. | |
| unescapeUnicode | back to summary |
|---|---|
| private char unescapeUnicode(final char[] ch, final int i) Retrieve the unicode character whose code is listed at position
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. | |
| Blank | back to summary |
|---|---|
| public Blank() | |
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. | |
| Comment | back to summary |
|---|---|
| public Comment(final String text) | |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | |
| private String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public String |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| text | back to summary |
|---|---|
| private String text | |
| LogicalLine | back to summary |
|---|---|
| public LogicalLine(final String text) | |
| setText | back to summary |
|---|---|
| public void setText(final String text) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private String | |
| private static final long | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Object | |
| private int | |
| public String | |
| public String | |
| private int | |
| public boolean | |
| private void | |
| public void | |
| public void | |
| private String |
| added | back to summary |
|---|---|
| private boolean added | |
| name | back to summary |
|---|---|
| private String name | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. | |
| value | back to summary |
|---|---|
| private String value | |
| Pair | back to summary |
|---|---|
| public Pair(final String text) | |
| Pair | back to summary |
|---|---|
| public Pair(final String name, final String value) | |
| clone | back to summary |
|---|---|
| public Object clone() Overrides java. Doc from java. Creates and returns a copy of this object. The precise meaning
of "copy" may depend on the class of the object. The general
intent is that, for any object will be true, and that the expression:x.clone() != x will bex.clone().getClass() == x.getClass() true, but these are not absolute requirements.
While it is typically the case that:
will bex.clone().equals(x) true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling
By convention, the object returned by this method should be independent
of this object (which is being cloned). To achieve this independence,
it may be necessary to modify one or more fields of the object returned
by | |
| findFirstSeparator | back to summary |
|---|---|
| private int findFirstSeparator(String s) | |
| getName | back to summary |
|---|---|
| public String getName() | |
| getValue | back to summary |
|---|---|
public String getValue()
| |
| indexOfAny | back to summary |
|---|---|
| private int indexOfAny(final String s, final String chars) | |
| isNew | back to summary |
|---|---|
| public boolean isNew() | |
| parsePair | back to summary |
|---|---|
| private void parsePair(final String text) | |
| setNew | back to summary |
|---|---|
| public void setNew(final boolean val) | |
| setValue | back to summary |
|---|---|
| public void setValue(final String value) | |
| stripStart | back to summary |
|---|---|
| private String stripStart(final String s, final String chars) | |