| 5 | import XmlNode from './xmlNode.js'; |
| 6 | |
| 7 | export default class XMLParser { |
| 8 | |
| 9 | constructor(options) { |
| 10 | this.externalEntities = {}; |
| 11 | this.options = buildOptions(options); |
| 12 | |
| 13 | } |
| 14 | /** |
| 15 | * Parse XML dats to JS object |
| 16 | * @param {string|Uint8Array} xmlData |
| 17 | * @param {boolean|Object} validationOption |
| 18 | */ |
| 19 | parse(xmlData, validationOption) { |
| 20 | if (typeof xmlData !== "string" && xmlData.toString) { |
| 21 | xmlData = xmlData.toString(); |
| 22 | } else if (typeof xmlData !== "string") { |
| 23 | throw new Error("XML data is accepted in String or Bytes[] form.") |
| 24 | } |
| 25 | |
| 26 | if (validationOption) { |
| 27 | if (validationOption === true) validationOption = {}; //validate with default options |
| 28 | |
| 29 | const result = validate(xmlData, validationOption); |
| 30 | if (result !== true) { |
| 31 | throw Error(`${result.err.msg}:${result.err.line}:${result.err.col}`) |
| 32 | } |
| 33 | } |
| 34 | const orderedObjParser = new OrderedObjParser(this.options, this.externalEntities); |
| 35 | // orderedObjParser.entityDecoder.setExternalEntities(this.externalEntities); |
| 36 | const orderedResult = orderedObjParser.parseXml(xmlData); |
| 37 | if (this.options.preserveOrder || orderedResult === undefined) return orderedResult; |
| 38 | else return prettify(orderedResult, this.options, orderedObjParser.matcher, orderedObjParser.readonlyMatcher); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add Entity which is not by default supported by this library |
| 43 | * @param {string} key |
| 44 | * @param {string} value |
| 45 | */ |
| 46 | addEntity(key, value) { |
| 47 | if (value.indexOf("&") !== -1) { |
| 48 | throw new Error("Entity value can't have '&'") |
| 49 | } else if (key.indexOf("&") !== -1 || key.indexOf(";") !== -1) { |
| 50 | throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'") |
| 51 | } else if (value === "&") { |
| 52 | throw new Error("An entity with value '&' is not permitted"); |
| 53 | } else { |
| 54 | this.externalEntities[key] = value; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a Symbol that can be used to access the metadata |
| 60 | * property on a node. |
| 61 | * |
| 62 | * If Symbol is not available in the environment, an ordinary property is used |
| 63 | * and the name of the property is here returned. |
| 64 | * |
nothing calls this directly
no outgoing calls
no test coverage detected