<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.Caching;
using Orchard.FileSystems.WebSite;
using Orchard.Logging;

namespace Orchard.DisplayManagement.Descriptors.ShapePlacementStrategy {

    /// <summary>
    /// Parses and caches the Placement.info file contents for a given IWebSiteFolder vdir
    /// </summary>
    public interface IPlacementFileParser : IDependency {
        PlacementFile Parse(string virtualPath);
    }


    public class PlacementFileParser : IPlacementFileParser {
        private readonly ICacheManager _cacheManager;
        private readonly IWebSiteFolder _webSiteFolder;

        public PlacementFileParser(ICacheManager cacheManager, IWebSiteFolder webSiteFolder) {
            _cacheManager = cacheManager;
            _webSiteFolder = webSiteFolder;
            Logger = NullLogger.Instance;
        }

        public ILogger Logger { get; set; }
        public bool DisableMonitoring { get; set; }

        public PlacementFile Parse(string virtualPath) {
            return _cacheManager.Get(virtualPath, context => {

                if (!DisableMonitoring) {
                    Logger.Debug("Monitoring virtual path \"{0}\"", virtualPath);
                    context.Monitor(_webSiteFolder.WhenPathChanges(virtualPath));
                }

                var placementText = _webSiteFolder.ReadFile(virtualPath);
                return ParseImplementation(virtualPath, placementText);
            });
        }

        private PlacementFile ParseImplementation(string virtualPath, string placementText) {
            if (placementText == null)
                return null;


            var element = XElement.Parse(placementText);
            return new PlacementFile {
                Nodes = Accept(element).ToList()
            };
        }

        private IEnumerable<PlacementNode> Accept(XElement element) {
            switch (element.Name.LocalName) {
                case "Placement":
                    return AcceptMatch(element);
                case "Match":
                    return AcceptMatch(element);
                case "Place":
                    return AcceptPlace(element);
            }
            return Enumerable.Empty<PlacementNode>();<commit_msg>#20309: Surfacing method from PlacementFileParser to parse placement texts.<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.Caching;
using Orchard.FileSystems.WebSite;
using Orchard.Logging;

namespace Orchard.DisplayManagement.Descriptors.ShapePlacementStrategy {

    /// <summary>
    /// Parses and caches the Placement.info file contents for a given IWebSiteFolder vdir
    /// </summary>
    public interface IPlacementFileParser : IDependency {
        PlacementFile Parse(string virtualPath);
        PlacementFile ParseText(string placementText);
    }


    public class PlacementFileParser : IPlacementFileParser {
        private readonly ICacheManager _cacheManager;
        private readonly IWebSiteFolder _webSiteFolder;

        public PlacementFileParser(ICacheManager cacheManager, IWebSiteFolder webSiteFolder) {
            _cacheManager = cacheManager;
            _webSiteFolder = webSiteFolder;
            Logger = NullLogger.Instance;
        }

        public ILogger Logger { get; set; }
        public bool DisableMonitoring { get; set; }

        public PlacementFile Parse(string virtualPath) {
            return _cacheManager.Get(virtualPath, context => {

                if (!DisableMonitoring) {
                    Logger.Debug("Monitoring virtual path \"{0}\"", virtualPath);
                    context.Monitor(_webSiteFolder.WhenPathChanges(virtualPath));
                }

                var placementText = _webSiteFolder.ReadFile(virtualPath);
                return ParseText(placementText);
            });
        }

        public PlacementFile ParseText(string placementText) {
            if (placementText == null)
                return null;


            var element = XElement.Parse(placementText);
            return new PlacementFile {
                Nodes = Accept(element).ToList()
            };
        }

        private IEnumerable<PlacementNode> Accept(XElement element) {
            switch (element.Name.LocalName) {
                case "Placement":
                    return AcceptMatch(element);
                case "Match":
                    return AcceptMatch(element);
                case "Place":
                    return AcceptPlace(element);
            }
            return Enumerable.Empty<PlacementNode>();
{'fraction_non_alphanumeric': 0.07597668897042953, 'fraction_numerical': 0.00151090006475286, 'mean_word_length': 2.178326474622771, 'pattern_counts': {'":': 6, '<': 11, '<?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': '3320596', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1084, 'n_words': 263}