Package dk.netarkivet.common.utils
Class XmlTree<T>
- java.lang.Object
-
- dk.netarkivet.common.utils.XmlTree<T>
-
- Type Parameters:
T
- The type of XmlTree
- All Implemented Interfaces:
StringTree<T>
public class XmlTree<T> extends Object implements StringTree<T>
A class that implements the StringTreeinterface by backing it with XML. The name of each XML node corresponds to the identifier of a node in the tree.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,StringTree<T>>
getChildMap()
Get a map of all direct subtrees, assuming that all subtrees are uniquely named.Map<String,List<StringTree<T>>>
getChildMultimap()
Get a map of all the children of this node.Map<String,T>
getLeafMap()
Get a map of the names and values of all subtrees, assuming that all subtrees are leafs and are uniquely named.Map<String,List<T>>
getLeafMultimap()
Get a multimap of the names and values of all subtrees, assuming that all subtrees are leafs.static StringTree<String>
getStringTree(org.dom4j.Node n)
Returns a StringTree<String> view of the given XML node.StringTree<T>
getSubTree(String name)
Get the only subtree with the given name.List<StringTree<T>>
getSubTrees(String name)
Get the named subtrees.T
getValue()
Get the value of a leaf.T
getValue(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.
-
-
-
Method Detail
-
getStringTree
public static StringTree<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 interfaceStringTree<T>
- Returns:
- True if the implementing object is a leaf, false otherwise.
-
getValue
public T getValue(String name)
Get the value of a named sub-leaf.- Specified by:
getValue
in interfaceStringTree<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 interfaceStringTree<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(String name)
Get the only subtree with the given name.- Specified by:
getSubTree
in interfaceStringTree<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 List<StringTree<T>> getSubTrees(String name)
Get the named subtrees.- Specified by:
getSubTrees
in interfaceStringTree<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 Map<String,List<StringTree<T>>> getChildMultimap()
Get a map of all the children of this node.- Specified by:
getChildMultimap
in interfaceStringTree<T>
- Returns:
- Map of children of this node.
- Throws:
IllegalState
- if this object is a leaf.
-
getChildMap
public Map<String,StringTree<T>> getChildMap()
Get a map of all direct subtrees, assuming that all subtrees are uniquely named.- Specified by:
getChildMap
in interfaceStringTree<T>
- Returns:
- Map of all subtrees.
- Throws:
IllegalState
- if this object is a leaf, or if the subtrees are not uniquely named.
-
getLeafMultimap
public Map<String,List<T>> getLeafMultimap()
Get a multimap of the names and values of all subtrees, assuming that all subtrees are leafs.- Specified by:
getLeafMultimap
in interfaceStringTree<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 Map<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 interfaceStringTree<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.
-
-