namespace HaWeb.XMLParser; using HaWeb.Settings.XMLRoots; using System.Xml.Linq; using Microsoft.AspNetCore.Mvc.ModelBinding; public class XMLService : IXMLService { private Dictionary? _Roots; public XMLService() { var types = _GetAllTypesThatImplementInterface().ToList(); types.ForEach( x => { if (this._Roots == null) this._Roots = new Dictionary(); var instance = (IXMLRoot)Activator.CreateInstance(x)!; if (instance != null) this._Roots.Add(instance.Type, instance); }); } public List? GetRoots() => this._Roots == null ? null : this._Roots.Values.ToList(); public List? ProbeHamannFile(XDocument document, ModelStateDictionary ModelState) { if (document.Root!.Name != "opus") { ModelState.AddModelError("Error", "A valid Hamann-Docuemnt must begin with "); return null; } var res = _testElements(document.Root.Elements()); if (document.Root.Element("data") != null) { var datares = _testElements(document.Element("data")!.Elements()); if (datares != null && res == null) res = datares; else if (datares != null) res!.AddRange(datares); } return res; } private List? _testElements(IEnumerable? elements) { if (elements == null) return null; List? res = null; foreach (var elem in elements) { var doc = _testElement(elem); if (doc != null) { if (res == null) res = new List(); res.Add(doc); } } return res; } private XMLRootDocument? _testElement(XElement? element) { if (element == null || _Roots == null) return null; foreach (var (_, root) in _Roots) { if(root.IsTypeOf(element)) return _createXMLRootDocument(root, element); } return null; } private XMLRootDocument _createXMLRootDocument(IXMLRoot Root, XElement element) { var doc = new XMLRootDocument(Root.Type, Root.GenerateIdentificationString(element), element); doc.Elements = Root.GetCollectedObjects(doc); doc.Fields = Root.GenerateFields(doc); return doc; } private IEnumerable _GetAllTypesThatImplementInterface() { return System.Reflection.Assembly.GetExecutingAssembly() .GetTypes() .Where(type => typeof(T).IsAssignableFrom(type) && !type.IsInterface); } }