Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.docx4j.model.styles;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
Represents a Tree of Objects of generic type T. The Tree is represented as a single rootElement which points to a List<Node<T>> of children. There is no restriction on the number of children that a particular node may have. This Tree provides a method to serialize the Tree into a List by doing a pre-order traversal. It has several methods to allow easy updation of Nodes in the Tree. Original code from
 public class Tree<T> {
 	private static Logger log = LoggerFactory.getLogger(Tree.class);	
     private Node<T> rootElement;
 //	    private Map<String, Node<T>> nodes = new HashMap<String, Node<T>>(); // weird compile errors on put with this
     protected Map<StringObjectnodes = new HashMap<StringObject>();
Quick access to any node in the tree.

     public Node<T> get(String name) {
 //    	if (log.isDebugEnabled()) {
 //    		Node<T> result = (Node<T>)nodes.get(name);
 //    		if (result==null) {
 //    			log.warn("Null result for " + name);
 //    		} 
 //    		return result;
 //    	}
     	return (Node<T>).get(name);
Return the root Node of the tree.

the root element.
     public Node<T> getRootElement() {
         return this.;
Set the root Element for the tree.

rootElement the root element to set.
     public void setRootElement(Node<T> rootElement) {
         this. = rootElement;
Returns the Tree<T> as a List of Node<T> objects. The elements of the List are generated from a pre-order traversal of the tree.

a List<Node<T>>.
     public List<Node<T>> toList() {
         List<Node<T>> list = new ArrayList<Node<T>>();
         return list;
Returns a String representation of the Tree. The elements are generated from a pre-order traversal of the Tree.

the String representation of the Tree.
     public String toString() {
     	StringBuffer sb = new StringBuffer();
     	for (Node<T> n : toList() ) {
     		sb.append( + "\n");	    		
         return sb.toString();
Walks the Tree in pre-order style. This is a recursive method, and is called from the toList() method with the root element as the first argument. It appends to the second argument, which is passed by reference as it recurses down the tree.

element the starting element.
list the output of the walk.
    private void walk(Node<T> elementList<Node<T>> list) {
    	if (element==null) {return;}
        for (Node<T> data : element.getChildren()) {
    public List<Node<T>> climb(Node<T> n) {
        List<Node<T>> list = new ArrayList<Node<T>>();
        return list;
    private List<Node<T>> climb(Node<T> nList<Node<T>> list) {
    	if (n.equals(this.)) {
    		return list;
    	} else {
    		Node<T> parent = n.getParent();
    		if (parent==null) { // no basedOn    			
    			return list
        return list;
New to GrepCode? Check out our FAQ X