@genexus/web-standard-functions
Version:
GeneXus JavaScript standard functions library for web generators
326 lines • 9.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLWriter = void 0;
const helpers_1 = require("../misc/helpers");
const xmlcommon_1 = require("./xmlcommon");
class XMLWriter extends xmlcommon_1.XMLBase {
constructor() {
super(...arguments);
// Internal variables
this.document = null;
this.elemStack = new XMLWriter.XMLWriterElementStack();
this.encoding = null;
this.standalone = null;
}
// Properties
/**
* Allows to inquire the value of the XML document that is in the internal buffer
* when the document was created with the OpenToString() method
*/
get resultingString() {
let content = this.document.firstElementChild.outerHTML;
if (this.encoding) {
let standalone = this.standalone ? ' standalone="yes"' : "";
let hedader = `<?xml version="1.0" encoding="${this.encoding}"${standalone}?>`;
content = `${hedader}${content}`;
}
return content;
}
// Opening documents
/**
* Allows the creation of an XML document by an internal buffer instead of a file
* @return {number}
*/
openToString() {
this.document = document.implementation.createDocument("", "", null);
this.elemStack = new XMLWriter.XMLWriterElementStack();
this.resetErrors();
return 0;
}
/**
* Closes the current writing session
* @return {number}
*/
close() {
this.document = null;
this.elemStack = null;
return 0;
}
// Writing
/**
* Writes the XML declaration using version 1.0 and ISO-8859-1 coding
* @param {string} encoding Encoding to be used in the XML file
* @param {number} standalone
* @return {number}
*/
writeStartDocument(encoding = "ISO-8859-1", standalone = 0) {
this.encoding = encoding;
this.standalone = standalone !== 0;
return 0;
}
/**
* Writes an element with the indicated value
* @param {string} name Element's name
* @param {string} value Element's value
* @return {number}
*/
writeElement(name, value) {
if (this.errCode === 0) {
this.writeStartElement(name);
}
if (this.errCode === 0) {
this.writeText(value);
this.writeEndElement();
}
return this.errCode;
}
/**
* Starts a compound element
* @param {string} name Element's name
* @return {number}
*/
writeStartElement(name) {
if (this.errCode === 0) {
if (this.document) {
let elem = this.document.createElement(name);
this.elemStack.push(elem);
}
else {
this.mErrCode = xmlcommon_1.XMLErrorCodes.no_open_document;
this.mErrDescription = "No open document";
}
}
return this.mErrCode;
}
/**
* Closes the last element that was opened using the WriteStartElement method
* @return {number}
*/
writeEndElement() {
if (this.errCode === 0) {
let elem = this.elemStack.pop();
if (elem) {
let parent = this.elemStack.top() || this.document;
parent.appendChild(elem);
}
else {
this.mErrCode = xmlcommon_1.XMLErrorCodes.missing_start_element;
this.mErrDescription = "Missing start element";
}
}
return this.errCode;
}
/**
* Generates character data with the indicated value string
* @param {string} text
* @return {number}
*/
writeText(text) {
return this.appendNodeToCurrentElement(false, () => {
return this.document.createTextNode(text);
});
}
/**
* Creates an attribute in the current element
* @param {string} name Attribute's name
* @param {string} value Attribute's value
* @return {number}
*/
writeAttribute(name, value) {
if (this.errCode === 0) {
let elem = this.elemStack.top();
if (elem) {
elem.setAttribute(name, value);
}
else {
this.mErrCode = xmlcommon_1.XMLErrorCodes.missing_start_element;
this.mErrDescription = "Missing start element";
}
}
return this.errCode;
}
/**
* Generates any text without replacing special characters with character sequences
* @param {string} text Text to append
* @return {number}
*/
writeRawText(text) {
return this.appendNodeToCurrentElement(true, () => {
let xml = new DOMParser().parseFromString(text, "application/xml");
if (xml.documentElement.nodeName === "parsererror") {
this.mErrCode = xmlcommon_1.XMLErrorCodes.unknown_error;
this.mErrDescription = xml.documentElement.firstChild.nodeValue;
return null;
}
else {
return xml.documentElement;
}
});
}
/**
* Writes the comment indicated
* @param {string} comment
* @return {number}
*/
writeComment(comment) {
return this.appendNodeToCurrentElement(true, () => {
return this.document.createComment(comment);
});
}
/**
* Writes a CData section with the indicated value
* @param {string} cData Value to write as a CData section
* @return any
*/
writeCData(cData) {
return this.appendNodeToCurrentElement(false, () => {
return this.document.createCDATASection(cData);
});
}
// Private methods
appendNodeToCurrentElement(allowAppendToDocument, nodeCreation) {
if (this.errCode === 0) {
let elem = this.elemStack.top() || (allowAppendToDocument ? this.document : null);
if (elem) {
let xml = nodeCreation();
if (xml) {
elem.appendChild(xml);
}
}
else {
this.mErrCode = xmlcommon_1.XMLErrorCodes.missing_start_element;
this.mErrDescription = "Missing start element";
}
}
return this.errCode;
}
resetErrors() {
this.mErrCode = xmlcommon_1.XMLErrorCodes.no_error;
this.mErrDescription = "";
}
// Not (yet) supported
/**
* @param {string} file
* @return number
*/
open(file) {
(0, helpers_1.notSupported)();
return null;
}
// ===========================
// To check
// ===========================
/**
* @param client
* @return any
*/
openRequest(client) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param response
* @return any
*/
openResponse(response) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param entity
* @return any
*/
writeEntityReference(entity) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param target
* @param value
* @return any
*/
writeProcessingInstruction(target, value) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param name
* @param subset
* @return any
*/
writeDocType(name, subset) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param name
* @param uri
* @param subset
* @return any
*/
writeDocTypeSystem(name, uri, subset) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param name
* @param pubId
* @param uri
* @param subset
* @return any
*/
writeDocTypePublic(name, pubId, uri, subset) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param localName
* @param prefix
* @param nameSpaceURI
* @return any
*/
writeNSStartElement(localName, prefix, nameSpaceURI) {
(0, helpers_1.notImplemented)();
return null;
}
/**
* @param localName
* @param nameSpaceURI
* @param value
* @return any
*/
writeNSElement(localName, nameSpaceURI, value) {
(0, helpers_1.notImplemented)();
return null;
}
get indentation() {
return this.mindentation;
}
set indentation(value) {
this.mindentation = value;
}
get indentChar() {
return this.mindentChar;
}
set indentChar(value) {
this.mindentChar = value;
}
}
exports.XMLWriter = XMLWriter;
XMLWriter.XMLWriterElementStack = class {
constructor() {
this.elements = new Array();
}
push(elem) {
this.elements.push(elem);
}
pop() {
return this.elements.length > 0 ? this.elements.pop() : undefined;
}
top() {
return this.elements.length > 0
? this.elements[this.elements.length - 1]
: undefined;
}
};
//# sourceMappingURL=xmlwriter.js.map