Exemplo n.º 1
0
        private void ApplyDiffgram(XmlNode diffgramParent, XmlDiffViewParentNode sourceParent)
        {
            sourceParent.CreateSourceNodesIndex();
            XmlDiffViewNode currentPosition = null;
            var             enumerator      = diffgramParent.ChildNodes.GetEnumerator();

            while (enumerator.MoveNext())
            {
                if (((XmlNode)enumerator.Current).NodeType != XmlNodeType.Comment)
                {
                    if (!(enumerator.Current is XmlElement))
                    {
                        throw new Exception("Invalid node in diffgram.");
                    }
                    var current = enumerator.Current as XmlElement;
                    if (current.NamespaceURI != "http://schemas.microsoft.com/xmltools/2002/xmldiff")
                    {
                        throw new Exception("Invalid element in diffgram.");
                    }
                    var attribute1 = current.GetAttribute("match");
                    XmlDiffPathNodeList matchNodes = null;
                    if (attribute1 != string.Empty)
                    {
                        matchNodes = XmlDiffPath.SelectNodes(this._doc, sourceParent, attribute1);
                    }
                    switch (current.LocalName)
                    {
                    case "node":
                        if (matchNodes.Count != 1)
                        {
                            throw new Exception("The 'match' attribute of 'node' element must select a single node.");
                        }
                        matchNodes.MoveNext();
                        if (current.ChildNodes.Count > 0)
                        {
                            this.ApplyDiffgram(current, (XmlDiffViewParentNode)matchNodes.Current);
                        }
                        currentPosition = matchNodes.Current;
                        continue;

                    case "add":
                        if (attribute1 != string.Empty)
                        {
                            this.OnAddMatch(current, matchNodes, sourceParent, ref currentPosition);
                            continue;
                        }
                        var attribute2 = current.GetAttribute("type");
                        if (attribute2 != string.Empty)
                        {
                            this.OnAddNode(current, attribute2, sourceParent, ref currentPosition);
                            continue;
                        }
                        this.OnAddFragment(current, sourceParent, ref currentPosition);
                        continue;

                    case "remove":
                        this.OnRemove(current, matchNodes, sourceParent, ref currentPosition);
                        continue;

                    case "change":
                        this.OnChange(current, matchNodes, sourceParent, ref currentPosition);
                        continue;

                    default:
                        continue;
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ApplyDiffgram(XmlNode diffgramParent, XmlDiffViewParentNode sourceParent)
        {
            sourceParent.CreateSourceNodesIndex();
            XmlDiffViewNode currentPosition = null;

            IEnumerator diffgramChildren = diffgramParent.ChildNodes.GetEnumerator();

            while (diffgramChildren.MoveNext())
            {
                XmlNode diffgramNode = (XmlNode)diffgramChildren.Current;
                if (diffgramNode.NodeType == XmlNodeType.Comment)
                {
                    continue;
                }
                XmlElement diffgramElement = diffgramChildren.Current as XmlElement;
                if (diffgramElement == null)
                {
                    throw new Exception("Invalid node in diffgram.");
                }

                if (diffgramElement.NamespaceURI != XmlDiff.NamespaceUri)
                {
                    throw new Exception("Invalid element in diffgram.");
                }

                string matchAttr = diffgramElement.GetAttribute("match");
                XmlDiffPathNodeList matchNodes = null;
                if (matchAttr != string.Empty)
                {
                    matchNodes = XmlDiffPath.SelectNodes(_doc, sourceParent, matchAttr);
                }

                switch (diffgramElement.LocalName)
                {
                case "node":
                    if (matchNodes.Count != 1)
                    {
                        throw new Exception("The 'match' attribute of 'node' element must select a single node.");
                    }
                    matchNodes.MoveNext();
                    if (diffgramElement.ChildNodes.Count > 0)
                    {
                        ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)matchNodes.Current);
                    }
                    currentPosition = matchNodes.Current;
                    break;

                case "add":
                    if (matchAttr != string.Empty)
                    {
                        OnAddMatch(diffgramElement, matchNodes, sourceParent, ref currentPosition);
                    }
                    else
                    {
                        string typeAttr = diffgramElement.GetAttribute("type");
                        if (typeAttr != string.Empty)
                        {
                            OnAddNode(diffgramElement, typeAttr, sourceParent, ref currentPosition);
                        }
                        else
                        {
                            OnAddFragment(diffgramElement, sourceParent, ref currentPosition);
                        }
                    }
                    break;

                case "remove":
                    OnRemove(diffgramElement, matchNodes, sourceParent, ref currentPosition);
                    break;

                case "change":
                    OnChange(diffgramElement, matchNodes, sourceParent, ref currentPosition);
                    break;
                }
            }
        }