UNPKG

@cyclonedx/cyclonedx-library

Version:

Core functionality of CycloneDX for JavaScript (Node.js or WebBrowser).

68 lines (64 loc) 2.16 kB
"use strict"; /*! This file is part of CycloneDX JavaScript Library. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache-2.0 Copyright (c) OWASP Foundation. All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); const xmlbuilder2_1 = require("xmlbuilder2"); if (typeof xmlbuilder2_1.create !== 'function') { throw new Error('`create` is not a function'); } exports.default = (function (rootElement, { space } = {}) { const indent = makeIndent(space); const doc = (0, xmlbuilder2_1.create)({ encoding: 'UTF-8' }); addEle(doc, rootElement); return doc.end({ format: 'xml', newline: '\n', prettyPrint: indent.length > 0, indent }); }); function addEle(parent, element, parentNS = null) { if (element.type !== 'element') { return; } const ns = getNS(element) ?? parentNS; const ele = parent.ele(ns, element.name, element.attributes); if (element.children === undefined) { } else if (typeof element.children === 'string' || typeof element.children === 'number') { ele.txt(element.children.toString()); } else { for (const child of element.children) { addEle(ele, child, ns); } } } function getNS(element) { const ns = (element.namespace ?? element.attributes?.xmlns)?.toString() ?? ''; return ns.length > 0 ? ns : null; } function makeIndent(space) { if (typeof space === 'number') { return ' '.repeat(Math.max(0, space)); } if (typeof space === 'string') { return space; } return ''; } //# sourceMappingURL=xmlbuilder2.js.map