wasStitchNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 8  →  ?path2? @ 7
/Patchers/Utilities.cs
@@ -4,9 +4,11 @@
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// 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.Text;
using System.Reflection;
using System.Xml.Linq;
 
namespace wasStitchNET.Patchers
@@ -13,13 +15,14 @@
{
public static class Utilities
{
/// <summary>
/// Get the absolute XPath to a given XElement
/// (e.g. "/people/person[6]/name[1]/last[1]").
/// </summary>
/// <param name="element">
/// The element to get the index of.
/// </param>
 
/// <summary>
/// Get the absolute XPath to a given XElement
/// (e.g. "/people/person[6]/name[1]/last[1]").
/// </summary>
/// <param name="element">
/// The element to get the index of.
/// </param>
public static string GetAbsoluteXPath(this XElement element)
{
if (element == null)
@@ -32,38 +35,40 @@
 
// If the element is the root, no index is required
 
return index == -1
? "/" + name
: string.Format
(
"/{0}[{1}]",
name,
index.ToString()
);
return (index == -1) ? "/" + name : string.Format
(
"/{0}[{1}]",
name,
index.ToString()
);
};
 
var ancestors = from e in element.Ancestors()
select relativeXPath(e);
select relativeXPath(e);
 
return string.Concat(ancestors.Reverse().ToArray()) +
relativeXPath(element);
}
 
/// <summary>
/// Get the index of the given XElement relative to its
/// siblings with identical names. If the given element is
/// the root, -1 is returned.
/// </summary>
/// <param name="element">
/// The element to get the index of.
/// </param>
/// <summary>
/// Get the index of the given XElement relative to its
/// siblings with identical names. If the given element is
/// the root, -1 is returned.
/// </summary>
/// <param name="element">
/// The element to get the index of.
/// </param>
public static int IndexPosition(this XElement element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
 
if (element.Parent == null)
{
return -1;
}
 
var i = 1; // Indexes for nodes start at 1, not 0
 
@@ -70,7 +75,9 @@
foreach (var sibling in element.Parent.Elements(element.Name))
{
if (sibling == element)
{
return i;
}
 
i++;
}
@@ -78,5 +85,6 @@
throw new InvalidOperationException
("element has been removed from its parent.");
}
 
}
}
}
/Patchers/XML.cs
@@ -13,34 +13,11 @@
{
public static class XML
{
public static HashSet<string> GetFileDelta(string cfg, string nfg)
public static XDocument PatchConfiguration(XDocument cfg, XDocument nfg, HashSet<string> configuredTags)
{
var configuredTags = new HashSet<string>();
 
var configuration = XDocument.Load(cfg);
foreach (var e in XDocument.Load(nfg).Descendants())
{
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) &&
!e.Ancestors().Any(o => configuredTags.Contains(o.Name.LocalName))))*/
foreach (var e in nfg.Descendants()
.Where(e => configuredTags.Contains(e.Name.LocalName)))
{
@@ -57,19 +34,16 @@
// Element not found in the current configuration.
default:
// Find the first existing parent of the default configuration in the current configuration.
var parent = e;
var parent = e.Parent;
XElement cfgParentElement = null;
do
{
var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath());
if (cfgParentElement != null)
{
// Add the default configuration parent to the current configuration.
cfgParentElement.ReplaceWith(
nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath()));
break;
}
parent = parent.Parent;
} while (parent != null);
cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath());
parent = e.Parent;
} while (cfgParentElement == null);
 
// Add the default configuration parent to the current configuration.
cfgParentElement = nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath());
break;
}
}
@@ -76,4 +50,9 @@
return cfg;
}
}
}
}