Last updated

API / geotoolkit / report / parsers / ElementParser / ElementParser

Class: ElementParser

parsers.ElementParser.ElementParser

Define an abstract parser of element

Example

// How to create a custom parser
import {ElementParser} from '@int/geotoolkit/report/parsers/ElementParser';
import {obfuscate} from '@int/geotoolkit/lib';
import {CrossPlot} from '@int/geotoolkit/widgets/CrossPlot';
import {Events} from '@int/geotoolkit/report/elements/Events';
import {NodeParser} from '@int/geotoolkit/report/parsers/NodeParser';
import {Registry} from '@int/geotoolkit/report/parsers/Registry';
import {DOMParser as cgDOMParser} from '@int/geotoolkit/report/dom/DOMParser';
import {Parser} from '@int/geotoolkit/report/Parser';

class CrossPlotParser extends ElementParser {
constructor () {
super();
}
createElement (node, context) {
// if (node.nodeType !== node.ELEMENT_NODE) return null;
const element = new CrossPlot();
context.setElement(element);
context.notify(Events.ElementCreated, this, element);
return context;
}
parseAttributes (node, context) {
const element = context.getElement();
const properties = {};
if (node.attributes != null) {
for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes.item(i);
const value = attr.value;
const name = attr.name;
if (value == null || typeof value !== 'string') {
properties[name] = value;
continue;
}
properties[name] = NodeParser.tryParseJSON(value) || NodeParser.tryParseValue(value);
}
}
element.setProperties(properties);
}
addElements (childContexts, context) {
return context;
}
}
obfuscate(CrossPlotParser);
const registry = Registry.getDefaultInstance();
registry.register(new CrossPlotParser(), 'crossplot', window.Node.ELEMENT_NODE);
const xml = '<document width="100%"><body width="100%"><crossplot tag="8"></crossplot></body></document>';
cgDOMParser
.parse(xml)
.then( (cgDomDocument) => Parser.parse(cgDomDocument));

Hierarchy

Table of contents

Constructors
Methods

Contents

Constructors

new ElementParser(nodeName, nodeType)

Protected new ElementParser(nodeName?, nodeType?)

Parameters

Name Type
Optional nodeNamestring
Optional nodeTypenumber

Overrides

NodeParser.constructor

Methods

addElements

addElements(childElements, context): ParserContext

add child elements

Parameters

Name Type
childElementsParserContext[]
contextParserContext

Returns

ParserContext

Inherited from

NodeParser.addElements


createElement

Abstract createElement(node, context): ParserContext

Create element associated with node

Parameters

Name Type Description
nodeElement | Node
contextParserContextThe parsing context to be used to parse the node

Returns

ParserContext

Inherited from

NodeParser.createElement


getAttribute

getAttribute(node, attrName, defaultValue?): string

Extracts requested attribute from provided node

Parameters

Name Type Description
nodeElement | NodeXML node
attrNamestringThe name of attribute to obtain
Optional defaultValuestringdefault value

Returns

string

Value of the attribute

Inherited from

NodeParser.getAttribute


getClassName

getClassName(): string

Returns

string

Inherited from

NodeParser.getClassName


getName

getName(): string

Return parser name

Returns

string

Inherited from

NodeParser.getName


getType

getType(): number

Return node type

Returns

number

Inherited from

NodeParser.getType


parseAttributes

parseAttributes(node, context): ParserContext

Parameters

Name Type Description
nodeElement | Nodenode to parse
contextParserContextparser context

Returns

ParserContext

Overrides

NodeParser.parseAttributes


parseElement

parseElement(node, context): ParserContext

Parameters

Name Type Description
nodeElement | Nodenode
contextParserContextcontext

Returns

ParserContext

Inherited from

NodeParser.parseElement


tryParseArray

Protected tryParseArray(value): any

Parse string into array of objects, assuming that string value looks like "[value,value,value]" for example "[10,20,30,75.8999,36]" or "[x:10;y:20,x:15;y:45]"

Parameters

Name Type Description
valuestringvalue

Returns

any

Inherited from

NodeParser.tryParseArray


tryParseJSON

Protected tryParseJSON(value): object

Parse string in to object, assuming that string value looks like "name:value;name:value" for example or

Parameters

Name Type Description
valuestringvalue

Returns

object

Inherited from

NodeParser.tryParseJSON


tryParseValue

Protected tryParseValue(value): string | number | boolean

Try parse value to avoid converting it later in run-time

Parameters

Name Type Description
valuestringvalue

Returns

string | number | boolean

Inherited from

NodeParser.tryParseValue


getAttribute

Static getAttribute(node, attrName, defaultValue?): string

Extracts requested attribute from provided node

Parameters

Name Type Description
nodeElement | NodeXML node
attrNamestringThe name of attribute to obtain
Optional defaultValuestringdefault value

Returns

string

Value of the attribute

Inherited from

NodeParser.getAttribute


tryParseArray

Static tryParseArray(value): any

Parse string into array of objects, assuming that string value looks like "[value,value,value]" for example "[10,20,30,75.8999,36]" or "[x:10;y:20,x:15;y:45]"

Parameters

Name Type Description
valuestringvalue

Returns

any

Inherited from

NodeParser.tryParseArray


tryParseJSON

Static tryParseJSON(value): object

Parse string in to object, assuming that string value looks like "name:value;name:value" for example or

Parameters

Name Type Description
valuestringvalue

Returns

object

Inherited from

NodeParser.tryParseJSON


tryParseValue

Static tryParseValue(value): string | number | boolean

Try parse value to avoid converting it later in run-time

Parameters

Name Type Description
valuestringvalue

Returns

string | number | boolean

Inherited from

NodeParser.tryParseValue