UNPKG

typesxml

Version:

Open source XML library written in TypeScript

67 lines 2.24 kB
/******************************************************************************* * 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 *******************************************************************************/ import { appendFileSync, writeFileSync } from "node:fs"; import { XMLDeclaration } from "./XMLDeclaration.js"; export class XMLWriter { file; options = { encoding: 'utf8' }; started; constructor(file) { this.file = file; this.started = false; } writeNode(node) { if (node instanceof XMLDeclaration) { let enc = node.getEncoding(); if (enc === 'UTF-16LE') { this.options.encoding = 'utf16le'; if (!this.started) { // write BOM for UTF-16LE writeFileSync(this.file, '\ufeff', this.options); this.started = true; } } } if (!this.started) { this.started = true; writeFileSync(this.file, node.toString(), this.options); return; } appendFileSync(this.file, node.toString(), this.options); } writeString(str) { if (!this.started) { this.started = true; writeFileSync(this.file, str, this.options); return; } 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 writeFileSync(file, '\ufeff' + doc.toString(), options); return; } writeFileSync(file, doc.toString(), options); } } //# sourceMappingURL=XMLWriter.js.map