// same as resolveReference but return a vector containing all interstitial // nodes: top-level instance data node first, and target node last // returns null in all the same situations as resolveReference EXCEPT ref // '/' will instead return empty vector public ArrayList explodeReference(TreeReference ref_) { if (!ref_.isAbsolute()) { return(null); } ArrayList nodes = new ArrayList(); TreeElement cur = root; for (int i = 0; i < ref_.size(); i++) { String name = ref_.getName(i); int mult = ref_.getMultiplicity(i); //If the next node down the line is an attribute if (mult == TreeReference.INDEX_ATTRIBUTE) { //This is not the attribute we're testing if (cur != root) { //Add the current node nodes.Add(cur); } cur = cur.getAttribute(null, name); } //Otherwise, it's another child element else { if (mult == TreeReference.INDEX_UNBOUND) { if (cur.getChildMultiplicity(name) == 1) { mult = 0; } else { // reference is not unambiguous return(null); } } if (cur != root) { nodes.Add(cur); } cur = cur.getChild(name, mult); if (cur == null) { return(null); } } } return(nodes); }
// take a ref that unambiguously refers to a single node and return that node // return null if ref is ambiguous, node does not exist, ref is relative, or ref is '/' // can be used to retrieve template nodes public TreeElement resolveReference(TreeReference ref_) { if (!ref_.isAbsolute()) { return(null); } TreeElement node = root; for (int i = 0; i < ref_.size(); i++) { String name = ref_.getName(i); int mult = ref_.getMultiplicity(i); if (mult == TreeReference.INDEX_ATTRIBUTE) { //Should we possibly just return here? //I guess technically we could step back... node = node.getAttribute(null, name); continue; } if (mult == TreeReference.INDEX_UNBOUND) { if (node.getChildMultiplicity(name) == 1) { mult = 0; } else { // reference is not unambiguous node = null; break; } } node = node.getChild(name, mult); if (node == null) { break; } } return(node == root ? null : node); // never return a reference to '/' }
// 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); }
// recursive helper function for expandReference // sourceRef: original path we're matching against // node: current node that has matched the sourceRef thus far // templateRef: explicit path that refers to the current node // refs: Vector to collect matching paths; if 'node' is a target node that // matches sourceRef, templateRef is added to refs private void expandReference(TreeReference sourceRef, TreeElement node, List <TreeReference> refs, Boolean includeTemplates) { int depth = node.getDepth(); if (depth == sourceRef.size()) { refs.Add(node.getRef()); } else { String name = sourceRef.getName(depth); int mult = sourceRef.getMultiplicity(depth); List <TreeElement> set = new List <TreeElement>(); if (node.getNumChildren() > 0) { if (mult == TreeReference.INDEX_UNBOUND) { int count = node.getChildMultiplicity(name); for (int i = 0; i < count; i++) { TreeElement child = node.getChild(name, i); if (child != null) { set.Add(child); } else { throw new InvalidOperationException(); // missing/non-sequential // nodes } } if (includeTemplates) { TreeElement template = node.getChild(name, TreeReference.INDEX_TEMPLATE); if (template != null) { set.Add(template); } } } else if (mult != TreeReference.INDEX_ATTRIBUTE) { //TODO: Make this test mult >= 0? //If the multiplicity is a simple integer, just get //the appropriate child TreeElement child = node.getChild(name, mult); if (child != null) { set.Add(child); } } } if (mult == TreeReference.INDEX_ATTRIBUTE) { TreeElement attribute = node.getAttribute(null, name); set.Add(attribute); } for (IEnumerator e = set.GetEnumerator(); e.MoveNext();) { expandReference(sourceRef, (TreeElement)e.Current, refs, includeTemplates); } } }