<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using Autofac.Features.Metadata;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Mvc;
using Orchard.Settings;

namespace Orchard.UI.Resources {
    public class ResourceManager : IResourceManager, IUnitOfWorkDependency {
        private readonly Dictionary<Tuple<String, String>, RequireSettings> _required = new Dictionary<Tuple<String, String>, RequireSettings>();
        private readonly List<LinkEntry> _links = new List<LinkEntry>();
        private readonly Dictionary<string, MetaEntry> _metas = new Dictionary<string, MetaEntry> {
            { "generator", new MetaEntry { Content = "Orchard", Name = "generator" } }
        };
        private readonly Dictionary<string, IList<ResourceRequiredContext>> _builtResources = new Dictionary<string, IList<ResourceRequiredContext>>(StringComparer.OrdinalIgnoreCase);
        private readonly IEnumerable<Meta<IResourceManifestProvider>> _providers;
        private readonly Work<WorkContext> _workContext;
        private readonly Work<IHttpContextAccessor> _httpContextAccessor;
        private ResourceManifest _dynamicManifest;
        private List<String> _headScripts;
        private List<String> _footScripts;
        private IEnumerable<IResourceManifest> _manifests;

        private const string NotIE = "!IE";

        private static string ToAppRelativePath(string resourcePath) {
            if (!String.IsNullOrEmpty(resourcePath) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                resourcePath = VirtualPathUtility.ToAppRelative(resourcePath);
            }
            return resourcePath;
        }

        private static string ToAbsolutePath(string resourcePath, string relativeFromPath) {
            if (!String.IsNullOrEmpty(resourcePath) && !VirtualPathUtility.IsAbsolute(resourcePath) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                // appears to be a relative path (e.g. 'foo.js' or '../foo.js', not "/foo.js" or "http://..")
                if (String.IsNullOrEmpty(relativeFromPath)) {
                    throw new InvalidOperationException("ResourcePath cannot be relative unless a base relative path is also provided.");
                }
                resourcePath = VirtualPathUtility.ToAbsolute(VirtualPathUtility.Combine(relativeFromPath, resourcePath));
            }
            return resourcePath;
        }

        private static string ToPhysicalPath(string resourcePath) {
            if (!String.IsNullOrEmpty(resourcePath) && (VirtualPathUtility.IsAppRelative(resourcePath) || VirtualPathUtility.IsAbsolute(resourcePath)) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                return HostingEnvironment.MapPath(resourcePath.Split(new[] { '?' })[0]);
            }
            return null;
        }

        private static TagBuilder GetTagBuilder(ResourceDefinition resource, string url) {
            var tagBuilder = new TagBuilder(resource.TagName);
            tagBuilder.MergeAttributes(resource.TagBuilder.Attributes);
            if (!String.IsNullOrEmpty(resource.FilePathAttributeName)) {
                if (!String.IsNullOrEmpty(url)) {
                    if (VirtualPathUtility.IsAppRelative(url)) {
                        url = VirtualPathUtility.ToAbsolute(url);
                    }
                    tagBuilder.MergeAttribute(resource.FilePathAttributeName, url, true);
                }
            }
            return tagBuilder;
        }

        public static void WriteResource(TextWriter writer, ResourceDefinition resource, string url, string condition, Dictionary<string, string> attributes) {
            if (!string.IsNullOrEmpty(condition)) {
                if (condition == NotIE) {
                    writer.WriteLine("<!--[if " + condition + "]>-->");
                }
                else {
                    writer.WriteLine("<!--[if " + condition + "]>");
                }
            }

            var tagBuilder = GetTagBuilder(resource, url);
            
            if (attributes != null) {
                // todo: try null value
                tagBuilder.MergeAttributes(attributes, true);
            }

            writer.WriteLine(tagBuilder.ToString(resource.TagRenderMode));
            
            if (!string.IsNullOrEmpty(condition)) {
                if (condition == NotIE) {
                    writer.WriteLine("<!--<![endif]-->");
                }
                else {
                    writer.WriteLine("<![endif]-->");
                }
            }
        }

        public ResourceManager(IEnumerable<Meta<IResourceManifestProvider>> resourceProviders,
            Work<WorkContext> workContext,
            Work<IHttpContextAccessor> httpContextAccessor
            ) {
            _providers = resourceProviders;
            _workContext = workContext;
            _httpContextAccessor = httpContextAccessor;
        }

        public IEnumerable<IResourceManifest> ResourceProviders {
            get {
                if (_manifests == null) {
                    var builder = new ResourceManifestBuilder();
                    foreach (var provider in _providers) {
                        builder.Feature = provider.Metadata.ContainsKey("Feature") ?
                            (Feature)provider.Metadata["Feature"] :
                            null;
                        provider.Value.BuildManifests(builder);
                    }
                    _manifests = builder.ResourceManifests;
                }
                return _manifests;
            }
        }

        public virtual ResourceManifest DynamicResources {
            get {
                return _dynamicManifest ?? (_dynamicManifest = new ResourceManifest());
            }
        }

        public virtual RequireSettings Require(string resourceType, string resourceName) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null) {
                throw new ArgumentNullException("resourceName");
            }
            RequireSettings settings;
            var key = new Tuple<string, string>(resourceType, resourceName);
            if (!_required.TryGetValue(key, out settings)) {
                settings = new RequireSettings { Type = resourceType, Name = resourceName };
                _required[key] = settings;
            }
            _builtResources[resourceType] = null;
            return settings;
        }

        public virtual RequireSettings Include(string resourceType, string resourcePath, string resourceDebugPath) {
            return Include(resourceType, resourcePath, resourceDebugPath, null);
        }

        public virtual RequireSettings Include(string resourceType, string resourcePath, string resourceDebugPath, string relativeFromPath) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourcePath == null) {
                throw new ArgumentNullException("resourcePath");
            }

            // Convert app-relative paths (~/) to absolute paths (e.g. /orchard/..)
            if (VirtualPathUtility.IsAppRelative(resourcePath)) {
                resourcePath = VirtualPathUtility.ToAbsolute(resourcePath);
            }
            if (resourceDebugPath != null && VirtualPathUtility.IsAppRelative(resourceDebugPath)) {
                resourceDebugPath = VirtualPathUtility.ToAbsolute(resourceDebugPath);
            }
            
            // Convert relative paths (e.g. dir/file.css) to absolute paths.
            resourcePath = ToAbsolutePath(resourcePath, relativeFromPath);
            resourceDebugPath = ToAbsolutePath(resourceDebugPath, relativeFromPath);

            // Resolve absolute paths (not full URLs) to physical file paths.
            var resourcePhysicalPath = ToPhysicalPath(resourcePath);
            var resourceDebugPhysicalPath = ToPhysicalPath(resourceDebugPath);

            return Require(resourceType, ToAppRelativePath(resourcePath)).Define(d => {
                d.SetUrl(resourcePath, resourceDebugPath);
                if (resourcePhysicalPath != null)
                    d.SetPhysicalPath(resourcePhysicalPath, resourceDebugPhysicalPath);
            });
        }

        public virtual void RegisterHeadScript(string script) {
            if (_headScripts == null) {
                _headScripts = new List<string>();
            }
            _headScripts.Add(script);
        }

        public virtual void RegisterFootScript(string script) {
            if (_footScripts == null) {
                _footScripts = new List<string>();
            }
            _footScripts.Add(script);
        }

        public virtual void NotRequired(string resourceType, string resourceName) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null) {
                throw new ArgumentNullException("resourceName");
            }
            var key = new Tuple<string, string>(resourceType, resourceName);
            _builtResources[resourceType] = null;
            _required.Remove(key);<commit_msg>Framework/Resources: Fixing automatic merge mistake in ResourceManager<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using Autofac.Features.Metadata;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;
using Orchard.Mvc;
using Orchard.Settings;

namespace Orchard.UI.Resources {
    public class ResourceManager : IResourceManager, IUnitOfWorkDependency {
        private readonly Dictionary<Tuple<String, String>, RequireSettings> _required = new Dictionary<Tuple<String, String>, RequireSettings>();
        private readonly List<LinkEntry> _links = new List<LinkEntry>();
        private readonly Dictionary<string, MetaEntry> _metas = new Dictionary<string, MetaEntry> {
            { "generator", new MetaEntry { Content = "Orchard", Name = "generator" } }
        };
        private readonly Dictionary<string, IList<ResourceRequiredContext>> _builtResources = new Dictionary<string, IList<ResourceRequiredContext>>(StringComparer.OrdinalIgnoreCase);
        private readonly IEnumerable<Meta<IResourceManifestProvider>> _providers;
        private readonly Work<WorkContext> _workContext;
        private readonly Work<IHttpContextAccessor> _httpContextAccessor;
        private ResourceManifest _dynamicManifest;
        private List<String> _headScripts;
        private List<String> _footScripts;
        private IEnumerable<IResourceManifest> _manifests;

        private const string NotIE = "!IE";

        private static string ToAppRelativePath(string resourcePath) {
            if (!String.IsNullOrEmpty(resourcePath) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                resourcePath = VirtualPathUtility.ToAppRelative(resourcePath);
            }
            return resourcePath;
        }

        private static string ToAbsolutePath(string resourcePath, string relativeFromPath) {
            if (!String.IsNullOrEmpty(resourcePath) && !VirtualPathUtility.IsAbsolute(resourcePath) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                // appears to be a relative path (e.g. 'foo.js' or '../foo.js', not "/foo.js" or "http://..")
                if (String.IsNullOrEmpty(relativeFromPath)) {
                    throw new InvalidOperationException("ResourcePath cannot be relative unless a base relative path is also provided.");
                }
                resourcePath = VirtualPathUtility.ToAbsolute(VirtualPathUtility.Combine(relativeFromPath, resourcePath));
            }
            return resourcePath;
        }

        private static string ToPhysicalPath(string resourcePath) {
            if (!String.IsNullOrEmpty(resourcePath) && (VirtualPathUtility.IsAppRelative(resourcePath) || VirtualPathUtility.IsAbsolute(resourcePath)) && !Uri.IsWellFormedUriString(resourcePath, UriKind.Absolute) && !resourcePath.StartsWith("//")) {
                return HostingEnvironment.MapPath(resourcePath.Split(new[] { '?' })[0]);
            }
            return null;
        }

        private static TagBuilder GetTagBuilder(ResourceDefinition resource, string url) {
            var tagBuilder = new TagBuilder(resource.TagName);
            tagBuilder.MergeAttributes(resource.TagBuilder.Attributes);
            if (!String.IsNullOrEmpty(resource.FilePathAttributeName)) {
                if (!String.IsNullOrEmpty(url)) {
                    if (VirtualPathUtility.IsAppRelative(url)) {
                        url = VirtualPathUtility.ToAbsolute(url);
                    }
                    tagBuilder.MergeAttribute(resource.FilePathAttributeName, url, true);
                }
            }
            return tagBuilder;
        }

        public static void WriteResource(TextWriter writer, ResourceDefinition resource, string url, string condition, Dictionary<string, string> attributes) {
            if (!string.IsNullOrEmpty(condition)) {
                if (condition == NotIE) {
                    writer.WriteLine("<!--[if " + condition + "]>-->");
                }
                else {
                    writer.WriteLine("<!--[if " + condition + "]>");
                }
            }

            var tagBuilder = GetTagBuilder(resource, url);
            
            if (attributes != null) {
                // todo: try null value
                tagBuilder.MergeAttributes(attributes, true);
            }

            writer.WriteLine(tagBuilder.ToString(resource.TagRenderMode));
            
            if (!string.IsNullOrEmpty(condition)) {
                if (condition == NotIE) {
                    writer.WriteLine("<!--<![endif]-->");
                }
                else {
                    writer.WriteLine("<![endif]-->");
                }
            }
        }

        public ResourceManager(IEnumerable<Meta<IResourceManifestProvider>> resourceProviders,
            Work<WorkContext> workContext,
            Work<IHttpContextAccessor> httpContextAccessor
            ) {
            _providers = resourceProviders;
            _workContext = workContext;
            _httpContextAccessor = httpContextAccessor;
        }

        public IEnumerable<IResourceManifest> ResourceProviders {
            get {
                if (_manifests == null) {
                    var builder = new ResourceManifestBuilder();
                    foreach (var provider in _providers) {
                        builder.Feature = provider.Metadata.ContainsKey("Feature") ?
                            (Feature)provider.Metadata["Feature"] :
                            null;
                        provider.Value.BuildManifests(builder);
                    }
                    _manifests = builder.ResourceManifests;
                }
                return _manifests;
            }
        }

        public virtual ResourceManifest DynamicResources {
            get {
                return _dynamicManifest ?? (_dynamicManifest = new ResourceManifest());
            }
        }

        public virtual RequireSettings Require(string resourceType, string resourceName) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null) {
                throw new ArgumentNullException("resourceName");
            }
            RequireSettings settings;
            var key = new Tuple<string, string>(resourceType, resourceName);
            if (!_required.TryGetValue(key, out settings)) {
                settings = new RequireSettings { Type = resourceType, Name = resourceName };
                _required[key] = settings;
            }
            _builtResources[resourceType] = null;
            return settings;
        }

        public virtual RequireSettings Include(string resourceType, string resourcePath, string resourceDebugPath) {
            return Include(resourceType, resourcePath, resourceDebugPath, null);
        }

        public virtual RequireSettings Include(string resourceType, string resourcePath, string resourceDebugPath, string relativeFromPath) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourcePath == null) {
                throw new ArgumentNullException("resourcePath");
            }

            // Convert app-relative paths (~/) to absolute paths (e.g. /orchard/..)
            if (VirtualPathUtility.IsAppRelative(resourcePath)) {
                resourcePath = VirtualPathUtility.ToAbsolute(resourcePath);
            }
            if (resourceDebugPath != null && VirtualPathUtility.IsAppRelative(resourceDebugPath)) {
                resourceDebugPath = VirtualPathUtility.ToAbsolute(resourceDebugPath);
            }
            
            // Convert relative paths (e.g. dir/file.css) to absolute paths.
            resourcePath = ToAbsolutePath(resourcePath, relativeFromPath);
            resourceDebugPath = ToAbsolutePath(resourceDebugPath, relativeFromPath);

            // Resolve absolute paths (not full URLs) to physical file paths.
            var resourcePhysicalPath = ToPhysicalPath(resourcePath);
            var resourceDebugPhysicalPath = ToPhysicalPath(resourceDebugPath);

            return Require(resourceType, ToAppRelativePath(GetResourceKey(resourcePath, resourceDebugPath))).Define(d => {
                d.SetUrl(resourcePath, resourceDebugPath);
                if (resourcePhysicalPath != null)
                    d.SetPhysicalPath(resourcePhysicalPath, resourceDebugPhysicalPath);
            });
        }

        public virtual void RegisterHeadScript(string script) {
            if (_headScripts == null) {
                _headScripts = new List<string>();
            }
            _headScripts.Add(script);
        }

        public virtual void RegisterFootScript(string script) {
            if (_footScripts == null) {
                _footScripts = new List<string>();
            }
            _footScripts.Add(script);
        }

        public virtual void NotRequired(string resourceType, string resourceName) {
            if (resourceType == null) {
                throw new ArgumentNullException("resourceType");
            }
            if (resourceName == null) {
                throw new ArgumentNullException("resourceName");
            }
            var key = new Tuple<string, string>(resourceType, resourceName);
            _builtResources[resourceType] = null;
            _required.Remove(key);
{'fraction_non_alphanumeric': 0.0901554937699516, 'fraction_numerical': 0.00010297600659046442, 'mean_word_length': 2.179407431658209, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 73, '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': '3822394', 'n_tokens_mistral': 4567, 'n_tokens_neox': 4417, 'n_words': 1000}