typesxml
Version:
Open source XML library written in TypeScript
71 lines • 2.48 kB
JavaScript
"use strict";
/*******************************************************************************
* Copyright (c) 2023-2026 Maxprograms.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse License 1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-v10.html
*
* Contributors:
* Maxprograms - initial API and implementation
*******************************************************************************/
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLWriter = void 0;
const node_fs_1 = require("node:fs");
const XMLDeclaration_js_1 = require("./XMLDeclaration.js");
class XMLWriter {
file;
options = {
encoding: 'utf8'
};
started;
constructor(file) {
this.file = file;
this.started = false;
}
writeNode(node) {
if (node instanceof XMLDeclaration_js_1.XMLDeclaration) {
let enc = node.getEncoding();
if (enc === 'UTF-16LE') {
this.options.encoding = 'utf16le';
if (!this.started) {
// write BOM for UTF-16LE
(0, node_fs_1.writeFileSync)(this.file, '\ufeff', this.options);
this.started = true;
}
}
}
if (!this.started) {
this.started = true;
(0, node_fs_1.writeFileSync)(this.file, node.toString(), this.options);
return;
}
(0, node_fs_1.appendFileSync)(this.file, node.toString(), this.options);
}
writeString(str) {
if (!this.started) {
this.started = true;
(0, node_fs_1.writeFileSync)(this.file, str, this.options);
return;
}
(0, node_fs_1.appendFileSync)(this.file, str, this.options);
}
static writeDocument(doc, file) {
let options = {
encoding: 'utf8'
};
let decl = doc.getXmlDeclaration();
if (decl && decl.getEncoding() === 'UTF-16LE') {
options.encoding = 'utf16le';
}
if (options.encoding === 'utf16le') {
// write BOM for UTF-16LE
(0, node_fs_1.writeFileSync)(file, '\ufeff' + doc.toString(), options);
return;
}
(0, node_fs_1.writeFileSync)(file, doc.toString(), options);
}
}
exports.XMLWriter = XMLWriter;
//# sourceMappingURL=XMLWriter.js.map