mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 17:25:32 +00:00
Added Classes for detecting Root Elements of Hamann-Collections
This commit is contained in:
47
HaWeb/XMLParser/IXMLRoot.cs
Normal file
47
HaWeb/XMLParser/IXMLRoot.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace HaWeb.XMLParser;
|
||||
using System.Xml.Linq;
|
||||
|
||||
public interface IXMLRoot {
|
||||
// Name of the IXMLRoot
|
||||
public abstract string Type { get; }
|
||||
|
||||
// Tag Name of the Container
|
||||
public abstract string Container { get; }
|
||||
|
||||
// Tag Name of child objects to be collected
|
||||
public abstract Predicate<XElement> IsCollectedObject { get; }
|
||||
|
||||
// Gets the Key of a collected object
|
||||
public abstract Func<XElement, string?> GetKey { get; }
|
||||
|
||||
// Is the pesented XElement such a root?
|
||||
public bool IsTypeOf(XElement xelement) {
|
||||
if (xelement.Name == this.Container) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Generate certain metadat fields to display about this root
|
||||
public abstract List<(string, string)>? GenerateFields(XMLRootDocument document);
|
||||
|
||||
// Generate an identification string of which the hash will be the filename.
|
||||
// The second string will be appended literally for convenience.
|
||||
// If the queries of two document are equal they replace each other
|
||||
// If the queries and the date of two documents are equal the later one gets deleted
|
||||
public abstract (string?, string) GenerateIdentificationString(XElement element);
|
||||
|
||||
// Further deciding which of two documents replaces which
|
||||
public abstract bool Replaces(XMLRootDocument doc1, XMLRootDocument doc2);
|
||||
|
||||
public Dictionary<string, XElement>? GetCollectedObjects(XMLRootDocument document) {
|
||||
Dictionary<string, XElement>? ret = null;
|
||||
var root = document.Root;
|
||||
root.Elements().Where(x => this.IsCollectedObject(x)).ToList().ForEach(x => {
|
||||
var id = this.GetKey(x);
|
||||
if (id != null) {
|
||||
if (ret == null) ret = new Dictionary<string, XElement>();
|
||||
ret.Add(id, x);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
8
HaWeb/XMLParser/IXMLService.cs
Normal file
8
HaWeb/XMLParser/IXMLService.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace HaWeb.XMLParser;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
public interface IXMLService {
|
||||
public List<IXMLRoot>? GetRoots();
|
||||
public List<XMLRootDocument>? ProbeHamannFile(XDocument document, ModelStateDictionary ModelState);
|
||||
}
|
||||
41
HaWeb/XMLParser/XMLRootDocument.cs
Normal file
41
HaWeb/XMLParser/XMLRootDocument.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace HaWeb.XMLParser;
|
||||
using System.Xml.Linq;
|
||||
|
||||
public class XMLRootDocument {
|
||||
public string Type {get; private set; }
|
||||
public DateTime Date { get; private set; }
|
||||
public XElement Root { get; private set; }
|
||||
public (string?, string) IdentificationString { get; private set; }
|
||||
|
||||
public Dictionary<string, XElement>? Elements { get; set; }
|
||||
public List<(string, string)>? Fields { get; set; }
|
||||
|
||||
public XMLRootDocument(string type, (string?, string) idString, XElement root) {
|
||||
Type = type;
|
||||
IdentificationString = idString;
|
||||
Date = DateTime.Today;
|
||||
Root = root;
|
||||
}
|
||||
|
||||
public string CreateFilename() {
|
||||
var filename = _removeInvalidChars(Type) + "_";
|
||||
if (IdentificationString.Item1 != null) filename += _removeInvalidChars(IdentificationString.Item1) + "_";
|
||||
filename += _removeInvalidChars(IdentificationString.Item2) + "_";
|
||||
filename += _removeInvalidChars(Date.Year.ToString() + "-" + Date.Month.ToString() + "-" + Date.Day.ToString());
|
||||
return filename;
|
||||
}
|
||||
|
||||
private string _removeInvalidChars(string? s) {
|
||||
if (String.IsNullOrWhiteSpace(s)) return "";
|
||||
foreach (var c in Path.GetInvalidFileNameChars()) {
|
||||
s = s.Replace(c, '-');
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public async void Save(Stream stream) {
|
||||
var nr = new XElement("opus");
|
||||
nr.AddFirst(Root);
|
||||
await nr.SaveAsync(stream, SaveOptions.DisableFormatting, new CancellationToken());
|
||||
}
|
||||
}
|
||||
72
HaWeb/XMLParser/XMLService.cs
Normal file
72
HaWeb/XMLParser/XMLService.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
namespace HaWeb.XMLParser;
|
||||
using HaWeb.Settings.XMLRoots;
|
||||
using System.Xml.Linq;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
public class XMLService : IXMLService {
|
||||
private Dictionary<string, IXMLRoot>? _Roots;
|
||||
|
||||
public XMLService() {
|
||||
var types = _GetAllTypesThatImplementInterface<IXMLRoot>().ToList();
|
||||
types.ForEach( x => {
|
||||
if (this._Roots == null) this._Roots = new Dictionary<string, IXMLRoot>();
|
||||
var instance = (IXMLRoot)Activator.CreateInstance(x)!;
|
||||
if (instance != null) this._Roots.Add(instance.Type, instance);
|
||||
});
|
||||
}
|
||||
|
||||
public List<IXMLRoot>? GetRoots() => this._Roots == null ? null : this._Roots.Values.ToList();
|
||||
|
||||
public List<XMLRootDocument>? ProbeHamannFile(XDocument document, ModelStateDictionary ModelState) {
|
||||
if (document.Root!.Name != "opus") {
|
||||
ModelState.AddModelError("Error", "A valid Hamann-Docuemnt must begin with <opus>");
|
||||
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<XMLRootDocument>? _testElements(IEnumerable<XElement>? elements) {
|
||||
if (elements == null) return null;
|
||||
List<XMLRootDocument>? res = null;
|
||||
foreach (var elem in elements) {
|
||||
var doc = _testElement(elem);
|
||||
if (doc != null) {
|
||||
if (res == null) res = new List<XMLRootDocument>();
|
||||
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<Type> _GetAllTypesThatImplementInterface<T>()
|
||||
{
|
||||
return System.Reflection.Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(type => typeof(T).IsAssignableFrom(type) && !type.IsInterface);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user