org.apache.openjpa.lib.util
Class FormatPreservingProperties

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable<Object,Object>
          extended by java.util.Properties
              extended by org.apache.openjpa.lib.util.FormatPreservingProperties
All Implemented Interfaces:
Serializable, Cloneable, Map<Object,Object>

public class FormatPreservingProperties
extends Properties

A specialization of Properties that stores its contents in the same order and with the same formatting as was used to read the contents from an input stream. This is useful because it means that a properties file loaded via this object and then written back out later on will only be different where changes or additions were made. By default, the store(java.io.OutputStream, java.lang.String) method in this class does not behave the same as Properties.store(java.io.OutputStream, java.lang.String). You can cause an instance to approximate the behavior of Properties.store(java.io.OutputStream, java.lang.String) by invoking setDefaultEntryDelimiter(char) with =, setAddWhitespaceAfterDelimiter(boolean) with false, and setAllowDuplicates(boolean) with true. However, this will only influence how the instance will write new values, not how it will write existing key-value pairs that are modified. In conjunction with a conservative output writer, it is possible to only write to disk changes / additions. This implementation does not permit escaped ' ', '=', ':' characters in key names.

Since:
0.3.3
See Also:
Serialized Form

Nested Class Summary
static class FormatPreservingProperties.DuplicateKeyException
           
 
Field Summary
 
Fields inherited from class java.util.Properties
defaults
 
Constructor Summary
FormatPreservingProperties()
           
FormatPreservingProperties(Properties defaults)
           
 
Method Summary
 void clear()
           
 Object clone()
           
 boolean getAddWhitespaceAfterDelimiter()
          If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character).
 boolean getAllowDuplicates()
          If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings.
 char getDefaultEntryDelimiter()
          See setDefaultEntryDelimiter(char)
 boolean getInsertTimestamp()
          If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed.
 String getProperty(String key)
           
 String getProperty(String key, String defaultValue)
           
 void load(InputStream in)
          Loads the properties in in, according to the rules described in Properties.load(java.io.InputStream).
 Object put(Object key, Object val)
           
 void putAll(Map m)
          Circumvents the superclass putAll(java.util.Map) implementation, putting all the key-value pairs via put(java.lang.Object, java.lang.Object).
 Object remove(Object key)
          Removes the key from the bookkeeping collectiotns as well.
 void save(OutputStream out, String header)
           
 void setAddWhitespaceAfterDelimiter(boolean add)
          If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character).
 void setAllowDuplicates(boolean allowDuplicates)
          If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings.
 void setDefaultEntryDelimiter(char defaultEntryDelimiter)
          The character to use as a delimiter between property keys and values.
 void setInsertTimestamp(boolean insertTimestamp)
          If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed.
 Object setProperty(String key, String value)
           
 void store(OutputStream out, String header)
           
 
Methods inherited from class java.util.Properties
list, list, loadFromXML, propertyNames, storeToXML, storeToXML
 
Methods inherited from class java.util.Hashtable
contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, rehash, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FormatPreservingProperties

public FormatPreservingProperties()

FormatPreservingProperties

public FormatPreservingProperties(Properties defaults)
Method Detail

setDefaultEntryDelimiter

public void setDefaultEntryDelimiter(char defaultEntryDelimiter)
The character to use as a delimiter between property keys and values.

Parameters:
defaultEntryDelimiter - either ':' or '='

getDefaultEntryDelimiter

public char getDefaultEntryDelimiter()
See setDefaultEntryDelimiter(char)


setAddWhitespaceAfterDelimiter

public void setAddWhitespaceAfterDelimiter(boolean add)
If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(java.io.OutputStream, java.lang.String) never writes whitespace.


getAddWhitespaceAfterDelimiter

public boolean getAddWhitespaceAfterDelimiter()
If set to true, this properties object will add a space after the delimiter character(if the delimiter is not the space character). Else, this will not add a space. Default value: true. Note that Properties.store(java.io.OutputStream, java.lang.String) never writes whitespace.


setInsertTimestamp

public void setInsertTimestamp(boolean insertTimestamp)
If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(java.io.OutputStream, java.lang.String) always writes a timestamp.


getInsertTimestamp

public boolean getInsertTimestamp()
If set to true, this properties object will add a timestamp to the beginning of the file, just after the header (if any) is printed. Else, this will not add a timestamp. Default value: false. Note that Properties.store(java.io.OutputStream, java.lang.String) always writes a timestamp.


setAllowDuplicates

public void setAllowDuplicates(boolean allowDuplicates)
If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(java.io.InputStream). Default value: false. Note that Properties.store(java.io.OutputStream, java.lang.String) always allows duplicates.


getAllowDuplicates

public boolean getAllowDuplicates()
If set to true, duplicate properties are allowed, and the last property setting in the input will overwrite any previous settings. If set to false, duplicate property definitions in the input will cause an exception to be thrown during load(java.io.InputStream). Default value: false. Note that Properties.store(java.io.OutputStream, java.lang.String) always allows duplicates.


getProperty

public String getProperty(String key)
Overrides:
getProperty in class Properties

getProperty

public String getProperty(String key,
                          String defaultValue)
Overrides:
getProperty in class Properties

setProperty

public Object setProperty(String key,
                          String value)
Overrides:
setProperty in class Properties

putAll

public void putAll(Map m)
Circumvents the superclass putAll(java.util.Map) implementation, putting all the key-value pairs via put(java.lang.Object, java.lang.Object).

Specified by:
putAll in interface Map<Object,Object>
Overrides:
putAll in class Hashtable<Object,Object>

remove

public Object remove(Object key)
Removes the key from the bookkeeping collectiotns as well.

Specified by:
remove in interface Map<Object,Object>
Overrides:
remove in class Hashtable<Object,Object>

clear

public void clear()
Specified by:
clear in interface Map<Object,Object>
Overrides:
clear in class Hashtable<Object,Object>

clone

public Object clone()
Overrides:
clone in class Hashtable<Object,Object>

put

public Object put(Object key,
                  Object val)
Specified by:
put in interface Map<Object,Object>
Overrides:
put in class Hashtable<Object,Object>

load

public void load(InputStream in)
          throws IOException
Loads the properties in in, according to the rules described in Properties.load(java.io.InputStream). If getAllowDuplicates() returns true, this will throw a FormatPreservingProperties.DuplicateKeyException if duplicate property declarations are encountered.

Overrides:
load in class Properties
Throws:
IOException
See Also:
Properties.load(java.io.InputStream)

save

public void save(OutputStream out,
                 String header)
Overrides:
save in class Properties

store

public void store(OutputStream out,
                  String header)
           throws IOException
Overrides:
store in class Properties
Throws:
IOException


Copyright © 2006-2010 Apache Software Foundation. All Rights Reserved.