com.jfx.io
Class ResourceReader

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by com.jfx.io.ResourceReader
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class ResourceReader
extends java.util.HashMap

This class is used to read .property files of the extended syntax. It has functionality of java.util.Properties()

See Also:
Serialized Form

Field Summary
protected  ResourceReader defaults
           
static java.lang.String GLOBAL_RESOURCE_DIR
           
static java.lang.String VARIABLE_PREFIX
           
 
Constructor Summary
ResourceReader()
           
ResourceReader(ResourceReader defaults)
           
 
Method Summary
 java.util.ArrayList fillPropertyGroup(java.lang.String prefix)
           
static ResourceReader getClassResourceReader(java.lang.Class clazz)
           
static ResourceReader getClassResourceReader(java.lang.Class clazz, boolean reload)
           
static ResourceReader getClassResourceReader(java.lang.Class clazz, java.lang.String resourceName)
           
static ResourceReader getClassResourceReader(java.lang.Class clazz, java.lang.String resourceName, boolean reload)
           
static java.lang.String getObjectProperty(java.lang.Object o, java.lang.String propertyName)
           
static ResourceReader getObjectResourceReader(java.lang.Object o)
           
static ResourceReader getObjectResourceReader(java.lang.Object o, boolean reload)
           
static ResourceReader getPackageResourceReader(java.lang.String packageName, java.lang.String resourceFileName)
           
static ResourceReader getPackageResourceReader(java.lang.String packageName, java.lang.String resourceFileName, boolean reload)
           
static java.io.InputStream getPackageResourceStream(java.lang.String packageName, java.lang.String resourceFileName)
           
 java.lang.String getProperty(java.lang.String key)
           
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
           
 boolean getPropertyAsBoolean(java.lang.String key)
           
 int getPropertyAsInt(java.lang.String key)
           
 int getPropertyAsInt(java.lang.String key, int dflt)
           
 void load(java.io.InputStream inStream)
          Reads a property list (key and element pairs) from the input stream.
 void load(java.io.InputStream inStream, java.lang.String charsetName)
           
static void main(java.lang.String[] args)
           
 java.lang.String processString(java.lang.String key, java.lang.String value)
           
 ResourceReader setNoVariableTranslation(boolean noVariableTranslation)
           
 java.lang.Object setProperty(java.lang.String key, java.lang.String value)
          Calls the Hashtable method put.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, 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
equals, hashCode
 

Field Detail

GLOBAL_RESOURCE_DIR

public static final java.lang.String GLOBAL_RESOURCE_DIR
See Also:
Constant Field Values

VARIABLE_PREFIX

public static final java.lang.String VARIABLE_PREFIX
See Also:
Constant Field Values

defaults

protected ResourceReader defaults
Constructor Detail

ResourceReader

public ResourceReader()

ResourceReader

public ResourceReader(ResourceReader defaults)
Method Detail

getObjectResourceReader

public static ResourceReader getObjectResourceReader(java.lang.Object o)

getObjectResourceReader

public static ResourceReader getObjectResourceReader(java.lang.Object o,
                                                     boolean reload)

getClassResourceReader

public static ResourceReader getClassResourceReader(java.lang.Class clazz)

getClassResourceReader

public static ResourceReader getClassResourceReader(java.lang.Class clazz,
                                                    boolean reload)

getClassResourceReader

public static ResourceReader getClassResourceReader(java.lang.Class clazz,
                                                    java.lang.String resourceName)

getClassResourceReader

public static ResourceReader getClassResourceReader(java.lang.Class clazz,
                                                    java.lang.String resourceName,
                                                    boolean reload)

getPackageResourceReader

public static ResourceReader getPackageResourceReader(java.lang.String packageName,
                                                      java.lang.String resourceFileName)

getPackageResourceReader

public static ResourceReader getPackageResourceReader(java.lang.String packageName,
                                                      java.lang.String resourceFileName,
                                                      boolean reload)

getPackageResourceStream

public static java.io.InputStream getPackageResourceStream(java.lang.String packageName,
                                                           java.lang.String resourceFileName)

getObjectProperty

public static java.lang.String getObjectProperty(java.lang.Object o,
                                                 java.lang.String propertyName)

load

public void load(java.io.InputStream inStream)
          throws java.io.IOException
Reads a property list (key and element pairs) from the input stream. By default, the stream is assumed to be using the ISO 8859-1 character encoding. If the stream is encoded differently, one should use "encoding=yourCharSet" definition at the very first property in a stream to identify true encoding:
 encoding = Windows-1251
 p1   p1Value
 p2 = [p2
 Multiline
 Value]
 p4 p4 \
 split \
 value
 p4 : p4-Value
 

Every property occupies one line of the input stream. Extended syntax allows to setup multi-line properties, using "[","]" separators. Each line is terminated by a line terminator (\n or \r or \r\n). Lines from the input stream are processed until end of file is reached on the input stream.

A line that contains only whitespace or whose first non-whitespace character is an # or ! is ignored (thus, # or ! indicate comment lines).

Every line other than a blank line or a comment line describes one property to be added to the table. Any whitespace after the key is skipped; if the first non-whitespace character after the key is = or :, then it is ignored and any whitespace characters after it are also skipped. All remaining characters on the line become part of the associated element string. Within the element string, the ASCII escape sequences \t, \n, \r, \\, \", \', \ (a backslash and a space), and \uxxxx are recognized and converted to single characters. Moreover, if the first character on the line is "[", then the value of a property can occupy multiple lines until the close bracet symbol ("]") is encountered.

As an example, each of the following four lines specifies the key "Truth" and the associated element value "Beauty":

 Truth = Beauty
        Truth:Beauty
 Truth                  :Beauty
 
As another example, the following three lines specify a single property:

 fruits = [apple, banana, pear,
 cantaloupe, watermelon,
 kiwi, mango]
 
The key is "fruits" and the associated element is:

"apple, banana, pear,\n cantaloupe, watermelon,\n kiwi, mango"

As a third example, the line:

cheeses
 
specifies that the key is "cheeses" and the associated element is the empty string.

Parameters:
inStream - the input stream.
Throws:
java.io.IOException - if an error occurred when reading from the input stream.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException

load

public void load(java.io.InputStream inStream,
                 java.lang.String charsetName)
          throws java.io.IOException
Throws:
java.io.IOException

setNoVariableTranslation

public ResourceReader setNoVariableTranslation(boolean noVariableTranslation)

getProperty

public java.lang.String getProperty(java.lang.String key)

getPropertyAsBoolean

public boolean getPropertyAsBoolean(java.lang.String key)

getPropertyAsInt

public int getPropertyAsInt(java.lang.String key)

getPropertyAsInt

public int getPropertyAsInt(java.lang.String key,
                            int dflt)

processString

public java.lang.String processString(java.lang.String key,
                                      java.lang.String value)

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue)

fillPropertyGroup

public java.util.ArrayList fillPropertyGroup(java.lang.String prefix)

setProperty

public java.lang.Object setProperty(java.lang.String key,
                                    java.lang.String value)
Calls the Hashtable 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 Hashtable call to put.

Parameters:
key - the key to be placed into this property list.
value - the value corresponding to key.
Returns:
the previous value of the specified key in this property list, or null if it did not have one.
Since:
1.2
See Also:
getProperty(java.lang.String)


Copyright © 2008-2009 Roman Gerasimenko.