Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.gersonworks.xml.util
Class XMLProperties  view XMLProperties download XMLProperties.java

java.lang.Object
  extended bycom.gersonworks.xml.util.XMLProperties

public class XMLProperties
extends java.lang.Object

The XMLProperties class represents a way of storing and retrieving properties data into and from an XML file. Each property key corresponds to an XMLPropertyValues object which may contain one or more property values.

Since the idea of implementing this class came from how the java.util.Properties class works, most of the methods provided by this class are similar to the functionality provided by the java.util.Properties' methods.

The main difference of this class from java.util.Properties class is that a property in XMLProperties may contain one or more property values. Another difference is that this class now reads in an XML document.

The order of how the keys were originally sorted cannot be guaranteed when the loaded property list is stored back into the XML properties file. It was implemented this way for simplicity. What I can only guarantee is that all the keys and its corresponding value/s will be in the properties file.

The XMLProperties class reads an XML document of the following format:

  <properties>
  <!-- This is my XML configuration file -->
    <property key="key1">
      <value>key1Value1</value>
      <value>key1Value2</value>
    </property>
    <property key="key2">
      <value></value>
    </property>
  </properties>
 
XMLProperties can only recognize the following elements:

  1. properties - is the root element of the XML properties document. This element is usually composed of more than one property elements but it is also possible for this element to be an empty element.
  2. property - is the child of the properties element and the parent of the value element. The property element has a key attribute which serves as the key to the property values. The property element is only allowed to have the value element as its child. It cannot contain any text (or simple) contents under it.
  3. value - is the child of the property element. There should be at least one value element for each property element. The value element can be an empty element but it cannot contain any other elements except a text (or a simple) content.

The rules specified by each element should be followed to avoid XMLPropertiesFormatException to be thrown. This exception is always thrown whenever an unrecognized element is inserted into the XML properties document.

Copyright: Copyright (c) 21 January 2004

Version:
1.1, 29 January 2004

Field Summary
protected  XMLProperties defaultXMLProps
           
private  java.util.Map xmlProperties
           
 
Constructor Summary
XMLProperties()
          Creates an empty property list with no defaults.
XMLProperties(java.util.Properties defaultJavaProps)
          Creates an empty property list with a default java.util.Properties.
XMLProperties(XMLProperties defaultXMLProps)
          Creates an empty property list with a default XMLProperties.
 
Method Summary
 void clear()
          Removes all the entries from the property list.
 boolean containsKey(java.lang.String key)
          Checks if the specified key is in the property list.
 boolean containsValue(java.lang.String value)
          Returns true if XMLProperties maps one or more keys for this value.
 java.util.Properties getJavaUtilProperties()
          This method converts this class to its equivalent java.util.Properties object.
 java.util.Iterator getPropertyNames()
          Returns an iterator of all the keys in the property list.
 java.lang.String getPropertyValue(java.lang.String key, int index)
          Locates the property value for the corresponding key and property value's index.
 java.lang.String getPropertyValue(java.lang.String key, int index, java.lang.String defaultValue)
          Locates the property value for the corresponding key and property value's index.
 XMLPropertyValues getPropertyValues(java.lang.String key)
          Locates the property values for the corresponding key.
 XMLPropertyValues getPropertyValues(java.lang.String key, XMLPropertyValues defaultValues)
          Locates the property values for the corresponding key.
 java.util.Iterator getPropertyValuesIterator()
          Returns an iterator of all XMLPropertyValues objects in the property list.
 boolean isEmpty()
          Returns true if the property list is empty.
 void load(java.io.File propertiesFile)
          Reads the property list key-value/s pair from the properties file.
 void load(java.io.InputStream inStream)
          Reads the property list of key-value/s pairs from the input stream
 void load(java.lang.String propertiesURI)
          Reads the property list of key-value/s pair from the properties URI.
 XMLPropertyValues remove(java.lang.String key)
          Removes the property specified by the property key.
 XMLPropertyValues setProperty(java.lang.String key, java.lang.String value)
          Puts the key-value pair parameter into the property list.
 XMLPropertyValues setProperty(java.lang.String key, XMLPropertyValues values)
          Puts the key-values pair parameter into the property list.
 int size()
          Returns the number of elements in the property list.
 void store(java.io.OutputStream outStream)
          Writes the property list wrapped by this class into the output stream.
 void store(java.io.OutputStream outStream, java.lang.String headerComment)
          Writes the property list wrapped by this class into the output stream.
 void store(java.io.Writer writer)
          Writes the property list wrapped by this class into a Writer object.
 void store(java.io.Writer writer, java.lang.String headerComment)
          Writes the property list wrapped by this class into a Writer object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xmlProperties

private java.util.Map xmlProperties

defaultXMLProps

protected XMLProperties defaultXMLProps
Constructor Detail

XMLProperties

public XMLProperties()
Creates an empty property list with no defaults.


XMLProperties

public XMLProperties(XMLProperties defaultXMLProps)
Creates an empty property list with a default XMLProperties.

Since:
XMLProperties version 1.1

XMLProperties

public XMLProperties(java.util.Properties defaultJavaProps)
Creates an empty property list with a default java.util.Properties.

Since:
XMLProperties version 1.1
Method Detail

getPropertyValues

public XMLPropertyValues getPropertyValues(java.lang.String key)
Locates the property values for the corresponding key.


getPropertyValues

public XMLPropertyValues getPropertyValues(java.lang.String key,
                                           XMLPropertyValues defaultValues)
Locates the property values for the corresponding key. If the key is not found, the search returns the default XMLPropertyValues parameter of this method.


getPropertyValue

public java.lang.String getPropertyValue(java.lang.String key,
                                         int index)
                                  throws XMLPropertiesException
Locates the property value for the corresponding key and property value's index. If the property key is found but the index is not within the property values' range, the method will throw an XMLPropertiesException.


getPropertyValue

public java.lang.String getPropertyValue(java.lang.String key,
                                         int index,
                                         java.lang.String defaultValue)
Locates the property value for the corresponding key and property value's index. This method will return the defaultValue if the property key is not found, or if the value's index is not within the range of the found propertyValues.


load

public void load(java.io.File propertiesFile)
Reads the property list key-value/s pair from the properties file.


load

public void load(java.lang.String propertiesURI)
Reads the property list of key-value/s pair from the properties URI.


load

public void load(java.io.InputStream inStream)
Reads the property list of key-value/s pairs from the input stream

Since:
XMLProperties version 1.1

getPropertyNames

public java.util.Iterator getPropertyNames()
Returns an iterator of all the keys in the property list.


setProperty

public XMLPropertyValues setProperty(java.lang.String key,
                                     java.lang.String value)
Puts the key-value pair parameter into the property list. If the argument key exists in the property list, the old XMLPropertyValues object will be returned, otherwise null will be returned.


setProperty

public XMLPropertyValues setProperty(java.lang.String key,
                                     XMLPropertyValues values)
Puts the key-values pair parameter into the property list. If the argument key exists in the property list, the old XMLPropertyValues object will be returned, otherwise null will be returned.


store

public void store(java.io.Writer writer)
Writes the property list wrapped by this class into a Writer object.


store

public void store(java.io.OutputStream outStream)
Writes the property list wrapped by this class into the output stream.

Since:
XMLProperties version 1.1

store

public void store(java.io.OutputStream outStream,
                  java.lang.String headerComment)
Writes the property list wrapped by this class into the output stream. This method adds additional header information to the output through the use of the headerComment.

Since:
XMLProperties version 1.1

store

public void store(java.io.Writer writer,
                  java.lang.String headerComment)
Writes the property list wrapped by this class into a Writer object. This method adds additional header information to the output through the use of the headerComment.


getPropertyValuesIterator

public java.util.Iterator getPropertyValuesIterator()
Returns an iterator of all XMLPropertyValues objects in the property list.

Since:
XMLProperties version 1.1

getJavaUtilProperties

public java.util.Properties getJavaUtilProperties()
This method converts this class to its equivalent java.util.Properties object. It should be noted that only the first element (or propery value) of all the XMLPropertyValues objects in this class' property list will be stored in the output java.util.Properties object. The property list of the defaults are also not stored in the output java.util.Properties object.

Since:
XMLProperties version 1.1

clear

public void clear()
Removes all the entries from the property list.

Since:
XMLProperties version 1.1

containsKey

public boolean containsKey(java.lang.String key)
Checks if the specified key is in the property list.

Since:
XMLProperties version 1.1

containsValue

public boolean containsValue(java.lang.String value)
Returns true if XMLProperties maps one or more keys for this value.

Since:
XMLProperties version 1.1

isEmpty

public boolean isEmpty()
Returns true if the property list is empty.

Since:
XMLProperties version 1.1

remove

public XMLPropertyValues remove(java.lang.String key)
Removes the property specified by the property key.

Since:
XMLProperties version 1.1

size

public int size()
Returns the number of elements in the property list.

Since:
XMLProperties version 1.1