wasStitchNET

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 6  →  ?path2? @ 7
File deleted
/Patchers/XML/Utilities.cs
File deleted
/Patchers/XML/SerializationUtils.cs
/Patchers/Utilities.cs
@@ -0,0 +1,90 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// 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
{
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>
public static string GetAbsoluteXPath(this XElement element)
{
if (element == null)
throw new ArgumentNullException("element");
 
Func<XElement, string> relativeXPath = e =>
{
var index = e.IndexPosition();
var name = e.Name.LocalName;
 
// If the element is the root, no index is required
 
return (index == -1) ? "/" + name : string.Format
(
"/{0}[{1}]",
name,
index.ToString()
);
};
 
var ancestors = from e in element.Ancestors()
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>
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
 
foreach (var sibling in element.Parent.Elements(element.Name))
{
if (sibling == element)
{
return i;
}
 
i++;
}
 
throw new InvalidOperationException
("element has been removed from its parent.");
}
 
}
}
/Patchers/XML.cs
@@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
 
namespace wasStitchNET.Patchers
{
public static class XML
{
public static XDocument PatchConfiguration(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)))
{
// Select the element in the current configuration that is found in the default configuration.
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.
if (!cfgElement.Descendants().Any())
cfgElement.Value = e.Value;
break;
// Element not found in the current configuration.
default:
// Find the first existing parent of the default configuration in the current configuration.
var parent = e.Parent;
XElement cfgParentElement = null;
do
{
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;
}
}
return cfg;
}
}
}