<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Markup;

namespace Node.Net.Factory.Factories
{
    public class ManifestResourceFactory : IFactory
    {
        public List<Assembly> Assemblies = new List<Assembly>();
        public Func<Stream, object> ReadFunction;

        public object Create(Type targetType, object source)
        {
            if (source == null) return null;
            foreach (var assembly in Assemblies)
            {
                var resources = GetResources(source.ToString());
                foreach (var resourceName in resources.Keys)
                {
                    var resource = resources[resourceName];
                    if (resource != null && targetType.IsAssignableFrom(resource.GetType()))
                    {
                        return resource;
                    }
                }
            }
            return null;
        }

        private IDictionary GetResources(string name)
        {
            var dictionary = new Dictionary<string, dynamic>();
            foreach (var assembly in Assemblies)
            {
                foreach (var resourceName in assembly.GetManifestResourceNames())
                {
                    if (!dictionary.ContainsKey(resourceName) && resourceName == name)
                    {
                        dictionary.Add(resourceName, LoadResource(resourceName));
                    }
                }
            }
            if (dictionary.Count == 0)
            {
                foreach (var resourceName in GetPartialMatchResourceNames(name))
                {
                    if (!dictionary.ContainsKey(resourceName))
                    {
                        dictionary.Add(resourceName, LoadResource(resourceName));
                    }
                }
            }

            return dictionary;
        }

        private string[] GetPartialMatchResourceNames(string name)
        {
            var results = new List<string>();
            if (name.Length == 0) return results.ToArray();
            foreach (var assembly in Assemblies)
            {
                foreach (var resourceName in assembly.GetManifestResourceNames())
                {
                    if (resourceName.Contains(name) && !results.Contains(resourceName))
                    {
                        results.Add(resourceName);
                    }
                }
            }
            return results.ToArray();
        }

        private object LoadResource(string resourceName)
        {
            foreach (var assembly in Assemblies)
            {
                var stream = assembly.GetManifestResourceStream(resourceName);
                if (stream != null)
                {
                    if (ReadFunction != null) return ReadFunction(stream);
                    return XamlReader.Load(stream);
                }
            }
            return null;
        }
    }
}
<commit_msg>'1.0.105'<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Markup;

namespace Node.Net.Factory.Factories
{
    public class ManifestResourceFactory : IFactory
    {
        public List<Assembly> Assemblies = new List<Assembly>();
        public Func<Stream, object> ReadFunction;
        public IFactory Factory { get; set; }
        public object Create(Type targetType, object source)
        {
            if (source == null) return null;
            foreach (var assembly in Assemblies)
            {
                var resources = GetResources(source.ToString());
                foreach (var resourceName in resources.Keys)
                {
                    var resource = resources[resourceName];
                    if (resource != null)// && targetType.IsAssignableFrom(resource.GetType()))
                    {
                        if (targetType.IsAssignableFrom(resource.GetType())) return resource;
                        if (Factory != null) return Factory.Create(targetType, resource);
                    }
                }
            }
            return null;
        }

        private IDictionary GetResources(string name)
        {
            var dictionary = new Dictionary<string, dynamic>();
            foreach (var assembly in Assemblies)
            {
                foreach (var resourceName in assembly.GetManifestResourceNames())
                {
                    if (!dictionary.ContainsKey(resourceName) && resourceName == name)
                    {
                        dictionary.Add(resourceName, LoadResource(resourceName));
                    }
                }
            }
            if (dictionary.Count == 0)
            {
                foreach (var resourceName in GetPartialMatchResourceNames(name))
                {
                    if (!dictionary.ContainsKey(resourceName))
                    {
                        dictionary.Add(resourceName, LoadResource(resourceName));
                    }
                }
            }

            return dictionary;
        }

        private string[] GetPartialMatchResourceNames(string name)
        {
            var results = new List<string>();
            if (name.Length == 0) return results.ToArray();
            foreach (var assembly in Assemblies)
            {
                foreach (var resourceName in assembly.GetManifestResourceNames())
                {
                    if (resourceName.Contains(name) && !results.Contains(resourceName))
                    {
                        results.Add(resourceName);
                    }
                }
            }
            return results.ToArray();
        }

        private object LoadResource(string resourceName)
        {
            foreach (var assembly in Assemblies)
            {
                var stream = assembly.GetManifestResourceStream(resourceName);
                if (stream != null)
                {
                    if (ReadFunction != null) return ReadFunction(stream);
                    return XamlReader.Load(stream);
                }
            }
            return null;
        }
    }
}

{'fraction_non_alphanumeric': 0.08002550613741431, 'fraction_numerical': 0.0014347202295552368, 'mean_word_length': 1.3384271338054416, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '822122', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1330, 'n_words': 321}