xv.text
Class PropertiesReader

java.lang.Object
  extended by xv.text.PropertiesReader

public class PropertiesReader
extends Object

A class designed to allow reading Properties files using the format as specified in Properties.load(java.io.Reader).

There is one notable difference between the format as implemented by this class and the one read by the Properties class. The Properties class will throw an IllegalArgumentException if the file contains an invalid Unicode escape sequence (e.g., "\uxxxx"), while this class will treat the escape as an escaped 'u' character and treat the characters that follow as normal characters.

This means that there are no invalid inputs: all inputs will produce results, although they may not be the intended results.

Generally this class can be used by the following loop:

PropertiesReader propertiesReader = new PropertiesReader(stream);
String key;
while ((key = propertiesReader.readKey()) != null) {
    String value = propertiesReader.readValue();
    // Use the properties as required
}

Author:
xv

Constructor Summary
PropertiesReader(InputStream stream)
          Loads the given property stream using the ISO 8859-1 character encoding, which is the official property stream encoding.
PropertiesReader(Reader reader)
          Loads the given property stream as characters from the given reader.
PropertiesReader(URL url)
          Creates a new properties reader using the given URL.
 
Method Summary
static boolean isWhitespace(int c)
          Determines if the given character is a whitespace character according to the .properties spec and not according to Character.isWhitespace(int).
static HashMap<String,String> loadProperties(InputStream stream)
          Utility method for reading properties into a new HashMap.
static HashMap<String,String> loadProperties(PropertiesReader reader)
          Utility method for reading properties into a new HashMap.
static void loadProperties(PropertiesReader reader, Map<? super String,? super String> properties)
          Utility method for reading properties into a pre-existing Map.
static HashMap<String,String> loadProperties(Reader reader)
          Utility method for reading properties into a new HashMap.
static HashMap<String,String> loadProperties(URL url)
          Utility method for reading properties into a new HashMap.
 String readKey()
          Reads through the stream, finding the next actual key and reading through to the next separator character.
 String readValue()
          Reads through the stream, finding the next value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesReader

public PropertiesReader(InputStream stream)
Loads the given property stream using the ISO 8859-1 character encoding, which is the official property stream encoding.

Parameters:
stream - the stream to read the properties from
Throws:
NullPointerException - if stream is null

PropertiesReader

public PropertiesReader(URL url)
                 throws IOException
Creates a new properties reader using the given URL. This will use the character encoding specified by the URL if there is one, if none is specified, it defaults to the standard ISO 8859-1 as specified by the ".properties" standard.

If you always want to use the standard encoding, use new PropertiesReader(url.openStream()) instead.

Parameters:
url - the URL to use
Throws:
IOException - if an I/O error occurs while connecting to the URL
NullPointerException - if url is null

PropertiesReader

public PropertiesReader(Reader reader)
Loads the given property stream as characters from the given reader.

Parameters:
reader - the reader to read properties from
Throws:
NullPointerException - if reader is null
Method Detail

readKey

public String readKey()
               throws IOException
Reads through the stream, finding the next actual key and reading through to the next separator character. This method will skip comments.

Examples:

Upcoming Characters Return value Remaining String
" foo=bar" "foo" "bar"
" foo:=bar" "foo" "=bar"
" foo = bar" "foo" " bar"

Returns:
the next key or null if the end-of-stream is reached
Throws:
IOException

readValue

public String readValue()
                 throws IOException
Reads through the stream, finding the next value. This may be the empty string.

Returns:
the next value
Throws:
IOException

loadProperties

public static HashMap<String,String> loadProperties(InputStream stream)
                                             throws IOException
Utility method for reading properties into a new HashMap. The characters are read in ISO 8859-1 encoding.

Parameters:
stream - the stream to read the properties from
Throws:
IOException - if an I/O exception occurs while reading the properties

loadProperties

public static HashMap<String,String> loadProperties(Reader reader)
                                             throws IOException
Utility method for reading properties into a new HashMap.

Parameters:
reader - the reader to read the properties from
Throws:
IOException - if an I/O exception occurs while reading the properties

loadProperties

public static HashMap<String,String> loadProperties(URL url)
                                             throws IOException
Utility method for reading properties into a new HashMap.

Parameters:
url - the URL to load properties from
Throws:
IOException - if an I/O exception occurs while reading the properties

loadProperties

public static HashMap<String,String> loadProperties(PropertiesReader reader)
                                             throws IOException
Utility method for reading properties into a new HashMap.

Parameters:
reader - the properties reader to read the properties from
Throws:
IOException - if an I/O exception occurs while reading the properties

loadProperties

public static void loadProperties(PropertiesReader reader,
                                  Map<? super String,? super String> properties)
                           throws IOException
Utility method for reading properties into a pre-existing Map.

Parameters:
reader - the properties reader to read the properties from
properties - the map to place properties into
Throws:
IOException - if an I/O exception occurs while reading the properties

isWhitespace

public static boolean isWhitespace(int c)
Determines if the given character is a whitespace character according to the .properties spec and not according to Character.isWhitespace(int).

Parameters:
c - the character to check
Returns:
true if the character is a whitespace character according to the .properties spec


Copyright © 2008-2011. All Rights Reserved.