/** * Create a new tree. */ public Tree() { this(new TreeNode()); }
/** * Inserts a node at the specified position in the child list. Will * throw an ArrayIndexOutOfBoundsException if the index does not exist. * @param index the position to insert at. * @param child the Node object to insert. * @throws IndexOutOfBoundsException if thrown. */ public void insertChildAt(int index, TreeNode child) throws IndexOutOfBoundsException { if(index==getNumberOfChildren()) { addChild(child); } else { if(children==null) children = new ArrayList(); children.add(index, child); } }
List children = node.getChildren(); if(children!=null) if(tag.equals(tmp.getData())) ret = tmp; ret = new TreeNode(tag); node.addChild(ret);
/** * Get the string representation. * @return The string representation. */ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{").append(""+getData()).append(",["); List children = getChildren(); if(children!=null) { for(int i=0; i<children.size(); i++) { TreeNode node = (TreeNode)children.get(i); if(i > 0) sb.append(","); sb.append(""+node.getData()); i++; } } sb.append("]").append("}"); return sb.toString(); } }
/** * Add a subobject. */ public void addSubobject(QName[] pathname, Object subobject, boolean flatten) { // System.out.println("added: "+SUtil.arrayToString(pathname)+" "+subobject+" "+flatten); if(subobjects==null) subobjects = new Tree(); // Build the path in the tree (on each level a decision about flattening needs to be done) TreeNode node = subobjects.getRootNode(); for(int i=0; i<pathname.length; i++) { // Never flatten last (object) layer (is this a hack?) node = getOrCreateChild(node, pathname[i], i+1==pathname.length? false: flatten); } // Last node data is [tag, object] node.setData(new Object[]{node.getData(), subobject}); }
/** * Walks the tree in pre-order style. * @param node The starting element. * @param list The output of the walk. */ protected void walkPreorder(TreeNode node, List list) { list.add(node); List children = node.getChildren(); if(children!=null) { for(int i=0; i<children.size(); i++) { TreeNode child = (TreeNode)children.get(i); walkPreorder(child, list); } } }
/** * Test if empty. */ public boolean isEmpty() { return root==null || root.getNumberOfChildren()==0; }
List stack = wc.getStack(); List children = node.getChildren(); for(int i=0; i<children.size(); i++) Object tmp = subnode.getData(); if(tmp instanceof QName)
/** * Walks the tree in post-order style. * @param node The starting element. * @param list The output of the walk. */ protected void walkPostorder(TreeNode node, List list) { List children = node.getChildren(); if(children!=null) { for(int i=0; i<children.size(); i++) { TreeNode child = (TreeNode)children.get(i); walkPostorder(child, list); } } list.add(node); } }