libs/sensorml/src/lib/services/AbstractXmlService.ts
Methods |
|
Protected Abstract decode | ||||||
decode(document: Document)
|
||||||
Parameters :
Returns :
T
|
deserialize | ||||||
deserialize(xml: string | Document)
|
||||||
Parameters :
Returns :
T
|
Protected Abstract encode | ||||||
encode(object: T)
|
||||||
Parameters :
Returns :
Document
|
Private prettyPrint | ||||||
prettyPrint(source: Document)
|
||||||
Parameters :
Returns :
Document
|
serialize | |||||||||
serialize(description: T, prettify?: boolean)
|
|||||||||
Parameters :
Returns :
any
|
Abstract deserialize | ||||||
deserialize(xml: string | Document)
|
||||||
Inherited from
XmlService
|
||||||
Defined in
XmlService:4
|
||||||
Parameters :
Returns :
T
|
Abstract serialize | |||||||||
serialize(description: T, prettify: boolean)
|
|||||||||
Inherited from
XmlService
|
|||||||||
Defined in
XmlService:3
|
|||||||||
Parameters :
Returns :
string
|
import { XmlService } from './XmlService';
export abstract class AbstractXmlService<T> extends XmlService<T> {
serialize(description: T, prettify?: boolean) {
const serializer = new XMLSerializer();
let document = this.encode(description);
if (prettify) { document = this.prettyPrint(document); }
return serializer.serializeToString(document);
}
deserialize(xml: string | Document): T {
let document: Document;
if (xml instanceof Document) {
document = xml;
} else if (typeof (xml) === 'string') {
const deserializer = new DOMParser();
document = deserializer.parseFromString(xml as string, 'application/xml');
} else {
return null;
}
return this.decode(document);
}
protected abstract decode(document: Document): T;
protected abstract encode(object: T): Document;
private prettyPrint(source: Document): Document {
const stylesheet: Document = new DOMParser().parseFromString(
`<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>`, 'application/xml');
const processor = new XSLTProcessor();
processor.importStylesheet(stylesheet);
return processor.transformToDocument(source);
}
}