util
Class LinkedProperties

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<String,String>
              extended by util.LinkedProperties
All Implemented Interfaces:
Serializable, Cloneable, Map<String,String>

public class LinkedProperties
extends LinkedHashMap<String,String>

Linked properties retain the order of key insertion. This in particular applies to reading the properties from a stream. Additionally, comments are kept as special properties.

See Also:
Serialized Form

Nested Class Summary
static class LinkedProperties.Test
          Test class.
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
static String COMMENT
          Commants are entered with this key, postfixed with a running number.
private  int commentcount
          Current comment count.
private static String SEPARATORS
          A string containing possible key-value separators.
 
Constructor Summary
LinkedProperties()
          Supported constructor as in Properties.
LinkedProperties(Map<String,String> org)
          Supported constructor as in Properties, except for the fact that we directly load the map into our internal map via the LinkedHashMap.LinkedHashMap(Map) constructor.
 
Method Summary
 void addComment(String rem)
          Adds a comment to the end of the properties.
private  String convertUnicode(String src)
          Converts characters in a string less than 20 or above e7 to unicode.
 void insertComment(int index, String comment)
          Inserts a comment at the given position.
static boolean isComment(String key)
          Returns true if the key represents a comment.
 int load(InputStream in)
          Loads the property, keeps comments.
 int load(InputStream in, boolean keeprem)
          Loads the properties from the stream.
static LinkedProperties loadFromFile(File loc)
          Loads linked properties from a file.
static LinkedProperties loadFromURL(URL loc)
          Loads linked properties from an url.
static boolean saveToFile(File loc, Map<String,String> prop)
          Saves linked properties to a file.
private  int scanCommentCount()
          Traverses all keys to find the current number of comments in the map.
 void store(OutputStream into)
          Write the properties with comments.
 void store(OutputStream into, boolean writerem)
          Writes the properties and possible comments to the stream.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get, removeEldestEntry
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

COMMENT

public static final String COMMENT
Commants are entered with this key, postfixed with a running number.

See Also:
Constant Field Values

SEPARATORS

private static final String SEPARATORS
A string containing possible key-value separators.

See Also:
Constant Field Values

commentcount

private int commentcount
Current comment count.

Constructor Detail

LinkedProperties

public LinkedProperties()
Supported constructor as in Properties.


LinkedProperties

public LinkedProperties(Map<String,String> org)
Supported constructor as in Properties, except for the fact that we directly load the map into our internal map via the LinkedHashMap.LinkedHashMap(Map) constructor.

Method Detail

load

public int load(InputStream in)
         throws IOException
Loads the property, keeps comments.

Throws:
IOException

load

public int load(InputStream in,
                boolean keeprem)
         throws IOException
Loads the properties from the stream. Comments are inserted in the backing map. The order as properties are encountered is retained via the parental method. We first read the input stream to the end, then construct a piped stream from it which we pass to Properties.load(java.io.Reader).

The input stream is not closed.

Throws:
IOException

store

public void store(OutputStream into)
           throws IOException
Write the properties with comments.

Throws:
IOException

store

public void store(OutputStream into,
                  boolean writerem)
           throws IOException
Writes the properties and possible comments to the stream. Comments are denoted in the file with a leading '#'.

The output stream is not closed.

Throws:
IOException

isComment

public static boolean isComment(String key)
Returns true if the key represents a comment.


insertComment

public void insertComment(int index,
                          String comment)
Inserts a comment at the given position.


addComment

public void addComment(String rem)
Adds a comment to the end of the properties.


scanCommentCount

private int scanCommentCount()
Traverses all keys to find the current number of comments in the map.


loadFromURL

public static LinkedProperties loadFromURL(URL loc)
Loads linked properties from an url. This method is not part of the PropertyLoader as it requires jdk 1.4 for compile. It does not throw an exception, rather returns null if the properties cannot be loaded.


loadFromFile

public static LinkedProperties loadFromFile(File loc)
Loads linked properties from a file. This method is not part of the PropertyLoader as it requires jdk 1.4 for compile. It does not throw an exception, rather returns null if the properties cannot be loaded.


saveToFile

public static boolean saveToFile(File loc,
                                 Map<String,String> prop)
Saves linked properties to a file. This method is not part of the PropertyLoader as it requires jdk 1.4 for compile. It does not throw an exception, rather returns null if the properties cannot be loaded.


convertUnicode

private String convertUnicode(String src)
Converts characters in a string less than 20 or above e7 to unicode.