<commit_before>﻿namespace XamlCombine
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.IO;
	using System.Reflection;
	using System.Xml;

	public class Combiner
	{
		/// <summary>
		/// Dynamic resource string.
		/// </summary>
		private const string DynamicResourceString = "{DynamicResource ";

		/// <summary>
		/// Static resource string.
		/// </summary>
		private const string StaticResourceString = "{StaticResource ";

		private Lazy<string> appPath;

		public Combiner()
		{
			this.appPath = new Lazy<string>(this.GetAppPath);
		}

		/// <summary>
		/// Combines multiple XAML resource dictionaries in one.
		/// </summary>
		/// <param name="sourceFile">Filename of list of XAML's.</param>
		/// <param name="resultFile">Result XAML filename.</param>
		public void Combine(string sourceFile, string resultFile)
		{
			// Write to console
			Console.WriteLine("Loading resources list from \"{0}\"", sourceFile);

			sourceFile = this.GetFilePath(sourceFile);

			// Load resources lists
			var resources = File.ReadAllLines(sourceFile);

			// Create result XML document
			var finalDocument = new XmlDocument();
			var rootNode = finalDocument.CreateElement("ResourceDictionary", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
			finalDocument.AppendChild(rootNode);

			// List of existing keys, to avoid duplicates
			var keys = new List<string>();

			// Associate key with ResourceElement
			var resourceElements = new Dictionary<string, ResourceElement>();

			// List of readed resources
			var resourcesList = new List<ResourceElement>();

			// For each resource file
			for (var i = 0; i < resources.Length; i++)
			{
				var current = new XmlDocument();
				var file = this.GetFilePath(resources[i]);

				current.Load(file);

				// Write to console
				//Console.WriteLine("Loading resource \"{0}\"", resources[i]);

				// Set and fix resource dictionary attributes
				var root = current.DocumentElement;
				if (root == null)
				{
					continue;
				}

				for (var j = 0; j < root.Attributes.Count; j++)
				{
					XmlAttribute attr = root.Attributes[j];
					if (rootNode.HasAttribute(attr.Name))
					{
						// If namespace with this name exists and not equal
						if (attr.Value != rootNode.Attributes[attr.Name].Value
							&& attr.Prefix == "xmlns")
						{
							// Create new namespace name
							var index = 0;
							string name;
							do
							{
								name = attr.LocalName + "_" + index.ToString(CultureInfo.InvariantCulture);
							}
							while (rootNode.HasAttribute("xmlns:" + name));

							root.SetAttribute("xmlns:" + name, attr.Value);<commit_msg>Minor refactoring<commit_after>﻿namespace XamlCombine
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.IO;
	using System.Reflection;
	using System.Xml;

	public class Combiner
	{
		/// <summary>
		/// Dynamic resource string.
		/// </summary>
		private const string DynamicResourceString = "{DynamicResource ";

		/// <summary>
		/// Static resource string.
		/// </summary>
		private const string StaticResourceString = "{StaticResource ";

		private Lazy<string> appPath;

		public Combiner()
		{
			this.appPath = new Lazy<string>(this.GetAppPath);
		}

		/// <summary>
		/// Combines multiple XAML resource dictionaries in one.
		/// </summary>
		/// <param name="sourceFile">Filename of list of XAML's.</param>
		/// <param name="resultFile">Result XAML filename.</param>
		public void Combine(string sourceFile, string resultFile)
		{
			// Write to console
			Console.WriteLine("Loading resources list from \"{0}\"", sourceFile);

			sourceFile = this.GetFilePath(sourceFile);

			// Load resources lists
			var resources = File.ReadAllLines(sourceFile);

			// Create result XML document
			var finalDocument = new XmlDocument();
			var rootNode = finalDocument.CreateElement("ResourceDictionary", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
			finalDocument.AppendChild(rootNode);

			// List of existing keys, to avoid duplicates
			var keys = new List<string>();

			// Associate key with ResourceElement
			var resourceElements = new Dictionary<string, ResourceElement>();

			// List of read resources
			var resourcesList = new List<ResourceElement>();

			// For each resource file
			for (var i = 0; i < resources.Length; i++)
			{
				var current = new XmlDocument();
				current.Load(this.GetFilePath(resources[i]));

				// Write to console
				//Console.WriteLine("Loading resource \"{0}\"", resources[i]);

				// Set and fix resource dictionary attributes
				var root = current.DocumentElement;
				if (root == null)
				{
					continue;
				}

				for (var j = 0; j < root.Attributes.Count; j++)
				{
					XmlAttribute attr = root.Attributes[j];
					if (rootNode.HasAttribute(attr.Name))
					{
						// If namespace with this name exists and not equal
						if (attr.Value != rootNode.Attributes[attr.Name].Value
							&& attr.Prefix == "xmlns")
						{
							// Create new namespace name
							var index = 0;
							string name;
							do
							{
								name = attr.LocalName + "_" + index.ToString(CultureInfo.InvariantCulture);
							}
							while (rootNode.HasAttribute("xmlns:" + name));

							root.SetAttribute("xmlns:" + name, attr.Value);
{'fraction_non_alphanumeric': 0.12688253012048192, 'fraction_numerical': 0.00338855421686747, 'mean_word_length': 3.0037678975131876, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7171771', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1476, 'n_words': 421}