PropertyTree

public class PropertyTree extends PersistentComponent implements PropertySheet

PropertyTree is an implementation of a PropertySheet. It represents a property sheet with a tree structure and is actually backed by a Document object.

Author:Hugo Y. K. Lam

See also: org.dom4j.Document

Constructors

PropertyTree

public PropertyTree()

Creates a new instance of PropertyTree.

PropertyTree

public PropertyTree(URL url)

Creates a new instance of PropertyTree.

Parameters:
  • url – the url of the properties source.
Throws:
  • ComponentException – if the properties could not be loaded from the specified url.

PropertyTree

public PropertyTree(org.w3c.dom.Node node)

Creates a new instance of PropertyTree.

Parameters:
  • node – the root node of the properties source.
Throws:
  • ComponentException – if the properties could not be constructed from the specified node.

PropertyTree

public PropertyTree(InputStream ins)

Creates a new instance of PropertyTree.

Parameters:
  • ins – the input stream of the properties source.
Throws:
  • ComponentException – if the properties could not be loaded from the specified input stream.

Methods

addProperty

protected boolean addProperty(String xpath, String value)

Adds a property to this property tree.

Parameters:
  • xpath – the property xpath.
  • value – the property value.
Returns:

true if the operation is successful. false otherwise.

append

public boolean append(PropertySheet p)

Appends a property sheet to this property tree. The specified property sheet can only be appended if it is of the PropertyTree type.

Parameters:
  • p – the property sheet to be appended.
Returns:

true if the operation is successful. false otherwise.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.append(hk.hku.cecid.piazza.commons.util.PropertySheet)

containsKey

public boolean containsKey(String xpath)

Checks if the specified xpath exists in this property tree.

Parameters:
  • xpath – the property xpath.
Returns:

true if the specified xpath exists in this property tree.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.containsKey(java.lang.String)

countProperties

public int countProperties(String xpath)

Counts the number of properties with the specified xpath.

Parameters:
  • xpath – the properties xpath.
Returns:

the number of properties with the specified xpath.

createProperties

public Properties createProperties(String xpath)

Creates a Properties object which stores the properties retrieved by the specified xpath.

Parameters:
  • xpath – the properties xpath.
Returns:

a Properties object which stores the retrieved properties.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.createProperties(java.lang.String)

getProperties

public String[] getProperties(String xpath)

Gets a list of properties with the specified xpath.

Parameters:
  • xpath – the properties xpath.
Returns:

the properties with the specified xpath.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.getProperties(java.lang.String)

getProperties

public String[][] getProperties(String xpath, String xpath2)

Gets a two-dimensional list of properties with the specified xpaths. The first xpath will define the first dimension of the list while the second xpath will define the second dimension. E.g.

<!- Properties content -->
<application>
  <listener>
    <id>MyListener</id>
    <name>My Listener</name>
  </listener>
  <listener>
    <id>MyListener2</id>
    <name>My Listener 2</name>
  </listener>
</application>

First xpath: /application/listener
Second xpath: ./id|./name

Returned array:
{{"MyListener","My Listener"},{"MyListener2","My Listener 2"}}
Parameters:
  • xpath – the first xpath.
  • xpath2 – the second xpath.
Returns:

a two-dimensional list of properties with the specified xpaths.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.getProperties(java.lang.String, java.lang.String)

getProperty

public String getProperty(String xpath)

Gets a property with the specified xpath. If the xpath refers to more than one properpty, the first one will be returned.

Parameters:
  • xpath – the property xpath.
Returns:

the property with the specified xpath.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.getProperty(java.lang.String)

getProperty

public String getProperty(String xpath, String def)

Gets a property with the specified xpath. If the xpath refers to more than one properpty, the first one will be returned.

Parameters:
  • xpath – the property xpath.
  • def – the default value.
Returns:

the property with the specified xpath.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.getProperty(java.lang.String, java.lang.String)

getPropertyNode

protected Node getPropertyNode(String xpath)

Gets a property node with the specified xpath. If the xpath refers to more than one properpty node, the first one will be returned.

Parameters:
  • xpath – the property xpath.
Returns:

the property node with the specified xpath.

getPropertyNodes

protected List getPropertyNodes(String xpath)

Gets a list of property nodes with the specified xpath.

Parameters:
  • xpath – the properties xpath.
Returns:

the property nodes with the specified xpath.

getSource

public Source getSource()

Gets the docment source.

Returns:the document source.

loading

protected void loading(URL url)

Loads the properties from the specified url location.

Parameters:
  • url – the url of the properties source.
Throws:
  • Exception – if the operation is unsuccessful.

See also: hk.hku.cecid.piazza.commons.module.PersistentComponent.loading(java.net.URL)

propertyNames

public Enumeration propertyNames()

Gets all the existing property xpaths.

Returns:all the existing property xpaths.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.propertyNames()

removeProperty

public boolean removeProperty(String xpath)

Removes a property with the specified xpath. If the xpath refers to more than one properpty, the first one will be removed.

Parameters:
  • xpath – the property xpath.
Returns:

true if the operation is successful. false otherwise.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.removeProperty(java.lang.String)

setProperty

public boolean setProperty(String xpath, String value)

Sets a property value with the specified key.

Parameters:
  • xpath – the property xpath.
  • value – the property value.
Returns:

true if the operation is successful. false otherwise.

See also: hk.hku.cecid.piazza.commons.util.PropertySheet.setProperty(java.lang.String, java.lang.String)

storing

protected void storing(URL url)

Stores the properties to the specified url location.

Parameters:
  • url – the url of the properties source.
Throws:
  • Exception – if the operation is unsuccessful.

See also: hk.hku.cecid.piazza.commons.module.PersistentComponent.storing(java.net.URL)

subtree

public PropertyTree subtree(String xpath)

Creates a sub-tree from this property tree.

Parameters:
  • xpath – the xpath for locating the subtree.
Returns:

a new property tree.

toDocument

public org.w3c.dom.Document toDocument()

Returns a W3C document representation of this property tree.

Returns:a new W3C document.

toString

public String toString()

Returns a string representation of this property tree, which is the XML text.

Returns:a string representation of this property tree.

See also: java.lang.Object.toString()