API / geotoolkit / report / parsers / ElementParser / 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));↳
ElementParser
Constructors
Methods
Constructors
• Protected new ElementParser(nodeName?, nodeType?)
| Name | Type |
|---|---|
Optional nodeName | string |
Optional nodeType | number |
NodeParser.constructor
Methods
▸ addElements(childElements, context): ParserContext
add child elements
| Name | Type |
|---|---|
childElements | ParserContext[] |
context | ParserContext |
▸ Abstract createElement(node, context): ParserContext
Create element associated with node
| Name | Type | Description |
|---|---|---|
node | Element | Node | |
context | ParserContext | The parsing context to be used to parse the node |
▸ getAttribute(node, attrName, defaultValue?): string
Extracts requested attribute from provided node
| Name | Type | Description |
|---|---|---|
node | Element | Node | XML node |
attrName | string | The name of attribute to obtain |
Optional defaultValue | string | default value |
string
Value of the attribute
▸ getClassName(): string
string
▸ getName(): string
Return parser name
string
▸ getType(): number
Return node type
number
▸ parseAttributes(node, context): ParserContext
| Name | Type | Description |
|---|---|---|
node | Element | Node | node to parse |
context | ParserContext | parser context |
▸ parseElement(node, context): ParserContext
| Name | Type | Description |
|---|---|---|
node | Element | Node | node |
context | ParserContext | context |
▸ 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]"
| Name | Type | Description |
|---|---|---|
value | string | value |
any
▸ Protected tryParseJSON(value): object
Parse string in to object, assuming that string value looks like "name:value;name:value" for example
| Name | Type | Description |
|---|---|---|
value | string | value |
object
▸ Protected tryParseValue(value): string | number | boolean
Try parse value to avoid converting it later in run-time
| Name | Type | Description |
|---|---|---|
value | string | value |
string | number | boolean
▸ Static getAttribute(node, attrName, defaultValue?): string
Extracts requested attribute from provided node
| Name | Type | Description |
|---|---|---|
node | Element | Node | XML node |
attrName | string | The name of attribute to obtain |
Optional defaultValue | string | default value |
string
Value of the attribute
▸ 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]"
| Name | Type | Description |
|---|---|---|
value | string | value |
any
▸ Static tryParseJSON(value): object
Parse string in to object, assuming that string value looks like "name:value;name:value" for example
| Name | Type | Description |
|---|---|---|
value | string | value |
object
▸ Static tryParseValue(value): string | number | boolean
Try parse value to avoid converting it later in run-time
| Name | Type | Description |
|---|---|---|
value | string | value |
string | number | boolean