| 496 | } |
| 497 | |
| 498 | export class TemplateParser implements XmlStateConsumer { |
| 499 | private _context: any; |
| 500 | private _recordedXmlStream: Array<xml.ParserEvent>; |
| 501 | private _templateProperty: TemplateProperty; |
| 502 | private _nestingLevel: number; |
| 503 | private _state: TemplateParser.State; |
| 504 | |
| 505 | private parent: XmlStateConsumer; |
| 506 | private _setTemplateProperty: boolean; |
| 507 | |
| 508 | constructor(parent: XmlStateConsumer, templateProperty: TemplateProperty, setTemplateProperty = true) { |
| 509 | this.parent = parent; |
| 510 | this._context = templateProperty.context; |
| 511 | this._recordedXmlStream = new Array<xml.ParserEvent>(); |
| 512 | this._templateProperty = templateProperty; |
| 513 | this._nestingLevel = 0; |
| 514 | this._state = TemplateParser.State.EXPECTING_START; |
| 515 | this._setTemplateProperty = setTemplateProperty; |
| 516 | } |
| 517 | |
| 518 | public parse(args: xml.ParserEvent): XmlStateConsumer { |
| 519 | if (args.eventType === xml.ParserEventType.StartElement) { |
| 520 | this.parseStartElement(args.prefix, args.namespace, args.elementName, args.attributes); |
| 521 | } else if (args.eventType === xml.ParserEventType.EndElement) { |
| 522 | this.parseEndElement(args.prefix, args.elementName); |
| 523 | } |
| 524 | |
| 525 | this._recordedXmlStream.push(args); |
| 526 | |
| 527 | return this._state === TemplateParser.State.FINISHED ? this.parent : this; |
| 528 | } |
| 529 | |
| 530 | public get elementName(): string { |
| 531 | return this._templateProperty.elementName; |
| 532 | } |
| 533 | |
| 534 | private parseStartElement(prefix: string, namespace: string, elementName: string, attributes: Object) { |
| 535 | if (this._state === TemplateParser.State.EXPECTING_START) { |
| 536 | this._state = TemplateParser.State.PARSING; |
| 537 | } else if (this._state === TemplateParser.State.FINISHED) { |
| 538 | throw new Error('Template must have exactly one root element but multiple elements were found.'); |
| 539 | } |
| 540 | |
| 541 | this._nestingLevel++; |
| 542 | } |
| 543 | |
| 544 | private parseEndElement(prefix: string, elementName: string) { |
| 545 | if (this._state === TemplateParser.State.EXPECTING_START) { |
| 546 | throw new Error('Template must have exactly one root element but none was found.'); |
| 547 | } else if (this._state === TemplateParser.State.FINISHED) { |
| 548 | throw new Error('No more closing elements expected for this template.'); |
| 549 | } |
| 550 | |
| 551 | this._nestingLevel--; |
| 552 | |
| 553 | if (this._nestingLevel === 0) { |
| 554 | this._state = TemplateParser.State.FINISHED; |
| 555 |
nothing calls this directly
no outgoing calls
no test coverage detected