public class PropertiesConfigurationLayout extends Object implements EventListener<ConfigurationEvent>
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 blank line is stored.windowColors
two comment lines and two leading blank 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()
Creates a new, empty instance of
PropertiesConfigurationLayout . |
PropertiesConfigurationLayout(PropertiesConfigurationLayout c)
Creates a new instance of
PropertiesConfigurationLayout and copies the data of the specified layout object. |
Modifier and Type | Method and Description |
---|---|
int |
getBlancLinesBefore(String key)
Deprecated.
|
int |
getBlankLinesBefore(String key)
Returns the number of blank 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.
|
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(PropertiesConfiguration config,
Reader reader)
Reads a properties file and stores its internal structure.
|
void |
onEvent(ConfigurationEvent event)
The event listener callback.
|
void |
save(PropertiesConfiguration config,
Writer writer)
Writes the properties file to the given writer, preserving as much of its structure as possible.
|
void |
setBlancLinesBefore(String key,
int number)
Deprecated.
|
void |
setBlankLinesBefore(String key,
int number)
Sets the number of blank 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()
PropertiesConfigurationLayout
.public PropertiesConfigurationLayout(PropertiesConfigurationLayout c)
PropertiesConfigurationLayout
and copies the data of the specified layout object.c
- the layout object to be copiedpublic 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)@Deprecated public int getBlancLinesBefore(String key)
getBlankLinesBefore(String)
.key
- the property keypublic int getBlankLinesBefore(String key)
key
- the property key@Deprecated public void setBlancLinesBefore(String key, int number)
setBlankLinesBefore(String, int)
.key
- the property keynumber
- the number of blank lines to add before this property definitionpublic void setBlankLinesBefore(String key, int number)
key
- the property keynumber
- the number of blank 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(PropertiesConfiguration config, Reader reader) throws ConfigurationException
config
- the associated configuration objectreader
- the reader to the properties fileConfigurationException
- if an error occurspublic void save(PropertiesConfiguration config, Writer writer) throws ConfigurationException
config
- the associated configuration objectwriter
- the writerConfigurationException
- if an error occurspublic void onEvent(ConfigurationEvent event)
onEvent
in interface EventListener<ConfigurationEvent>
event
- the event objectCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.