mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 09:15:33 +00:00
117 lines
3.7 KiB
C#
117 lines
3.7 KiB
C#
namespace HaWeb.HTMLHelpers;
|
|
using HaXMLReader.Interfaces;
|
|
using HaXMLReader.EvArgs;
|
|
using System.Text;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
|
|
public class GenericXMLHelper<T>
|
|
{
|
|
private T _caller;
|
|
private IReader _in;
|
|
private StringBuilder _target;
|
|
private List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? _OTag_Funcs;
|
|
private List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? _STag_Funcs;
|
|
private List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? _CTag_Funcs;
|
|
private List<(Func<Text, T, bool>, Action<StringBuilder, Text, T>)>? _Text_Funcs;
|
|
private List<(Func<Whitespace, T, bool>, Action<StringBuilder, Whitespace, T>)>? _WS_Funcs;
|
|
private bool _deleteLeadingWS;
|
|
private bool _deleteTrailingWS;
|
|
|
|
public GenericXMLHelper(
|
|
T caller,
|
|
IReader input,
|
|
StringBuilder target,
|
|
List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? OTag_Funcs = null,
|
|
List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? STag_Funcs = null,
|
|
List<(Func<Tag, T, bool>, Action<StringBuilder, Tag, T>)>? CTag_Funcs = null,
|
|
List<(Func<Text, T, bool>, Action<StringBuilder, Text, T>)>? Text_Funcs = null,
|
|
List<(Func<Whitespace, T, bool>, Action<StringBuilder, Whitespace, T>)>? WS_Funcs = null,
|
|
bool deleteLeadingWS = false,
|
|
bool deleteTrailingWS = false
|
|
)
|
|
{
|
|
if (input == null || target == null || caller == null) throw new ArgumentNullException();
|
|
|
|
_caller = caller;
|
|
_in = input;
|
|
_target = target;
|
|
_deleteLeadingWS = deleteLeadingWS;
|
|
_deleteTrailingWS = deleteTrailingWS;
|
|
|
|
_OTag_Funcs = OTag_Funcs;
|
|
_STag_Funcs = STag_Funcs;
|
|
_CTag_Funcs = CTag_Funcs;
|
|
_Text_Funcs = Text_Funcs;
|
|
_WS_Funcs = WS_Funcs;
|
|
|
|
if (_OTag_Funcs != null)
|
|
_in.OpenTag += OnOTag;
|
|
if (_STag_Funcs != null)
|
|
_in.SingleTag += OnSTag;
|
|
if (_CTag_Funcs != null)
|
|
_in.CloseTag += OnCTag;
|
|
if (_Text_Funcs != null)
|
|
_in.Text += OnText;
|
|
if (_WS_Funcs != null)
|
|
_in.Whitespace += OnWS;
|
|
}
|
|
|
|
void OnOTag(object _, Tag tag)
|
|
{
|
|
if (_OTag_Funcs != null)
|
|
foreach (var entry in _OTag_Funcs)
|
|
if (entry.Item1(tag, _caller)) entry.Item2(_target, tag, _caller);
|
|
}
|
|
|
|
void OnText(object _, Text text)
|
|
{
|
|
if (_deleteLeadingWS) text.Value = text.Value.TrimStart();
|
|
if (_deleteTrailingWS) text.Value = text.Value.TrimEnd();
|
|
foreach (var entry in _Text_Funcs)
|
|
if (entry.Item1(text, _caller)) entry.Item2(_target, text, _caller);
|
|
}
|
|
|
|
void OnSTag(object _, Tag tag)
|
|
{
|
|
foreach (var entry in _STag_Funcs)
|
|
if (entry.Item1(tag, _caller)) entry.Item2(_target, tag, _caller);
|
|
}
|
|
|
|
void OnCTag(object _, Tag tag)
|
|
{
|
|
foreach (var entry in _CTag_Funcs)
|
|
if (entry.Item1(tag, _caller)) entry.Item2(_target, tag, _caller);
|
|
}
|
|
|
|
void OnWS(object _, Whitespace ws)
|
|
{
|
|
foreach (var entry in _WS_Funcs)
|
|
{
|
|
if (entry.Item1(ws, _caller)) entry.Item2(_target, ws, _caller);
|
|
}
|
|
}
|
|
|
|
internal void Dispose()
|
|
{
|
|
if (_in != null)
|
|
{
|
|
if (_OTag_Funcs != null)
|
|
_in.OpenTag -= OnOTag;
|
|
if (_STag_Funcs != null)
|
|
_in.SingleTag -= OnSTag;
|
|
if (_CTag_Funcs != null)
|
|
_in.CloseTag -= OnCTag;
|
|
if (_Text_Funcs != null)
|
|
_in.Text -= OnText;
|
|
if (_WS_Funcs != null)
|
|
_in.Whitespace -= OnWS;
|
|
}
|
|
}
|
|
|
|
~GenericXMLHelper()
|
|
{
|
|
Dispose();
|
|
}
|
|
}
|