123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.parse = exports.parseToExistingElement = exports.Absent = void 0;
- var xmlcreate_1 = require("xmlcreate");
- var options_1 = require("./options");
- var utils_1 = require("./utils");
- var Absent = (function () {
- function Absent() {
- }
- Object.defineProperty(Absent, "instance", {
-
- get: function () {
- return Absent._instance;
- },
- enumerable: false,
- configurable: true
- });
- Absent._instance = new Absent();
- return Absent;
- }());
- exports.Absent = Absent;
- function getHandler(value, options) {
- var type = Object.prototype.toString.call(value);
- var handler;
- if (Object.prototype.hasOwnProperty.call(options.typeHandlers, "*")) {
- handler = options.typeHandlers["*"];
- }
- if (Object.prototype.hasOwnProperty.call(options.typeHandlers, type)) {
- handler = options.typeHandlers[type];
- }
- return handler;
- }
- function parseString(str, parentElement, options) {
- var requiresCdata = function (s) {
- return ((options.cdataInvalidChars &&
- (s.indexOf("<") !== -1 || s.indexOf("&") !== -1)) ||
- options.cdataKeys.indexOf(parentElement.name) !== -1 ||
- options.cdataKeys.indexOf("*") !== -1);
- };
- if (parentElement instanceof xmlcreate_1.XmlElement) {
- if (requiresCdata(str)) {
- var cdataStrs = str.split("]]>");
- for (var i = 0; i < cdataStrs.length; i++) {
- if (requiresCdata(cdataStrs[i])) {
- parentElement.cdata({
- charData: cdataStrs[i],
- replaceInvalidCharsInCharData: options.replaceInvalidChars,
- });
- }
- else {
- parentElement.charData({
- charData: cdataStrs[i],
- replaceInvalidCharsInCharData: options.replaceInvalidChars,
- });
- }
- if (i < cdataStrs.length - 1) {
- parentElement.charData({
- charData: "]]>",
- replaceInvalidCharsInCharData: options.replaceInvalidChars,
- });
- }
- }
- }
- else {
- parentElement.charData({
- charData: str,
- replaceInvalidCharsInCharData: options.replaceInvalidChars,
- });
- }
- }
- else {
- parentElement.text({
- charData: str,
- replaceInvalidCharsInCharData: options.replaceInvalidChars,
- });
- }
- }
- function parseAttribute(name, value, parentElement, options) {
- var attribute = parentElement.attribute({
- name: name,
- replaceInvalidCharsInName: options.replaceInvalidChars,
- });
- parseString((0, utils_1.stringify)(value), attribute, options);
- }
- function parseObjectOrMapEntry(key, value, parentElement, options) {
-
- if (key === options.aliasString) {
- parentElement.name = (0, utils_1.stringify)(value);
- return;
- }
-
- if (key.indexOf(options.attributeString) === 0 && (0, utils_1.isObject)(value)) {
- for (var _i = 0, _a = Object.keys(value); _i < _a.length; _i++) {
- var subkey = _a[_i];
- parseAttribute(subkey, (0, utils_1.stringify)(value[subkey]), parentElement, options);
- }
- return;
- }
-
- if (key.indexOf(options.valueString) === 0) {
- parseValue(key, (0, utils_1.stringify)(value), parentElement, options);
- return;
- }
-
- var element = parentElement;
- if (!(0, utils_1.isArray)(value) && !(0, utils_1.isSet)(value)) {
-
- var handler = getHandler(value, options);
- if (!(0, utils_1.isUndefined)(handler)) {
- if (handler(value) === Absent.instance) {
- return;
- }
- }
- element = parentElement.element({
- name: key,
- replaceInvalidCharsInName: options.replaceInvalidChars,
- useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
- });
- }
- parseValue(key, value, element, options);
- }
- function parseObjectOrMap(objectOrMap, parentElement, options) {
- if ((0, utils_1.isMap)(objectOrMap)) {
- objectOrMap.forEach(function (value, key) {
- parseObjectOrMapEntry((0, utils_1.stringify)(key), value, parentElement, options);
- });
- }
- else {
- for (var _i = 0, _a = Object.keys(objectOrMap); _i < _a.length; _i++) {
- var key = _a[_i];
- parseObjectOrMapEntry(key, objectOrMap[key], parentElement, options);
- }
- }
- }
- function parseArrayOrSet(key, arrayOrSet, parentElement, options) {
- var arrayNameFunc;
- if (Object.prototype.hasOwnProperty.call(options.wrapHandlers, "*")) {
- arrayNameFunc = options.wrapHandlers["*"];
- }
- if (Object.prototype.hasOwnProperty.call(options.wrapHandlers, key)) {
- arrayNameFunc = options.wrapHandlers[key];
- }
- var arrayKey = key;
- var arrayElement = parentElement;
- if (!(0, utils_1.isUndefined)(arrayNameFunc)) {
- var arrayNameFuncKey = arrayNameFunc(arrayKey, arrayOrSet);
- if (!(0, utils_1.isNull)(arrayNameFuncKey)) {
- arrayKey = arrayNameFuncKey;
- arrayElement = parentElement.element({
- name: key,
- replaceInvalidCharsInName: options.replaceInvalidChars,
- useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
- });
- }
- }
- arrayOrSet.forEach(function (item) {
- var element = arrayElement;
- if (!(0, utils_1.isArray)(item) && !(0, utils_1.isSet)(item)) {
-
- var handler = getHandler(item, options);
- if (!(0, utils_1.isUndefined)(handler)) {
- if (handler(item) === Absent.instance) {
- return;
- }
- }
- element = arrayElement.element({
- name: arrayKey,
- replaceInvalidCharsInName: options.replaceInvalidChars,
- useSelfClosingTagIfEmpty: options.useSelfClosingTagIfEmpty,
- });
- }
- parseValue(arrayKey, item, element, options);
- });
- }
- function parseValue(key, value, parentElement, options) {
-
-
- var handler = getHandler(value, options);
- if (!(0, utils_1.isUndefined)(handler)) {
- value = handler(value);
- }
- if ((0, utils_1.isObject)(value) || (0, utils_1.isMap)(value)) {
- parseObjectOrMap(value, parentElement, options);
- return;
- }
- if ((0, utils_1.isArray)(value) || (0, utils_1.isSet)(value)) {
- parseArrayOrSet(key, value, parentElement, options);
- return;
- }
- parseString((0, utils_1.stringify)(value), parentElement, options);
- }
- function parseToExistingElement(element, object, options) {
- var opts = new options_1.Options(options);
- parseValue(element.name, object, element, opts);
- }
- exports.parseToExistingElement = parseToExistingElement;
- function parse(root, object, options) {
- var opts = new options_1.Options(options);
- var document = new xmlcreate_1.XmlDocument({
- validation: opts.validation,
- });
- if (opts.declaration.include) {
- document.decl(opts.declaration);
- }
- if (opts.dtd.include) {
- document.dtd({
-
-
- name: opts.dtd.name,
- pubId: opts.dtd.pubId,
- sysId: opts.dtd.sysId,
- });
- }
- var rootElement = document.element({
- name: root,
- replaceInvalidCharsInName: opts.replaceInvalidChars,
- useSelfClosingTagIfEmpty: opts.useSelfClosingTagIfEmpty,
- });
- parseToExistingElement(rootElement, object, options);
- return document.toString(opts.format);
- }
- exports.parse = parse;
|