typesxml
Version:
Open source XML library written in TypeScript
98 lines • 3 kB
JavaScript
;
/*******************************************************************************
* Copyright (c) 2023 - 2024 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.XMLDocument = void 0;
const Constants_1 = require("./Constants");
const XMLDeclaration_1 = require("./XMLDeclaration");
const XMLElement_1 = require("./XMLElement");
const XMLUtils_1 = require("./XMLUtils");
class XMLDocument {
content;
documentType;
constructor() {
this.content = new Array();
}
contentIterator() {
return this.content.values();
}
setRoot(root) {
this.content.push(root);
}
getRoot() {
for (let node of this.content) {
if (node instanceof XMLElement_1.XMLElement) {
return node;
}
}
return undefined;
}
setDocumentType(documentType) {
this.documentType = documentType;
this.content.push(documentType);
}
getDocumentType() {
if (this.documentType) {
return this.documentType;
}
return undefined;
}
setXmlDeclaration(declaration) {
this.content.unshift(declaration);
}
getXmlDeclaration() {
if (this.content[0] instanceof XMLDeclaration_1.XMLDeclaration) {
return this.content[0];
}
return undefined;
}
addComment(comment) {
this.content.push(comment);
}
addProcessingInstruction(pi) {
this.content.push(pi);
}
addTextNode(node) {
this.content.push(node);
}
getNodeType() {
return Constants_1.Constants.DOCUMENT_NODE;
}
toString() {
let result = '';
let isXml10 = true;
let xmlDeclaration = this.getXmlDeclaration();
if (xmlDeclaration) {
isXml10 = xmlDeclaration.getVersion() === '1.0';
}
this.content.forEach((node) => {
result += isXml10 ? XMLUtils_1.XMLUtils.validXml10Chars(node.toString()) : XMLUtils_1.XMLUtils.validXml11Chars(node.toString());
});
return result;
}
equals(node) {
if (node instanceof XMLDocument) {
if (this.content.length !== node.content.length) {
return false;
}
for (let i = 0; i < this.content.length; i++) {
if (!this.content[i].equals(node.content[i])) {
return false;
}
}
return true;
}
return false;
}
}
exports.XMLDocument = XMLDocument;
//# sourceMappingURL=XMLDocument.js.map