Example #1
0
        public virtual Element serializeNode(TreeElement instanceNode)
        {
            Element e = new Element();             //don't set anything on this element yet, as it might get overwritten

            //don't serialize template nodes or non-relevant nodes
            if ((respectRelevance && !instanceNode.isRelevant()) || instanceNode.Mult == TreeReference.INDEX_TEMPLATE)
            {
                return(null);
            }

            if (instanceNode.Value != null)
            {
                System.Object serializedAnswer = serializer.serializeAnswerData(instanceNode.Value, instanceNode.DataType);

                if (serializedAnswer is Element)
                {
                    e = (Element)serializedAnswer;
                }
                else if (serializedAnswer is System.String)
                {
                    e = new Element();
                    e.addChild(Node.TEXT, (System.String)serializedAnswer);
                }
                else
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    throw new System.SystemException("Can't handle serialized output for" + instanceNode.Value.ToString() + ", " + serializedAnswer);
                }

                if (serializer.containsExternalData(instanceNode.Value))
                {
                    IDataPointer[] pointer = serializer.retrieveExternalDataPointer(instanceNode.Value);
                    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
                System.Collections.ArrayList childNames = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                for (int i = 0; i < instanceNode.NumChildren; i++)
                {
                    System.String childName = instanceNode.getChildAt(i).Name;
                    if (!childNames.Contains(childName))
                    {
                        childNames.Add(childName);
                    }
                }

                for (int i = 0; i < childNames.Count; i++)
                {
                    System.String childName = (System.String)childNames[i];
                    int           mult      = instanceNode.getChildMultiplicity(childName);
                    for (int j = 0; j < mult; j++)
                    {
                        Element child = serializeNode(instanceNode.getChild(childName, j));
                        if (child != null)
                        {
                            e.addChild(Node.ELEMENT, child);
                        }
                    }
                }
            }

            e.setName(instanceNode.Name);

            // add hard-coded attributes
            for (int i = 0; i < instanceNode.AttributeCount; i++)
            {
                System.String namespace_Renamed = instanceNode.getAttributeNamespace(i);
                System.String name = instanceNode.getAttributeName(i);
                System.String val  = instanceNode.getAttributeValue(i);
                // is it legal for getAttributeValue() to return null? playing it safe for now and assuming yes
                if (val == null)
                {
                    val = "";
                }
                e.setAttribute(namespace_Renamed, name, val);
            }
            if (instanceNode.Namespace != null)
            {
                e.setNamespace(instanceNode.Namespace);
            }

            return(e);
        }