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C,EAAE;QACrB,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxB,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAA;QAC5C,CAAC;IACH,CAAC,CAAA;IAED,MAAM,CAAC,OAAO,GAAG,KAAK,CAAC,EAAE;QACvB,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QAC7C,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;QACrB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAA;IACxB,CAAC,CAAA;IAED,MAAM,CAAC,OAAO,GAAG,GAAG,CAAC,EAAE;QACrB,MAAM,GAAG,CAAA;IACX,CAAC,CAAA;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAClB,OAAO,WAAY,CAAA;AACrB,CAAC;AA3CD,4BA2CC","sourcesContent":["import * as sax from \"sax\"\nimport { newError } from \"./index\"\n\nexport class XElement {\n value = \"\"\n attributes: { [key: string]: string } | null = null\n isCData = false\n elements: Array<XElement> | null = null\n\n constructor(readonly name: string) {\n if (!name) {\n throw newError(\"Element name cannot be empty\", \"ERR_XML_ELEMENT_NAME_EMPTY\")\n }\n if (!isValidName(name)) {\n throw newError(`Invalid element name: ${name}`, \"ERR_XML_ELEMENT_INVALID_NAME\")\n }\n }\n\n attribute(name: string): string {\n const result = this.attributes === null ? null : this.attributes[name]\n if (result == null) {\n throw newError(`No attribute \"${name}\"`, \"ERR_XML_MISSED_ATTRIBUTE\")\n }\n return result\n }\n\n removeAttribute(name: string): void {\n if (this.attributes !== null) {\n delete this.attributes[name]\n }\n }\n\n element(name: string, ignoreCase = false, errorIfMissed: string | null = null): XElement {\n const result = this.elementOrNull(name, ignoreCase)\n if (result === null) {\n throw newError(errorIfMissed || `No element \"${name}\"`, \"ERR_XML_MISSED_ELEMENT\")\n }\n return result\n }\n\n elementOrNull(name: string, ignoreCase = false): XElement | null {\n if (this.elements === null) {\n return null\n }\n\n for (const element of this.elements) {\n if (isNameEquals(element, name, ignoreCase)) {\n return element\n }\n }\n\n return null\n }\n\n getElements(name: string, ignoreCase = false) {\n if (this.elements === null) {\n return []\n }\n return this.elements.filter(it => isNameEquals(it, name, ignoreCase))\n }\n\n elementValueOrEmpty(name: string, ignoreCase = false): string {\n const element = this.elementOrNull(name, ignoreCase)\n return element === null ? \"\" : element.value\n }\n}\n\nconst NAME_REG_EXP = new RegExp(/^[A-Za-z_][:A-Za-z0-9_-]*$/i)\n\nfunction isValidName(name: string) {\n return NAME_REG_EXP.test(name)\n}\n\nfunction isNameEquals(element: XElement, name: string, ignoreCase: boolean) {\n const elementName = element.name\n return elementName === name || (ignoreCase === true && elementName.length === name.length && elementName.toLowerCase() === name.toLowerCase())\n}\n\nexport function parseXml(data: string): XElement {\n let rootElement: XElement | null = null\n const parser = sax.parser(true, {})\n const elements: Array<XElement> = []\n\n parser.onopentag = saxElement => {\n const element = new XElement(saxElement.name)\n element.attributes = saxElement.attributes as { [key: string]: string }\n\n if (rootElement === null) {\n rootElement = element\n } else {\n const parent = elements[elements.length - 1]\n if (parent.elements == null) {\n parent.elements = []\n }\n parent.elements.push(element)\n }\n elements.push(element)\n }\n\n parser.onclosetag = () => {\n elements.pop()\n }\n\n parser.ontext = text => {\n if (elements.length > 0) {\n elements[elements.length - 1].value = text\n }\n }\n\n parser.oncdata = cdata => {\n const element = elements[elements.length - 1]\n element.value = cdata\n element.isCData = true\n }\n\n parser.onerror = err => {\n throw err\n }\n\n parser.write(data)\n return rootElement!\n}\n"]} |