wasStitchNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 8  →  ?path2? @ 9
/Patchers/Utilities.cs
@@ -6,6 +6,7 @@
// Based on: https://seattlesoftware.wordpress.com/2009/03/13/get-the-xpath-to-an-xml-element-xelement/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
 
@@ -13,6 +14,19 @@
{
public static class Utilities
{
public static IEnumerable<XElement> ParentsAndSelf(this XElement e)
{
if (e == null)
yield break;
 
yield return e;
 
foreach (var p in ParentsAndSelf(e.Parent))
{
yield return p;
}
}
 
/// <summary>
/// Get the absolute XPath to a given XElement
/// (e.g. "/people/person[6]/name[1]/last[1]").
/Patchers/XML.cs
@@ -13,45 +13,29 @@
{
public static class XML
{
public static HashSet<string> GetFileDelta(string cfg, string nfg)
public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> forceXPaths,
HashSet<string> excludeXPaths)
{
var configuredTags = new HashSet<string>();
 
var configuration = XDocument.Load(cfg);
foreach (var e in XDocument.Load(nfg).Descendants())
// 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 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)))
{
// Get the XPath to this element.
var xpath = e.GetAbsoluteXPath();
// Select the element in the current configuration that is found in the default configuration.
var cfgElement = cfg.XPathSelectElement(e.GetAbsoluteXPath());
var cfgElement = cfg.XPathSelectElement(xpath);
switch (cfgElement != null)
{
// Element found in current configuration.
case true:
// Only set the element value if it has no descendants.
if (!cfgElement.Descendants().Any())
// 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)))
cfgElement.Value = e.Value;
break;
// Element not found in the current configuration.
@@ -64,7 +48,7 @@
if (cfgParentElement != null)
{
// Add the default configuration parent to the current configuration.
cfgParentElement.ReplaceWith(
cfgParentElement.Add(
nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath()));
break;
}