MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / XmlParser

Class XmlParser

packages/core/xml/index.ts:468–653  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

466 * A simple non-validating SAX parser based on https://github.com/vflash/easysax version 0.1.14
467 */
468export class XmlParser {
469 //TODO: Add option to configure whether the parser should report ignorable whitespace, i.e. document formatting whitespace.
470 private _parser: EasySAXParser;
471
472 private _processNamespaces: boolean;
473 private _namespaceStack: Array<any>;
474
475 /**
476 * Creates a new instance of the XmlParser class.
477 * @param onEvent The callback to execute when a parser event occurs. The 'event' parameter contains information about the event.
478 * @param onError The callback to execute when a parser error occurs. The 'error' parameter contains the error.
479 * @param processNamespaces Specifies whether namespaces should be processed.
480 */
481 constructor(onEvent: (event: ParserEvent) => void, onError?: (error: Error, position: Position) => void, processNamespaces?: boolean) {
482 this._processNamespaces = processNamespaces;
483 this._parser = new EasySAXParser();
484
485 const that = this;
486 this._parser.on('startNode', function (elem, attr, uq, tagend, str, pos) {
487 let attributes = attr();
488
489 if (attributes === true) {
490 //HACK: For some reason easysax returns the true literal when an element has no attributes.
491 attributes = undefined;
492 }
493
494 if (attributes) {
495 for (const key in attributes) {
496 if (attributes.hasOwnProperty(key)) {
497 // Convert entities such as &gt; to >
498 attributes[key] = XmlParser._dereferenceEntities(attributes[key]);
499 }
500 }
501 }
502
503 let prefix = undefined;
504 let namespace = undefined;
505 let name = elem;
506
507 if (that._processNamespaces) {
508 const stackEntry = XmlParser._getNamespacesStackEntry(attributes);
509 that._namespaceStack.push(stackEntry);
510
511 const resolved = that._resolveNamespace(name);
512 prefix = resolved.prefix;
513 namespace = resolved.namespace;
514 name = resolved.name;
515 }
516
517 onEvent(new ParserEvent(ParserEventType.StartElement, pos(), prefix, namespace, name, attributes, undefined));
518 });
519
520 this._parser.on('textNode', function (text, uq, pos) {
521 const data = uq(XmlParser._dereferenceEntities(text)); // Decode entity references such as &lt; and &gt;
522 onEvent(new ParserEvent(ParserEventType.Text, pos(), undefined, undefined, undefined, undefined, data));
523 });
524
525 this._parser.on('endNode', function (elem, uq, tagstart, str, pos) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected