Skip to content

Commit e3b0726

Browse files
committed
refactor(diff): get rid of own indentation, it is in-place
fix(diff): fix XPath generation
1 parent 4447837 commit e3b0726

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

XMLDiff/Program.cs

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ private static void ProcessSingleFile(string originalXmlPath, string modifiedXml
211211
XDocument modifiedDoc = XDocument.Load(modifiedXmlPath);
212212
Logger.Info($"Parsed modified XML: {modifiedXmlPath}");
213213

214-
string indentStr = DetectIndentation(originalXmlPath);
215-
Logger.Info($"Detected indentation: {ConsoleEscape(indentStr)}");
216-
217-
XElement diffRoot = GenerateDiff(originalDoc, modifiedDoc, indentStr);
214+
XElement diffRoot = GenerateDiff(originalDoc, modifiedDoc);
218215

219216
if (!diffRoot.HasElements)
220217
{
@@ -223,7 +220,6 @@ private static void ProcessSingleFile(string originalXmlPath, string modifiedXml
223220
}
224221

225222
XDocument diffDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), diffRoot);
226-
IndentXml(diffDoc, indentStr);
227223

228224

229225
diffDoc.Save(diffXmlPath);
@@ -317,33 +313,26 @@ private static string DetectIndentation(string xmlPath)
317313

318314
#region Diff Generation
319315

320-
private static XElement GenerateDiff(XDocument original, XDocument modified, string indentStr)
316+
private static XElement GenerateDiff(XDocument original, XDocument modified)
321317
{
322318
XElement diffRoot = new XElement("diff");
323319
if (original.Root == null || modified.Root == null)
324320
{
325321
Logger.Error("Original or modified XML does not have a root element.");
326322
return diffRoot;
327323
}
328-
CompareElements(original, modified, diffRoot, indentStr);
324+
CompareElements(original, modified, diffRoot);
329325
return diffRoot;
330326
}
331327

332-
private static bool CompareElements(XDocument original, XDocument modified, XElement diffRoot, string indentStr, XElement ?originalElem = null, XElement ?modifiedElem = null, bool checkOnly = false)
328+
private static bool CompareElements(XDocument original, XDocument modified, XElement diffRoot, XElement ?originalElem = null, XElement ?modifiedElem = null, bool checkOnly = false)
333329
{
334330
if (originalElem != null && modifiedElem != null)
335331
{
336332
if (originalElem.Name != modifiedElem.Name)
337333
{
338-
if (!checkOnly) {
339-
string sel = GenerateXPath(originalElem, original.Root);
340-
XElement replaceOp = new XElement("replace",
341-
new XAttribute("sel", sel),
342-
modifiedElem
343-
);
344-
diffRoot.Add(replaceOp);
345-
Logger.Debug($"Replaced element '{originalElem.Name}' with '{modifiedElem.Name}'.");
346-
}
334+
// Process can be there only in case of changes detection, not for the real diff generation
335+
Logger.Error($"Element names do not match: {originalElem.Name} vs {modifiedElem.Name}");
347336
return true;
348337
}
349338

@@ -470,14 +459,14 @@ private static bool CompareElements(XDocument original, XDocument modified, XEle
470459
return true;
471460
}
472461
matchedEnough = false;
473-
if (! CompareElements(original, modified, diffRoot, indentStr, originalChild, modifiedChild, true)) {
462+
if (! CompareElements(original, modified, diffRoot, originalChild, modifiedChild, true)) {
474463
bool nextMatched = true;
475464
if ( i + 1 < originalChildren.Count && j + 1 < modifiedChildren.Count) {
476465
XElement originalTemp = new XElement("temp");
477466
originalTemp.Add(originalChildren[i + 1]);
478467
XElement modifiedTemp = new XElement("temp");
479468
modifiedTemp.Add(modifiedChildren[j + 1]);
480-
nextMatched = ! CompareElements(original, modified, diffRoot, indentStr, originalTemp, modifiedTemp, true);
469+
nextMatched = ! CompareElements(original, modified, diffRoot, originalTemp, modifiedTemp, true);
481470
}
482471
if (nextMatched) {
483472
if (savedOp != null) {
@@ -490,7 +479,7 @@ private static bool CompareElements(XDocument original, XDocument modified, XEle
490479
}
491480
Logger.Debug($"Matched enough: {matchedEnough}, i: {i}, j: {j}");
492481
if (matchedEnough) {
493-
if (CompareElements(original, modified, diffRoot, indentStr, originalChild, modifiedChild, checkOnly))
482+
if (CompareElements(original, modified, diffRoot, originalChild, modifiedChild, checkOnly))
494483
{
495484
if (checkOnly) {
496485
return true;
@@ -516,8 +505,8 @@ private static bool CompareElements(XDocument original, XDocument modified, XEle
516505
{
517506
var addedChild = modifiedChildren[l];
518507
XElement addOp = new XElement("add",
519-
new XAttribute("sel", GenerateXPath(addedChild, original.Root)),
520-
new XAttribute("pos", "after"),
508+
new XAttribute("sel", GenerateXPath(originalChild, originalChild.Document.Root)),
509+
new XAttribute("pos", "before"),
521510
addedChild
522511
);
523512
diffRoot.Add(addOp);
@@ -580,7 +569,7 @@ private static string GenerateXPath(XElement element, XElement? root)
580569

581570
var path = new System.Text.StringBuilder();
582571
XElement? current = element;
583-
while (current != null && current != root)
572+
while (current != null)
584573
{
585574
string step = current.Name.LocalName;
586575
var siblings = current.Parent?.Elements(current.Name.LocalName).ToList();
@@ -630,16 +619,6 @@ private static string GenerateXPath(XElement element, XElement? root)
630619

631620
#endregion
632621

633-
#region XML Indentation
634-
635-
private static void IndentXml(XDocument doc, string indentStr)
636-
{
637-
// Currently, XDocument.Save with SaveOptions.None preserves indentation
638-
// Custom indentation can be implemented if necessary
639-
}
640-
641-
#endregion
642-
643622
#region Diff Validation
644623

645624
private static bool ValidateXsdPath(string? xsdPath)

0 commit comments

Comments
 (0)