/** * recursively write out a node of the instance * @param out * @param e * @param ref * @throws IOException */ private void writeTreeElement(BinaryWriter out_, TreeElement e) { TreeElement templ = instance.getTemplatePath(e.getRef()); Boolean isGroup = !templ.isLeaf(); if (isGroup) { ArrayList childTypesHandled = new ArrayList(); for (int i = 0; i < templ.getNumChildren(); i++) { String childName = templ.getChildAt(i).getName(); if (!childTypesHandled.Contains(childName)) { childTypesHandled.Add(childName); int mult = e.getChildMultiplicity(childName); if (mult > 0 && !e.getChild(childName, 0).isRelevant()) { mult = 0; } ExtUtil.writeNumeric(out_, mult); for (int j = 0; j < mult; j++) { writeTreeElement(out_, e.getChild(childName, j)); } } } } else { ExtUtil.write(out_, new ExtWrapAnswerData(this, e.dataType, e.getValue())); } }
/** * recursively read in a node of the instance, by filling out the template instance * @param e * @param ref * @param in * @param pf * @throws IOException * @throws DeserializationException */ private void readTreeElement(TreeElement e, BinaryReader in_, PrototypeFactory pf) { TreeElement templ = instance.getTemplatePath(e.getRef()); Boolean isGroup = !templ.isLeaf(); if (isGroup) { ArrayList childTypes = new ArrayList(); for (int i = 0; i < templ.getNumChildren(); i++) { String childName = templ.getChildAt(i).getName(); if (!childTypes.Contains(childName)) { childTypes.Add(childName); } } for (int i = 0; i < childTypes.Count; i++) { String childName = (String)childTypes[i]; TreeReference childTemplRef = e.getRef().extendRef(childName, 0); TreeElement childTempl = instance.getTemplatePath(childTemplRef); Boolean repeatable = childTempl.repeatable; int n = ExtUtil.readInt(in_); Boolean relevant = (n > 0); if (!repeatable && n > 1) { throw new DeserializationException("Detected repeated instances of a non-repeatable node"); } if (repeatable) { int mult = e.getChildMultiplicity(childName); for (int j = mult - 1; j >= 0; j--) { e.removeChild(childName, j); } for (int j = 0; j < n; j++) { TreeReference dstRef = e.getRef().extendRef(childName, j); try { instance.copyNode(childTempl, dstRef); } catch (InvalidReferenceException ire) { //If there is an invalid reference, this is a malformed instance, //so we'll throw a Deserialization exception. TreeReference r = ire.InvalidReference; if (r == null) { throw new DeserializationException("Null Reference while attempting to deserialize! " + ire.Message); } else { throw new DeserializationException("Invalid Reference while attemtping to deserialize! Reference: " + r.toString(true) + " | " + ire.Message); } } TreeElement child = e.getChild(childName, j); child.setRelevant(true); readTreeElement(child, in_, pf); } } else { TreeElement child = e.getChild(childName, 0); child.setRelevant(relevant); if (relevant) { readTreeElement(child, in_, pf); } } } } else { e.setValue((IAnswerData)ExtUtil.read(in_, new ExtWrapAnswerData(e.dataType))); } }
public XmlElement serializeNode(TreeElement instanceNode) { XmlElement e = theXmlDoc.CreateElement(instanceNode.getName());//TODO Name //don't serialize template nodes or non-relevant nodes if (!instanceNode.isRelevant() || instanceNode.getMult() == TreeReference.INDEX_TEMPLATE) { return(null); } if (instanceNode.getValue() != null) { Object serializedAnswer = serializer.serializeAnswerData(instanceNode.getValue(), instanceNode.dataType); if (serializedAnswer is XmlElement) { e = (XmlElement)serializedAnswer; } else if (serializedAnswer is String) { e = e.OwnerDocument.CreateElement(instanceNode.getName()); XmlText xmlText = theXmlDoc.CreateTextNode((String)serializedAnswer); //TODO name e.AppendChild(xmlText); } else { throw new SystemException("Can't handle serialized output for" + instanceNode.getValue().ToString() + ", " + serializedAnswer); } if (serializer.containsExternalData(instanceNode.getValue())) { IDataPointer[] pointer = serializer.retrieveExternalDataPointer(instanceNode.getValue()); for (int i = 0; i < pointer.Length; ++i) { dataPointers.Add(pointer[i]); } } } else { //make sure all children of the same tag name are written en bloc ArrayList childNames = new ArrayList(); for (int i = 0; i < instanceNode.getNumChildren(); i++) { String childName = instanceNode.getChildAt(i).getName(); Console.WriteLine("CHILDNAME: " + childName); if (!childNames.Contains(childName)) { childNames.Add(childName); } } for (int i = 0; i < childNames.Count; i++) { String childName = (String)childNames[i]; int mult = instanceNode.getChildMultiplicity(childName); for (int j = 0; j < mult; j++) { XmlElement child = serializeNode(instanceNode.getChild(childName, j)); if (child != null) { e.AppendChild(child); } } } } XmlElement e1 = e.OwnerDocument.CreateElement(instanceNode.getName()); e.ParentNode.ReplaceChild(e1, e); // add hard-coded attributes for (int i = 0; i < instanceNode.getAttributeCount(); i++) { String namespace_ = instanceNode.getAttributeNamespace(i); String name = instanceNode.getAttributeName(i); String val = instanceNode.getAttributeValue(i); e.SetAttribute(name, namespace_, val); } return(e); }