org.xins.common.collections
Class PropertyReaderUtils

java.lang.Object
  extended byorg.xins.common.collections.PropertyReaderUtils

public final class PropertyReaderUtils
extends Object

Utility functions for dealing with PropertyReader objects.

Since:
XINS 1.0.0
Version:
$Revision: 1.41 $ $Date: 2006/10/24 09:31:13 $
Author:
Ernst de Haan
See Also:
PropertyReader

Field Summary
static PropertyReader EMPTY_PROPERTY_READER
          An empty and unmodifiable PropertyReader instance.
 
Method Summary
static PropertyReader createPropertyReader(InputStream in)
          Constructs a PropertyReader from the specified input stream.
static boolean getBooleanProperty(PropertyReader properties, String propertyName, boolean fallbackDefault)
          Gets the property with the specified name and converts it to a boolean.
static int getIntProperty(PropertyReader properties, String propertyName)
          Gets the property with the specified name and converts it to an int.
static String getRequiredProperty(PropertyReader properties, String name)
          Retrieves the specified property and throws a MissingRequiredPropertyException if it is not set.
static void serialize(PropertyReader properties, FastStringBuffer buffer, String valueIfEmpty)
          Serializes the specified PropertyReader to a FastStringBuffer.
static void serialize(PropertyReader properties, LogdocStringBuffer buffer)
          Serializes the specified PropertyReader for Logdoc.
static LogdocSerializable serialize(PropertyReader p, String valueIfEmpty)
          Constructs a LogdocSerializable for the specified PropertyReader.
static LogdocSerializable serialize(PropertyReader p, String valueIfEmpty, String prefixIfNotEmpty, String suffix)
          Constructs a LogdocSerializable for the specified PropertyReader.
static String toString(PropertyReader properties, String valueIfEmpty)
          Serializes the specified PropertyReader to a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_PROPERTY_READER

public static final PropertyReader EMPTY_PROPERTY_READER
An empty and unmodifiable PropertyReader instance. This field is not null.

Since:
XINS 1.1.0
Method Detail

getBooleanProperty

public static boolean getBooleanProperty(PropertyReader properties,
                                         String propertyName,
                                         boolean fallbackDefault)
                                  throws IllegalArgumentException,
                                         InvalidPropertyValueException
Gets the property with the specified name and converts it to a boolean.

Parameters:
properties - the set of properties to read from, cannot be null.
propertyName - the name of the property to read, cannot be null.
fallbackDefault - the fallback default value, returned if the value of the property is either null or "" (an empty string).
Returns:
the value of the property.
Throws:
IllegalArgumentException - if properties == null || propertyName == null.
InvalidPropertyValueException - if the value of the property is neither null nor "" (an empty string), nor "true" nor "false".

getIntProperty

public static int getIntProperty(PropertyReader properties,
                                 String propertyName)
                          throws IllegalArgumentException,
                                 MissingRequiredPropertyException,
                                 InvalidPropertyValueException
Gets the property with the specified name and converts it to an int.

Parameters:
properties - the set of properties to read from, cannot be null.
propertyName - the name of the property to read, cannot be null.
Returns:
the value of the property, as an int.
Throws:
IllegalArgumentException - if properties == null || propertyName == null.
MissingRequiredPropertyException - if the specified property is not set, or if it is set to an empty string.
InvalidPropertyValueException - if the conversion to an int failed.

getRequiredProperty

public static String getRequiredProperty(PropertyReader properties,
                                         String name)
                                  throws IllegalArgumentException,
                                         MissingRequiredPropertyException
Retrieves the specified property and throws a MissingRequiredPropertyException if it is not set.

Parameters:
properties - the set of properties to retrieve a specific proeprty from, cannot be null.
name - the name of the property, cannot be null.
Returns:
the value of the property, guaranteed not to be null and guaranteed to contain at least one character.
Throws:
IllegalArgumentException - if properties == null || name == null.
MissingRequiredPropertyException - if the value of the property is either null or an empty string.

createPropertyReader

public static PropertyReader createPropertyReader(InputStream in)
                                           throws IllegalArgumentException,
                                                  IOException
Constructs a PropertyReader from the specified input stream.

The parsing done is similar to the parsing done by the Properties.load(InputStream) method. Empty values will be ignored.

Parameters:
in - the input stream to read from, cannot be null.
Returns:
a PropertyReader instance that contains all the properties defined in the specified input stream.
Throws:
IllegalArgumentException - if in == null.
IOException - if there was an I/O error while reading from the stream.

serialize

public static void serialize(PropertyReader properties,
                             LogdocStringBuffer buffer)
                      throws IllegalArgumentException
Serializes the specified PropertyReader for Logdoc. For each entry, both the key and the value are encoded using the URL encoding (see URLEncoding). The key and value are separated by a literal equals sign ('='). The entries are separated using an ampersand ('&').

If the value for an entry is either null or an empty string (""), then nothing is added to the buffer for that entry.

Parameters:
properties - the PropertyReader to serialize, cannot be null.
buffer - the buffer to write the serialized data to, cannot be null.
Throws:
IllegalArgumentException - if properties == null || buffer == null.

serialize

public static void serialize(PropertyReader properties,
                             FastStringBuffer buffer,
                             String valueIfEmpty)
                      throws IllegalArgumentException
Serializes the specified PropertyReader to a FastStringBuffer. For each entry, both the key and the value are encoded using the URL encoding (see URLEncoding). The key and value are separated by a literal equals sign ('='). The entries are separated using an ampersand ('&').

If the value for an entry is either null or an empty string (""), then nothing is added to the buffer for that entry.

Parameters:
properties - the PropertyReader to serialize, can be null.
buffer - the buffer to write the serialized data to, cannot be null.
valueIfEmpty - the string to append to the buffer in case properties == null || properties.size() == 0; if this argument is null, however, then nothing will be appended in the mentioned case.
Throws:
IllegalArgumentException - if properties == null || buffer == null.

serialize

public static LogdocSerializable serialize(PropertyReader p,
                                           String valueIfEmpty)
Constructs a LogdocSerializable for the specified PropertyReader.

Parameters:
p - the PropertyReader to construct a LogdocSerializable for, or null.
valueIfEmpty - the value to return if the specified set of properties is either null or empty, can be null.
Returns:
a new LogdocSerializable, never null.

serialize

public static LogdocSerializable serialize(PropertyReader p,
                                           String valueIfEmpty,
                                           String prefixIfNotEmpty,
                                           String suffix)
Constructs a LogdocSerializable for the specified PropertyReader.

Parameters:
p - the PropertyReader to construct a LogdocSerializable for, or null.
valueIfEmpty - the value to return if the specified set of properties is either null or empty, can be null.
prefixIfNotEmpty - the prefix to add to the value if the PropertyReader is not empty, can be null.
suffix - the suffix to add to the value, can be null. The suffix will be added even if the PropertyReaderis empty.
Returns:
a new LogdocSerializable, never null.
Since:
XINS 1.4.0

toString

public static String toString(PropertyReader properties,
                              String valueIfEmpty)
Serializes the specified PropertyReader to a String. For each entry, both the key and the value are encoded using the URL encoding (see URLEncoding). The key and value are separated by a literal equals sign ('='). The entries are separated using an ampersand ('&').

If the value for an entry is either null or an empty string (""), then nothing is added to the buffer for that entry.

Parameters:
properties - the PropertyReader to serialize, can be null.
valueIfEmpty - the string to append to the buffer in case properties == null || properties.size() == 0; if this argument is null, however, then nothing will be appended in the mentioned case..
Returns:
the character string with the serialized data, never null.


See http://www.xins.org/.