projects/wms-framework/src/lib/baseframework/XmlWriter.ts
Class for creating xml
Properties |
|
Methods |
|
constructor(innerWriter: TextWriter, settings: XmlWriterSettings)
|
|||||||||
Creates an instance of XmlWriter.
Parameters :
|
Private internalState |
Default value : InternalState.StartDocument
|
Private pendingClosingTags |
Type : []
|
Default value : []
|
Private Readonly xmlHeader |
Type : string
|
Default value : '<?xml version="1.0" encoding="utf-8"?>'
|
Static Create | |||||||||
Create(innerWriter: TextWriter, settings?: XmlWriterSettings)
|
|||||||||
Create a new instance of this writer
Parameters :
Returns :
XmlWriter
|
Dispose |
Dispose()
|
Cleanup inner data
Returns :
void
|
Private PerformPendingStartElementActions |
PerformPendingStartElementActions()
|
Returns :
void
|
Private PreprocessAttributeValue | ||||||
PreprocessAttributeValue(processedValue: string)
|
||||||
Parameters :
Returns :
string
|
Private PreprocessStringValue | ||||||
PreprocessStringValue(processedValue: string)
|
||||||
Parameters :
Returns :
string
|
Public WriteAttributeString |
WriteAttributeString(name: string, value: string)
|
Writes a string attribute with value
Returns :
void
|
Private WriteClosingTag | ||||||
WriteClosingTag(tagText: any)
|
||||||
Parameters :
Returns :
void
|
Public WriteEndDocument |
WriteEndDocument()
|
Returns :
void
|
Public WriteEndElement |
WriteEndElement()
|
Writes the end of the element
Returns :
void
|
Public WriteStartDocument |
WriteStartDocument()
|
Writes the start of the document
Returns :
void
|
Public WriteStartElement | ||||||||||||
WriteStartElement(prefixOrName: string, nameOrNamespace?: string, namespaceOpt?: string)
|
||||||||||||
Writes the start of an element
Parameters :
Returns :
void
|
Public WriteValue | ||||||
WriteValue(value: unknown)
|
||||||
Write literal value
Parameters :
Returns :
void
|
import { TextWriter } from '../helpers/StreamsSupport';
import { IDisposable } from './IDisposable';
/**
* Writer settings
*/
type XmlWriterSettings = {
Indent: boolean;
};
/**
* Default writer settings
*/
const defaultSettings = { Indent: false };
/**
* Internal state
*
* @enum {number}
*/
enum InternalState {
WritingElement,
WritingElementContent,
StartDocument,
}
/**
* Class for creating xml
*
* @export
* @class XmlWriter
* @wType System.Xml.XmlWriter
* @wNetSupport
*/
export class XmlWriter implements IDisposable {
private readonly xmlHeader = '<?xml version="1.0" encoding="utf-8"?>';
private pendingClosingTags = [];
private internalState = InternalState.StartDocument;
/**
* Creates an instance of XmlWriter.
* @param {TextWriter} innerWriter
* @param {XmlWriterSettings} settings
* @memberof XmlWriter
*/
constructor(
private innerWriter: TextWriter,
private settings: XmlWriterSettings
) {}
/**
* Cleanup inner data
*
* @memberof XmlWriter
*/
Dispose(): void {
this.innerWriter = null;
}
/**
* Create a new instance of this writer
*
* @static
* @param {TextWriter} innerWriter
* @param {XmlWriterSettings} [settings]
* @return {*}
* @memberof XmlWriter
*/
public static Create(innerWriter: TextWriter, settings?: XmlWriterSettings) {
return new XmlWriter(innerWriter, settings ?? defaultSettings);
}
/**
* Write literal value
*
* @param {unknown} value
* @memberof XmlWriter
*/
public WriteValue(value: unknown) {
this.PerformPendingStartElementActions();
let processedValue = value?.toString() ?? '';
processedValue = this.PreprocessStringValue(processedValue);
this.innerWriter.Write(processedValue);
}
/**
* Writes a string attribute with value
*
* @param {string} name
* @param {string} value
* @memberof XmlWriter
*/
public WriteAttributeString(name: string, value: string): void {
value = this.PreprocessAttributeValue(value);
this.innerWriter.Write(` ${name}="${value}"`);
}
/**
* Writes the start of an element
*
* @param {string} prefixOrName
* @param {string} [nameOrNamespace]
* @param {string} [namespaceOpt]
* @memberof XmlWriter
*/
public WriteStartElement(
prefixOrName: string,
nameOrNamespace?: string,
namespaceOpt?: string
): void {
let name: string = null;
let namespace: string = null;
let prefix: string = null;
if (namespaceOpt && nameOrNamespace) {
name = `${prefixOrName}:${nameOrNamespace}`;
namespace = namespaceOpt;
prefix = prefixOrName;
} else if (nameOrNamespace) {
prefix = 'ns';
name = `${prefix}:${prefixOrName}`;
namespace = nameOrNamespace;
} else {
name = prefixOrName;
}
this.PerformPendingStartElementActions();
this.pendingClosingTags.push(name);
this.innerWriter.Write(`<${name}`);
if (namespace && prefix) {
this.innerWriter.Write(` xmlns:${prefix}='${namespace}'`);
}
this.internalState = InternalState.WritingElement;
}
/**
* Writes the end of the element
*
* @memberof XmlWriter
*/
public WriteEndElement(): void {
this.PerformPendingStartElementActions();
const lastTag = this.pendingClosingTags[this.pendingClosingTags.length - 1];
this.WriteClosingTag(lastTag);
this.pendingClosingTags.splice(this.pendingClosingTags.length - 1, 1);
}
/**
* Writes the start of the document
*
* @memberof XmlWriter
*/
public WriteStartDocument(): void {
this.innerWriter.Write(this.xmlHeader);
}
public WriteEndDocument() {
this.PerformPendingStartElementActions();
for (let i = this.pendingClosingTags.length - 1; i >= 0; i--) {
const tagText = this.pendingClosingTags[i];
this.WriteClosingTag(tagText);
}
}
private PreprocessStringValue(processedValue: string): string {
return processedValue.replace(/[<>]/g, (x) => {
return x == '<' ? '<' : '>';
});
}
private PreprocessAttributeValue(processedValue: string): string {
return processedValue.replace(/"/g, '"');
}
private WriteClosingTag(tagText: any) {
this.innerWriter.Write(`</${tagText}>`);
}
private PerformPendingStartElementActions() {
if (this.internalState == InternalState.WritingElement) {
this.innerWriter.Write('>');
this.internalState = InternalState.WritingElementContent;
}
}
}