facsimile-parser.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { xmlParser } from '.';
  2. import { Graphic, Point, Surface, XMLElement, Zone, ZoneHotSpot, ZoneLine, ZoneRendition } from '../../models/evt-models';
  3. import { AttributeParser, EmptyParser } from './basic-parsers';
  4. import { createParser, getID, parseChildren, Parser } from './parser-models';
  5. @xmlParser('zone', ZoneParser)
  6. export class ZoneParser extends EmptyParser implements Parser<XMLElement> {
  7. attributeParser = createParser(AttributeParser, this.genericParse);
  8. public parse(xml: XMLElement): Zone {
  9. let coords: Point[];
  10. const attributes = this.attributeParser.parse(xml);
  11. if (xml.getAttribute('points')) {
  12. coords = attributes.points.split(' ')
  13. .map(stringPoint => {
  14. const points = stringPoint.split(',');
  15. return {
  16. x: parseInt(points[0], 10),
  17. y: parseInt(points[1], 10),
  18. };
  19. });
  20. } else {
  21. const ul: Point = {
  22. x: parseFloat(attributes.ulx) || undefined,
  23. y: parseFloat(attributes.uly) || undefined,
  24. };
  25. const lr: Point = {
  26. x: parseFloat(attributes.lrx) || undefined,
  27. y: parseFloat(attributes.lry) || undefined,
  28. };
  29. const ur: Point = {
  30. x: lr.x,
  31. y: ul.y,
  32. };
  33. const ll: Point = {
  34. x: ul.x,
  35. y: lr.y,
  36. };
  37. coords = [ul, ur, lr, ll];
  38. }
  39. const id = getID(xml);
  40. const surface = xml.closest<XMLElement>('surface');
  41. return {
  42. type: Zone,
  43. attributes,
  44. id,
  45. coords,
  46. corresp: attributes.corresp?.replace('#', '') ?? id,
  47. rend: attributes.rend,
  48. rendition: attributes.rendition as ZoneRendition,
  49. rotate: attributes.rotate ? parseInt(attributes.rotate, 10) || 0 : 0,
  50. content: parseChildren(xml, this.genericParse),
  51. surface: surface ? getID(surface) : '',
  52. };
  53. }
  54. }
  55. @xmlParser('graphic', GraphicParser)
  56. export class GraphicParser extends EmptyParser implements Parser<XMLElement> {
  57. attributeParser = createParser(AttributeParser, this.genericParse);
  58. public parse(xml: XMLElement): Graphic {
  59. return {
  60. type: Graphic,
  61. url: xml.getAttribute('url') || '',
  62. height: xml.getAttribute('height') || '',
  63. width: xml.getAttribute('width') || '',
  64. attributes: this.attributeParser.parse(xml),
  65. content: parseChildren(xml, this.genericParse),
  66. };
  67. }
  68. }
  69. @xmlParser('surface', SurfaceParser)
  70. export class SurfaceParser extends EmptyParser implements Parser<XMLElement> {
  71. attributeParser = createParser(AttributeParser, this.genericParse);
  72. graphicParser = createParser(GraphicParser, this.genericParse);
  73. zoneParser = createParser(ZoneParser, this.genericParse);
  74. public parse(xml: XMLElement): Surface {
  75. const zones = Array.from(xml.querySelectorAll<XMLElement>('zone')).map(z => this.zoneParser.parse(z));
  76. return {
  77. type: Surface,
  78. id: getID(xml),
  79. corresp: xml.getAttribute('corresp')?.replace('#', ''),
  80. graphics: Array.from(xml.querySelectorAll<XMLElement>('graphic')).map(g => this.graphicParser.parse(g)),
  81. zones: {
  82. lines: zones.filter(z => z.rendition === 'Line') as ZoneLine[],
  83. hotspots: zones.filter(z => z.rendition === 'HotSpot') as ZoneHotSpot[],
  84. },
  85. attributes: this.attributeParser.parse(xml),
  86. content: parseChildren(xml, this.genericParse),
  87. };
  88. }
  89. }