//this method is for copying in the answers to an itemset. the template node of the destination //is used for overall structure (including data types), and the itemset source node is used for //raw data. note that data may be coerced across types, which may result in type conversion error //very similar in structure to populate() public void populateTemplate(TreeElement incoming, FormDef f) { if (this.isLeaf()) { IAnswerData value = incoming.value; if (value == null) { this.setValue(null); } else { Type classType = CompactInstanceWrapper.classForDataType(this.dataType); if (classType == null) { throw new SystemException("data type [" + value.GetType().Name + "] not supported inside itemset"); } else if (classType.IsAssignableFrom(value.GetType()) && !(value is SelectOneData || value is SelectMultiData)) { this.setValue(value); } else { String textVal = RestoreUtils.xfFact.serializeData(value); IAnswerData typedVal = RestoreUtils.xfFact.parseData(textVal, this.dataType, this.getRef(), f); this.setValue(typedVal); } } } else { for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); List <TreeElement> newChildren = incoming.getChildrenWithName(child.getName()); if (child.repeatable) { for (int k = 0; k < newChildren.Count; k++) { TreeElement template = f.Instance.getTemplate(child.getRef()); TreeElement newChild = template.deepCopy(false); newChild.setMult(k); this.children.Insert(i + k + 1, newChild); newChild.populateTemplate((TreeElement)newChildren[k], f); } i += newChildren.Count; } else { child.populateTemplate((TreeElement)newChildren[0], f); } } } }
// 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); } } }