@aurahelper/languages
Version:
Language Libraries to work with XML, Aura, Apex... files. tokenizers, parsers, system classes and much more
258 lines • 8.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLParser = void 0;
var fast_xml_parser_1 = require("fast-xml-parser");
var he = require('he');
/**
* Class to parse and extract data from XML files
*/
var XMLParser = /** @class */ (function () {
function XMLParser() {
}
/**
* Method to get the XML to JSON Options
* @returns Return XML To JSON Options object
*/
XMLParser.getParserXMLToJSONOptions = function () {
return {
attributeNamePrefix: "",
attributesGroupName: "@attrs",
textNodeName: "#text",
ignoreAttributes: false,
removeNSPrefix: false,
allowBooleanAttributes: true,
ignoreDeclaration: true,
parseTagValue: true,
parseAttributeValue: false,
trimValues: true,
cdataPropName: "__cdata",
cdataPositionChar: "\\c",
localeRange: "",
parseTrueNumberOnly: false,
arrayMode: false,
commentPropName: "#comment",
attributeValueProcessor: function (_tagName, val) { return he.decode(val, { isAttributeValue: true }); },
tagValueProcessor: function (_tagName, val) { return he.decode(val); },
stopNodes: ["parse-me-as-string"]
};
};
/**
* Method to get the JSON to XML Options
* @returns Return JSON To XML Options object
*/
XMLParser.getParserJSONToXMLOptions = function () {
return {
attributeNamePrefix: "",
attributesGroupName: "@attrs",
textNodeName: "#text",
ignoreAttributes: false,
cdataPropName: "__cdata",
cdataPositionChar: "\\c",
format: true,
indentBy: "\t",
suppressEmptyNode: false,
commentPropName: "#comment",
};
};
/**
* Method to parse XML file
* @param {string} [content] XML file content
* @param {boolean} [parseComments] true tu parse comments too.
* @returns {any} Return the XML file data
*/
XMLParser.parseXML = function (content, parseComments) {
if (content && content.length > 0) {
if (parseComments) {
content = content.split('<!--').join('«!--');
content = content.split('-->').join('--»');
}
var parser = new fast_xml_parser_1.XMLParser(XMLParser.getParserXMLToJSONOptions());
return parser.parse(content);
}
return {};
};
/**
* Method to transform JSON Object into XML file
* @param {any} jsonObj JSON to transform
* @returns {string} Return the XML Content
*/
XMLParser.toXML = function (jsonObj) {
jsonObj = fixObjValues(jsonObj);
var builder = new fast_xml_parser_1.XMLBuilder(XMLParser.getParserJSONToXMLOptions());
var content = builder.build(jsonObj);
content = XMLParser.getXMLFirstLine() + '\n' + content;
return content;
};
/**
* Method to get the XML first line
* @returns {string} Return XML first line
*/
XMLParser.getXMLFirstLine = function () {
return '<?xml version="1.0" encoding="UTF-8"?>';
};
/**
* Method to check if start tags exists on string
* @param {string} text Text to get tag
* @param {string} tag tag name
* @returns {string | undefined} return the tag name or undefined if not exists
*/
XMLParser.startTag = function (text, tag) {
if (text.indexOf('<' + tag + '>') !== -1) {
return tag;
}
return undefined;
};
/**
* Method to check if end tag exists on string
* @param {string} text Text to get tag
* @param {string} tag tag name
* @returns {string | undefined} return the tag name or undefined if not exists
*/
XMLParser.endTag = function (text, tag) {
if (text.indexOf('</' + tag + '>') !== -1) {
return tag;
}
return undefined;
};
/**
* Method to get an XML Element String
* @param {string} tag Tag name
* @param {string[]} attributes Attributes to add
* @param {any} value value to add
* @returns
*/
XMLParser.getXMLElement = function (tag, attributes, value) {
var empty = value === undefined || value === null || value === '';
if (!empty && value['#text'] !== undefined) {
value = value['#text'];
}
if (!empty && value !== undefined && value['@attrs'] !== undefined) {
delete value['@attrs'];
}
if (value && !Array.isArray(value) && typeof value === 'object' && Object.keys(value).length === 0) {
value = '';
empty = true;
}
var isJSONValue;
if (empty) {
return XMLParser.getStartTag(tag, attributes, empty);
}
else {
try {
var jsonVal = JSON.parse(value);
if (jsonVal) {
isJSONValue = true;
}
else {
isJSONValue = false;
}
}
catch (error) {
isJSONValue = false;
}
if (typeof value === 'string' && !isJSONValue) {
value = escapeChars(value);
}
if (attributes && attributes.includes('xsi:type="xsd:double"')) {
var strVal = '' + value;
if (strVal.indexOf('.') === -1) {
strVal += '.0';
}
value = strVal;
}
return XMLParser.getStartTag(tag, attributes, empty) + value + XMLParser.getEndTag(tag);
}
};
/**
* Method to get start tag string
* @param {string} tag Tag name
* @param {string[]} attributes Attributes to add
* @param {boolean} empty true if tag is empty
* @returns {string} Return an string with starts tag
*/
XMLParser.getStartTag = function (tag, attributes, empty) {
if (!empty) {
if (attributes && attributes.length > 0) {
return '<' + tag.trim() + ' ' + attributes.join(' ') + '>';
}
else {
return '<' + tag.trim() + '>';
}
}
else {
if (attributes && attributes.length > 0) {
return '<' + tag.trim() + ' ' + attributes.join(' ') + '/>';
}
else {
return '<' + tag.trim() + '/>';
}
}
};
/**
* Method to get end tag string
* @param {string} tag Tag name
* @returns {string} Return end tag as string
*/
XMLParser.getEndTag = function (tag) {
return '</' + tag.trim() + '>';
};
return XMLParser;
}());
exports.XMLParser = XMLParser;
function escapeChars(value) {
if (typeof value === "string") {
value = value.split('&').join('&');
value = value.split('"').join('"');
value = value.split(''').join('\'');
value = value.split('<').join('<');
value = value.split('>').join('>');
value = value.split('«!--').join('<!--');
value = value.split('--»').join('-->');
value = value.split('&').join('&');
value = value.split('"').join('"');
value = value.split('\'').join(''');
if (value.indexOf('<!') === -1) {
value = value.split('<').join('<');
value = value.split('>').join('>');
}
}
return value;
}
function fixObjValues(jsonObj) {
var jsonRes = {};
Object.keys(jsonObj).forEach(function (key) {
var value = jsonObj[key];
if (Array.isArray(value)) {
jsonRes[key] = fixArrayValues(value);
}
else if (typeof value === 'object') {
jsonRes[key] = fixObjValues(value);
}
else {
if (value !== undefined) {
jsonRes[key] = value.toString();
}
else {
jsonRes[key] = value;
}
}
});
return jsonRes;
}
function fixArrayValues(jsonArray) {
var arrayRes = [];
for (var _i = 0, jsonArray_1 = jsonArray; _i < jsonArray_1.length; _i++) {
var element = jsonArray_1[_i];
if (Array.isArray(element)) {
arrayRes.push(fixArrayValues(element));
}
else if (typeof element === 'object') {
arrayRes.push(fixObjValues(element));
}
else {
arrayRes.push(element.toString());
}
}
return arrayRes;
}
//# sourceMappingURL=parser.js.map