/** * Sets the root element of this Model's tree * * @param root * The root of the tree for this data model. */ public void setRoot(TreeElement topLevel) { root = new TreeElement(); if (topLevel != null) { root.addChild(topLevel); } }
public TreeElement deepCopy(Boolean includeTemplates) { TreeElement newNode = shallowCopy(); newNode.children = new ArrayList(); for (int i = 0; i < children.Count; i++) { TreeElement child = (TreeElement)children[i]; if (includeTemplates || child.getMult() != TreeReference.INDEX_TEMPLATE) { newNode.addChild(child.deepCopy(includeTemplates)); } } return(newNode); }
// for making new repeat instances; 'from' and 'to' must be unambiguous // references EXCEPT 'to' may be ambiguous at its final step // return true is successfully copied, false otherwise public TreeElement copyNode(TreeElement src, TreeReference to) { if (!to.isAbsolute()) { throw new InvalidReferenceException("Destination reference must be absolute for copying", to); } // strip out dest node info and get dest parent String dstName = to.getNameLast(); int dstMult = to.getMultLast(); TreeReference toParent = to.getParentRef(); TreeElement parent = resolveReference(toParent); if (parent == null) { throw new InvalidReferenceException("Null parent reference whle attempting to copy", toParent); } if (!parent.isChildable()) { throw new InvalidReferenceException("Invalid Parent Node: cannot accept children.", toParent); } if (dstMult == TreeReference.INDEX_UNBOUND) { dstMult = parent.getChildMultiplicity(dstName); } else if (parent.getChild(dstName, dstMult) != null) { throw new InvalidReferenceException("Destination already exists!", to); } TreeElement dest = src.deepCopy(false); dest.setName(dstName); dest.multiplicity = dstMult; parent.addChild(dest); return(dest); }
/* * create the specified node in the tree, creating all intermediary nodes at * each step, if necessary. if specified node already exists, return null * * creating a duplicate node is only allowed at the final step. it will be * done if the multiplicity of the last step is ALL or equal to the count of * nodes already there * * at intermediate steps, the specified existing node is used; if * multiplicity is ALL: if no nodes exist, a new one is created; if one node * exists, it is used; if multiple nodes exist, it's an error * * return the newly-created node; modify ref so that it's an unambiguous ref * to the node */ private TreeElement createNode(TreeReference ref_) { TreeElement node = root; for (int k = 0; k < ref_.size(); k++) { String name = ref_.getName(k); int count = node.getChildMultiplicity(name); int mult = ref_.getMultiplicity(k); TreeElement child; if (k < ref_.size() - 1) { if (mult == TreeReference.INDEX_UNBOUND) { if (count > 1) { return(null); // don't know which node to use } else { // will use existing (if one and only one) or create new mult = 0; ref_.setMultiplicity(k, 0); } } // fetch child = node.getChild(name, mult); if (child == null) { if (mult == 0) { // create child = new TreeElement(name, count); node.addChild(child); ref_.setMultiplicity(k, count); } else { return(null); // intermediate node does not exist } } } else { if (mult == TreeReference.INDEX_UNBOUND || mult == count) { if (k == 0 && root.getNumChildren() != 0) { return(null); // can only be one top-level node, and it // already exists } if (!node.isChildable()) { return(null); // current node can't have children } // create new child = new TreeElement(name, count); node.addChild(child); ref_.setMultiplicity(k, count); } else { return(null); // final node must be a newly-created node } } node = child; } return(node); }