/** * Returns a String representation of the tree. * The elements are generated from a pre-order traversal of the tree. * @return the String representation of the Tree. */ public String toString() { try { return toList(PREORDER).toString(); } catch(Exception e) { e.printStackTrace(); } return "error"; }
/** * Returns the tree as a List of node objects. The elements of the * List are generated from a pre-order traversal of the tree. * @return Tree elements. */ public List toList(String order) { List ret = new ArrayList(); if(root!=null) { if(PREORDER.equals(order)) { walkPreorder(root, ret); } else if(POSTORDER.equals(order)) { walkPostorder(root, ret); } } return ret; }
/** * 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}); }
boolean subs = wi.getSubobjects()!=null && !wi.getSubobjects().isEmpty(); if(subs) writeNewline(writer); writeSubobjects(wc, subobs.getRootNode(), typeinfo);
/** * 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); } } }
/** * 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); } }