private void OnRemove(XmlElement diffgramElement, XmlDiffPathNodeList matchNodes, XmlDiffViewParentNode sourceParent, ref XmlDiffViewNode currentPosition) { // opid & descriptor XmlDiffViewOperation op = XmlDiffViewOperation.Remove; int opid = 0; OperationDescriptor opDesc = null; string opidAttr = diffgramElement.GetAttribute("opid"); if (opidAttr != string.Empty) { opid = int.Parse(opidAttr); opDesc = GetDescriptor(opid); if (opDesc._type == OperationDescriptor.Type.Move) { op = XmlDiffViewOperation.MoveFrom; } } // subtree string subtreeAttr = diffgramElement.GetAttribute("subtree"); bool bSubtree = (subtreeAttr != "no"); if (!bSubtree) { if (matchNodes.Count != 1) { throw new Exception("The 'match' attribute of 'remove' element must select a single node when the 'subtree' attribute is specified."); } // annotate node matchNodes.MoveNext(); XmlDiffViewNode node = matchNodes.Current; AnnotateNode(node, op, opid, false); if (opid != 0) { opDesc._nodeList.AddNode(node); } // recurse ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)node); } else { // annotate nodes matchNodes.Reset(); while (matchNodes.MoveNext()) { if (opid != 0) { opDesc._nodeList.AddNode(matchNodes.Current); } AnnotateNode(matchNodes.Current, op, opid, true); } } }
private void OnAddMatch(XmlElement diffgramElement, XmlDiffPathNodeList matchNodes, XmlDiffViewParentNode sourceParent, ref XmlDiffViewNode currentPosition) { string opidAttr = diffgramElement.GetAttribute("opid"); if (opidAttr == string.Empty) { throw new Exception("Missing opid attribute."); } // opid & descriptor int opid = int.Parse(opidAttr); OperationDescriptor opDesc = GetDescriptor(opid); string subtreeAttr = diffgramElement.GetAttribute("subtree"); bool bSubtree = (subtreeAttr != "no"); // move single node without subtree if (!bSubtree) { if (matchNodes.Count != 1) { throw new Exception("The 'match' attribute of 'add' element must select a single node when the 'subtree' attribute is specified."); } // clone node matchNodes.MoveNext(); XmlDiffViewNode newNode = matchNodes.Current.Clone(false); AnnotateNode(newNode, XmlDiffViewOperation.MoveTo, opid, true); opDesc._nodeList.AddNode(newNode); // insert in tree sourceParent.InsertChildAfter(newNode, currentPosition, false); currentPosition = newNode; // recurse ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)newNode); } // move subtree else { matchNodes.Reset(); while (matchNodes.MoveNext()) { XmlDiffViewNode newNode = matchNodes.Current.Clone(true); AnnotateNode(newNode, XmlDiffViewOperation.MoveTo, opid, true); opDesc._nodeList.AddNode(newNode); sourceParent.InsertChildAfter(newNode, currentPosition, false); currentPosition = newNode; } } }
static XmlDiffPathNodeList SelectAttributes(XmlDiffViewElement parentElement, string path) { Debug.Assert(path[0] == '@'); int pos = 1; XmlDiffPathNodeList nodeList = null; //NCOVER DOESN'T LIKE: for (;;) while (true) { string name = ReadAttrName(path, ref pos); if (nodeList == null) { if (pos == path.Length) { nodeList = new XmlDiffPathSingleNodeList(); } else { nodeList = new XmlDiffPathMultiNodeList(); } } XmlDiffViewAttribute attr = parentElement.GetAttribute(name); if (attr == null) { OnNoMatchingNode(path); } nodeList.AddNode(attr); if (pos == path.Length) { break; } else if (path[pos] == '|') { pos++; if (path[pos] != '@') { OnInvalidExpression(path); } pos++; } else { OnInvalidExpression(path); } } return(nodeList); }
private void OnChange(XmlElement diffgramElement, XmlDiffPathNodeList matchNodes, XmlDiffViewParentNode sourceParent, ref XmlDiffViewNode currentPosition) { Debug.Assert(matchNodes.Count == 1); matchNodes.Reset(); matchNodes.MoveNext(); XmlDiffViewNode node = matchNodes.Current; if (node._nodeType != XmlNodeType.Attribute) { currentPosition = node; } XmlDiffViewNode.ChangeInfo changeInfo = new XmlDiffViewNode.ChangeInfo(); string name = diffgramElement.HasAttribute("name") ? diffgramElement.GetAttribute("name") : null; string prefix = diffgramElement.HasAttribute("prefix") ? diffgramElement.GetAttribute("prefix") : null; string ns = diffgramElement.HasAttribute("ns") ? diffgramElement.GetAttribute("ns") : null; switch (node._nodeType) { case XmlNodeType.Element: changeInfo._localName = (name == null) ? ((XmlDiffViewElement)node)._localName : name; changeInfo._prefix = (prefix == null) ? ((XmlDiffViewElement)node)._prefix : prefix; changeInfo._ns = (ns == null) ? ((XmlDiffViewElement)node)._ns : ns; break; case XmlNodeType.Attribute: string value = diffgramElement.InnerText; if (name == string.Empty && prefix == string.Empty && value == string.Empty) { return; } changeInfo._localName = (name == null) ? ((XmlDiffViewAttribute)node)._localName : name; changeInfo._prefix = (prefix == null) ? ((XmlDiffViewAttribute)node)._prefix : prefix; changeInfo._ns = (ns == null) ? ((XmlDiffViewAttribute)node)._ns : ns; changeInfo._value = diffgramElement.InnerText; break; case XmlNodeType.Text: case XmlNodeType.CDATA: Debug.Assert(diffgramElement.FirstChild != null); changeInfo._value = diffgramElement.InnerText; break; case XmlNodeType.Comment: Debug.Assert(diffgramElement.FirstChild != null); Debug.Assert(diffgramElement.FirstChild.NodeType == XmlNodeType.Comment); changeInfo._value = diffgramElement.FirstChild.Value; break; case XmlNodeType.ProcessingInstruction: if (name == null) { Debug.Assert(diffgramElement.FirstChild != null); Debug.Assert(diffgramElement.FirstChild.NodeType == XmlNodeType.ProcessingInstruction); changeInfo._localName = diffgramElement.FirstChild.Name; changeInfo._value = diffgramElement.FirstChild.Value; } else { changeInfo._localName = name; changeInfo._value = ((XmlDiffViewPI)node)._value; } break; case XmlNodeType.EntityReference: Debug.Assert(name != null); changeInfo._localName = name; break; case XmlNodeType.XmlDeclaration: Debug.Assert(diffgramElement.FirstChild != null); changeInfo._value = diffgramElement.InnerText; break; case XmlNodeType.DocumentType: changeInfo._localName = (name == null) ? ((XmlDiffViewDocumentType)node)._name : name; if (diffgramElement.HasAttribute("publicId")) { changeInfo._prefix = diffgramElement.GetAttribute("publicId"); } else { changeInfo._prefix = ((XmlDiffViewDocumentType)node)._publicId; } if (diffgramElement.HasAttribute("systemId")) { changeInfo._ns = diffgramElement.GetAttribute("systemId"); } else { changeInfo._ns = ((XmlDiffViewDocumentType)node)._systemId; } if (diffgramElement.FirstChild != null) { changeInfo._value = diffgramElement.InnerText; } else { changeInfo._value = ((XmlDiffViewDocumentType)node)._subset; } break; default: Debug.Assert(false, "Invalid node type."); break; } node._changeInfo = changeInfo; node._op = XmlDiffViewOperation.Change; string opidAttr = diffgramElement.GetAttribute("opid"); if (opidAttr != string.Empty) { node._opid = int.Parse(opidAttr); } if (node._nodeType == XmlNodeType.Element && diffgramElement.FirstChild != null) { ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)node); } }
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; } } }
static XmlDiffPathNodeList SelectChildNodes(XmlDiffViewParentNode parentNode, string path, int startPos) { int pos = startPos; XmlDiffPathNodeList nodeList = null; //NCOVER DOESN'T LIKE: for (;;) while (true) { int nodePos = ReadPosition(path, ref pos); if (pos == path.Length) { nodeList = new XmlDiffPathSingleNodeList(); } else { nodeList = new XmlDiffPathMultiNodeList(); } if (nodePos <= 0 || nodePos > parentNode._sourceChildNodesCount) { OnNoMatchingNode(path); } nodeList.AddNode(parentNode.GetSourceChildNode(nodePos - 1)); if (pos == path.Length) { break; } else if (path[pos] == '|') { pos++; } else if (path[pos] == '-') { pos++; int endNodePos = ReadPosition(path, ref pos); if (endNodePos <= 0 || endNodePos > parentNode._sourceChildNodesCount) { OnNoMatchingNode(path); } while (nodePos < endNodePos) { nodePos++; nodeList.AddNode(parentNode.GetSourceChildNode(nodePos - 1)); } if (pos == path.Length) { break; } else if (path[pos] == '|') { pos++; } else { OnInvalidExpression(path); } } } return(nodeList); }