dk.netarkivet.common.utils
Class XmlTree<T>

java.lang.Object
  extended by dk.netarkivet.common.utils.XmlTree<T>
Type Parameters:
T - The type of XmlTree
All Implemented Interfaces:
StringTree<T>

public class XmlTree<T>
extends java.lang.Object
implements StringTree<T>

A class that implements the StringTree interface by backing it with XML. The name of each XML node corresponds to the identifier of a node in the tree.


Nested Class Summary
(package private) static interface XmlTree.ValueParser<T>
          This interface defines how the value of an xml leaf is parsed to get a value of type T.
 
Method Summary
 java.util.Map<java.lang.String,StringTree<T>> getChildMap()
          Get a map of all direct subtrees, assuming that all subtrees are uniquely named.
 java.util.Map<java.lang.String,java.util.List<StringTree<T>>> getChildMultimap()
          Get a map of all the children of this node.
 java.util.Map<java.lang.String,T> getLeafMap()
          Get a map of the names and values of all subtrees, assuming that all subtrees are leafs and are uniquely named.
 java.util.Map<java.lang.String,java.util.List<T>> getLeafMultimap()
          Get a multimap of the names and values of all subtrees, assuming that all subtrees are leafs.
static StringTree<java.lang.String> getStringTree(org.dom4j.Node n)
          Returns a StringTree<String> view of the given XML node.
 StringTree<T> getSubTree(java.lang.String name)
          Get the only subtree with the given name.
 java.util.List<StringTree<T>> getSubTrees(java.lang.String name)
          Get the named subtrees.
 T getValue()
          Get the value of a leaf.
 T getValue(java.lang.String name)
          Get the value of a named sub-leaf.
 boolean isLeaf()
          Returns true if this object is a leaf, and thus if getValue is legal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getStringTree

public static StringTree<java.lang.String> getStringTree(org.dom4j.Node n)
Returns a StringTree<String> view of the given XML node.

Parameters:
n - A part of an XML document structure
Returns:
A StringTree<String> backed by the given XML part.
Throws:
ArgumentNotValid - on null argument.

isLeaf

public boolean isLeaf()
Returns true if this object is a leaf, and thus if getValue is legal.

Specified by:
isLeaf in interface StringTree<T>
Returns:
True if the implementing object is a leaf, false otherwise.

getValue

public T getValue(java.lang.String name)
Get the value of a named sub-leaf.

Specified by:
getValue in interface StringTree<T>
Parameters:
name - Name of the sub-leaf to get the value of. These are strings, and as a shorthand may specify subtrees of subtrees by separating each level with '.', i.e. getSubtrees("subtree.subsubtree").
Returns:
The value of the named leaf of this Tree, if it exists.
Throws:
IllegalState - if this StringTree does not have exactly one leaf sub-node with the given name.
ArgumentNotValid - if argument is null or empty.

getValue

public T getValue()
Get the value of a leaf.

Specified by:
getValue in interface StringTree<T>
Returns:
The value of this Tree, if it is a leaf.
Throws:
IllegalState - if this Tree is a node.

getSubTree

public StringTree<T> getSubTree(java.lang.String name)
Get the only subtree with the given name.

Specified by:
getSubTree in interface StringTree<T>
Parameters:
name - The name of the subtree. These are strings, and as a shorthand may specify subtrees of subtrees by separating each level with '.', i.e. getSubtrees("subtree.subsubtree").
Returns:
The single subtree with the given name.
Throws:
IllegalState - if this object is a leaf, or there is not exactly one subtree with the given name.
ArgumentNotValid - if argument is null or empty.

getSubTrees

public java.util.List<StringTree<T>> getSubTrees(java.lang.String name)
Get the named subtrees.

Specified by:
getSubTrees in interface StringTree<T>
Parameters:
name - The name of the subtrees. These are strings, and as a shorthand may specify subtrees of subtrees by separating each level with '.', i.e. getSubtrees("subtree.subsubtree").
Returns:
All subtrees with the given name, or an empty list for none.
Throws:
IllegalState - if this object is a leaf.
ArgumentNotValid - if argument is null or empty.

getChildMultimap

public java.util.Map<java.lang.String,java.util.List<StringTree<T>>> getChildMultimap()
Get a map of all the children of this node.

Specified by:
getChildMultimap in interface StringTree<T>
Returns:
Map of children of this node.
Throws:
IllegalState - if this object is a leaf.

getChildMap

public java.util.Map<java.lang.String,StringTree<T>> getChildMap()
Get a map of all direct subtrees, assuming that all subtrees are uniquely named.

Specified by:
getChildMap in interface StringTree<T>
Returns:
Map of all subtrees.
Throws:
IllegalState - if this object is a leaf, or if the subtrees are not uniquely named.

getLeafMultimap

public java.util.Map<java.lang.String,java.util.List<T>> getLeafMultimap()
Get a multimap of the names and values of all subtrees, assuming that all subtrees are leafs.

Specified by:
getLeafMultimap in interface StringTree<T>
Returns:
Multimap from subtree names to values of their leaves.
Throws:
ArgumentNotValid - if this object is not a node, or if any of its children are not leaves.

getLeafMap

public java.util.Map<java.lang.String,T> getLeafMap()
Get a map of the names and values of all subtrees, assuming that all subtrees are leafs and are uniquely named.

Specified by:
getLeafMap in interface StringTree<T>
Returns:
Map from subtree names to values of their leaves.
Throws:
IllegalState - if this object is a leaf or if the subtrees are not uniquely named, or if any of its children are not leaves.