org.apache.commons.configuration
public class PropertiesConfigurationLayout extends Object implements ConfigurationListener
A helper class used by PropertiesConfiguration
to keep
the layout of a properties file.
Instances of this class are associated with a
PropertiesConfiguration
object. They are responsible for
analyzing properties files and for extracting as much information about the
file layout (e.g. empty lines, comments) as possible. When the properties
file is written back again it should be close to the original.
The PropertiesConfigurationLayout
object associated with a
PropertiesConfiguration
object can be obtained using the
getLayout()
method of the configuration. Then the methods
provided by this class can be used to alter the properties file's layout.
Implementation note: This is a very simple implementation, which is far away from being perfect, i.e. the original layout of a properties file won't be reproduced in all cases. One limitation is that comments for multi-valued property keys are concatenated. Maybe this implementation can later be improved.
To get an impression how this class works consider the following properties file:
# A demo configuration file # for Demo App 1.42 # Application name AppName=Demo App # Application vendor AppVendor=DemoSoft # GUI properties # Window Color windowColors=0xFFFFFF,0x000000 # Include some setting include=settings.properties # Another vendor AppVendor=TestSoft
For this example the following points are relevant:
AppName
one comment line and one
leading blanc line is stored.windowColors
two comment lines and two
leading blanc lines are stored.AppVendor
the "single
line" flag is set. This is relevant only for windowColors
,
which has multiple values defined in one line using the separator character.AppVendor
property appears twice. The comment lines
are concatenated, so that layout.getComment("AppVendor");
will
result in Application vendor<CR>Another vendor
, whith
<CR>
meaning the line separator. In addition the
"single line" flag is set to false for this property. When
the file is saved, two property definitions will be written (in series).Since: 1.3
Version: $Id: PropertiesConfigurationLayout.java 439648 2006-09-02 20:42:10Z oheger $
Nested Class Summary | |
---|---|
static class | PropertiesConfigurationLayout.PropertyLayoutData
A helper class for storing all layout related information for a
configuration property. |
Field Summary | |
---|---|
PropertiesConfiguration | configuration Stores the associated configuration object. |
static String | COMMENT_PREFIX Constant for the default comment prefix. |
static String | CR Constant for the line break character. |
boolean | forceSingleLine Stores the force single line flag. |
String | headerComment Stores the header comment. |
Map | layoutData Stores a map with the contained layout information. |
int | loadCounter A counter for determining nested load calls. |
Constructor Summary | |
---|---|
PropertiesConfigurationLayout(PropertiesConfiguration config)
Creates a new instance of PropertiesConfigurationLayout
and initializes it with the associated configuration object.
| |
PropertiesConfigurationLayout(PropertiesConfiguration config, PropertiesConfigurationLayout c)
Creates a new instance of PropertiesConfigurationLayout
and initializes it with the given configuration object. |
Method Summary | |
---|---|
int | checkHeaderComment(List commentLines)
Checks if parts of the passed in comment can be used as header comment.
|
void | clear()
Removes all content from this layout object. |
void | configurationChanged(ConfigurationEvent event)
The event listener callback. |
void | copyFrom(PropertiesConfigurationLayout c)
Copies the data from the given layout object.
|
String | extractComment(List commentLines, int from, int to)
Extracts a comment string from the given range of the specified comment
lines. |
PropertiesConfigurationLayout.PropertyLayoutData | fetchLayoutData(String key)
Returns a layout data object for the specified key. |
int | getBlancLinesBefore(String key)
Returns the number of blanc lines before this property key. |
String | getCanonicalComment(String key, boolean commentChar)
Returns the comment for the specified property key in a cononical form.
|
String | getCanonicalHeaderComment(boolean commentChar)
Returns the header comment of the represented properties file in a
canonical form. |
String | getComment(String key)
Returns the comment for the specified property key. |
PropertiesConfiguration | getConfiguration()
Returns the associated configuration object.
|
String | getHeaderComment()
Returns the header comment of the represented properties file. |
Set | getKeys()
Returns a set with all property keys managed by this object.
|
static boolean | isCommentLine(String line)
Tests whether a line is a comment, i.e. whether it starts with a comment
character.
|
boolean | isForceSingleLine()
Returns the "force single line" flag.
|
boolean | isSingleLine(String key)
Returns a flag whether the specified property is defined on a single
line. |
void | load(Reader in)
Reads a properties file and stores its internal structure. |
void | save(Writer out)
Writes the properties file to the given writer, preserving as much of its
structure as possible.
|
void | setBlancLinesBefore(String key, int number)
Sets the number of blanc lines before the given property key. |
void | setComment(String key, String comment)
Sets the comment for the specified property key. |
void | setForceSingleLine(boolean f)
Sets the "force single line" flag. |
void | setHeaderComment(String comment)
Sets the header comment for the represented properties file. |
void | setSingleLine(String key, boolean f)
Sets the "single line flag" for the specified property key.
|
static String | stripCommentChar(String s, boolean comment)
Either removes the comment character from the given comment line or
ensures that the line starts with a comment character.
|
static String | trimComment(String s, boolean comment)
Trims a comment. |
PropertiesConfigurationLayout
and initializes it with the associated configuration object.
Parameters: config the configuration (must not be null)
PropertiesConfigurationLayout
and initializes it with the given configuration object. The data of the
specified layout object is copied.
Parameters: config the configuration (must not be null) c the layout object to be copied
Parameters: commentLines the comment lines
Returns: the index of the next line after the header comment
Parameters: event the event object
Parameters: c the layout object to copy
Parameters: commentLines a list with comment lines from the start index to the end index (inclusive)
Returns: the comment string (null if it is undefined)
Parameters: key the key
Returns: the corresponding layout data object
Parameters: key the property key
Returns: the number of blanc lines before the property definition for this key
commentChar
parameter is false,
all comment characters are removed, so that the result is only the plain
text of the comment. Otherwise it is ensured that each line of the
comment starts with a comment character.
Parameters: key the key of the property commentChar determines whether all lines should start with comment characters or not
Returns: the canonical comment for this key (can be null)
commentChar
parameter it can be
specified whether comment characters should be stripped or be always
present.
Parameters: commentChar determines the presence of comment characters
Returns: the header comment (can be null)
setComment()
or when it was loaded from a properties
file). No modifications are performed.
Parameters: key the key of the property
Returns: the comment for this key (can be null)
Returns: the associated configuration
setHeaderComment()
or extracted from the loaded properties
file.
Returns: the header comment (can be null)
Returns: a set with all contained property keys
Parameters: line the line
Returns: a flag if this is a comment line
Returns: the force single line flag
See Also: PropertiesConfigurationLayout
Parameters: key the property key
Returns: a flag if this property is defined on a single line
Parameters: in the reader to the properties file
Throws: ConfigurationException if an error occurs
Parameters: out the writer
Throws: ConfigurationException if an error occurs
Parameters: key the property key number the number of blanc lines to add before this property definition
Parameters: key the key of the property comment the comment for this key (can be null, then the comment will be removed)
java.lang.Properties
,
which cannot deal with multiple definitions of a single property.
Parameters: f the force single line flag
Parameters: comment the comment
Parameters: key the property key f the single line flag
Parameters: s the comment line comment if true, a comment character will always be enforced; if false, it will be removed
Returns: the line without comment character
Parameters: s the string to be processed comment if true, a comment character will always be enforced; if false, it will be removed
Returns: the trimmed comment