Ejemplo n.º 1
0
        /// <summary>
        ///		Applies the differences from the diff document to transform the Original document to
        ///		the Modified document it was compared against.
        /// </summary>
        /// <param name="doc"> the Original document, used to provide contex for created attributes </param>
        /// <param name="modifiedNode"> the node resulting from a diff operation </param>
        /// <param name="originalNode"> the node to be transformed in the merge </param>
        /// <param name="ignoreName"> whether to ignore the name difference in the merge operation </param>
        protected static void MergeAttrs
        (
            XDocument doc,
            XElement modifiedNode,
            ref XElement originalNode,
            bool ignoreName
        )
        {
            // assumes the nodes are "matched" in in the document by FindNode()

            if (!ignoreName && !Persistence.XNamesEqual(modifiedNode.Name, originalNode.Name))
            {
                XElement newNode = new XElement(modifiedNode.Name);
                MergeAttrs(doc, originalNode, ref newNode, true);
                foreach (XElement node in originalNode.Elements())
                {
                    newNode.Add(CopyNode(doc, node, true));
                }
                originalNode.ReplaceWith(newNode);
                originalNode = newNode;
                newNode      = null;
            }

            // merge the attributes
            foreach (XAttribute modifiedAttr in modifiedNode.Attributes())
            {
                if (!(modifiedAttr.Name == (XNamespace.Xmlns + IBOPNamespacePrefix)) && !Persistence.XNamesEqual(modifiedAttr.Name, XmlIBOPOrder))                 // don't put the ibop:order in the merged doc
                {
                    // if the attribute name is default-<attribute name> and it's value is true then remove the appropriate attribute from the original node.
                    if (modifiedAttr.Name.NamespaceName == Persistence.BOPNamespaceURI && modifiedAttr.Name.LocalName.StartsWith(Persistence.BOPDefault))
                    {
                        string attributeName = modifiedAttr.Name.LocalName.Substring(Persistence.BOPDefault.Length);
                        originalNode.SetAttributeValue(attributeName, null);
                    }
                    else
                    {
                        // assumed each node has a name and never delete the name attr
                        XAttribute originalAttr = originalNode.Attribute(modifiedAttr.Name);
                        if (originalAttr == null)                         // add attr
                        {
                            AddAttr
                            (
                                doc,
                                ref originalNode,
                                modifiedNode.Name,
                                modifiedAttr.Name,
                                modifiedAttr.Value
                            );
                        }
                        else                         // change attr value
                        {
                            originalAttr.Value = modifiedAttr.Value;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary> Compares the Modified node's atributes to those of the Original node. </summary>
        /// <remarks> Copies the differences, using AModifiedNode, into ADiffNode. </remarks>
        /// <param name="diffDoc"> document used to provide context for the DiffNode </param>
        /// <param name="AModifiedNode"> node that the Original should look like after a merge operation </param>
        /// <param name="AOriginalNode"> node describing the initial state of the node</param>
        /// <param name="ADiffNode"> node describing the operations required to make the Original node into the Modified </param>
        /// <returns> ADiffNode </returns>
        protected static XElement DiffAttrs
        (
            XDocument diffDoc,
            XElement modifiedElement,
            XElement originalElement,
            ref XElement diffElement
        )
        {
            // assumes both nodes exist!
            bool diffFound = false;

            // assumes the nodes are "matched" in the document by the name attribute, see FindNode()
            foreach (XAttribute modifiedAttr in modifiedElement.Attributes())
            {
                XAttribute originalAttr = originalElement.Attribute(modifiedAttr.Name);

                if (originalAttr == null)                 // add attr
                {
                    diffFound = true;
                    if (!Persistence.XNamesEqual(modifiedAttr.Name, XmlIBOPDiffFound))
                    {
                        AddAttr(diffDoc, ref diffElement, originalElement.Name, modifiedAttr.Name, modifiedAttr.Value);
                    }
                }
                else                 // compare attr value
                {
                    if (!String.Equals(modifiedAttr.Value, originalAttr.Value))
                    {
                        diffFound = true;
                        AddAttr(diffDoc, ref diffElement, originalElement.Name, modifiedAttr.Name, modifiedAttr.Value);
                    }
                    else
                    {
                        if
                        (
                            String.Equals(modifiedAttr.Name.NamespaceName, Serializer.BOPNamespaceURI, StringComparison.OrdinalIgnoreCase) ||
                            String.Equals(modifiedAttr.Name.NamespaceName, IBOPNamespaceURI, StringComparison.OrdinalIgnoreCase)
                        )
                        {
                            AddAttr(diffDoc, ref diffElement, originalElement.Name, modifiedAttr.Name, modifiedAttr.Value);
                        }
                    }
                    // delete the original attribute so we know which ones do not appear in the modified node
                    originalElement.SetAttributeValue(originalAttr.Name, null);
                }
            }

            foreach (XAttribute defaultAttr in originalElement.Attributes())
            {
                // If the attr is in the orig but not in modified, then explicitly mark the attr as "default" (it has changed from something to the default value)
                diffFound = true;
                AddAttr
                (
                    diffDoc,
                    ref diffElement,
                    originalElement.Name,
                    XName.Get(Persistence.BOPDefault + defaultAttr.Name, Persistence.BOPNamespaceURI),
                    "True"
                );
            }

            if (diffFound)
            {
                AddAttr
                (
                    diffDoc,
                    ref diffElement,
                    originalElement.Name,
                    XmlIBOPDiffFound,
                    "yes"
                );
            }
            else
            {
                AddAttr
                (
                    diffDoc,
                    ref diffElement,
                    originalElement.Name,
                    XmlIBOPDiffFound,
                    "no"
                );
            }

            return(diffElement);
        }