<commit_before>﻿// <copyright file="WebDavXmlOutputFormatter.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>

using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using FubarDev.WebDavServer.Model;

using JetBrains.Annotations;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace FubarDev.WebDavServer.Formatters
{
    public class WebDavXmlOutputFormatter : IWebDavOutputFormatter
    {
        [NotNull]
        private static readonly Encoding _defaultEncoding = new UTF8Encoding(false);

        [NotNull]
        private readonly ILogger<WebDavXmlOutputFormatter> _logger;

        [NotNull]
        private readonly string _namespacePrefix;

        public WebDavXmlOutputFormatter([NotNull] IOptions<WebDavFormatterOptions> options, [NotNull] ILogger<WebDavXmlOutputFormatter> logger)
        {
            _logger = logger;
            Encoding = _defaultEncoding;

            var contentType = options.Value.ContentType ?? "text/xml";
            ContentType = $"{contentType}; charset=\"{Encoding.WebName}\"";

            _namespacePrefix = options.Value.NamespacePrefix;
        }

        [NotNull]
        public string ContentType { get; }

        [NotNull]
        public Encoding Encoding { get; }

        public void Serialize<T>(Stream output, T data)
        {
            var writerSettings = new XmlWriterSettings { Encoding = Encoding };

            var ns = new XmlSerializerNamespaces();
            if (!string.IsNullOrEmpty(_namespacePrefix))
            {
                ns.Add(_namespacePrefix, WebDavXml.Dav.NamespaceName);
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                var debugOutput = new StringWriter();
                SerializerInstance<T>.Serializer.Serialize(debugOutput, data, ns);
                _logger.LogDebug(debugOutput.ToString());
            }

            using (var writer = XmlWriter.Create(output, writerSettings))
            {
                SerializerInstance<T>.Serializer.Serialize(writer, data, ns);
            }
        }

        private static class SerializerInstance<T>
        {
            [NotNull]
            public static readonly XmlSerializer Serializer = new XmlSerializer(typeof(T));
        }
    }
}<commit_msg>Handle XElement responses to use the correct namespace prefix<commit_after>﻿// <copyright file="WebDavXmlOutputFormatter.cs" company="Fubar Development Junker">
// Copyright (c) <NAME> Junker. All rights reserved.
// </copyright>

using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

using FubarDev.WebDavServer.Model;

using JetBrains.Annotations;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace FubarDev.WebDavServer.Formatters
{
    public class WebDavXmlOutputFormatter : IWebDavOutputFormatter
    {
        [NotNull]
        private static readonly Encoding _defaultEncoding = new UTF8Encoding(false);

        [NotNull]
        private readonly ILogger<WebDavXmlOutputFormatter> _logger;

        [NotNull]
        private readonly string _namespacePrefix;

        public WebDavXmlOutputFormatter([NotNull] IOptions<WebDavFormatterOptions> options, [NotNull] ILogger<WebDavXmlOutputFormatter> logger)
        {
            _logger = logger;
            Encoding = _defaultEncoding;

            var contentType = options.Value.ContentType ?? "text/xml";
            ContentType = $"{contentType}; charset=\"{Encoding.WebName}\"";

            _namespacePrefix = options.Value.NamespacePrefix;
        }

        [NotNull]
        public string ContentType { get; }

        [NotNull]
        public Encoding Encoding { get; }

        public void Serialize<T>(Stream output, T data)
        {
            var writerSettings = new XmlWriterSettings { Encoding = Encoding };

            var ns = new XmlSerializerNamespaces();
            if (!string.IsNullOrEmpty(_namespacePrefix))
            {
                ns.Add(_namespacePrefix, WebDavXml.Dav.NamespaceName);

                var xelem = data as XElement;
                if (xelem != null && xelem.GetPrefixOfNamespace(WebDavXml.Dav) != _namespacePrefix)
                {
                    xelem.SetAttributeValue(XNamespace.Xmlns + _namespacePrefix, WebDavXml.Dav.NamespaceName);
                }
            }

            if (_logger.IsEnabled(LogLevel.Debug))
            {
                var debugOutput = new StringWriter();
                SerializerInstance<T>.Serializer.Serialize(debugOutput, data, ns);
                _logger.LogDebug(debugOutput.ToString());
            }

            using (var writer = XmlWriter.Create(output, writerSettings))
            {
                SerializerInstance<T>.Serializer.Serialize(writer, data, ns);
            }
        }

        private static class SerializerInstance<T>
        {
            [NotNull]
            public static readonly XmlSerializer Serializer = new XmlSerializer(typeof(T));
        }
    }
}
{'fraction_non_alphanumeric': 0.08870652598671357, 'fraction_numerical': 0.00039077764751856197, 'mean_word_length': 2.387822634017207, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6100306', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1275, 'n_words': 292}