← All source files

V13 / 7879e8b5740b

ProjectIntegrationCodec.cs

Lilith/Core/Projects/ProjectIntegrationCodec.cs

Unchanged from the preceding version
CS
using System.Xml;
using System.Xml.Linq;

namespace Lilith.Core.Projects;

internal static class ProjectIntegrationCodec
{
    internal static ProjectIntegration ParseXml(string text)
    {
        using var reader = XmlReader.Create(new StringReader(ProjectGeneration.CleanCode(text)), new XmlReaderSettings {
            DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = 200000 });
        var root = XDocument.Load(reader).Root ?? throw new InvalidDataException("Missing changes root.");
        if (root.Name != "changes" || root.HasAttributes) throw new InvalidDataException("Expected a changes document.");
        var changes = new List<ProjectChange>();
        foreach (var change in root.Elements())
        {
            if (change.Name != "change" || change.Attributes().Any(a => a.Name != "path") ||
                change.Elements().Count() != 2 || change.Elements("old").Count() != 1 || change.Elements("new").Count() != 1 ||
                change.Elements().Any(e => e.HasAttributes || e.HasElements)) throw new InvalidDataException("Use change path with old/new CDATA only.");
            changes.Add(new((string?)change.Attribute("path") ?? "", change.Element("old")!.Value, change.Element("new")!.Value));
        }
        return new(changes.ToArray());
    }
    internal static string Display(ProjectBundle bundle) => string.Join("\n\n", bundle.Files.Select(f => "===== FILE: " + f.Path + " =====\n" + f.Content));
}