private DiffResult PerformDiff(TextReader reader1, TextReader reader2) { _xmlDiff = new SIL.Lift.Merging.xmldiff.XmlDiff(reader1, reader2); DiffResult result = _xmlDiff.Compare(); return(result); }
[Test] public void SameResultForTwoInvocations() { TextReader reader = new StringReader("<empty/>"); DiffResult result1 = PerformDiff(reader, reader); DiffResult result2 = _xmlDiff.Compare(); Assert.AreSame(result1, result2); }
private DiffResult PerformDiff(TextReader reader1, TextReader reader2) { _xmlDiff = new SIL.Lift.Merging.xmldiff.XmlDiff(reader1, reader2); DiffResult result = _xmlDiff.Compare(); return result; }
private void ComputeDiff(TextReader original, TextReader modified, IProgress progress) { //enchance: just get a checksum for each entry in both files, use that to determine everything else _idsOfDeletedEntries = new List<string>(); _idsOfAddedEntries = new List<string>(); _idsOfEditedEntries = new List<string>(); _idsInOriginal = new List<string>(); XPathDocument modifiedDoc = new XPathDocument(modified); XPathNavigator modifiedNav = modifiedDoc.CreateNavigator(); XPathDocument originalDoc = new XPathDocument(original); XPathNavigator originalNav = originalDoc.CreateNavigator(); XPathNodeIterator liftElement = originalNav.SelectChildren(XPathNodeType.Element); liftElement.MoveNext();//move to the one and only <lift> element if (liftElement.Current == null) return; XPathNodeIterator originalChildren = liftElement.Current.SelectChildren(XPathNodeType.Element); StringDictionary idToContentsOfModifiedEntries = new StringDictionary(); XPathNodeIterator liftOfModifiedFile = modifiedNav.SelectChildren(XPathNodeType.Element); liftOfModifiedFile.MoveNext(); if (liftOfModifiedFile.Current != null) { XPathNodeIterator modifiedChildren = liftOfModifiedFile.Current.SelectChildren(XPathNodeType.Element); while (modifiedChildren.MoveNext()) { //TODO: consider if there are benefits to using guid as the first key, then try id if (modifiedChildren.Current != null) { string id = modifiedChildren.Current.GetAttribute("id", string.Empty); idToContentsOfModifiedEntries.Add(id, modifiedChildren.Current.OuterXml); } } } while (originalChildren.MoveNext()) { if (originalChildren.Current != null) { string id = originalChildren.Current.GetAttribute("id", string.Empty); _idsInOriginal.Add(id); if(!idToContentsOfModifiedEntries.ContainsKey(id)) { _idsOfDeletedEntries.Add(id); } else { var diff = new XmlDiff(originalChildren.Current.OuterXml, idToContentsOfModifiedEntries[id]); DiffResult result = diff.Compare(); if (!result.AreEqual) { _idsOfEditedEntries.Add(id); } } } } foreach (string id in idToContentsOfModifiedEntries.Keys) { if(!_idsInOriginal.Contains(id)) _idsOfAddedEntries.Add(id); } }
/// <summary> /// Check whether the two XML elements are equal. /// </summary> public static bool AreXmlElementsEqual(XmlNode ours, XmlNode theirs) { if (ours.NodeType == XmlNodeType.Text) { if (ours.NodeType != XmlNodeType.Text) { return false; } bool oursIsEmpty = (ours.InnerText.Trim() == string.Empty); bool theirsIsEmpty = (theirs.InnerText.Trim() == string.Empty); if(oursIsEmpty != theirsIsEmpty) { return false; } return ours.InnerText.Trim() == theirs.InnerText.Trim(); } // DiffConfiguration config = new DiffConfiguration(WhitespaceHandling.None); var diff = new XmlDiff(new XmlInput(ours.OuterXml), new XmlInput(theirs.OuterXml));//, config); DiffResult d = diff.Compare(); return (d == null || d.Difference == null || !d.Difference.HasMajorDifference); }