internal void WriteTo(XmlWriter xmlWriter) { Debug.Assert(_xmlDiff._fragments != TriStateBool.DontKnown); xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement(XmlDiff.Prefix, "xmldiff", XmlDiff.NamespaceUri); xmlWriter.WriteAttributeString("version", "1.0"); xmlWriter.WriteAttributeString("srcDocHash", _xmlDiff._sourceDoc.HashValue.ToString()); xmlWriter.WriteAttributeString("options", _xmlDiff.GetXmlDiffOptionsString()); xmlWriter.WriteAttributeString("fragments", (_xmlDiff._fragments == TriStateBool.Yes) ? "yes" : "no"); WriteChildrenTo(xmlWriter, _xmlDiff); OperationDescriptor curOD = _descriptors; while (curOD != null) { curOD.WriteTo(xmlWriter); curOD = curOD._nextDescriptor; } xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); }
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 OperationDescriptor GetDescriptor(int opid) { OperationDescriptor opDesc = (OperationDescriptor)_descriptors[opid]; if (opDesc == null) { throw new Exception("Invalid operation id."); } return(opDesc); }
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; } } }
private void PreprocessDiffgram(XmlDocument diffgramDoc) { // read xmldiff options XmlAttribute attr = (XmlAttribute)diffgramDoc.DocumentElement.Attributes.GetNamedItem("options"); if (attr == null) { throw new Exception("Missing 'options' attribute in the diffgram."); } string optionsAttr = attr.Value; XmlDiffOptions options = XmlDiff.ParseOptions(optionsAttr); _bIgnoreChildOrder = (((int)options & (int)(XmlDiffOptions.IgnoreChildOrder)) > 0); _bIgnoreComments = (((int)options & (int)(XmlDiffOptions.IgnoreComments)) > 0); _bIgnorePI = (((int)options & (int)(XmlDiffOptions.IgnorePI)) > 0); _bIgnoreWhitespace = (((int)options & (int)(XmlDiffOptions.IgnoreWhitespace)) > 0); _bIgnoreNamespaces = (((int)options & (int)(XmlDiffOptions.IgnoreNamespaces)) > 0); _bIgnorePrefixes = (((int)options & (int)(XmlDiffOptions.IgnorePrefixes)) > 0); _bIgnoreDtd = (((int)options & (int)(XmlDiffOptions.IgnoreDtd)) > 0); if (_bIgnoreNamespaces) { _bIgnorePrefixes = true; } // read descriptors XmlNodeList children = diffgramDoc.DocumentElement.ChildNodes; IEnumerator e = children.GetEnumerator(); while (e.MoveNext()) { XmlElement desc = e.Current as XmlElement; if (desc != null && desc.LocalName == "descriptor") { int opid = int.Parse(desc.GetAttribute("opid")); OperationDescriptor.Type type; switch (desc.GetAttribute("type")) { case "move": type = OperationDescriptor.Type.Move; break; case "prefix change": type = OperationDescriptor.Type.PrefixChange; break; case "namespace change": type = OperationDescriptor.Type.NamespaceChange; break; default: throw new Exception("Invalid descriptor type."); } OperationDescriptor od = new OperationDescriptor(opid, type); _descriptors[opid] = od; } } }
private void PreprocessDiffgram(XmlDocument diffgramDoc) { var namedItem = (XmlAttribute)diffgramDoc.DocumentElement.Attributes.GetNamedItem("options"); if (namedItem == null) { throw new Exception("Missing 'options' attribute in the diffgram."); } var options = XmlDiff.ParseOptions(namedItem.Value); this._bIgnoreChildOrder = (options & XmlDiffOptions.IgnoreChildOrder) > XmlDiffOptions.None; this._bIgnoreComments = (options & XmlDiffOptions.IgnoreComments) > XmlDiffOptions.None; this._bIgnorePI = (options & XmlDiffOptions.IgnorePI) > XmlDiffOptions.None; this._bIgnoreWhitespace = (options & XmlDiffOptions.IgnoreWhitespace) > XmlDiffOptions.None; this._bIgnoreNamespaces = (options & XmlDiffOptions.IgnoreNamespaces) > XmlDiffOptions.None; this._bIgnorePrefixes = (options & XmlDiffOptions.IgnorePrefixes) > XmlDiffOptions.None; this._bIgnoreDtd = (options & XmlDiffOptions.IgnoreDtd) > XmlDiffOptions.None; if (this._bIgnoreNamespaces) { this._bIgnorePrefixes = true; } foreach (var childNode in diffgramDoc.DocumentElement.ChildNodes) { var xmlElement = childNode as XmlElement; if (xmlElement is XmlElement && xmlElement.LocalName == "descriptor") { var opid = int.Parse(xmlElement.GetAttribute("opid")); OperationDescriptor.Type type; switch (xmlElement.GetAttribute("type")) { case "move": type = OperationDescriptor.Type.Move; break; case "prefix change": type = OperationDescriptor.Type.PrefixChange; break; case "namespace change": type = OperationDescriptor.Type.NamespaceChange; break; default: throw new Exception("Invalid descriptor type."); } var operationDescriptor = new OperationDescriptor(opid, type); this._descriptors[opid] = operationDescriptor; } } }
private void OnRemove( XmlElement diffgramElement, XmlDiffPathNodeList matchNodes, XmlDiffViewParentNode sourceParent, ref XmlDiffViewNode currentPosition) { var op = XmlDiffViewOperation.Remove; var opid = 0; OperationDescriptor operationDescriptor = null; var attribute = diffgramElement.GetAttribute("opid"); if (attribute != string.Empty) { opid = int.Parse(attribute); operationDescriptor = this.GetDescriptor(opid); if (operationDescriptor._type == OperationDescriptor.Type.Move) { op = XmlDiffViewOperation.MoveFrom; } } if (!(diffgramElement.GetAttribute("subtree") != "no")) { if (matchNodes.Count != 1) { throw new Exception("The 'match' attribute of 'remove' element must select a single node when the 'subtree' attribute is specified."); } matchNodes.MoveNext(); var current = matchNodes.Current; this.AnnotateNode(current, op, opid, false); if (opid != 0) { operationDescriptor._nodeList.AddNode(current); } this.ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)current); } else { matchNodes.Reset(); while (matchNodes.MoveNext()) { if (opid != 0) { operationDescriptor._nodeList.AddNode(matchNodes.Current); } this.AnnotateNode(matchNodes.Current, op, opid, true); } } }
// Methods internal void AddDescriptor( OperationDescriptor desc ) { desc._nextDescriptor = _descriptors; _descriptors = desc; }
/// <summary> /// Adjust the diffgram data for the comparison options. /// </summary> /// <param name="diffgramDoc">diffgram data</param> private void PreprocessDiffgram(XmlDocument diffgramDoc) { // read xmldiff options XmlAttribute attr = (XmlAttribute) diffgramDoc.DocumentElement.Attributes.GetNamedItem("options"); if (attr == null) { throw new NullReferenceException( "Missing 'options' attribute in the diffgram."); } string optionsAttr = attr.Value; XmlDiffOptions options = ParseOptions(optionsAttr); this.ignoreChildOrder = (((int)options & (int) (XmlDiffOptions.IgnoreChildOrder)) > 0); this.ignoreComments = (((int)options & (int)(XmlDiffOptions.IgnoreComments)) > 0); this.ignorePI = (((int)options & (int)(XmlDiffOptions.IgnorePI)) > 0); this.ignoreWhitespace = (((int)options & (int)(XmlDiffOptions.IgnoreWhitespace)) > 0); this.ignoreNamespaces = (((int)options & (int)(XmlDiffOptions.IgnoreNamespaces)) > 0); this.ignorePrefixes = (((int)options & (int)(XmlDiffOptions.IgnorePrefixes)) > 0); this.ignoreDtd = (((int)options & (int)(XmlDiffOptions.IgnoreDtd)) > 0); if (this.ignoreNamespaces) { this.ignorePrefixes = true; } // read descriptors XmlNodeList children = diffgramDoc.DocumentElement.ChildNodes; IEnumerator e = children.GetEnumerator(); while (e.MoveNext()) { XmlElement desc = e.Current as XmlElement; if (desc != null && desc.LocalName == "descriptor") { int opid = ParseOpId(desc.GetAttribute("opid")); OperationDescriptor.Type type; switch (desc.GetAttribute("type")) { case "move": type = OperationDescriptor.Type.Move; break; case "prefix change": type = OperationDescriptor.Type.PrefixChange; break; case "namespace change": type = OperationDescriptor.Type.NamespaceChange; break; default: throw new ArgumentException( "Invalid descriptor type."); } OperationDescriptor od = new OperationDescriptor( opid, type); // save this change operation in the hashtable. this.descriptors[opid] = od; } } }
// Methods internal void AddDescriptor(OperationDescriptor desc) { desc._nextDescriptor = _descriptors; _descriptors = desc; }