choice-parser.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { xmlParser } from '.';
  2. import { Choice, ChoiceType, XMLElement } from '../../models/evt-models';
  3. import { AttributeParser, EmptyParser } from './basic-parsers';
  4. import { createParser, parseChildren, Parser } from './parser-models';
  5. @xmlParser('choice', ChoiceParser)
  6. export class ChoiceParser extends EmptyParser implements Parser<XMLElement> {
  7. attributeParser = createParser(AttributeParser, this.genericParse);
  8. parse(xml: XMLElement): Choice {
  9. const attributes = this.attributeParser.parse(xml);
  10. const choiceComponent: Choice = {
  11. type: Choice,
  12. content: parseChildren(xml, this.genericParse),
  13. attributes,
  14. editorialInterventionType: this.getEditorialInterventionType(xml),
  15. originalContent: this.getOriginalContent(xml),
  16. normalizedContent: this.getNormalizedContent(xml),
  17. };
  18. return choiceComponent;
  19. }
  20. private getEditorialInterventionType(xml: XMLElement): ChoiceType | '' {
  21. const sicCorEls = Array.from(xml.querySelectorAll<XMLElement>('sic, corr'))
  22. .filter(el => el.parentElement === xml);
  23. if (sicCorEls.length > 0) {
  24. return 'emendation';
  25. }
  26. const origRegEls = Array.from(xml.querySelectorAll<XMLElement>('orig, reg, abbr, expan'))
  27. .filter(el => el.parentElement === xml);
  28. if (origRegEls.length > 0) {
  29. return 'normalization';
  30. }
  31. return '';
  32. }
  33. private getOriginalContent(xml: XMLElement) {
  34. return Array.from(xml.querySelectorAll<XMLElement>('orig, sic, abbr'))
  35. .filter(el => el.parentElement === xml)
  36. .map(el => this.genericParse(el));
  37. }
  38. private getNormalizedContent(xml: XMLElement) {
  39. return Array.from(xml.querySelectorAll<XMLElement>('reg, corr, expan'))
  40. .filter(el => el.parentElement === xml)
  41. .map(el => this.genericParse(el));
  42. }
  43. }