예제 #1
0
        /**
         * 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()));
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
 /**
  * commitAnswer actually saves the data into the datamodel.
  *
  * @param element
  * @param index
  * @param data
  * @return true if saved successfully, false otherwise
  */
 private Boolean commitAnswer(TreeElement element, FormIndex index, IAnswerData data)
 {
     if (data != null || element.getValue() != null)
     {
         // we should check if the data to be saved is already the same as
         // the data in the model, but we can't (no IAnswerData.equals())
         model.getForm().setValue(data, index.getReference(), element);
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #4
0
//
//	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));
            }
        }
예제 #5
0
        //note: code overlap with FormDef.copyItemsetAnswer
        public IAnswerData getAnswerValue()
        {
            QuestionDef q = getQuestion();

            ItemsetBinding itemset = q.getDynamicChoices();

            if (itemset != null)
            {
                if (itemset.valueRef != null)
                {
                    List <SelectChoice> choices           = getSelectChoices();
                    List <String>       preselectedValues = new List <String>();

                    //determine which selections are already present in the answer
                    if (itemset.copyMode)
                    {
                        TreeReference        destRef  = itemset.getDestRef().contextualize(mTreeElement.getRef());
                        List <TreeReference> subNodes = form.Instance.expandReference(destRef);
                        for (int i = 0; i < subNodes.Count; i++)
                        {
                            TreeElement node  = form.Instance.resolveReference(subNodes[i]);
                            String      value = itemset.getRelativeValue().evalReadable(form.Instance, new EvaluationContext(form.exprEvalContext, node.getRef()));
                            preselectedValues.Add(value);
                        }
                    }
                    else
                    {
                        List <Selection> sels = new List <Selection>();
                        IAnswerData      data = mTreeElement.getValue();
                        if (data is SelectMultiData)
                        {
                            sels = (List <Selection>)data.Value;
                        }
                        else if (data is SelectOneData)
                        {
                            sels = new List <Selection>();
                            sels.Add((Selection)data.Value);
                        }
                        for (int i = 0; i < sels.Count; i++)
                        {
                            preselectedValues.Add(sels[i].xmlValue);
                        }
                    }

                    //populate 'selection' with the corresponding choices (matching 'value') from the dynamic choiceset
                    List <Selection> selection = new List <Selection>();
                    for (int i = 0; i < preselectedValues.Count; i++)
                    {
                        String       value  = preselectedValues[i];
                        SelectChoice choice = null;
                        for (int j = 0; j < choices.Count; j++)
                        {
                            SelectChoice ch = choices[j];
                            if (value.Equals(ch.Value))
                            {
                                choice = ch;
                                break;
                            }
                        }

                        selection.Add(choice.selection());
                    }

                    //convert to IAnswerData
                    if (selection.Count == 0)
                    {
                        return(null);
                    }
                    else if (q.ControlType == Constants.CONTROL_SELECT_MULTI)
                    {
                        return(new SelectMultiData(selection));
                    }
                    else if (q.ControlType == Constants.CONTROL_SELECT_ONE)
                    {
                        return(new SelectOneData(selection[0])); //do something if more than one selected?
                    }
                    else
                    {
                        throw new SystemException("can't happen");
                    }
                }
                else
                {
                    return(null); //cannot map up selections without <value>
                }
            }
            else
            { //static choices
                return(mTreeElement.getValue());
            }
        }
예제 #6
0
        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);
        }