Exemplo n.º 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()));
            }
        }
Exemplo n.º 2
0
        /**
         * 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)));
            }
        }