public TreeElement getTemplatePath(TreeReference ref_) { if (!ref_.isAbsolute()) { return(null); } TreeElement node = root; for (int i = 0; i < ref_.size(); i++) { String name = ref_.getName(i); if (ref_.getMultiplicity(i) == TreeReference.INDEX_ATTRIBUTE) { node = node.getAttribute(null, name); } else { TreeElement newNode = node.getChild(name, TreeReference.INDEX_TEMPLATE); if (newNode == null) { newNode = node.getChild(name, 0); } if (newNode == null) { return(null); } node = newNode; } } return(node); }
// 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 '/' }
// 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); } } }