<commit_before>﻿using System.IO;
using System.Xml;

namespace LunaCommon.Xml
{
    /// <summary>
    /// I really hate the config node structure so this class converts a config node to a XML and a XML to a config node
    /// </summary>
    public static class ConfigNodeXmlParser
    {
        public const string StartElement = "LMPConfigNodeToXML";
        public const string ParentNode = "Node";
        public const string ValueNode = "Parameter";
        public const string AttributeName = "name";

        public static string ConvertToXml(string configNode)
        {
            using (var reader = new StringReader(configNode))
            using (var writer = new StringWriter())
            using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, CheckCharacters = false }))
            {
                xmlWriter.WriteStartElement(StartElement);
                var previous = string.Empty;

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Contains(" = "))
                    {
                        WriteXmlValue(line, xmlWriter);
                    }
                    else if (line.Contains("{") && line.Trim().Length == 1)
                    {
                        xmlWriter.WriteStartElement(ParentNode);
                        xmlWriter.WriteAttributeString(AttributeName, previous.Trim());
                    }
                    else if (line.Contains("}") && line.Trim().Length == 1)
                    {
                        xmlWriter.WriteEndElement();
                    }
                    else
                    {
                        previous = line;
                    }
                }
                xmlWriter.WriteEndElement();
                xmlWriter.Close();
                return writer.ToString();
            }
        }

        public static string ConvertToConfigNode(string xmlDoc)
        {
            using (var reader = new StringReader(xmlDoc))
            using (var xmlReader = new XmlTextReader(reader))
            using (var writer = new StringWriter())
            {
                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (xmlReader.Name != StartElement)
                            {
                                if (xmlReader.Name == ValueNode)
                                {
                                    var valueName = xmlReader.GetAttribute(AttributeName);
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + valueName + " = " + xmlReader.ReadString());
                                }
                                else
                                {
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + xmlReader.Name);
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "{");
                                    if (xmlReader.IsEmptyElement)
                                        writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "}");
                                }
                            }
                            break;
                        case XmlNodeType.EndElement:
                            if (xmlReader.Name != StartElement && xmlReader.Name != ValueNode)
                            {
                                writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "}");
                            }
                            break;
                    }
                }<commit_msg>Fix xml parser<commit_after>﻿using System.IO;
using System.Xml;

namespace LunaCommon.Xml
{
    /// <summary>
    /// I really hate the config node structure so this class converts a config node to a XML and a XML to a config node
    /// </summary>
    public static class ConfigNodeXmlParser
    {
        public const string StartElement = "LMPConfigNodeToXML";
        public const string ParentNode = "Node";
        public const string ValueNode = "Parameter";
        public const string AttributeName = "name";

        public static string ConvertToXml(string configNode)
        {
            using (var reader = new StringReader(configNode))
            using (var writer = new StringWriter())
            using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, CheckCharacters = false }))
            {
                xmlWriter.WriteStartElement(StartElement);
                var previous = string.Empty;

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Contains(" = "))
                    {
                        WriteXmlValue(line, xmlWriter);
                    }
                    else if (line.Contains("{") && line.Trim().Length == 1)
                    {
                        xmlWriter.WriteStartElement(ParentNode);
                        xmlWriter.WriteAttributeString(AttributeName, previous.Trim());
                    }
                    else if (line.Contains("}") && line.Trim().Length == 1)
                    {
                        xmlWriter.WriteEndElement();
                    }
                    else
                    {
                        previous = line;
                    }
                }
                xmlWriter.WriteEndElement();
                xmlWriter.Close();
                return writer.ToString();
            }
        }

        public static string ConvertToConfigNode(string xmlDoc)
        {
            using (var reader = new StringReader(xmlDoc))
            using (var xmlReader = new XmlTextReader(reader))
            using (var writer = new StringWriter())
            {
                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (xmlReader.Name != StartElement)
                            {
                                if (xmlReader.Name == ValueNode)
                                {
                                    var valueName = xmlReader.GetAttribute(AttributeName);
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + valueName + " = " + xmlReader.ReadString());
                                }
                                else
                                {
                                    var nodeName = xmlReader.GetAttribute(AttributeName);
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + nodeName);
                                    writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "{");
                                    if (xmlReader.IsEmptyElement)
                                        writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "}");
                                }
                            }
                            break;
                        case XmlNodeType.EndElement:
                            if (xmlReader.Name != StartElement && xmlReader.Name != ValueNode)
                            {
                                writer.WriteLine(GetDepthTabs(xmlReader.Depth - 1) + "}");
                            }
                            break;
                    }
                }
{'fraction_non_alphanumeric': 0.08206081529677334, 'fraction_numerical': 0.0018589828708006906, 'mean_word_length': 1.0165997322623828, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 53}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5374420', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1549, 'n_words': 357}