Moved unused code into Achive

This commit is contained in:
Simon Martens
2022-11-25 19:15:23 +01:00
parent 5cc684550b
commit 02abfb111a
239 changed files with 51948 additions and 1673 deletions

View File

@@ -0,0 +1,178 @@
using HaXMLReader.Interfaces;
using HaXMLReader.EvArgs;
using HaDocument.Models;
using System.Collections.Generic;
using System;
using HaDocument.Comparers;
namespace HaDocument.Reactors {
class CommentReactor : Reactor {
Dictionary<string, Comment> CreatedInstances;
private bool _normalizeWhitespace = false;
// State
private string Type = "";
private string Index = "";
private string Lemma = "";
private string Entry = "";
private int Order = -1;
private SortedDictionary<string, Comment> Subcomments;
private bool subsection = false;
private string subsectionIndex = "";
private string subsectionLemma = "";
private string subsectionEntry = "";
private int subsectionOrder = -1;
internal CommentReactor(IReader reader, IntermediateLibrary lib, bool normalizeWhitespace) : base(reader, lib) {
_normalizeWhitespace = normalizeWhitespace;
_lib.Comments = _lib.Comments ?? new Dictionary<string, Comment>();
CreatedInstances = _lib.Comments;
reader.OpenTag += Listen;
}
protected override void Listen(object _, Tag tag) {
if (
!tag.IsEmpty &&
!tag.EndTag &&
tag.Name == "kommcat" &&
!string.IsNullOrWhiteSpace(tag["value"])
) {
Type = tag["value"];
Activate(_reader, tag);
}
else if (
tag.EndTag &&
tag.Name == "kommcat"
) {
ResetType();
Deactivate();
}
}
protected override void Activate(IReader _, Tag tag) {
_reader.Tag += OnTag;
}
private void OnTag(object _, Tag tag) {
if (
!tag.IsEmpty &&
!tag.EndTag &&
tag.Name == "kommentar" &&
!string.IsNullOrWhiteSpace(tag["id"])
) {
Index = tag["id"];
Order = String.IsNullOrWhiteSpace(tag["sort"]) ? 0 : Int32.Parse(tag["sort"]);
}
else if (
tag.EndTag &&
tag.Name == "kommentar"
) {
AddComment();
}
else if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "lemma"
) {
_ = new ElementStringBinder(_reader, tag, AddLemma, _normalizeWhitespace);
}
else if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "eintrag"
) {
_ = new ElementStringBinder(_reader, tag, AddEntry, _normalizeWhitespace);
}
else if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "subsection"
) {
if (!String.IsNullOrWhiteSpace(tag["id"])) subsectionIndex = tag["id"];
if (!String.IsNullOrWhiteSpace(tag["sort"])) subsectionOrder = Int32.Parse(tag["sort"]);
if (Subcomments ==null) Subcomments = new SortedDictionary<string, Comment>();
subsection = true;
}
else if (
tag.EndTag &&
tag.Name == "subsection"
) {
subsection = false;
AddSubsection();
}
}
private void Deactivate() {
_reader.Tag -= OnTag;
}
private void AddLemma(string str) {
if (String.IsNullOrEmpty(str)) return;
if (subsection) subsectionLemma = str;
else Lemma = str;
}
private void AddEntry(string str) {
if (String.IsNullOrWhiteSpace(str)) str = "";
if (subsection) subsectionEntry = str;
else Entry = str;
}
private void AddSubsection() {
if (String.IsNullOrWhiteSpace(subsectionIndex)) return;
if (String.IsNullOrWhiteSpace(subsectionLemma)) return;
if (!Subcomments.ContainsKey(subsectionIndex))
{
Subcomments.Add(subsectionIndex, new Comment(
subsectionEntry,
subsectionIndex,
Type,
subsectionLemma,
subsectionOrder,
null,
Index
));
}
ResetSubsection();
}
private void AddComment() {
if (String.IsNullOrWhiteSpace(Index)) return;
if (String.IsNullOrWhiteSpace(Lemma)) return;
if (CreatedInstances.ContainsKey(Index)) return;
CreatedInstances.Add(Index, new Comment(
Entry,
Index,
Type,
Lemma,
Order,
Subcomments
));
Reset();
}
protected override void Reset() {
Index = "";
Lemma = "";
Entry = "";
Order = -1;
Subcomments = null;
ResetSubsection();
}
private void ResetSubsection() {
subsection = false;
subsectionEntry = "";
subsectionIndex = "";
subsectionLemma = "";
subsectionOrder = -1;
}
private void ResetType() {
Type = "";
}
}
}

View File

@@ -0,0 +1,154 @@
using HaDocument.Models;
using System.Collections.Generic;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using System;
namespace HaDocument.Reactors {
class EditreasonReactor : Reactor {
internal Dictionary<string, Editreason> CreatedInstances;
private Dictionary<string, string[]> _intermediateReasons = new Dictionary<string, string[]>();
private bool _normalizeWhitespace = false;
// State
private string Index = "";
private string letter = "";
private string page = "";
private string line = "";
private Stack<string> index;
private ElementStringBinder _element = null;
internal EditreasonReactor(IReader reader, IntermediateLibrary lib, bool normalizeWhitespace) : base (reader, lib) {
CreatedInstances = lib.Editreasons;
index = new Stack<string>();
_normalizeWhitespace = normalizeWhitespace;
lib.Editreasons = new Dictionary<string, Editreason>();
CreatedInstances = lib.Editreasons;
reader.Tag += Listen;
reader.ReadingStop += Scaffold;
}
protected void Scaffold(object _, EventArgs __) {
foreach (var entry in _intermediateReasons) {
if (
!String.IsNullOrWhiteSpace(entry.Value[0]) &&
!String.IsNullOrWhiteSpace(entry.Value[1]) &&
!String.IsNullOrWhiteSpace(entry.Value[2]) &&
!String.IsNullOrWhiteSpace(entry.Value[3])
) {
CreatedInstances.Add(
entry.Key,
new Editreason(
entry.Key,
entry.Value[0],
entry.Value[1],
entry.Value[2],
entry.Value[3],
entry.Value[5],
entry.Value[6],
entry.Value[4]
)
);
}
}
}
protected override void Listen(object sender, Tag tag) {
if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "editreason" &&
!String.IsNullOrWhiteSpace(tag["index"])
) {
Activate(_reader, tag);
}
else if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "letterText"
) {
letter = tag["index"];
}
else if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "letterTradition"
) {
letter = tag["ref"];
}
else if (
!tag.EndTag &&
tag.IsEmpty &&
tag.Name == "page"
) {
page = tag["index"];
}
else if (
!tag.EndTag &&
tag.IsEmpty &&
tag.Name == "line"
) {
line = tag["index"];
}
else if (
!tag.EndTag &&
tag.Name == "edit" &&
!String.IsNullOrWhiteSpace(tag["ref"])
) {
if (!_intermediateReasons.ContainsKey(tag["ref"])) {
_intermediateReasons.Add(tag["ref"], new string[7]);
}
_intermediateReasons[tag["ref"]][1] = letter;
_intermediateReasons[tag["ref"]][2] = page;
_intermediateReasons[tag["ref"]][3] = line;
if (!tag.IsEmpty) {
index.Push(tag["ref"]);
new ElementStringBinder(_reader, tag, AddReference);
}
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
_active = true;
_reader = reader;
Index = tag["index"];
_element = new ElementStringBinder(reader, tag, Add, _normalizeWhitespace);
}
}
private void Add(string element) {
if (String.IsNullOrWhiteSpace(element)) return;
if (!_intermediateReasons.ContainsKey(Index)) {
_intermediateReasons.Add(Index, new string[7]);
}
_intermediateReasons[Index][0] = element;
Reset();
}
private void AddReference(string element) {
if (String.IsNullOrWhiteSpace(element)) return;
var ci = index.Pop();
if (!_intermediateReasons.ContainsKey(ci)) {
_intermediateReasons.Add(ci, new string[7]);
}
_intermediateReasons[ci][4] = element;
_intermediateReasons[ci][5] = page;
_intermediateReasons[ci][6] = line;
}
protected override void Reset() {
Index = "";
_active = false;
_element = null;
}
protected void Deactivate() {
_element.Unsubscribe();
Reset();
}
}
}

View File

@@ -0,0 +1,99 @@
using System;
using HaDocument.Models;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using System.Text;
using System.Xml;
using System.Collections.Generic;
namespace HaDocument.Reactors {
class ElementStringBinder {
private StringBuilder _stringBuilder;
private XmlWriter _txtwriter;
private Stack<Tag> _stack;
private IReader _reader;
private Action<string> _callback;
private bool _normalizeWhitespace = false;
internal ElementStringBinder(IReader reader, Tag start, Action<string> callback) {
_reader = reader;
_callback = callback;
_stack = new Stack<Tag>();
_stringBuilder = new StringBuilder();
_txtwriter = XmlWriter.Create(_stringBuilder, new XmlWriterSettings() {
CheckCharacters = false,
ConformanceLevel = ConformanceLevel.Fragment,
Encoding = Encoding.UTF8,
OmitXmlDeclaration = true,
NewLineHandling = NewLineHandling.None
});
_stack.Push(start);
_writeTag(start);
Subscribe();
}
internal ElementStringBinder(IReader reader, Tag start, Action<string> callback, bool normalizeWhitespace) : this(reader, start, callback) {
_normalizeWhitespace = normalizeWhitespace;
}
internal void Subscribe() {
_reader.Tag += OnTag;
_reader.Text += OnText;
_reader.Whitespace += OnWhiteSpace;
}
internal void Unsubscribe() {
_reader.Tag -= OnTag;
_reader.Text -= OnText;
_reader.Whitespace -= OnWhiteSpace;
}
internal string GetElementText() {
_txtwriter.Flush();
_txtwriter.Dispose();
Unsubscribe();
var ret = _stringBuilder.ToString();
_stringBuilder = new StringBuilder();
return ret;
}
private void _writeTag(Tag tag) {
var writer = _txtwriter;
if (tag.EndTag) {
writer.WriteEndElement();
return;
}
writer.WriteStartElement(tag.Name);
if (tag.Values != null && tag.Values.Count > 0)
foreach (var kv in tag.Values)
writer.WriteAttributeString(kv.Key, kv.Value);
if ( tag.IsEmpty )
writer.WriteEndElement();
}
private void OnTag(object _, Tag tag) {
_writeTag(tag);
if (!tag.IsEmpty && tag.EndTag) _stack.Pop();
else if (!tag.IsEmpty) _stack.Push(tag);
if (_stack.Count == 0 ) _callback(GetElementText());
}
private void OnText(object _, Text text) {
if (_normalizeWhitespace) {
var neu = text.Value.Replace('\n', ' ').Replace("\t", "").Replace("\r", "");
_txtwriter.WriteString(neu);
}
else
_txtwriter.WriteString(text.Value);
}
private void OnWhiteSpace(object _, Whitespace ws) {
if (_normalizeWhitespace)
_txtwriter.WriteWhitespace(" ");
else
_txtwriter.WriteWhitespace(ws.Value);
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using HaDocument.Models;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace HaDocument.Reactors {
class HandDefsReactor : Reactor {
internal Dictionary<string, Person> CreatedInstances;
// State
private string Index;
private string Name;
internal HandDefsReactor(IReader reader, IntermediateLibrary lib) : base(reader, lib) {
lib.HandPersons = new Dictionary<string, Person>();
CreatedInstances = lib.HandPersons;
reader.Tag += Listen;
}
protected override void Listen(object sender, Tag tag) {
if (
!tag.EndTag &&
tag.IsEmpty &&
tag.Name == "handDef" &&
!String.IsNullOrWhiteSpace(tag["index"]) &&
!String.IsNullOrWhiteSpace(tag["name"])
) {
Activate(_reader, tag);
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
Reset();
_active = true;
Index = tag["index"];
Name = tag["name"];
Add();
_active = false;
}
}
protected override void Reset() {
Index = "";
Name = "";
}
protected void Add() {
CreatedInstances.Add(Index, new Person(Index, Name, "", ""));
}
}
}

View File

@@ -0,0 +1,151 @@
using System;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using HaDocument.Models;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Linq;
namespace HaDocument.Reactors
{
class LetterReactor : Reactor
{
internal Dictionary<string, Letter> CreatedInstances;
internal Dictionary<string, List<Hand>> CreatedHands;
internal Dictionary<string, Dictionary<string, HashSet<string>>> CreatedStructure;
// State
private string Index = "";
private ElementStringBinder _element = null;
private bool _normalizeWhitespace = false;
private string _page = "";
private string _line = "";
private List<Hand> _hands;
private string _person = "";
private string _handstartpg = "";
private string _handstartln = "";
internal LetterReactor(IReader reader, IntermediateLibrary lib, bool normalizeWhitespace) : base(reader, lib)
{
_normalizeWhitespace = normalizeWhitespace;
lib.Letters = new Dictionary<string, Letter>();
if (lib.Hands == null)
lib.Hands = new Dictionary<string, List<Hand>>();
if (lib.LetterPageLines == null)
lib.LetterPageLines = new Dictionary<string, Dictionary<string, HashSet<string>>>();
CreatedHands = lib.Hands;
CreatedInstances = lib.Letters;
CreatedStructure = lib.LetterPageLines;
reader.OpenTag += Listen;
reader.SingleTag += Listen;
reader.CloseTag += Listen;
}
protected override void Listen(object sender, Tag tag)
{
if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "letterText" &&
!String.IsNullOrWhiteSpace(tag["index"])
)
{
Activate(_reader, tag);
if (!CreatedStructure.ContainsKey(tag["index"]))
this.CreatedStructure.Add(tag["index"], new Dictionary<string, HashSet<string>>());
}
else if (
!tag.EndTag &&
_active &&
tag.Name == "line" &&
!String.IsNullOrWhiteSpace(tag["index"])
)
{
_line = tag["index"];
if (!CreatedStructure[Index][_page].Contains(_line))
{
CreatedStructure[Index][_page].Add(_line);
}
}
else if (
!tag.EndTag &&
_active &&
tag.Name == "page" &&
!String.IsNullOrWhiteSpace(tag["index"])
)
{
_page = tag["index"];
if (!CreatedStructure[Index].ContainsKey(_page))
CreatedStructure[Index].Add(_page, new HashSet<string>());
}
else if (
_active &&
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "hand" &&
!String.IsNullOrWhiteSpace(tag["ref"])
)
{
_person = tag["ref"];
_handstartln = _line;
_handstartpg = _page;
}
else if (
_active &&
tag.EndTag &&
tag.Name == "hand"
)
{
if (_hands == null)
_hands = new List<Hand>();
_hands.Add(new Hand(Index, _person, _handstartpg, _handstartln, _page, _line));
}
}
protected override void Activate(IReader reader, Tag tag)
{
if (!_active && reader != null && tag != null)
{
_active = true;
_reader = reader;
Index = tag["index"];
_element = new ElementStringBinder(reader, tag, Add, _normalizeWhitespace);
}
}
private void Add(string text)
{
if (String.IsNullOrWhiteSpace(text)) return;
var letter = new Letter(
Index,
text
);
CreatedInstances.TryAdd(Index, letter);
if (_hands != null)
{
if (!CreatedHands.ContainsKey(Index))
CreatedHands.Add(Index, _hands);
else
CreatedHands[Index].AddRange(_hands);
}
Reset();
}
protected override void Reset()
{
Index = "";
_active = false;
_element = null;
_hands = null;
}
protected void Deactivate()
{
_element.Unsubscribe();
Reset();
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using HaDocument.Models;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace HaDocument.Reactors {
class LocationDefsReactor : Reactor {
internal Dictionary<string, Location> CreatedInstances;
// State
private string Index;
private string Name;
internal LocationDefsReactor(IReader reader, IntermediateLibrary lib) : base(reader, lib) {
lib.Locations = new Dictionary<string, Location>();
CreatedInstances = lib.Locations;
reader.Tag += Listen;
}
protected override void Listen(object sender, Tag tag) {
if (
!tag.EndTag &&
tag.IsEmpty &&
tag.Name == "locationDef" &&
!String.IsNullOrWhiteSpace(tag["index"]) &&
!String.IsNullOrWhiteSpace(tag["name"])
) {
Activate(_reader, tag);
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
Reset();
_active = true;
Index = tag["index"];
Name = tag["name"];
Add();
_active = false;
}
}
protected override void Reset() {
Index = "";
Name = "";
}
protected void Add() {
CreatedInstances.Add(Index, new Location(Index, Name));
}
}
}

View File

@@ -0,0 +1,100 @@
using HaDocument.Models;
using System.Collections.Generic;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using System;
namespace HaDocument.Reactors {
class MarginalReactor : Reactor {
internal Dictionary<string, Marginal> CreatedInstances;
internal Dictionary<string, List<Backlink>> CreatedBacklinks;
private bool _normalizeWhitespace = false;
// State
private string Index = "";
private string Letter = "";
private string Page = "";
private string Line = "";
private ElementStringBinder _element = null;
internal MarginalReactor(IReader reader, IntermediateLibrary lib, bool normalizeWhitespace) : base(reader, lib) {
_normalizeWhitespace = normalizeWhitespace;
lib.Marginals = new Dictionary<string, Marginal>();
lib.Backlinks = new Dictionary<string, List<Backlink>>();
CreatedBacklinks = lib.Backlinks;
CreatedInstances = lib.Marginals;
reader.OpenTag += Listen;
}
protected override void Listen(object sender, Tag tag) {
if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "marginal" &&
!String.IsNullOrEmpty(tag["index"]) &&
!String.IsNullOrWhiteSpace(tag["letter"]) &&
!String.IsNullOrWhiteSpace(tag["page"]) &&
!String.IsNullOrWhiteSpace(tag["line"])
) {
Activate(_reader, tag);
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
_active = true;
_reader = reader;
Index = tag["index"];
Letter = tag["letter"];
Page = tag["page"];
Line = tag["line"];
_element = new ElementStringBinder(_reader, tag, Add, _normalizeWhitespace);
reader.Tag += OnTag;
}
}
private void OnTag(object _, Tag tag) {
if(
!tag.EndTag &&
tag.Name =="link"
) {
var id = "";
if (tag.Values.ContainsKey("subref"))
id = tag["subref"];
else if (tag.Values.ContainsKey("ref"))
id = tag["ref"];
if (!String.IsNullOrWhiteSpace(id)) {
if (!CreatedBacklinks.ContainsKey(id))
CreatedBacklinks.Add(id, new List<Backlink>());
CreatedBacklinks[id].Add(new Backlink(id, Letter, Page, Line, Index));
}
}
}
private void Add(string element) {
if (String.IsNullOrWhiteSpace(element)) return;
var marg = new Marginal(
Index,
Letter,
Page,
Line,
element
);
CreatedInstances.TryAdd(Index, marg);
Reset();
}
protected override void Reset() {
Index = "";
_active = false;
_element = null;
_reader.Tag -= OnTag;
}
public void Deactivate() {
_element.Unsubscribe();
Reset();
}
}
}

View File

@@ -0,0 +1,196 @@
using System;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using HaDocument.Models;
using System.Collections.Generic;
using System.Linq;
namespace HaDocument.Reactors {
class MetaReactor : Reactor {
internal Dictionary<string, Meta> CreatedInstances { get; }
private string[] _availableVolumes;
private (int, int) _availableYearRange;
// State
private string Index { get; set; } = "";
private string Autopsic { get; set; } = "";
private string Volume { get; set; } = "";
private string Page { get; set; } = "";
private string Date { get; set; } = "";
private DateTime Sort { get; set; } = new DateTime(1700, 1, 1);
private int Order { get; set; } = -1;
private bool AltLineNumbering { get; set; } = false;
private bool inZH { get; set; } = false;
private OptionalBool hasOriginal { get; set; } = OptionalBool.None;
private OptionalBool isProofread { get; set; } = OptionalBool.None;
private OptionalBool isDraft { get; set; } = OptionalBool.None;
private bool dateChanged {get; set; } = false;
private string Location { get; set; } = "";
private List<string> Senders { get; set; } = null;
private List<string> Receivers { get; set; } = null;
internal MetaReactor(IReader reader, IntermediateLibrary lib, string[] availableVolumes, (int, int) availableYearRange) : base(reader, lib) {
_availableVolumes = availableVolumes;
_availableYearRange = availableYearRange;
lib.Metas = new Dictionary<string, Meta>();
CreatedInstances = lib.Metas;
reader.OpenTag += Listen;
}
protected override void Listen(object sender, Tag tag) {
if (
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name =="letterDesc" &&
!String.IsNullOrWhiteSpace(tag["ref"])
) {
Activate(_reader, tag);
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
Reset();
_active = true;
Index = tag["ref"];
reader.Tag += OnTag;
_reader = reader;
}
}
public void Deactivate() {
if (_active) {
Abort();
Add();
}
}
public void Abort() {
if (_active) {
_active = false;
_reader.Tag -= OnTag;
}
}
private void OnTag(object _, Tag tag) {
switch (tag.Name)
{
case "autopsic":
Autopsic = tag["value"];
if (String.IsNullOrWhiteSpace(Autopsic)) Abort();
break;
case "begin":
Page = tag["page"];
Volume = tag["vol"];
break;
case "date":
Date = tag["value"];
break;
case "location":
Location = tag["ref"];
break;
case "sender":
if (!String.IsNullOrWhiteSpace(tag["ref"])) Senders.Add(tag["ref"]);
break;
case "receiver":
if (!String.IsNullOrWhiteSpace(tag["ref"])) Receivers.Add(tag["ref"]);
break;
case "sort":
DateTime res;
System.DateTime.TryParse(tag["value"], out res);
Sort = res;
int res2 = 0;
Int32.TryParse(tag["order"], out res2);
Order = res2;
break;
case "hasOriginal":
var val = tag["value"];
if (val.ToLower() == "true")
hasOriginal = OptionalBool.True;
else if (val.ToLower() == "false")
hasOriginal = OptionalBool.False;
else
hasOriginal = OptionalBool.None;
break;
case "isProofread":
var val2 = tag["value"];
if (val2.ToLower() == "true")
isProofread = OptionalBool.True;
else if (val2.ToLower() == "false")
isProofread = OptionalBool.False;
else
isProofread = OptionalBool.None;
break;
case "isDraft":
var val3 = tag["value"];
if (val3.ToLower() == "true")
isDraft = OptionalBool.True;
else if (val3.ToLower() == "false")
isDraft = OptionalBool.False;
else
isDraft = OptionalBool.None;
break;
case "ZHInfo":
if (!tag.EndTag) {
inZH = tag["inzh"] == "false" ? false : true;
}
break;
case "dateChanged":
dateChanged = tag["value"].ToLower() == "true" ? true : false;
break;
case "alternativeLineNumbering":
AltLineNumbering = tag["value"].ToLower() == "true" ? true : false;
break;
case "letterDesc":
if (tag.EndTag) Deactivate();
break;
}
}
private void Add() {
if (
_availableVolumes.Contains(Volume) ||
(Sort.Year >= _availableYearRange.Item1 && Sort.Year <= _availableYearRange.Item2) ||
(_availableVolumes == null && _availableYearRange.Item1 == 0 && _availableYearRange.Item2 == 0)
) {
var ZHInfo = !inZH ? null : new ZHInfo(AltLineNumbering, dateChanged, Volume, Page);
var meta = new Meta(
Index,
Autopsic,
Date,
Sort,
Order,
hasOriginal,
isProofread,
isDraft,
Location,
Senders,
Receivers,
ZHInfo
);
CreatedInstances.TryAdd(meta.Index, meta);
}
}
protected override void Reset() {
inZH = true;
hasOriginal = OptionalBool.None;
isProofread = OptionalBool.None;
isDraft = OptionalBool.None;
dateChanged = false;
Index = "";
Autopsic = "";
Volume = "";
Page = "";
Date = "";
DateTime Sort = new DateTime(1700, 1, 1);
Order = -1;
AltLineNumbering = false;
Location = "";
Senders = new List<string>();
Receivers = new List<string>();
}
}
}

View File

@@ -0,0 +1,60 @@
using System;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using HaDocument.Models;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace HaDocument.Reactors {
class PersonDefsReactor : Reactor {
internal Dictionary<string, Person> CreatedInstances;
// State
private string Index;
private string Name;
private string Prename = "";
private string Surname = "";
internal PersonDefsReactor(IReader reader, IntermediateLibrary lib) : base(reader, lib) {
lib.Persons = new Dictionary<string, Person>();
CreatedInstances = lib.Persons;
reader.Tag += Listen;
}
protected override void Listen(object sender, Tag tag) {
if (!tag.EndTag &&
tag.IsEmpty &&
tag.Name == "personDef" &&
!String.IsNullOrWhiteSpace(tag["index"]) &&
!String.IsNullOrWhiteSpace(tag["name"])
) {
Activate(_reader, tag);
}
}
protected override void Activate(IReader reader, Tag tag) {
if (!_active && reader != null && tag != null) {
Reset();
_active = true;
Index = tag["index"];
Name = tag["name"];
Prename = tag["vorname"];
Surname = tag["nachname"];
Add();
_active = false;
}
}
protected override void Reset() {
Index = "";
Name = "";
Prename = "";
Surname = "";
}
public void Add() {
CreatedInstances.Add(Index, new Person(Index, Name, Prename, Surname));
}
}
}

View File

@@ -0,0 +1,20 @@
using HaDocument.Models;
using HaXMLReader.Interfaces;
using HaXMLReader.EvArgs;
namespace HaDocument.Reactors {
abstract class Reactor {
protected IReader _reader;
protected IntermediateLibrary _lib;
protected bool _active = false;
internal Reactor(IReader reader, IntermediateLibrary lib) {
_reader = reader;
_lib = lib;
}
protected abstract void Listen(object sender, Tag tag);
protected abstract void Activate(IReader reader, Tag tag);
protected abstract void Reset();
}
}

View File

@@ -0,0 +1,156 @@
using HaDocument.Models;
using System.Collections.Generic;
using HaXMLReader.EvArgs;
using HaXMLReader.Interfaces;
using System;
namespace HaDocument.Reactors
{
class TraditionsReactor : Reactor
{
internal Dictionary<string, Tradition> CreatedInstances;
internal Dictionary<string, Dictionary<string, HashSet<string>>> CreatedStructure;
internal Dictionary<string, List<Hand>> CreatedHands;
private bool _normalizeWhitespace = false;
// State
private string Index = "";
private string _page = "";
private string _line = "";
private List<Hand> _hands;
private string _person = "";
private string _handstartpg = "";
private string _handstartln = "";
private ElementStringBinder _element = null;
internal TraditionsReactor(IReader reader, IntermediateLibrary lib, bool normalizeWhitespace) : base(reader, lib)
{
_normalizeWhitespace = normalizeWhitespace;
_lib.Traditions = new Dictionary<string, Tradition>();
if (lib.LetterPageLines == null)
lib.LetterPageLines = new Dictionary<string, Dictionary<string, HashSet<string>>>();
if (lib.Hands == null)
lib.Hands = new Dictionary<string, List<Hand>>();
CreatedHands = lib.Hands;
CreatedInstances = _lib.Traditions;
CreatedStructure = lib.LetterPageLines;
reader.Tag += Listen;
}
protected override void Listen(object sender, Tag tag)
{
if (!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "letterTradition" &&
!String.IsNullOrWhiteSpace(tag["ref"])
)
{
Activate(_reader, tag);
}
else if (
!tag.EndTag &&
_active &&
tag.Name == "ZHText"
)
{
if (!CreatedStructure.ContainsKey(tag["index"]))
this.CreatedStructure.Add(tag["index"], new Dictionary<string, HashSet<string>>());
}
else if (
!tag.EndTag &&
_active &&
tag.Name == "line" &&
!String.IsNullOrWhiteSpace(tag["index"])
)
{
_line = tag["index"];
if (!CreatedStructure[Index][_page].Contains(_line))
{
CreatedStructure[Index][_page].Add(_line);
}
}
else if (
!tag.EndTag &&
_active &&
tag.Name == "page" &&
!String.IsNullOrWhiteSpace(tag["index"])
)
{
_page = tag["index"];
if (!CreatedStructure[Index].ContainsKey(_page))
{
CreatedStructure[Index].Add(_page, new HashSet<string>());
}
}
else if (
_active &&
!tag.EndTag &&
!tag.IsEmpty &&
tag.Name == "hand" &&
!String.IsNullOrWhiteSpace(tag["ref"])
)
{
_person = tag["ref"];
_handstartln = _line;
_handstartpg = _page;
}
else if (
_active &&
tag.EndTag &&
tag.Name == "hand"
)
{
if (_hands == null)
_hands = new List<Hand>();
_hands.Add(new Hand(Index, _person, _handstartpg, _handstartln, _page, _line));
}
}
protected override void Activate(IReader reader, Tag tag)
{
if (!_active && reader != null && tag != null)
{
_active = true;
Index = tag["ref"];
_element = new ElementStringBinder(reader, tag, Add, _normalizeWhitespace);
}
}
private void Add(string element)
{
if (String.IsNullOrWhiteSpace(element)) return;
var reason = new Tradition(
Index,
element);
CreatedInstances.TryAdd(Index, reason);
if (_hands != null)
{
if (!CreatedHands.ContainsKey(Index))
CreatedHands.Add(Index, _hands);
else
CreatedHands[Index].AddRange(_hands);
}
Reset();
}
protected override void Reset()
{
Index = "";
_page = "";
_line = "";
_active = false;
_element = null;
_hands = null;
}
protected void Deactivate()
{
_element.Unsubscribe();
Reset();
}
}
}