Finsihed xml model & small bugfixes

This commit is contained in:
Simon Martens
2025-03-05 18:56:34 +01:00
parent e19fd47c17
commit e839bbebe8
46 changed files with 8928 additions and 136 deletions

View File

@@ -1,6 +1,9 @@
package xmlmodels
import "encoding/xml"
import (
"encoding/json"
"encoding/xml"
)
type Letter struct {
XMLName xml.Name `xml:"letterText"`
@@ -10,6 +13,22 @@ type Letter struct {
Content string `xml:",innerxml"`
}
func (l Letter) Keys() []any {
return []any{l.Letter}
}
func (l Letter) Type() string {
return LETTER
}
func (l Letter) String() string {
json, err := json.Marshal(l)
if err != nil {
return "Cant marshal to json, Letter: " + err.Error()
}
return string(json)
}
type Page struct {
XMLName xml.Name `xml:"page"`
Index int `xml:"index,attr"`

191
xmlmodels/library.go Normal file
View File

@@ -0,0 +1,191 @@
package xmlmodels
import (
"fmt"
"path/filepath"
"strings"
"sync"
"time"
xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
)
const (
REFERENCES_PATH = "data/xml/akteure.xml"
LETTERS_PATH = "data/xml/briefe.xml"
META_PATH = "data/xml/meta.xml"
TRADITIONS_PATH = "data/xml/traditions.xml"
)
type Library struct {
mu sync.Mutex
xmlparsing.Library
Persons *xmlparsing.XMLParser[PersonDef]
Places *xmlparsing.XMLParser[LocationDef]
AppDefs *xmlparsing.XMLParser[AppDef]
Letters *xmlparsing.XMLParser[Letter]
Traditions *xmlparsing.XMLParser[Tradition]
Metas *xmlparsing.XMLParser[Meta]
}
func (l *Library) String() string {
// TODO:
return ""
}
// INFO: this is the only place where the providers are created. There is no need for locking on access.
func NewLibrary() *Library {
return &Library{
Persons: xmlparsing.NewXMLParser[PersonDef](),
Places: xmlparsing.NewXMLParser[LocationDef](),
AppDefs: xmlparsing.NewXMLParser[AppDef](),
Letters: xmlparsing.NewXMLParser[Letter](),
Traditions: xmlparsing.NewXMLParser[Tradition](),
Metas: xmlparsing.NewXMLParser[Meta](),
}
}
func (l *Library) Parse(source xmlparsing.ParseSource, baseDir, commit string) error {
// INFO: this lock prevents multiple parses from happening at the same time.
l.mu.Lock()
defer l.mu.Unlock()
wg := sync.WaitGroup{}
meta := xmlparsing.ParseMeta{
Source: source,
BaseDir: baseDir,
Commit: commit,
Date: time.Now(),
}
metamu := sync.Mutex{}
l.prepare()
wg.Add(1)
go func() {
err := l.Persons.Serialize(&PersonDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := l.Places.Serialize(&LocationDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := l.AppDefs.Serialize(&AppDefs{}, filepath.Join(meta.BaseDir, REFERENCES_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, REFERENCES_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := l.Letters.Serialize(&DocumentsRoot{}, filepath.Join(meta.BaseDir, LETTERS_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, LETTERS_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := l.Traditions.Serialize(&TraditionsRoot{}, filepath.Join(meta.BaseDir, TRADITIONS_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, TRADITIONS_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := l.Metas.Serialize(&MetaRoot{}, filepath.Join(meta.BaseDir, META_PATH), meta)
if err != nil {
metamu.Lock()
meta.FailedPaths = append(meta.FailedPaths, filepath.Join(meta.BaseDir, META_PATH))
metamu.Unlock()
}
wg.Done()
}()
wg.Wait()
l.cleanup(meta)
l.Parses = append(l.Parses, meta)
var errors []string
if len(meta.FailedPaths) > 0 {
errors = append(errors, fmt.Sprintf("Failed paths: %v", meta.FailedPaths))
}
if len(errors) > 0 {
return fmt.Errorf("Parsing encountered errors: %v", strings.Join(errors, "; "))
}
return nil
}
func (l *Library) prepare() {
l.Persons.Prepare()
l.Places.Prepare()
l.AppDefs.Prepare()
l.Letters.Prepare()
l.Traditions.Prepare()
l.Metas.Prepare()
}
func (l *Library) cleanup(meta xmlparsing.ParseMeta) {
wg := sync.WaitGroup{}
wg.Add(6)
go func() {
l.Persons.Cleanup(meta)
wg.Done()
}()
go func() {
l.Places.Cleanup(meta)
wg.Done()
}()
go func() {
l.AppDefs.Cleanup(meta)
wg.Done()
}()
go func() {
l.Letters.Cleanup(meta)
wg.Done()
}()
go func() {
l.Traditions.Cleanup(meta)
wg.Done()
}()
go func() {
l.Metas.Cleanup(meta)
wg.Done()
}()
wg.Wait()
}

View File

@@ -1,6 +1,7 @@
package xmlmodels
import (
"encoding/json"
"encoding/xml"
xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml"
@@ -16,6 +17,22 @@ type Meta struct {
Recieved []Action `xml:"recieved"`
}
func (m Meta) Keys() []any {
return []any{m.Letter}
}
func (m Meta) Type() string {
return META
}
func (m Meta) String() string {
json, err := json.Marshal(m)
if err != nil {
return "Cant marshal to json, Meta: " + err.Error()
}
return string(json)
}
type Action struct {
Dates []Date `xml:"date,attr"`
Places []RefElement `xml:"place"`

View File

@@ -1,5 +1,7 @@
package xmlmodels
import "encoding/json"
type PersonDef struct {
Index int `xml:"index,attr"`
Name string `xml:"name,attr"`
@@ -9,14 +11,62 @@ type PersonDef struct {
Comment string `xml:"komm,attr"`
}
func (p PersonDef) Keys() []any {
return []any{p.Index}
}
func (p PersonDef) Type() string {
return PERSONREF
}
func (p PersonDef) String() string {
data, err := json.Marshal(p)
if err != nil {
return "Cant marshal to json, PersonDef: " + err.Error()
}
return string(data)
}
type LocationDef struct {
Index int `xml:"index,attr"`
Name string `xml:"name,attr"`
Ref string `xml:"ref,attr"`
}
func (l LocationDef) Keys() []any {
return []any{l.Index}
}
func (l LocationDef) Type() string {
return LOCATIONREF
}
func (l LocationDef) String() string {
data, err := json.Marshal(l)
if err != nil {
return "Cant marshal to json, LocationDef: " + err.Error()
}
return string(data)
}
type AppDef struct {
Index int `xml:"index,attr"`
Name string `xml:"name,attr"`
Category string `xml:"category,attr"`
}
func (a AppDef) Keys() []any {
return []any{a.Index}
}
func (a AppDef) Type() string {
return APPREF
}
func (a AppDef) String() string {
data, err := json.Marshal(a)
if err != nil {
return "Cant marshal to json, AppDef: " + err.Error()
}
return string(data)
}

View File

@@ -22,20 +22,40 @@ type PersonDefs struct {
Persons []PersonDef `xml:"personDef"`
}
func (p PersonDefs) Children() []PersonDef {
return p.Persons
}
type LocationDefs struct {
Locations []LocationDef `xml:"locationDef"`
}
func (l LocationDefs) Children() []LocationDef {
return l.Locations
}
type AppDefs struct {
Apps []AppDef `xml:"appDef"`
}
func (a AppDefs) Children() []AppDef {
return a.Apps
}
type TraditionsRoot struct {
XMLName xml.Name `xml:"traditions"`
Traditions []Tradition `xml:"tradition"`
}
func (t TraditionsRoot) Children() []Tradition {
return t.Traditions
}
type DocumentsRoot struct {
XMLName xml.Name `xml:"document"`
Documents []Letter `xml:"letterText"`
}
func (d DocumentsRoot) Children() []Letter {
return d.Documents
}

View File

@@ -1,6 +1,9 @@
package xmlmodels
import "encoding/xml"
import (
"encoding/json"
"encoding/xml"
)
type Tradition struct {
XMLName xml.Name `xml:"letterTradition"`
@@ -8,6 +11,22 @@ type Tradition struct {
Apps []App `xml:"app"`
}
func (t Tradition) Keys() []any {
return []any{t.Letter}
}
func (t Tradition) Type() string {
return TRADITION
}
func (t Tradition) String() string {
data, err := json.Marshal(t)
if err != nil {
return "Cant marshal to json, Tradition: " + err.Error()
}
return string(data)
}
type App struct {
Reference int `xml:"ref,attr"`
Content string `xml:",innerxml"`