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
, with
<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).Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
void |
configurationChanged(ConfigurationEvent event)
The event listener callback.
|
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 canonical form.
|
String |
getCanonicalFooterCooment(boolean commentChar)
Returns the footer comment of the represented properties file in a
canonical 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 |
getFooterComment()
Returns the footer comment of the represented properties file.
|
String |
getGlobalSeparator()
Returns the global separator.
|
String |
getHeaderComment()
Returns the header comment of the represented properties file.
|
Set<String> |
getKeys()
Returns a set with all property keys managed by this object.
|
String |
getLineSeparator()
Returns the line separator.
|
String |
getSeparator(String key)
Returns the separator for the property with the given key.
|
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 |
setFooterComment(String footerComment)
Sets the footer comment for the represented properties file.
|
void |
setForceSingleLine(boolean f)
Sets the "force single line" flag.
|
void |
setGlobalSeparator(String globalSeparator)
Sets the global separator for properties.
|
void |
setHeaderComment(String comment)
Sets the header comment for the represented properties file.
|
void |
setLineSeparator(String lineSeparator)
Sets the line separator.
|
void |
setSeparator(String key,
String sep)
Sets the separator to be used for the property with the given key.
|
void |
setSingleLine(String key,
boolean f)
Sets the "single line flag" for the specified property key.
|
public PropertiesConfigurationLayout(PropertiesConfiguration config)
PropertiesConfigurationLayout
and initializes it with the associated configuration object.config
- the configuration (must not be null)public PropertiesConfigurationLayout(PropertiesConfiguration config, PropertiesConfigurationLayout c)
PropertiesConfigurationLayout
and initializes it with the given configuration object. The data of the
specified layout object is copied.config
- the configuration (must not be null)c
- the layout object to be copiedpublic PropertiesConfiguration getConfiguration()
public String getCanonicalComment(String key, boolean commentChar)
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. Also, line breaks in the comment
are normalized to the line separator "\n".key
- the key of the propertycommentChar
- determines whether all lines should start with comment
characters or notpublic String getComment(String key)
setComment()
or when it was loaded from a properties
file). No modifications are performed.key
- the key of the propertypublic void setComment(String key, String comment)
key
- the key of the propertycomment
- the comment for this key (can be null, then the
comment will be removed)public int getBlancLinesBefore(String key)
key
- the property keypublic void setBlancLinesBefore(String key, int number)
key
- the property keynumber
- the number of blanc lines to add before this property
definitionpublic String getCanonicalHeaderComment(boolean commentChar)
commentChar
parameter it can be
specified whether comment characters should be stripped or be always
present.commentChar
- determines the presence of comment characterspublic String getHeaderComment()
setHeaderComment()
or extracted from the loaded properties
file.public void setHeaderComment(String comment)
comment
- the commentpublic String getCanonicalFooterCooment(boolean commentChar)
getCanonicalHeaderComment()
, but reads the footer comment.commentChar
- determines the presence of comment charactersgetCanonicalHeaderComment(boolean)
public String getFooterComment()
setFooterComment()
or extracted from the loaded properties
file.public void setFooterComment(String footerComment)
footerComment
- the footer commentpublic boolean isSingleLine(String key)
key
- the property keypublic void setSingleLine(String key, boolean f)
key
- the property keyf
- the single line flagpublic boolean isForceSingleLine()
setForceSingleLine(boolean)
public void setForceSingleLine(boolean f)
java.lang.Properties
,
which cannot deal with multiple definitions of a single property. This
mode has no effect if the list delimiter parsing is disabled.f
- the force single line flagpublic String getSeparator(String key)
key
- the property keypublic void setSeparator(String key, String sep)
=
and :
(with or
without whitespace) should be used, but this method does not enforce this
- it accepts arbitrary strings. If the key refers to a property with
multiple values that are written on multiple lines, this separator will
be used on all lines.key
- the key for the propertysep
- the separator to be used for this propertypublic String getGlobalSeparator()
public void setGlobalSeparator(String globalSeparator)
=
and :
(with or without whitespace)
should be used, but this method does not enforce this - it accepts
arbitrary strings. If the global separator is set to null,
property separators are not changed. This is the default behavior as it
produces results that are closer to the original properties file.globalSeparator
- the separator to be used for all propertiespublic String getLineSeparator()
public void setLineSeparator(String lineSeparator)
lineSeparator
- the line separatorpublic Set<String> getKeys()
public void load(Reader in) throws ConfigurationException
in
- the reader to the properties fileConfigurationException
- if an error occurspublic void save(Writer out) throws ConfigurationException
out
- the writerConfigurationException
- if an error occurspublic void configurationChanged(ConfigurationEvent event)
configurationChanged
in interface ConfigurationListener
event
- the event objectCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.