A lot of stuff related to parsing; index page input validation

This commit is contained in:
Simon Martens
2023-09-17 15:29:51 +02:00
parent d86d508786
commit b15ce8793c
23 changed files with 294 additions and 60 deletions

View File

@@ -6,10 +6,14 @@ using HaDocument.Models;
namespace HaDocument.Comparers {
public class CommentComparer : IComparer<Comment> {
public int Compare(Comment first, Comment second) {
if (first.Order != second.Order)
return first.Order.CompareTo(second.Order);
if (first.Order.HasValue && second.Order.HasValue)
return first.Order.Value.CompareTo(second.Order.Value);
else if (first.Order.HasValue)
return 1;
else if (second.Order.HasValue)
return -1;
else
return first.Index.CompareTo(second.Index);
return 0;
}
}
}

View File

@@ -5,6 +5,12 @@ VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaDocumentV6", "HaDocumentV6.csproj", "{2D3B1B5C-5E0B-4050-A102-E44F025511BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaWeb", "..\HaWeb\HaWeb.csproj", "{73B7CCC1-3486-4F6C-807C-F74BF86EE42F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HamannPrinter", "..\HamannPrinter\HamannPrinter.csproj", "{2E4EB350-E88B-40FF-BF0F-E3C98D0C1590}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaXMLReaderV6", "..\HaXMLReaderV6\HaXMLReaderV6.csproj", "{68504F7C-8919-4F92-91AA-3D2952AE942F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +21,18 @@ Global
{2D3B1B5C-5E0B-4050-A102-E44F025511BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D3B1B5C-5E0B-4050-A102-E44F025511BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D3B1B5C-5E0B-4050-A102-E44F025511BE}.Release|Any CPU.Build.0 = Release|Any CPU
{73B7CCC1-3486-4F6C-807C-F74BF86EE42F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73B7CCC1-3486-4F6C-807C-F74BF86EE42F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73B7CCC1-3486-4F6C-807C-F74BF86EE42F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73B7CCC1-3486-4F6C-807C-F74BF86EE42F}.Release|Any CPU.Build.0 = Release|Any CPU
{2E4EB350-E88B-40FF-BF0F-E3C98D0C1590}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E4EB350-E88B-40FF-BF0F-E3C98D0C1590}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E4EB350-E88B-40FF-BF0F-E3C98D0C1590}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E4EB350-E88B-40FF-BF0F-E3C98D0C1590}.Release|Any CPU.Build.0 = Release|Any CPU
{68504F7C-8919-4F92-91AA-3D2952AE942F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68504F7C-8919-4F92-91AA-3D2952AE942F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68504F7C-8919-4F92-91AA-3D2952AE942F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68504F7C-8919-4F92-91AA-3D2952AE942F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -1,5 +1,10 @@
using System.Xml.Linq;
public interface IHaElement {
public interface IHaElement : IComparable {
abstract public string ElementName { get; }
abstract public string[] XPath { get; }
abstract public string ElementRules { get; }
abstract public XElement? XElement { get; }
abstract public bool Searchable { get; }
abstract public string GetKey();
}

View File

@@ -1,13 +1,20 @@
using System.Xml.Linq;
namespace HaDocument.Models {
public class App {
public class App : IHaElement {
public string ElementName { get; } = "appDef";
public string[] XPath { get; } = {
"/opus/data/definitions/appDefs/appDef",
"/opus/definitions/appDefs/appDef"
};
public string ElementRules { get; } = "Pfad: /opus/definitions/appDefs. Pflicht-Attribute: index (einmalig), name, category.";
public bool Searchable { get; } = false;
public XElement? XElement { get; }
public string Index { get; } = "";
public string Name { get; } = "";
public string Category { get; } = "";
public XElement? XElement { get; }
public App(
string index,
string name,
@@ -28,7 +35,17 @@ namespace HaDocument.Models {
element.Attribute("name")!.Value,
element.Attribute("category")!.Value,
element
);
);
}
public string GetKey() => this.Index;
public int CompareTo(object? obj) {
if (obj == null) return 1;
var other = (App)obj;
if (Int32.TryParse(Index, out var thisindex) && Int32.TryParse(other.Index, out var otherindex) )
return thisindex.CompareTo(otherindex);
return String.Compare(this.Index, other.Index);
}
}
}

View File

@@ -1,9 +1,21 @@
using System.Xml.Linq;
namespace HaDocument.Models {
public class Backlink {
public string Href { get; } = "";
public class Backlink : IHaElement {
public string ElementName { get; } = "link";
public string[] XPath { get; } = {
"/opus/data/marginalien/marginal//link",
"/opus/marginalien/marginal//link",
"/opus/kommentare/kommentar//link",
"/opus/data/kommentare/kommentar//link",
"/opus/traditions/letterTradition//link",
"/opus/data/traditions/letterTradition//link",
};
public string ElementRules { get; } = "Pfad: /opus/marginalien, /opus/kommentare, /opus/traditions. Pflicht-Attribute: letter.";
public bool Searchable { get; } = false;
public XElement? XElement { get; } = null;
public string Href { get; } = "";
public string? Letter { get; } = "";
public string? Page { get; } = "";
public string? Line { get; } = "";
@@ -14,13 +26,15 @@ namespace HaDocument.Models {
string? letter,
string? page,
string? line,
string? comment = null
string? comment = null,
XElement? xElement = null
) {
Href = href;
Letter = letter;
Page = page;
Line = line;
Comment = comment;
XElement = xElement;
}
public static Backlink? FromXElement(XElement element) {
@@ -34,7 +48,9 @@ namespace HaDocument.Models {
element.Attribute("subref")?.Value ?? element.Attribute("ref")!.Value,
marginal.Attribute("letter")!.Value,
marginal.Attribute("page")!.Value,
marginal.Attribute("line")!.Value
marginal.Attribute("line")!.Value,
null,
element
);
}
@@ -46,7 +62,8 @@ namespace HaDocument.Models {
null,
null,
null,
subsection.Attribute("id")!.Value
subsection.Attribute("id")!.Value,
element
);
}
@@ -58,11 +75,20 @@ namespace HaDocument.Models {
null,
null,
null,
kommentar.Attribute("id")!.Value
kommentar.Attribute("id")!.Value,
element
);
}
return null;
}
public string GetKey() {
return string.Empty;
}
public int CompareTo(object? obj) {
return 0;
}
}
}

View File

@@ -1,25 +1,36 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Xml.Linq;
namespace HaDocument.Models {
public class Comment : IHaElement {
public string ElementName { get; } = "link";
public string[] XPath { get; } = {
"/opus/kommentare/kommentar/kommcat/kommentar",
"/opus/data/kommentare/kommentar/kommcat/kommentar",
};
public string ElementRules { get; } = "Pfad: /opus/kommentare/kommentar. Pflicht-Attribute: id (einmalig).";
public bool Searchable { get; } = true;
public XElement? XElement { get; }
namespace HaDocument.Models{
public class Comment {
public string Element { get; } = "";
public string Index { get; } = "";
public string Type { get; } = "";
public string Lemma { get; } = "";
public string Parent { get; } = "";
public int Order { get; } = -1;
public ImmutableSortedDictionary<string, Comment> Kommentare { get; }
public int? Order { get; } = null;
public ImmutableSortedDictionary<string, Comment>? Kommentare { get; }
public Comment(
string entry,
string index,
string type,
string lemma,
int order,
SortedDictionary<string, Comment> subComments,
string parent=""
string? type,
string? lemma,
int? order,
ImmutableSortedDictionary<string, Comment>? subComments,
string? parent = null,
XElement? xelement = null
) {
Element = entry;
Index = index;
@@ -27,10 +38,58 @@ namespace HaDocument.Models{
Lemma = lemma;
Order = order;
Parent = parent;
if (subComments != null)
Kommentare = ImmutableSortedDictionary.ToImmutableSortedDictionary(subComments);
else
Kommentare = null;
XElement = xelement;
Kommentare = subComments;
}
public String GetKey() => Index;
public int CompareTo(object? obj) {
if (obj == null) return 1;
var other = (Comment)obj;
if (!String.IsNullOrWhiteSpace(Parent) && !String.IsNullOrWhiteSpace(other.Parent) &&
(Parent == other.Parent)) {
if (Order.HasValue && other.Order.HasValue)
return Order.Value!.CompareTo(other.Order.Value);
else if (Order.HasValue)
return 1;
else if (other.Order.HasValue)
return -1;
else
return 0;
}
return String.Compare(Index, other.Index);
}
public Comment? FromXElement(XElement? element) {
if (element == null || !element.HasAttributes || element.IsEmpty) return null;
if (element.Attribute("id")?.Value == null) return null;
var cat = element.Ancestors("kommcat");
if (element.Name == "kommentar")
return new Comment(
element.ToString(),
element.Attribute("id")!.Value,
(cat.Any() ? cat.First().Attribute("value")?.Value : null) ?? element.Attribute("type")?.Value,
element.Element("lemma")?.Value,
element.Attribute("sort")?.Value != null ? (Int32.TryParse(element.Attribute("sort")!.Value, out var s) ? s : null) : null,
element.Elements("subsection").Any() ? element.Elements("subsection").Select(x => FromXElement(x)).ToImmutableSortedDictionary(x => x.Index, y => y) : null,
null,
element
);
else if (element.Name == "subsection") {
if (element.Ancestors("kommentar").Any() || element.Ancestors("kommentar")!.First().Attribute("id")?.Value == null) return null;
return new Comment(
element.ToString(),
element.Attribute("id")!.Value,
(cat.Any() ? cat.First().Attribute("value")?.Value : null) ?? element.Attribute("type")?.Value,
element.Element("lemma")?.Value,
element.Attribute("sort")?.Value != null ? (Int32.TryParse(element.Attribute("sort")!.Value, out var s) ? s : null) : null,
null,
element.Ancestors("kommentar")!.First().Attribute("id")!.Value,
element
);
}
return null;
}
}
}

View File

@@ -31,8 +31,9 @@ namespace HaDocument.Models {
Reference = reference;
}
public static Editreason? FromXElement(XElement element) {
throw new NotImplementedException("We need two Elements for editreason");
public static Editreason? FromXElement(XElement edit, XElement editreason) {
if (edit == null || editreason == null || !edit.HasAttributes || !editreason.HasAttributes) return null;
return null;
}
}
}

View File

@@ -4,6 +4,7 @@ using HaDocument.Models;
using System.Collections.Generic;
using System;
using HaDocument.Comparers;
using System.Collections.Immutable;
namespace HaDocument.Reactors {
class CommentReactor : Reactor {
@@ -149,7 +150,7 @@ namespace HaDocument.Reactors {
Type,
Lemma,
Order,
Subcomments
Subcomments == null ? null : ImmutableSortedDictionary.ToImmutableSortedDictionary(Subcomments, x => x.Key, y => y.Value)
));
Reset();
}