Skip to content

Commit 0b81ee8

Browse files
committed
refactor(XMLDiff): update logging to use Info level for operations and improve clarity
refactor(XMLDiff): if difference in one attribute, but there is one attribute in element - it will be marked as totally differ refactor(XMLDiff): if difference in one attribute when them more then one, and all inherited elements are equal - it will be marked as change of attribute
1 parent bb2a720 commit 0b81ee8

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

XMLDiff/Program.cs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private static bool CompareElements(
454454
}
455455
XElement replaceOp = new XElement("replace", new XAttribute("sel", sel), modifiedText);
456456
diffRoot.Add(replaceOp);
457-
Logger.Debug($"Replaced text in element '{GetElementInfo(originalElem)}' from '{originalText}' to '{modifiedText}'.");
457+
Logger.Info($"[Operation Replace] Text in element '{GetElementInfo(originalElem)}' from '{originalText}' to '{modifiedText}'.");
458458
}
459459
else
460460
{
@@ -465,7 +465,7 @@ private static bool CompareElements(
465465
}
466466
XElement removeOp = new XElement("remove", new XAttribute("sel", $"{sel}/text()"));
467467
diffRoot.Add(removeOp);
468-
Logger.Debug($"Removed text from element '{GetElementInfo(originalElem)}'.");
468+
Logger.Info($"[Operation Remove] Text from element '{GetElementInfo(originalElem)}'.");
469469
}
470470
}
471471
}
@@ -527,8 +527,9 @@ private static bool CompareElements(
527527
{
528528
Logger.Debug($"Original attributes does not contain key '{attr.Key}'.");
529529
differencesInAttributesCount++;
530-
if (differencesInAttributesCount > 1)
530+
if (differencesInAttributesCount > 1 || modifiedAttributes.Count == 1)
531531
{
532+
differencesInAttributesCount++;
532533
matchedEnough = false;
533534
break;
534535
}
@@ -542,8 +543,9 @@ private static bool CompareElements(
542543
$"Original attributes value '{originalAttributes[attr.Key]}' does not match with modified attributes value '{attr.Value}'."
543544
);
544545
differencesInAttributesCount++;
545-
if (differencesInAttributesCount > 1)
546+
if (differencesInAttributesCount > 1 || originalAttributes.Count == 1)
546547
{
548+
differencesInAttributesCount++;
547549
matchedEnough = false;
548550
break;
549551
}
@@ -587,24 +589,12 @@ private static bool CompareElements(
587589
matchedEnough = false;
588590
if (!CompareElements(original, modified, diffRoot, pathOptions, originalChild, modifiedChild, true))
589591
{
590-
bool nextMatched = true;
591-
if (i + 1 < originalChildren.Count && j + 1 < modifiedChildren.Count)
592+
if (savedOp != null)
592593
{
593-
XElement originalTemp = new XElement("temp");
594-
originalTemp.Add(originalChildren[i + 1]);
595-
XElement modifiedTemp = new XElement("temp");
596-
modifiedTemp.Add(modifiedChildren[j + 1]);
597-
nextMatched = !CompareElements(original, modified, diffRoot, pathOptions, originalTemp, modifiedTemp, true);
598-
}
599-
if (nextMatched)
600-
{
601-
if (savedOp != null)
602-
{
603-
diffRoot.Add(savedOp);
604-
Logger.Debug($"Added the saved operation to the diff.");
605-
}
606-
matchedEnough = true;
594+
diffRoot.Add(savedOp);
595+
Logger.Info($"[Operation {savedOp.Name}] Added the saved operation to the diff.");
607596
}
597+
matchedEnough = true;
608598
}
609599
}
610600
Logger.Debug($"Matched enough: {matchedEnough}, i: {i}, j: {j}");
@@ -640,6 +630,7 @@ private static bool CompareElements(
640630
var nextModifiedChild = modifiedChildren[k];
641631
if (
642632
originalChild.Name == nextModifiedChild.Name
633+
&& originalChild.Attributes().Count() == nextModifiedChild.Attributes().Count()
643634
&& originalChild.Attributes().All(attr => nextModifiedChild.Attribute(attr.Name)?.Value == attr.Value)
644635
)
645636
{
@@ -659,6 +650,7 @@ private static bool CompareElements(
659650
}
660651
}
661652
addOp = new XElement("add", new XAttribute("sel", xpath), new XAttribute("pos", pos));
653+
Logger.Info($"[Operation Add] Element '{GetElementInfo(originalChild)}' to parent '{GetElementInfo(originalElem)}'.");
662654
}
663655
else
664656
{
@@ -672,7 +664,7 @@ private static bool CompareElements(
672664
{
673665
var addedChild = modifiedChildren[l];
674666
addOp.Add(addedChild);
675-
Logger.Debug($"Added element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
667+
Logger.Info($"[Operation Add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
676668
}
677669
diffRoot.Add(addOp);
678670
j = k;
@@ -691,14 +683,15 @@ private static bool CompareElements(
691683
|| (i + 1 < originalChildren.Count)
692684
&& (j + 1 < modifiedChildren.Count)
693685
&& originalChildren[i + 1].Name == modifiedChildren[j + 1].Name
686+
&& originalChildren[i + 1].Attributes().Count() == modifiedChildren[j + 1].Attributes().Count()
694687
&& originalChildren[i + 1].Attributes().All(attr => modifiedChildren[j + 1].Attribute(attr.Name)?.Value == attr.Value)
695688
)
696689
)
697690
{
698691
string sel = GenerateXPath(originalChild, pathOptions);
699692
XElement replaceOp = new XElement("replace", new XAttribute("sel", sel), modifiedChild);
700693
diffRoot.Add(replaceOp);
701-
Logger.Debug($"Replaced element '{GetElementInfo(originalChild)}' with '{GetElementInfo(modifiedChild)}'.");
694+
Logger.Info($"[Operation replace] Element '{GetElementInfo(originalChild)}' with '{GetElementInfo(modifiedChild)}'.");
702695
i++;
703696
j++;
704697
}
@@ -707,7 +700,7 @@ private static bool CompareElements(
707700
string sel = GenerateXPath(originalChild, pathOptions);
708701
XElement removeOp = new XElement("remove", new XAttribute("sel", sel));
709702
diffRoot.Add(removeOp);
710-
Logger.Debug($"Removed element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
703+
Logger.Info($"[Operation remove] Element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
711704
i++;
712705
}
713706
}
@@ -720,7 +713,7 @@ private static bool CompareElements(
720713
string sel = GenerateXPath(originalChild, pathOptions);
721714
XElement removeOp = new XElement("remove", new XAttribute("sel", sel));
722715
diffRoot.Add(removeOp);
723-
Logger.Debug($"Removed element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
716+
Logger.Info($"[Operation remove] Element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
724717
i++;
725718
}
726719

@@ -732,7 +725,7 @@ private static bool CompareElements(
732725
{
733726
var addedChild = modifiedChildren[j];
734727
addOp.Add(addedChild);
735-
Logger.Debug($"Added element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
728+
Logger.Info($"[Operation add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
736729
j++;
737730
}
738731
diffRoot.Add(addOp);

0 commit comments

Comments
 (0)