Skip to content

Commit 817299a

Browse files
committed
fix(XMLPatch): fixed replace operation for elements
refactor(XMLPatch): enhance logging for add, replace, and remove operations in XMLPatch docs: updated Changelog in README's
1 parent ea146cd commit 817299a

File tree

4 files changed

+112
-16
lines changed

4 files changed

+112
-16
lines changed

Docs/README.egosoft

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,40 @@ To create such debug file please use the [b]--log-to-file[/b] option.
245245

246246
There is a MIT license for this tool. You can find it in the [url=https://github.com/chemodun/x4_XMLDiffAndPatch/raw/main//LICENSE]LICENSE[/url] file.
247247

248+
[size=130][u]Changelog[/u][/size]
249+
250+
[size=125][u][0.2.16] - 2025-02-25[/u][/size]
251+
[list]
252+
[*] Fixed
253+
[list]
254+
[*] Fixed issue with element replacements
255+
[/list]
256+
[*] Improved
257+
[list]
258+
[*] Logging information
259+
[/list]
260+
[/list]
261+
[size=125][u][0.2.15] - 2025-02-24[/u][/size]
262+
[list]
263+
[*] Fixed
264+
[list]
265+
[*] Fixed loading the diff.xsd
266+
[*] Fixed issue, if resulting file has to be located in current folder
267+
[/list]
268+
[*] Improved
269+
[/list] Logging information, especially about wrong sel value. More info logged about processed XML elements.
270+
[list][*] Added
271+
[list]
272+
[*] Possibility to append into existing debug log
273+
[/list]
274+
[/list]
275+
[size=125][u][0.2.14] - 2025-01-17[/u][/size]
276+
[list]
277+
[*] Added
278+
[list]
279+
[*] First public version coded in C#.
280+
[/list]
281+
[/list]
248282
[size=130][u]Additional links[/u][/size]
249283

250284
There is a topic on the [url=https://forum.egosoft.com/viewtopic.php?t=468623]EGOSOFT forum[/url], related to this toolset.

Docs/README.nexus

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,40 @@ To create such debug file please use the [b]--log-to-file[/b] option.
245245

246246
There is a MIT license for this tool. You can find it in the [url=https://github.com/chemodun/x4_XMLDiffAndPatch/raw/main//LICENSE]LICENSE[/url] file.
247247

248+
[size=3][u][b]Changelog[/b][/u][/size]
249+
250+
[size=3][u][i][0.2.16] - 2025-02-25[/i][/u][/size]
251+
[list]
252+
[*] Fixed
253+
[list]
254+
[*] Fixed issue with element replacements
255+
[/list]
256+
[*] Improved
257+
[list]
258+
[*] Logging information
259+
[/list]
260+
[/list]
261+
[size=3][u][i][0.2.15] - 2025-02-24[/i][/u][/size]
262+
[list]
263+
[*] Fixed
264+
[list]
265+
[*] Fixed loading the diff.xsd
266+
[*] Fixed issue, if resulting file has to be located in current folder
267+
[/list]
268+
[*] Improved
269+
[/list] Logging information, especially about wrong sel value. More info logged about processed XML elements.
270+
[list][*] Added
271+
[list]
272+
[*] Possibility to append into existing debug log
273+
[/list]
274+
[/list]
275+
[size=3][u][i][0.2.14] - 2025-01-17[/i][/u][/size]
276+
[list]
277+
[*] Added
278+
[list]
279+
[*] First public version coded in C#.
280+
[/list]
281+
[/list]
248282
[size=3][u][b]Additional links[/b][/u][/size]
249283

250284
There is a topic on the [url=https://forum.egosoft.com/viewtopic.php?t=468623]EGOSOFT forum[/url], related to this toolset.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ To create such debug file please use the `--log-to-file` option.
243243

244244
There is a MIT license for this tool. You can find it in the [LICENSE](LICENSE) file.
245245

246+
## Changelog
247+
248+
### [0.2.16] - 2025-02-25
249+
250+
- Fixed
251+
- Fixed issue with element replacements
252+
- Improved
253+
- Logging information
254+
255+
### [0.2.15] - 2025-02-24
256+
257+
- Fixed
258+
- Fixed loading the diff.xsd
259+
- Fixed issue, if resulting file has to be located in current folder
260+
- Improved
261+
Logging information, especially about wrong sel value. More info logged about processed XML elements.
262+
- Added
263+
- Possibility to append into existing debug log
264+
265+
### [0.2.14] - 2025-01-17
266+
267+
- Added
268+
- First public version coded in C#.
269+
246270
## Additional links
247271

248272
There is a topic on the [EGOSOFT forum](https://forum.egosoft.com/viewtopic.php?t=468623), related to this toolset.

XMLPatch/Program.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ private static int DetectIndentation(string xmlPath)
345345
private static void ApplyAdd(XElement addElement, XElement originalRoot)
346346
{
347347
string? sel = addElement.Attribute("sel")?.Value;
348+
Logger.Debug($"Applying add operation: '{sel}'");
348349
if (sel == null)
349350
{
350351
Logger.Warn("Add operation missing 'sel' attribute! Skipping operation.");
@@ -357,7 +358,7 @@ private static void ApplyAdd(XElement addElement, XElement originalRoot)
357358
pos = "append";
358359
}
359360

360-
Logger.Debug($"Applying add operation: '{sel}' at {pos!}");
361+
Logger.Info($"Applying add operation: '{sel}' at {pos!}");
361362

362363
var targetElements = originalRoot.XPathSelectElements(sel);
363364
if (targetElements == null || !targetElements.Any())
@@ -428,6 +429,7 @@ private static void ApplyAdd(XElement addElement, XElement originalRoot)
428429
private static void ApplyReplace(XElement replaceElement, XElement originalRoot)
429430
{
430431
string? sel = replaceElement.Attribute("sel")?.Value;
432+
Logger.Info($"Applying replace operation: '{sel}'");
431433
if (sel == null)
432434
{
433435
Logger.Warn("Replace operation missing 'sel' attribute! Skipping operation.");
@@ -447,38 +449,39 @@ private static void ApplyReplace(XElement replaceElement, XElement originalRoot)
447449
{
448450
if (targetObj is XElement target)
449451
{
450-
var newContent = replaceElement.Value;
452+
string targetName = target.Name.LocalName;
453+
XElement? replaceSubElement = replaceElement.Element(targetName);
454+
XElement? parent = target.Parent;
451455
string targetInfo = GetElementInfo(target);
452-
if (!string.IsNullOrEmpty(newContent))
456+
string parentInfo = GetElementInfo(parent);
457+
if (replaceSubElement != null)
453458
{
454-
target.Value = newContent;
455-
Logger.Debug($"Replaced text of element '{targetInfo}' with '{newContent}'.");
459+
string replaceInfo = GetElementInfo(replaceSubElement);
460+
target.ReplaceWith(replaceSubElement);
461+
Logger.Info($"Replaced element '{targetInfo}' with '{replaceInfo}' in '{parentInfo}'.");
456462
}
457-
458-
var newElement = replaceElement.Element("new");
459-
if (newElement != null)
463+
else
460464
{
461-
XElement replacement = new XElement(newElement);
462-
target.ReplaceWith(replacement);
463-
Logger.Info($"Replaced element '{targetInfo}' with '{GetElementInfo(replacement)}'.");
465+
Logger.Warn($"Can't process replacement for '{targetInfo}' in '{parentInfo}'. Skipping operation.");
464466
}
465467
}
466468
else if (targetObj is XText textNode)
467469
{
468470
textNode.Value = replaceElement.Value;
469-
Logger.Debug("Replaced text node.");
471+
Logger.Info("Replaced text node.");
470472
}
471473
else if (targetObj is XAttribute attr)
472474
{
473475
attr.Value = replaceElement.Value;
474-
Logger.Debug($"Replaced value of attribute '{attr.Name}' with '{replaceElement.Value}'.");
476+
Logger.Info($"Replaced value of attribute '{attr.Name}' with '{replaceElement.Value}'.");
475477
}
476478
}
477479
}
478480

479481
private static void ApplyRemove(XElement removeElement, XElement originalRoot)
480482
{
481483
string? sel = removeElement.Attribute("sel")?.Value;
484+
Logger.Debug($"Applying remove operation: '{sel}'");
482485
if (sel == null)
483486
{
484487
Logger.Warn("Remove operation missing 'sel' attribute! Skipping operation.");
@@ -499,14 +502,15 @@ private static void ApplyRemove(XElement removeElement, XElement originalRoot)
499502
if (targetObj is XElement target)
500503
{
501504
XElement? parent = target.Parent;
505+
string parentInfo = GetElementInfo(parent);
502506
string targetInfo = GetElementInfo(target);
503507
if (parent == null)
504508
{
505509
Logger.Warn($"Element '{targetInfo}' has no parent. Cannot remove.");
506510
continue;
507511
}
508512
target.Remove();
509-
Logger.Debug($"Removed element '{targetInfo}' from '{parent.Name}'.");
513+
Logger.Info($"Removed element '{targetInfo}' from '{parentInfo}'.");
510514
}
511515
else if (targetObj is XAttribute attr)
512516
{
@@ -518,12 +522,12 @@ private static void ApplyRemove(XElement removeElement, XElement originalRoot)
518522
continue;
519523
}
520524
attr.Remove();
521-
Logger.Debug($"Removed attribute '{attr.Name}' from '{parentInfo}'.");
525+
Logger.Info($"Removed attribute '{attr.Name}' from '{parentInfo}'.");
522526
}
523527
else if (targetObj is XText textNode)
524528
{
525529
textNode.Remove();
526-
Logger.Debug("Removed text node.");
530+
Logger.Info("Removed text node.");
527531
}
528532
}
529533
}

0 commit comments

Comments
 (0)