Class Properties
- java.lang.Object
-
- java.util.AbstractMap<java.lang.String,java.lang.String>
-
- org.apache.felix.utils.properties.Properties
-
- All Implemented Interfaces:
java.util.Map<java.lang.String,java.lang.String>
public class Properties extends java.util.AbstractMap<java.lang.String,java.lang.String>
Enhancement of the standard
Properties
managing the maintain of comments, etc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
Properties.Layout
TODOstatic class
Properties.PropertiesReader
This class is used to read properties lines.static class
Properties.PropertiesWriter
This class is used to write properties lines.
-
Field Summary
Fields Modifier and Type Field Description private InterpolationHelper.SubstitutionCallback
callback
private static java.lang.String
COMMENT_CHARS
Constant for the supported comment characters.(package private) static java.lang.String
DEFAULT_ENCODING
The default encoding (ISO-8859-1 as specified by http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html)private java.util.List<java.lang.String>
footer
private java.util.List<java.lang.String>
header
private static int
HEX_RADIX
Constant for the radix of hex numbers.private java.util.Map<java.lang.String,Properties.Layout>
layout
private static java.lang.String
LINE_SEPARATOR
Constant for the platform specific line separator.private java.io.File
location
private static char[]
SEPARATORS
The list of possible key/value separatorsprivate java.util.Map<java.lang.String,java.lang.String>
storage
(package private) boolean
substitute
(package private) boolean
typed
private static int
UNICODE_LEN
Constant for the length of a unicode literal.private static char[]
WHITE_SPACE
The white space characters used as key/value separators.
-
Constructor Summary
Constructors Constructor Description Properties()
Properties(boolean substitute)
Properties(java.io.File location)
Properties(java.io.File location, boolean substitute)
Properties(java.io.File location, InterpolationHelper.SubstitutionCallback callback)
Properties(java.io.File location, org.osgi.framework.BundleContext context)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
checkHeaderComment(java.util.List<java.lang.String> commentLines)
Checks if parts of the passed in comment can be used as header comment.void
clear()
static boolean
contains(char[] array, char valueToFind)
Checks if the value is in the given array.java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>>
entrySet()
protected static java.lang.String
escapeJava(java.lang.String str)
Escapes the characters in aString
using Java String rules.private static java.lang.String
escapeKey(java.lang.String key)
Escape the separators in the key.java.util.List<java.lang.String>
getComments(java.lang.String key)
java.util.List<java.lang.String>
getFooter()
Return the comment footer.java.util.List<java.lang.String>
getHeader()
Return the comment header.java.lang.String
getProperty(java.lang.String key)
Searches for the property with the specified key in this property list.java.lang.String
getProperty(java.lang.String key, java.lang.String defaultValue)
Searches for the property with the specified key in this property list.java.util.List<java.lang.String>
getRaw(java.lang.String key)
protected static java.lang.String
hex(char ch)
Returns an upper case hexadecimalString
for the given character.(package private) static boolean
isCommentLine(java.lang.String line)
Tests whether a line is a comment, i.e.void
load(java.io.File location)
void
load(java.io.InputStream is)
void
load(java.io.Reader reader)
void
load(java.net.URL location)
protected void
loadLayout(java.io.Reader in, boolean maybeTyped)
Reads a properties file and stores its internal structure.java.util.Enumeration<?>
propertyNames()
Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.java.lang.String
put(java.lang.String key, java.lang.String value)
java.lang.String
put(java.lang.String key, java.lang.String comment, java.lang.String value)
java.lang.String
put(java.lang.String key, java.util.List<java.lang.String> commentLines, java.lang.String value)
java.lang.String
put(java.lang.String key, java.util.List<java.lang.String> commentLines, java.util.List<java.lang.String> valueLines)
(package private) void
putAllSubstituted(java.util.Map<? extends java.lang.String,? extends java.lang.String> m)
java.lang.String
remove(java.lang.Object key)
void
save()
void
save(java.io.File location)
void
save(java.io.OutputStream os)
void
save(java.io.Writer writer)
protected void
saveLayout(java.io.Writer out, boolean typed)
Writes the properties file to the given writer, preserving as much of its structure as possible.void
setFooter(java.util.List<java.lang.String> footer)
Set the comment footer.void
setHeader(java.util.List<java.lang.String> header)
Set the comment header.java.lang.Object
setProperty(java.lang.String key, java.lang.String value)
Calls the map method put.void
store(java.io.OutputStream os, java.lang.String comment)
Store a properties into a output stream, preserving comments, special character, etc.void
substitute()
void
substitute(InterpolationHelper.SubstitutionCallback callback)
protected static java.lang.String
unescapeJava(java.lang.String str)
Unescapes any Java literals found in theString
to aWriter
.boolean
update(java.util.Map<java.lang.String,java.lang.String> props)
boolean
update(Properties properties)
-
Methods inherited from class java.util.AbstractMap
clone, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, size, toString, values
-
-
-
-
Field Detail
-
COMMENT_CHARS
private static final java.lang.String COMMENT_CHARS
Constant for the supported comment characters.- See Also:
- Constant Field Values
-
SEPARATORS
private static final char[] SEPARATORS
The list of possible key/value separators
-
WHITE_SPACE
private static final char[] WHITE_SPACE
The white space characters used as key/value separators.
-
DEFAULT_ENCODING
static final java.lang.String DEFAULT_ENCODING
The default encoding (ISO-8859-1 as specified by http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html)- See Also:
- Constant Field Values
-
LINE_SEPARATOR
private static final java.lang.String LINE_SEPARATOR
Constant for the platform specific line separator.
-
HEX_RADIX
private static final int HEX_RADIX
Constant for the radix of hex numbers.- See Also:
- Constant Field Values
-
UNICODE_LEN
private static final int UNICODE_LEN
Constant for the length of a unicode literal.- See Also:
- Constant Field Values
-
storage
private final java.util.Map<java.lang.String,java.lang.String> storage
-
layout
private final java.util.Map<java.lang.String,Properties.Layout> layout
-
header
private java.util.List<java.lang.String> header
-
footer
private java.util.List<java.lang.String> footer
-
location
private java.io.File location
-
callback
private InterpolationHelper.SubstitutionCallback callback
-
substitute
boolean substitute
-
typed
boolean typed
-
-
Constructor Detail
-
Properties
public Properties()
-
Properties
public Properties(java.io.File location) throws java.io.IOException
- Throws:
java.io.IOException
-
Properties
public Properties(java.io.File location, org.osgi.framework.BundleContext context) throws java.io.IOException
- Throws:
java.io.IOException
-
Properties
public Properties(java.io.File location, InterpolationHelper.SubstitutionCallback callback) throws java.io.IOException
- Throws:
java.io.IOException
-
Properties
public Properties(boolean substitute)
-
Properties
public Properties(java.io.File location, boolean substitute)
-
-
Method Detail
-
load
public void load(java.io.File location) throws java.io.IOException
- Throws:
java.io.IOException
-
load
public void load(java.net.URL location) throws java.io.IOException
- Throws:
java.io.IOException
-
load
public void load(java.io.InputStream is) throws java.io.IOException
- Throws:
java.io.IOException
-
load
public void load(java.io.Reader reader) throws java.io.IOException
- Throws:
java.io.IOException
-
save
public void save() throws java.io.IOException
- Throws:
java.io.IOException
-
save
public void save(java.io.File location) throws java.io.IOException
- Throws:
java.io.IOException
-
save
public void save(java.io.OutputStream os) throws java.io.IOException
- Throws:
java.io.IOException
-
save
public void save(java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
store
public void store(java.io.OutputStream os, java.lang.String comment) throws java.io.IOException
Store a properties into a output stream, preserving comments, special character, etc. This method is mainly to be compatible with the java.util.Properties class.- Parameters:
os
- an output stream.comment
- this parameter is ignored as this Properties- Throws:
java.io.IOException
- If storing fails
-
getProperty
public java.lang.String getProperty(java.lang.String key)
Searches for the property with the specified key in this property list.- Parameters:
key
- the property key.- Returns:
- the value in this property list with the specified key value.
-
getProperty
public java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns the default value argument if the property is not found.- Parameters:
key
- the property key.defaultValue
- a default value.- Returns:
- The property value of the default value
-
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.String>> entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<java.lang.String,java.lang.String>
- Specified by:
entrySet
in classjava.util.AbstractMap<java.lang.String,java.lang.String>
-
propertyNames
public java.util.Enumeration<?> propertyNames()
Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.- Returns:
- an enumeration of all the keys in this property list, including the keys in the default property list.
-
setProperty
public java.lang.Object setProperty(java.lang.String key, java.lang.String value)
Calls the map method 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 map call to put.- Parameters:
key
- the key to be placed into this property list.value
- the value corresponding to the key.- Returns:
- the previous value of the specified key in this property list, or null if it did not have one.
-
put
public java.lang.String put(java.lang.String key, java.lang.String value)
- Specified by:
put
in interfacejava.util.Map<java.lang.String,java.lang.String>
- Overrides:
put
in classjava.util.AbstractMap<java.lang.String,java.lang.String>
-
putAllSubstituted
void putAllSubstituted(java.util.Map<? extends java.lang.String,? extends java.lang.String> m)
-
put
public java.lang.String put(java.lang.String key, java.util.List<java.lang.String> commentLines, java.util.List<java.lang.String> valueLines)
-
put
public java.lang.String put(java.lang.String key, java.util.List<java.lang.String> commentLines, java.lang.String value)
-
put
public java.lang.String put(java.lang.String key, java.lang.String comment, java.lang.String value)
-
update
public boolean update(java.util.Map<java.lang.String,java.lang.String> props)
-
update
public boolean update(Properties properties)
-
getRaw
public java.util.List<java.lang.String> getRaw(java.lang.String key)
-
getComments
public java.util.List<java.lang.String> getComments(java.lang.String key)
-
remove
public java.lang.String remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<java.lang.String,java.lang.String>
- Overrides:
remove
in classjava.util.AbstractMap<java.lang.String,java.lang.String>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map<java.lang.String,java.lang.String>
- Overrides:
clear
in classjava.util.AbstractMap<java.lang.String,java.lang.String>
-
getHeader
public java.util.List<java.lang.String> getHeader()
Return the comment header.- Returns:
- the comment header
-
setHeader
public void setHeader(java.util.List<java.lang.String> header)
Set the comment header.- Parameters:
header
- the header to use
-
getFooter
public java.util.List<java.lang.String> getFooter()
Return the comment footer.- Returns:
- the comment footer
-
setFooter
public void setFooter(java.util.List<java.lang.String> footer)
Set the comment footer.- Parameters:
footer
- the footer to use
-
loadLayout
protected void loadLayout(java.io.Reader in, boolean maybeTyped) throws java.io.IOException
Reads a properties file and stores its internal structure. The found properties will be added to the associated configuration object.- Parameters:
in
- the reader to the properties file- Throws:
java.io.IOException
- if an error occurs
-
substitute
public void substitute()
-
substitute
public void substitute(InterpolationHelper.SubstitutionCallback callback)
-
saveLayout
protected void saveLayout(java.io.Writer out, boolean typed) throws java.io.IOException
Writes the properties file to the given writer, preserving as much of its structure as possible.- Parameters:
out
- the writer- Throws:
java.io.IOException
- if an error occurs
-
checkHeaderComment
private int checkHeaderComment(java.util.List<java.lang.String> commentLines)
Checks if parts of the passed in comment can be used as header comment. This method checks whether a header comment can be defined (i.e. whether this is the first comment in the loaded file). If this is the case, it is searched for the lates blank line. This line will mark the end of the header comment. The return value is the index of the first line in the passed in list, which does not belong to the header comment.- Parameters:
commentLines
- the comment lines- Returns:
- the index of the next line after the header comment
-
isCommentLine
static boolean isCommentLine(java.lang.String line)
Tests whether a line is a comment, i.e. whether it starts with a comment character.- Parameters:
line
- the line- Returns:
- a flag if this is a comment line
-
unescapeJava
protected static java.lang.String unescapeJava(java.lang.String str)
Unescapes any Java literals found in the
This is a slightly modified version of the StringEscapeUtils.unescapeJava() function in commons-lang that doesn't drop escaped separators (i.e '\,').String
to aWriter
.- Parameters:
str
- theString
to unescape, may be null- Returns:
- the processed string
- Throws:
java.lang.IllegalArgumentException
- if the Writer isnull
-
escapeJava
protected static java.lang.String escapeJava(java.lang.String str)
Escapes the characters in a
String
using Java String rules.Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
So a tab becomes the characters
'\\'
and't'
.The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.
Example:
input string: He didn't say, "Stop!" output string: He didn't say, \"Stop!\"
- Parameters:
str
- String to escape values in, may be null- Returns:
- String with escaped values,
null
if null string input
-
hex
protected static java.lang.String hex(char ch)
Returns an upper case hexadecimal
String
for the given character.- Parameters:
ch
- The character to convert.- Returns:
- An upper case hexadecimal
String
-
contains
public static boolean contains(char[] array, char valueToFind)
Checks if the value is in the given array.
The method returns
false
if anull
array is passed in.- Parameters:
array
- the array to search throughvalueToFind
- the value to find- Returns:
true
if the array contains the object
-
escapeKey
private static java.lang.String escapeKey(java.lang.String key)
Escape the separators in the key.- Parameters:
key
- the key- Returns:
- the escaped key
-
-