SK.gnome.dwarf.utils
Class StringProperties

java.lang.Object
  extended bySK.gnome.dwarf.utils.StringProperties

public class StringProperties
extends java.lang.Object

Provides a persistent set of read-only properties.

This class mimics the java.util.Properties with two main differences - it does not allow modifying of properties and it supports loading properties either from an URL or from a string.

To load properties from an URL, the string argument provided to the constructor must be a regular URL, preceded by the '@' character:

   // load properties from an URL
   String s = "@file:conf/main.xml";
   StringProperties props = new StringProperties(s);
  
 
Please remember that a valid URL must start with the protocol identifier, like "file:", "jar:", "ftp:", and so on.

To load properties from a string, the string argument must contain text in the format supported by the java.util.Properties class:

   // load properties from a string
   String s = "name1=value1\n";
   s += "name2=value2\n";
   s += "name3=value3";
   StringProperties props = new StringProperties(s);
 

If you need to use a property name starting with the '@' character, precede it with the backslash:

   // load properties from a string
   String s = "\@name1=value1\n";    // first property named "@name1"
   s += "name2=value2\n";
   s += "name3=value3";
   StringProperties props = new StringProperties(s);
 

In contrast to java.util.Properties, this class is unsynchronized, therefore it can be used where the performance is crucial and the properties don't need to be modified after they have been initialized.

It can be used in cooperation with the XMLConfiguration class, where a fixed small set of properties may be specified directly in the XML element, while a larger set of properties may be stored to an external location and referenced in the XML document by an URL pointing to it. This scheme may be also extended to support a runtime reloading of the externally stored properties.


Constructor Summary
StringProperties(java.lang.String s)
          Creates new StringProperties.
 
Method Summary
 java.lang.String getProperty(java.lang.String key)
          Returns property value by its name.
 java.net.URL getURL()
          Returns URL from which the properties were loaded.
 java.util.Enumeration propertyNames()
          Returns all property names.
 void reload()
          Reloads properties loaded from an URL.
 java.lang.String toString()
          Returns string representation of the properties.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringProperties

public StringProperties(java.lang.String s)
Creates new StringProperties.

If the string starts with the '@' character, the rest of it is taken as an URL from which the properties will be loaded. Rememeber, that each URL must start with a protocol identification, like "file:", for example.

If the string does not start with the '@' character, it must contain the properties themselves as supported by the java.io.Properties class, i.e. a list of name/value pairs, separated by the line terminator. If the name of the first property starts with the '@' character, it must be preceded with a backslash.

Parameters:
s - either an URL or the properties themselves
Method Detail

getProperty

public java.lang.String getProperty(java.lang.String key)
Returns property value by its name.

Parameters:
key - the property name
Returns:
the property value or null

propertyNames

public java.util.Enumeration propertyNames()
Returns all property names.

Returns:
the property names

reload

public void reload()
            throws java.io.IOException
Reloads properties loaded from an URL.

It returns silently if the properties were specified as a string containing "name=value" pairs instead of an URL.

Throws:
java.io.IOException - if an I/O error occured

getURL

public java.net.URL getURL()
Returns URL from which the properties were loaded.

It returns null if the properties were specified as a string containing the list of "name=value" pairs instead of an URL.

Returns:
the URL or null

toString

public java.lang.String toString()
Returns string representation of the properties.

It returns URL if the properties were loaded from an URL, otherwise returns a list of contained properties.

Returns:
the URL or the list of properties


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.