typesxml
Version:
Open source XML library written in TypeScript
214 lines • 6.44 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.XMLElement = void 0;
const ProcessingInstruction_js_1 = require("./ProcessingInstruction.js");
const TextNode_js_1 = require("./TextNode.js");
const Constants_js_1 = require("./Constants.js");
class XMLElement {
name;
attributes;
content;
constructor(name) {
this.name = name;
this.attributes = new Map();
this.content = new Array();
}
getName() {
return this.name;
}
getNamespace() {
if (this.name.indexOf(':') === -1) {
return '';
}
return this.name.substring(0, this.name.indexOf(':'));
}
hasAttribute(name) {
return this.attributes.has(name);
}
getAttribute(name) {
return this.attributes.get(name);
}
setAttribute(attribute) {
this.attributes.set(attribute.getName(), attribute);
}
removeAttribute(name) {
this.attributes.delete(name);
}
setAttributes(array) {
this.attributes.clear();
array.forEach((attribute) => {
this.attributes.set(attribute.getName(), attribute);
});
}
getAttributes() {
let result = new Array();
this.attributes.forEach((value) => {
result.push(value);
});
return result;
}
addString(text) {
this.addTextNode(new TextNode_js_1.TextNode(text));
}
addTextNode(node) {
if (this.content.length > 0 && this.content[this.content.length - 1].getNodeType() === Constants_js_1.Constants.TEXT_NODE) {
let lastNode = this.content[this.content.length - 1];
lastNode.setValue(lastNode.getValue() + node.getValue());
return;
}
this.content.push(node);
}
addElement(node) {
this.content.push(node);
}
addComment(node) {
this.content.push(node);
}
addProcessingInstruction(node) {
this.content.push(node);
}
addCData(node) {
this.content.push(node);
}
setContent(content) {
this.content = content;
}
getContent() {
return this.content;
}
getNodeType() {
return Constants_js_1.Constants.ELEMENT_NODE;
}
getHead() {
let result = '<' + this.name;
this.attributes.forEach((value) => {
result += ' ' + value.toString();
});
result += '>';
return result;
}
getTail() {
return '</' + this.name + '>';
}
toString() {
let result = '<' + this.name;
this.attributes.forEach((value) => {
result += ' ' + value.toString();
});
if (this.content.length > 0) {
result += '>';
this.content.forEach((node) => {
result += node.toString();
});
return result + '</' + this.name + '>';
}
return result + '/>';
}
equals(node) {
if (node instanceof XMLElement) {
if (this.name !== node.name || this.attributes.size !== node.attributes.size || this.content.length !== node.content.length) {
return false;
}
let sameAttributes = true;
this.attributes.forEach((att, key) => {
let other = node.getAttribute(key);
if (other === undefined || att.getValue() !== other.getValue()) {
sameAttributes = false;
}
});
if (!sameAttributes) {
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;
}
getChildren() {
let result = new Array();
this.content.forEach((node) => {
if (node instanceof XMLElement) {
result.push(node);
}
});
return result;
}
getChild(childName) {
let result = undefined;
let length = this.content.length;
for (let i = 0; i < length; i++) {
let node = this.content[i];
if (node instanceof XMLElement) {
if (node.getName() === childName) {
result = node;
break;
}
}
}
return result;
}
removeChild(child) {
let length = this.content.length;
for (let i = 0; i < length; i++) {
let node = this.content[i];
if (node instanceof XMLElement) {
if (node.equals(child)) {
this.content.splice(i, 1);
break;
}
}
}
}
getText() {
let result = '';
this.content.forEach((node) => {
if (node instanceof TextNode_js_1.TextNode) {
result += node.getValue();
}
if (node instanceof XMLElement) {
result += node.getText();
}
});
return result;
}
pureText() {
let result = '';
this.content.forEach((node) => {
if (node instanceof TextNode_js_1.TextNode) {
result += node.getValue();
}
});
return result;
}
getPI(target) {
let result = undefined;
let length = this.content.length;
for (let i = 0; i < length; i++) {
let node = this.content[i];
if (node instanceof ProcessingInstruction_js_1.ProcessingInstruction) {
if (node.getTarget() === target) {
result = node;
break;
}
}
}
return result;
}
}
exports.XMLElement = XMLElement;
//# sourceMappingURL=XMLElement.js.map