| 2 | import Xml2JsParser from './Xml2JsParser.js'; |
| 3 | |
| 4 | export default class XMLParser { |
| 5 | |
| 6 | constructor(options) { |
| 7 | this.externalEntities = {}; |
| 8 | this.options = buildOptions(options); |
| 9 | // console.log(this.options) |
| 10 | } |
| 11 | /** |
| 12 | * Parse XML data string to JS object |
| 13 | * @param {string|Buffer} xmlData |
| 14 | * @param {boolean|Object} validationOption |
| 15 | */ |
| 16 | parse(xmlData) { |
| 17 | if (Array.isArray(xmlData) && xmlData.byteLength !== undefined) { |
| 18 | return this.parse(xmlData); |
| 19 | } else if (xmlData.toString) { |
| 20 | xmlData = xmlData.toString(); |
| 21 | } else { |
| 22 | throw new Error("XML data is accepted in String or Bytes[] form.") |
| 23 | } |
| 24 | // if( validationOption){ |
| 25 | // if(validationOption === true) validationOption = {}; //validate with default options |
| 26 | |
| 27 | // const result = validator.validate(xmlData, validationOption); |
| 28 | // if (result !== true) { |
| 29 | // throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` ) |
| 30 | // } |
| 31 | // } |
| 32 | const parser = new Xml2JsParser(this.options); |
| 33 | parser.entityParser.addExternalEntities(this.externalEntities); |
| 34 | return parser.parse(xmlData); |
| 35 | } |
| 36 | /** |
| 37 | * Parse XML data buffer to JS object |
| 38 | * @param {string|Buffer} xmlData |
| 39 | * @param {boolean|Object} validationOption |
| 40 | */ |
| 41 | parseBytesArr(xmlData) { |
| 42 | if (Array.isArray(xmlData) && xmlData.byteLength !== undefined) { |
| 43 | } else { |
| 44 | throw new Error("XML data is accepted in Bytes[] form.") |
| 45 | } |
| 46 | const parser = new Xml2JsParser(this.options); |
| 47 | parser.entityParser.addExternalEntities(this.externalEntities); |
| 48 | return parser.parseBytesArr(xmlData); |
| 49 | } |
| 50 | /** |
| 51 | * Parse XML data stream to JS object |
| 52 | * @param {fs.ReadableStream} xmlDataStream |
| 53 | */ |
| 54 | parseStream(xmlDataStream) { |
| 55 | if (!isStream(xmlDataStream)) throw new Error("FXP: Invalid stream input"); |
| 56 | |
| 57 | const orderedObjParser = new Xml2JsParser(this.options); |
| 58 | orderedObjParser.entityParser.addExternalEntities(this.externalEntities); |
| 59 | return orderedObjParser.parseStream(xmlDataStream); |
| 60 | } |
| 61 |
nothing calls this directly
no outgoing calls
no test coverage detected