/Patchers/XML.cs |
@@ -13,29 +13,45 @@ |
{ |
public static class XML |
{ |
public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> forceXPaths, |
HashSet<string> excludeXPaths) |
public static HashSet<string> GetFileDelta(string cfg, string nfg) |
{ |
// Select all distinct paths in the new configuration without paths to exclude and with paths to force. |
foreach (var e in nfg |
.Descendants() |
.Where(e => !excludeXPaths.Any(o => e.GetAbsoluteXPath().Contains(o))) |
.Concat( |
nfg |
.Descendants() |
.Where(e => forceXPaths.Any(o => e.GetAbsoluteXPath().Contains(o)))) |
.Distinct()) |
var configuredTags = new HashSet<string>(); |
|
var configuration = XDocument.Load(cfg); |
foreach (var e in XDocument.Load(nfg).Descendants()) |
{ |
// Get the XPath to this element. |
var xpath = e.GetAbsoluteXPath(); |
var cfgElement = configuration.XPathSelectElement(e.GetAbsoluteXPath()); |
if (cfgElement == null) |
{ |
configuredTags.Add(e.Name.LocalName); |
continue; |
} |
|
if (e.Descendants().Any()) |
continue; |
|
if (!cfgElement.Value.Equals(e.Value)) |
continue; |
|
configuredTags.Add(e.Name.LocalName); |
} |
|
return configuredTags; |
} |
|
public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> configuredTags) |
{ |
foreach (var e in nfg.Descendants() |
.Where(e => configuredTags.Contains(e.Name.LocalName))) |
{ |
// Select the element in the current configuration that is found in the default configuration. |
var cfgElement = cfg.XPathSelectElement(xpath); |
var cfgElement = cfg.XPathSelectElement(e.GetAbsoluteXPath()); |
switch (cfgElement != null) |
{ |
// Element found in current configuration. |
case true: |
// Only set the element value if it has no descendants and it contained in the force paths. |
if (!cfgElement.Descendants().Any() && forceXPaths.Any(o => xpath.Contains(o))) |
// Only set the element value if it has no descendants. |
if (!cfgElement.Descendants().Any()) |
cfgElement.Value = e.Value; |
break; |
// Element not found in the current configuration. |
@@ -48,7 +64,7 @@ |
if (cfgParentElement != null) |
{ |
// Add the default configuration parent to the current configuration. |
cfgParentElement.Add( |
cfgParentElement.ReplaceWith( |
nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath())); |
break; |
} |