Esempio n. 1
0
        private void OnAddNode(
            XmlElement diffgramElement,
            string nodeTypeAttr,
            XmlDiffViewParentNode sourceParent,
            ref XmlDiffViewNode currentPosition)
        {
            var nodeType   = (XmlNodeType)int.Parse(nodeTypeAttr);
            var attribute1 = diffgramElement.GetAttribute("name");
            var attribute2 = diffgramElement.GetAttribute("prefix");
            var attribute3 = diffgramElement.GetAttribute("ns");
            var attribute4 = diffgramElement.GetAttribute("opid");
            var num        = attribute4 == string.Empty ? 0 : int.Parse(attribute4);

            if (nodeType == XmlNodeType.Attribute)
            {
                Debug.Assert(attribute1 != string.Empty);
                var newAttr = new XmlDiffViewAttribute(attribute1, attribute2, attribute3, diffgramElement.InnerText);
                newAttr._op   = XmlDiffViewOperation.Add;
                newAttr._opid = num;
                ((XmlDiffViewElement)sourceParent).InsertAttributeAfter(newAttr, null);
            }
            else
            {
                XmlDiffViewNode newChild = null;
                switch (nodeType)
                {
                case XmlNodeType.Element:
                    Debug.Assert(attribute1 != string.Empty);
                    newChild = new XmlDiffViewElement(attribute1, attribute2, attribute3, this._bIgnorePrefixes);
                    this.ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)newChild);
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.Comment:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    newChild = new XmlDiffViewCharData(diffgramElement.InnerText, nodeType);
                    break;

                case XmlNodeType.EntityReference:
                    Debug.Assert(attribute1 != string.Empty);
                    newChild = new XmlDiffViewER(attribute1);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    Debug.Assert(attribute1 != string.Empty);
                    newChild = new XmlDiffViewPI(attribute1, diffgramElement.InnerText);
                    break;

                case XmlNodeType.DocumentType:
                    newChild = new XmlDiffViewDocumentType(diffgramElement.GetAttribute("name"), diffgramElement.GetAttribute("publicId"), diffgramElement.GetAttribute("systemId"), diffgramElement.InnerText);
                    break;

                case XmlNodeType.XmlDeclaration:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    newChild = new XmlDiffViewXmlDeclaration(diffgramElement.InnerText);
                    break;

                default:
                    Debug.Assert(false, "Invalid node type.");
                    break;
                }
                Debug.Assert(newChild != null);
                newChild._op   = XmlDiffViewOperation.Add;
                newChild._opid = num;
                sourceParent.InsertChildAfter(newChild, currentPosition, false);
                currentPosition = newChild;
            }
        }
Esempio n. 2
0
        private void LoadSourceChildNodes(
            XmlDiffViewParentNode parent,
            XmlReader reader,
            bool bEmptyElement)
        {
            var loadState = this._loadState;

            this._loadState.Reset();
            while (reader.MoveToNextAttribute())
            {
                XmlDiffViewAttribute newAttr;
                if (reader.Prefix == "xmlns" || reader.Prefix == string.Empty && reader.LocalName == "xmlns")
                {
                    newAttr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, reader.Value);
                    if (this._bIgnoreNamespaces)
                    {
                        newAttr._op = XmlDiffViewOperation.Ignore;
                    }
                }
                else
                {
                    var str = this._bIgnoreWhitespace ? XmlDiffView.NormalizeText(reader.Value) : reader.Value;
                    newAttr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, str);
                }
                ((XmlDiffViewElement)parent).InsertAttributeAfter(newAttr, this._loadState._curLastAttribute);
                this._loadState._curLastAttribute = newAttr;
            }
            if (!bEmptyElement)
            {
                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Whitespace)
                    {
                        XmlDiffViewNode newChild = null;
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            var isEmptyElement     = reader.IsEmptyElement;
                            var xmlDiffViewElement = new XmlDiffViewElement(reader.LocalName, reader.Prefix, reader.NamespaceURI, this._bIgnorePrefixes);
                            this.LoadSourceChildNodes(xmlDiffViewElement, reader, isEmptyElement);
                            newChild = xmlDiffViewElement;
                            break;

                        case XmlNodeType.Attribute:
                            Debug.Assert(false, "We should never get to this point, attributes should be read at the beginning of thid method.");
                            break;

                        case XmlNodeType.Text:
                            newChild = new XmlDiffViewCharData(this._bIgnoreWhitespace ? XmlDiffView.NormalizeText(reader.Value) : reader.Value, XmlNodeType.Text);
                            break;

                        case XmlNodeType.CDATA:
                            newChild = new XmlDiffViewCharData(reader.Value, XmlNodeType.CDATA);
                            break;

                        case XmlNodeType.EntityReference:
                            newChild = new XmlDiffViewER(reader.Name);
                            break;

                        case XmlNodeType.ProcessingInstruction:
                            newChild = new XmlDiffViewPI(reader.Name, reader.Value);
                            if (this._bIgnorePI)
                            {
                                newChild._op = XmlDiffViewOperation.Ignore;
                                break;
                            }
                            break;

                        case XmlNodeType.Comment:
                            newChild = new XmlDiffViewCharData(reader.Value, XmlNodeType.Comment);
                            if (this._bIgnoreComments)
                            {
                                newChild._op = XmlDiffViewOperation.Ignore;
                                break;
                            }
                            break;

                        case XmlNodeType.DocumentType:
                            newChild = new XmlDiffViewDocumentType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                            if (this._bIgnoreDtd)
                            {
                                newChild._op = XmlDiffViewOperation.Ignore;
                                break;
                            }
                            break;

                        case XmlNodeType.SignificantWhitespace:
                            if (reader.XmlSpace == XmlSpace.Preserve)
                            {
                                newChild = new XmlDiffViewCharData(reader.Value, XmlNodeType.SignificantWhitespace);
                                if (this._bIgnoreWhitespace)
                                {
                                    newChild._op = XmlDiffViewOperation.Ignore;
                                    break;
                                }
                                break;
                            }
                            break;

                        case XmlNodeType.EndElement:
                            goto label_29;

                        case XmlNodeType.XmlDeclaration:
                            newChild = new XmlDiffViewXmlDeclaration(XmlDiffView.NormalizeText(reader.Value));
                            if (this._bIgnoreXmlDecl)
                            {
                                newChild._op = XmlDiffViewOperation.Ignore;
                                break;
                            }
                            break;

                        default:
                            Debug.Assert(false, "Invalid node type");
                            break;
                        }
                        parent.InsertChildAfter(newChild, this._loadState._curLastChild, true);
                        this._loadState._curLastChild = newChild;
                    }
                }
            }
label_29:
            this._loadState = loadState;
        }
Esempio n. 3
0
        private void OnAddNode(XmlElement diffgramElement, string nodeTypeAttr, XmlDiffViewParentNode sourceParent,
                               ref XmlDiffViewNode currentPosition)
        {
            XmlNodeType nodeType = (XmlNodeType)int.Parse(nodeTypeAttr);
            string      name     = diffgramElement.GetAttribute("name");
            string      prefix   = diffgramElement.GetAttribute("prefix");
            string      ns       = diffgramElement.GetAttribute("ns");
            string      opidAttr = diffgramElement.GetAttribute("opid");
            int         opid     = (opidAttr == string.Empty) ? 0 : int.Parse(opidAttr);

            if (nodeType == XmlNodeType.Attribute)
            {
                Debug.Assert(name != string.Empty);
                XmlDiffViewAttribute newAttr = new XmlDiffViewAttribute(name, prefix, ns, diffgramElement.InnerText);
                newAttr._op   = XmlDiffViewOperation.Add;
                newAttr._opid = opid;
                ((XmlDiffViewElement)sourceParent).InsertAttributeAfter(newAttr, null);
            }
            else
            {
                XmlDiffViewNode newNode = null;

                switch (nodeType)
                {
                case XmlNodeType.Element:
                    Debug.Assert(name != string.Empty);
                    newNode = new XmlDiffViewElement(name, prefix, ns, _bIgnorePrefixes);
                    ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)newNode);
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.Comment:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    newNode = new XmlDiffViewCharData(diffgramElement.InnerText, nodeType);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    Debug.Assert(name != string.Empty);
                    newNode = new XmlDiffViewPI(name, diffgramElement.InnerText);
                    break;

                case XmlNodeType.EntityReference:
                    Debug.Assert(name != string.Empty);
                    newNode = new XmlDiffViewER(name);
                    break;

                case XmlNodeType.XmlDeclaration:
                    Debug.Assert(diffgramElement.InnerText != string.Empty);
                    newNode = new XmlDiffViewXmlDeclaration(diffgramElement.InnerText);
                    break;

                case XmlNodeType.DocumentType:
                    newNode = new XmlDiffViewDocumentType(diffgramElement.GetAttribute("name"),
                                                          diffgramElement.GetAttribute("publicId"),
                                                          diffgramElement.GetAttribute("systemId"),
                                                          diffgramElement.InnerText);
                    break;

                default:
                    Debug.Assert(false, "Invalid node type.");
                    break;
                }
                Debug.Assert(newNode != null);
                newNode._op   = XmlDiffViewOperation.Add;
                newNode._opid = opid;
                sourceParent.InsertChildAfter(newNode, currentPosition, false);
                currentPosition = newNode;
            }
        }
Esempio n. 4
0
        private void LoadSourceChildNodes(XmlDiffViewParentNode parent, XmlReader reader, bool bEmptyElement)
        {
            LoadState savedLoadState = _loadState;

            _loadState.Reset();

            // load attributes
            while (reader.MoveToNextAttribute())
            {
                XmlDiffViewAttribute attr;
                if (reader.Prefix == "xmlns" ||
                    (reader.Prefix == string.Empty && reader.LocalName == "xmlns"))
                {
                    attr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, reader.Value);
                    if (_bIgnoreNamespaces)
                    {
                        attr._op = XmlDiffViewOperation.Ignore;
                    }
                }
                else
                {
                    string attrValue = _bIgnoreWhitespace ? NormalizeText(reader.Value) : reader.Value;
                    attr = new XmlDiffViewAttribute(reader.LocalName, reader.Prefix, reader.NamespaceURI, attrValue);
                }
                ((XmlDiffViewElement)parent).InsertAttributeAfter(attr, _loadState._curLastAttribute);
                _loadState._curLastAttribute = attr;
            }

            // empty element -> return, do not load chilren
            if (bEmptyElement)
            {
                goto End;
            }

            // load children
            while (reader.Read())
            {
                // ignore whitespaces between nodes
                if (reader.NodeType == XmlNodeType.Whitespace)
                {
                    continue;
                }

                XmlDiffViewNode child = null;
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    bool bEmptyEl           = reader.IsEmptyElement;
                    XmlDiffViewElement elem = new XmlDiffViewElement(reader.LocalName, reader.Prefix, reader.NamespaceURI, _bIgnorePrefixes);
                    LoadSourceChildNodes(elem, reader, bEmptyEl);
                    child = elem;
                    break;

                case XmlNodeType.Attribute:
                    Debug.Assert(false, "We should never get to this point, attributes should be read at the beginning of thid method.");
                    break;

                case XmlNodeType.Text:
                    child = new XmlDiffViewCharData((_bIgnoreWhitespace) ? NormalizeText(reader.Value) : reader.Value, XmlNodeType.Text);
                    break;

                case XmlNodeType.CDATA:
                    child = new XmlDiffViewCharData(reader.Value, XmlNodeType.CDATA);
                    break;

                case XmlNodeType.EntityReference:
                    child = new XmlDiffViewER(reader.Name);
                    break;

                case XmlNodeType.Comment:
                    child = new XmlDiffViewCharData(reader.Value, XmlNodeType.Comment);
                    if (_bIgnoreComments)
                    {
                        child._op = XmlDiffViewOperation.Ignore;
                    }
                    break;

                case XmlNodeType.ProcessingInstruction:
                    child = new XmlDiffViewPI(reader.Name, reader.Value);
                    if (_bIgnorePI)
                    {
                        child._op = XmlDiffViewOperation.Ignore;
                    }
                    break;

                case XmlNodeType.SignificantWhitespace:
                    if (reader.XmlSpace == XmlSpace.Preserve)
                    {
                        child = new XmlDiffViewCharData(reader.Value, XmlNodeType.SignificantWhitespace);
                        if (_bIgnoreWhitespace)
                        {
                            child._op = XmlDiffViewOperation.Ignore;
                        }
                    }
                    break;

                case XmlNodeType.XmlDeclaration:
                    child = new XmlDiffViewXmlDeclaration(NormalizeText(reader.Value));
                    if (_bIgnoreXmlDecl)
                    {
                        child._op = XmlDiffViewOperation.Ignore;
                    }
                    break;

                case XmlNodeType.EndElement:
                    goto End;

                case XmlNodeType.DocumentType:
                    child = new XmlDiffViewDocumentType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                    if (_bIgnoreDtd)
                    {
                        child._op = XmlDiffViewOperation.Ignore;
                    }
                    break;

                default:
                    Debug.Assert(false, "Invalid node type");
                    break;
                }
                parent.InsertChildAfter(child, _loadState._curLastChild, true);
                _loadState._curLastChild = child;
            }

End:
            _loadState = savedLoadState;
        }
        /// <summary>
        /// Add the new node or attribute 
        /// </summary>
        /// <param name="diffgramElement">node in diffgram</param>
        /// <param name="nodeTypeAttr">Whether this is an Attribute</param>
        /// <param name="sourceParent">the baseline parent node</param>
        /// <param name="currentPosition">the resulting node</param>
        private void OnAddNode(
            XmlElement diffgramElement,
            string nodeTypeAttr,
            XmlDiffViewParentNode sourceParent,
            ref XmlDiffViewNode currentPosition)
        {
            XmlNodeType nodeType = (XmlNodeType)
                int.Parse(nodeTypeAttr);
            string name = diffgramElement.GetAttribute("name");
            string prefix = diffgramElement.GetAttribute("prefix");
            string ns = diffgramElement.GetAttribute("ns");
            string opidAttr = diffgramElement.GetAttribute("opid");
            int opid = ParseOpId(opidAttr);

            if (nodeType == XmlNodeType.Attribute)
            {
                Debug.Assert(name != string.Empty);
                XmlDiffViewAttribute newAttr = new XmlDiffViewAttribute(
                    name,
                    prefix,
                    ns,
                    diffgramElement.InnerText);
                newAttr.Operation = XmlDiffViewOperation.Add;
                newAttr.OperationId = opid;
                ((XmlDiffViewElement)
                    sourceParent).InsertAttributeAfter(newAttr, null);
            }
            else
            {
                XmlDiffViewNode newNode = null;

                switch (nodeType)
                {
                    case XmlNodeType.Element:
                        Debug.Assert(name != string.Empty);
                        newNode = new XmlDiffViewElement(
                            name,
                            prefix,
                            ns,
                            this.ignorePrefixes);
                        this.ApplyDiffgram(
                            diffgramElement,
                            (XmlDiffViewParentNode)newNode);
                        break;
                    case XmlNodeType.Text:
                    case XmlNodeType.CDATA:
                    case XmlNodeType.Comment:
                        Debug.Assert(diffgramElement.InnerText != string.Empty);
                        newNode = new XmlDiffViewCharData(
                            diffgramElement.InnerText,
                            nodeType);
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        Debug.Assert(diffgramElement.InnerText != string.Empty);
                        Debug.Assert(name != string.Empty);
                        newNode = new XmlDiffViewPI(
                            name,
                            diffgramElement.InnerText);
                        break;
                    case XmlNodeType.EntityReference:
                        Debug.Assert(name != string.Empty);
                        Debug.Assert(false, "XmlDiffViewER was thought to be dead code");
                        //// newNode = new XmlDiffViewER(name);
                        break;
                    case XmlNodeType.XmlDeclaration:
                        Debug.Assert(diffgramElement.InnerText != string.Empty);
                        newNode = new XmlDiffViewXmlDeclaration(
                            diffgramElement.InnerText);
                        break;
                    case XmlNodeType.DocumentType:
                        newNode = new XmlDiffViewDocumentType(
                            diffgramElement.GetAttribute("name"),
                            diffgramElement.GetAttribute("publicId"),
                            diffgramElement.GetAttribute("systemId"),
                            diffgramElement.InnerText);
                        break;
                    default:
                        Debug.Assert(false, "Invalid node type.");
                        break;
                }
                Debug.Assert(newNode != null);
                newNode.Operation = XmlDiffViewOperation.Add;
                newNode.OperationId = opid;
                sourceParent.InsertChildAfter(newNode, currentPosition, false);
                currentPosition = newNode;
            }
        }
        /// <summary>
        /// Recurses through the baseline document loading the
        /// contents to the XmlDiffViewDocument object and tagging
        /// the pieces to be ignored later when the data is output.
        /// </summary>
        /// <param name="parent">Parent node</param>
        /// <param name="reader">The xml data</param>
        /// <param name="emptyElement">Node has no children</param>
        private void LoadSourceChildNodes(
            XmlDiffViewParentNode parent,
            XmlReader reader,
            bool emptyElement)
        {
            LoadState savedLoadState = this.loadState;
            this.loadState.Reset();

            // load attributes
            while (reader.MoveToNextAttribute())
            {
                XmlDiffViewAttribute attr;
                if (reader.Prefix == "xmlns" ||
                    (reader.Prefix == string.Empty &&
                    reader.LocalName == "xmlns"))
                {
                    // create new DiffView attribute
                    attr = new XmlDiffViewAttribute(
                        reader.LocalName,
                        reader.Prefix,
                        reader.NamespaceURI,
                        reader.Value);
                    if (this.ignoreNamespaces)
                    {
                        // set the output operation to be performed
                        attr.Operation = XmlDiffViewOperation.Ignore;
                    }
                }
                else
                {
                    string attrValue = this.ignoreWhitespace ? NormalizeText(reader.Value) : reader.Value;
                    attr = new XmlDiffViewAttribute(
                        reader.LocalName,
                        reader.Prefix,
                        reader.NamespaceURI,
                        attrValue);
                }
                ((XmlDiffViewElement)parent).InsertAttributeAfter(
                    attr,
                    this.loadState.LastAttribute);
                this.loadState.LastAttribute = attr;
            }

            // empty element -> return, do not load chilren
            if (emptyElement)
            {
                goto End;
            }

            // load children
            while (reader.Read())
            {
                // ignore whitespaces between nodes
                if (reader.NodeType == XmlNodeType.Whitespace)
                {
                    continue;
                }
                XmlDiffViewNode child = null;
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        bool emptyElementNode = reader.IsEmptyElement;
                        XmlDiffViewElement elem = new XmlDiffViewElement(
                            reader.LocalName,
                            reader.Prefix,
                            reader.NamespaceURI,
                            this.ignorePrefixes);
                        this.LoadSourceChildNodes(elem, reader, emptyElementNode);
                        child = elem;
                        break;
                    case XmlNodeType.Attribute:
                        string reason = "We should never get to this point, " +
                            "attributes should be read at the beginning of this method.";
                        Debug.Assert(false, reason);
                        break;
                    case XmlNodeType.Text:
                        child = new XmlDiffViewCharData((this.ignoreWhitespace) ? NormalizeText(reader.Value) : reader.Value, XmlNodeType.Text);
                        break;
                    case XmlNodeType.CDATA:
                        child = new XmlDiffViewCharData(reader.Value, XmlNodeType.CDATA);
                        break;
                    case XmlNodeType.EntityReference:
                        Debug.Assert(false, "XmlDiffViewER was thought to be dead code");

                        // child = new XmlDiffViewER(reader.Name);
                        break;
                    case XmlNodeType.Comment:
                        child = new XmlDiffViewCharData(reader.Value, XmlNodeType.Comment);
                        if (this.ignoreComments)
                        {
                            child.Operation = XmlDiffViewOperation.Ignore;
                        }
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        child = new XmlDiffViewPI(reader.Name, reader.Value);
                        if (this.ignorePI)
                        {
                            child.Operation = XmlDiffViewOperation.Ignore;
                        }
                        break;
                    case XmlNodeType.SignificantWhitespace:
                        if (reader.XmlSpace == XmlSpace.Preserve)
                        {
                            child = new XmlDiffViewCharData(reader.Value, XmlNodeType.SignificantWhitespace);
                            if (this.ignoreWhitespace)
                            {
                                child.Operation = XmlDiffViewOperation.Ignore;
                            }
                        }
                        break;
                    case XmlNodeType.XmlDeclaration:
                        child = new XmlDiffViewXmlDeclaration(NormalizeText(reader.Value));
                        if (this.ignoreXmlDecl)
                        {
                            child.Operation = XmlDiffViewOperation.Ignore;
                        }
                        break;
                    case XmlNodeType.EndElement:
                        goto End;

                    case XmlNodeType.DocumentType:
                        child = new XmlDiffViewDocumentType(
                            reader.Name,
                            reader.GetAttribute("PUBLIC"),
                            reader.GetAttribute("SYSTEM"),
                            reader.Value);
                        if (this.ignoreDtd)
                        {
                            child.Operation = XmlDiffViewOperation.Ignore;
                        }
                        break;

                    default:
                        Debug.Assert(false, "Invalid node type");
                        break;
                }
                parent.InsertChildAfter(child, this.loadState.LastChild, true);
                this.loadState.LastChild = child;
            }

            End:
            this.loadState = savedLoadState;
        }