@iotize/tap
Version:
IoTize Device client for Javascript
64 lines (59 loc) • 1.61 kB
JavaScript
import { IotizeConfigFactory, IotizeConfigModel } from '@iotize/tap/config/iotize-studio';
import { parseString } from 'xml2js';
// TODO remove xml2js dependencie
/**
* Convert XML to Javascript Object
*/
class XMLParser {
/**
* Parse XML input synchonously
* @param xml
*/
parseSync(xml) {
let res = null;
parseString(xml, function (err, result) {
if (err !== null) {
throw err;
}
res = result;
});
return res;
}
/**
* Parse XML input async
* @param xml
*/
parse(xml) {
xml = this.cleanifyXml(xml);
return new Promise((resolve, reject) => {
parseString(xml, function (err, result) {
if (err !== null) {
reject(err);
}
resolve(result);
});
});
}
/**
* Cleanify the xml input before parsing
* @see http://stackoverflow.com/questions/34783452/cannot-parse-xml-error-non-whitespace-before-first-tag-line-0-column-1-cha
*/
cleanifyXml(xml) {
if (!xml) {
return xml;
}
return xml.replace('\ufeff', '').trim();
}
}
/**
* Create IotizeConfigModel from a XML string
* @param xml
*/
IotizeConfigFactory.fromXML = function (xml) {
return new IotizeConfigModel(new XMLParser().parseSync(xml));
};
/**
* Generated bundle index. Do not edit.
*/
export { XMLParser };
//# sourceMappingURL=iotize-tap-config-iotize-studio-parser-xml.js.map