public String serializeNode(TreeElement instanceNode) { String ae = ""; // 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) { // DON"T handle this. throw new SystemException("Can't handle serialized output for" + instanceNode.getValue().ToString() + ", " + serializedAnswer); } else if (serializedAnswer is String) { XmlDocument theXmlDoc = new XmlDocument(); XmlElement e = theXmlDoc.CreateElement("TreeElement");//TODO XmlText xmlText = theXmlDoc.CreateTextNode((String)serializedAnswer); e.AppendChild(xmlText); String tag = instanceNode.getAttributeValue("", "tag"); ae += ((tag != null) ? tag + delimeter : delimeter); // tag // might // be // null for (int k = 0; k < e.ChildNodes.Count; k++) { ae += e.ChildNodes[k].InnerText.ToString() + delimeter; } } 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]); } } } return(ae); }
/** * Determine if the current FormIndex is relevant. Only relevant indexes * should be returned when filling out a form. * * @param index * @return true if current element at FormIndex is relevant */ public Boolean isIndexRelevant(FormIndex index) { TreeReference ref_ = form.getChildInstanceRef(index); Boolean isAskNewRepeat = (getEvent(index) == FormEntryController.EVENT_PROMPT_NEW_REPEAT); Boolean isRepeatJuncture = (getEvent(index) == FormEntryController.EVENT_REPEAT_JUNCTURE); Boolean relevant; if (isAskNewRepeat) { relevant = form.isRepeatRelevant(ref_) && form.canCreateRepeat(ref_, index); //repeat junctures are still relevant if no new repeat can be created; that option //is simply missing from the menu } else if (isRepeatJuncture) { relevant = form.isRepeatRelevant(ref_); } else { TreeElement node = form.Instance.resolveReference(ref_); relevant = node.isRelevant(); // check instance flag first } if (relevant) { // if instance flag/condition says relevant, we still // have to check the <group>/<repeat> hierarchy FormIndex ancestorIndex = index; while (!ancestorIndex.isTerminal()) { // This should be safe now that the TreeReference is contained // in the ancestor index itself TreeElement ancestorNode = form.Instance.resolveReference(ancestorIndex.getLocalReference()); if (!ancestorNode.isRelevant()) { relevant = false; break; } ancestorIndex = ancestorIndex.getNextLevel(); } } return(relevant); }
// // boolean nodeset = forceNodeset; // if (!nodeset) { // //is this a nodeset? it is if the ref contains any unbound multiplicities AND the unbound nodes are repeatable // //the way i'm calculating this sucks; there has got to be an easier way to find out if a node is repeatable // TreeReference repeatTestRef = TreeReference.rootRef(); // for (int i = 0; i < ref.size(); i++) { // repeatTestRef.add(ref.getName(i), ref.getMultiplicity(i)); // if (ref.getMultiplicity(i) == TreeReference.INDEX_UNBOUND) { // if (m.getTemplate(repeatTestRef) != null) { // nodeset = true; // break; // } // } // } // } public static Object getRefValue(FormInstance model, EvaluationContext ec, TreeReference ref_) { if (ec.isConstraint && ref_.Equals(ec.ContextRef)) { //ITEMSET TODO: need to update this; for itemset/copy constraints, need to simulate a whole xml sub-tree here return(unpackValue(ec.candidateValue)); } else { TreeElement node = model.resolveReference(ref_); if (node == null) { //shouldn't happen -- only existent nodes should be in nodeset throw new XPathTypeMismatchException("Node " + ref_.toString() + " does not exist!"); } return(unpackValue(node.isRelevant() ? node.getValue() : null)); } }
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); }