@loaders.gl/xml 4.2.0-alpha.4 → 4.2.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,42 +1,56 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { StreamingXMLParser } from "./streaming-xml-parser.js";
2
5
  import { uncapitalizeKeys } from "../xml-utils/uncapitalize.js";
3
6
  import { XMLParser as FastXMLParser } from 'fast-xml-parser';
4
7
  export function parseXMLSync(text, options) {
5
- if (options !== null && options !== void 0 && options._parser && options._parser !== 'fast-xml-parser') {
6
- throw new Error(options === null || options === void 0 ? void 0 : options._parser);
7
- }
8
- const fastXMLOptions = {
9
- allowBooleanAttributes: true,
10
- ignoreDeclaration: true,
11
- removeNSPrefix: options === null || options === void 0 ? void 0 : options.removeNSPrefix,
12
- textNodeName: options === null || options === void 0 ? void 0 : options.textNodeName,
13
- isArray: (name, jpath, isLeafNode, isAttribute) => {
14
- var _options$arrayPaths;
15
- const array = Boolean(options === null || options === void 0 ? void 0 : (_options$arrayPaths = options.arrayPaths) === null || _options$arrayPaths === void 0 ? void 0 : _options$arrayPaths.some(path => jpath === path));
16
- return array;
17
- },
18
- ...(options === null || options === void 0 ? void 0 : options._fastXML)
19
- };
20
- const xml = fastParseXML(text, fastXMLOptions);
21
- return options !== null && options !== void 0 && options.uncapitalizeKeys ? uncapitalizeKeys(xml) : xml;
8
+ if (options?._parser && options._parser !== 'fast-xml-parser') {
9
+ throw new Error(options?._parser);
10
+ }
11
+ const fastXMLOptions = {
12
+ // Default FastXML options
13
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#allowbooleanattributes
14
+ allowBooleanAttributes: true,
15
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#ignoredeclaration
16
+ ignoreDeclaration: true,
17
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#removensprefix
18
+ removeNSPrefix: options?.removeNSPrefix,
19
+ // https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#textnodename
20
+ textNodeName: options?.textNodeName,
21
+ // Let's application specify keys that are always arrays
22
+ isArray: (name, jpath, isLeafNode, isAttribute) => {
23
+ const array = Boolean(options?.arrayPaths?.some((path) => jpath === path));
24
+ return array;
25
+ },
26
+ // Application overrides
27
+ ...options?._fastXML
28
+ };
29
+ const xml = fastParseXML(text, fastXMLOptions);
30
+ // Note - could be done with FastXML tag processing
31
+ return options?.uncapitalizeKeys ? uncapitalizeKeys(xml) : xml;
22
32
  }
23
33
  export function fastParseXML(text, options) {
24
- const parser = new FastXMLParser({
25
- ignoreAttributes: false,
26
- attributeNamePrefix: '',
27
- ...options
28
- });
29
- const parsedXML = parser.parse(text);
30
- return parsedXML;
34
+ const parser = new FastXMLParser({
35
+ ignoreAttributes: false,
36
+ attributeNamePrefix: '',
37
+ ...options
38
+ });
39
+ const parsedXML = parser.parse(text);
40
+ return parsedXML;
31
41
  }
32
- export function parseXMLInBatches(text) {
33
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
34
- const parser = new StreamingXMLParser({
35
- ...options,
36
- strict: true
37
- });
38
- parser.write(text);
39
- parser.close();
40
- return parser.result;
42
+ /**
43
+ * @todo Build a streaming XML parser based on sax-js
44
+ * @param text
45
+ * @param options
46
+ * @returns
47
+ */
48
+ export function parseXMLInBatches(text, options = {}) {
49
+ const parser = new StreamingXMLParser({
50
+ ...options,
51
+ strict: true
52
+ });
53
+ parser.write(text);
54
+ parser.close();
55
+ return parser.result;
41
56
  }
42
- //# sourceMappingURL=parse-xml.js.map
@@ -1,4 +1,4 @@
1
- import { SAXParser, SAXParserOptions } from '../../sax-ts/sax';
1
+ import { SAXParser, SAXParserOptions } from "../../sax-ts/sax.js";
2
2
  export type StreamingXMLParserOptions = SAXParserOptions;
3
3
  /**
4
4
  * StreamingXMLParser builds a JSON object using the events emitted by the SAX parser
@@ -1 +1 @@
1
- {"version":3,"file":"streaming-xml-parser.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/streaming-xml-parser.ts"],"names":[],"mappings":"AAOA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAC,MAAM,kBAAkB,CAAC;AAG7D,MAAM,MAAM,yBAAyB,GAAG,gBAAgB,CAAC;AAEzD;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,MAAM,YAAa;IACnB,cAAc,UAAM;IACpB,YAAY;;;OAA6C;gBAG7C,OAAO,EAAE,gBAAgB;IA+CrC,KAAK,IAAI,IAAI;IAMb,KAAK,CAAC,KAAK,KAAA,GAAG,IAAI;IAIlB,KAAK,IAAI,IAAI;IAMb,UAAU,CAAC,KAAK,KAAA,GAAG,IAAI;IAYvB,UAAU,CAAC,YAAY,UAAK,GAAG,IAAI;IAOnC,WAAW,IAAI,IAAI;IAKnB,WAAW,CAAC,YAAY,KAAK,GAAG,IAAI;IAOpC,YAAY,IAAI,IAAI;CAIrB"}
1
+ {"version":3,"file":"streaming-xml-parser.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/streaming-xml-parser.ts"],"names":[],"mappings":"AAOA,OAAO,EAAC,SAAS,EAAE,gBAAgB,EAAC,4BAAyB;AAG7D,MAAM,MAAM,yBAAyB,GAAG,gBAAgB,CAAC;AAEzD;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,MAAM,YAAa;IACnB,cAAc,UAAM;IACpB,YAAY;;;OAA6C;gBAG7C,OAAO,EAAE,gBAAgB;IA+CrC,KAAK,IAAI,IAAI;IAMb,KAAK,CAAC,KAAK,KAAA,GAAG,IAAI;IAIlB,KAAK,IAAI,IAAI;IAMb,UAAU,CAAC,KAAK,KAAA,GAAG,IAAI;IAYvB,UAAU,CAAC,YAAY,UAAK,GAAG,IAAI;IAOnC,WAAW,IAAI,IAAI;IAKnB,WAAW,CAAC,YAAY,KAAK,GAAG,IAAI;IAOpC,YAAY,IAAI,IAAI;CAIrB"}
@@ -1,105 +1,97 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // @ts-nocheck
5
+ /* eslint-disable */
1
6
  import { SAXParser } from "../../sax-ts/sax.js";
7
+ /**
8
+ * StreamingXMLParser builds a JSON object using the events emitted by the SAX parser
9
+ */
2
10
  export class StreamingXMLParser {
3
- constructor(options) {
4
- this.parser = void 0;
5
- this.result = undefined;
6
- this.previousStates = [];
7
- this.currentState = Object.freeze({
8
- container: [],
9
- key: null
10
- });
11
- this.reset();
12
- this.parser = new SAXParser({
13
- onready: () => {
14
- this.previousStates.length = 0;
15
- this.currentState.container.length = 0;
16
- },
17
- onopentag: _ref => {
18
- let {
19
- name,
20
- attributes,
21
- isSelfClosing
22
- } = _ref;
23
- this._openObject({});
24
- if (typeof name !== 'undefined') {
25
- this.parser.emit('onkey', name);
11
+ // jsonpath: JSONPath = new JSONPath();
12
+ constructor(options) {
13
+ this.result = undefined;
14
+ this.previousStates = [];
15
+ this.currentState = Object.freeze({ container: [], key: null });
16
+ this.reset();
17
+ this.parser = new SAXParser({
18
+ onready: () => {
19
+ this.previousStates.length = 0;
20
+ this.currentState.container.length = 0;
21
+ },
22
+ onopentag: ({ name, attributes, isSelfClosing }) => {
23
+ this._openObject({});
24
+ if (typeof name !== 'undefined') {
25
+ this.parser.emit('onkey', name);
26
+ }
27
+ },
28
+ onkey: (name) => {
29
+ this.currentState.key = name;
30
+ },
31
+ onclosetag: () => {
32
+ this._closeObject();
33
+ },
34
+ onopenarray: () => {
35
+ this._openArray();
36
+ },
37
+ onclosearray: () => {
38
+ this._closeArray();
39
+ },
40
+ ontext: (value) => {
41
+ this._pushOrSet(value);
42
+ },
43
+ onerror: (error) => {
44
+ throw error;
45
+ },
46
+ onend: () => {
47
+ this.result = this.currentState.container.pop();
48
+ },
49
+ ...options
50
+ });
51
+ }
52
+ reset() {
53
+ this.result = undefined;
54
+ this.previousStates = [];
55
+ this.currentState = Object.freeze({ container: [], key: null });
56
+ }
57
+ write(chunk) {
58
+ this.parser.write(chunk);
59
+ }
60
+ close() {
61
+ this.parser.close();
62
+ }
63
+ // PRIVATE METHODS
64
+ _pushOrSet(value) {
65
+ const { container, key } = this.currentState;
66
+ if (key !== null) {
67
+ container[key] = value;
68
+ this.currentState.key = null;
26
69
  }
27
- },
28
- onkey: name => {
29
- this.currentState.key = name;
30
- },
31
- onclosetag: () => {
32
- this._closeObject();
33
- },
34
- onopenarray: () => {
35
- this._openArray();
36
- },
37
- onclosearray: () => {
38
- this._closeArray();
39
- },
40
- ontext: value => {
41
- this._pushOrSet(value);
42
- },
43
- onerror: error => {
44
- throw error;
45
- },
46
- onend: () => {
47
- this.result = this.currentState.container.pop();
48
- },
49
- ...options
50
- });
51
- }
52
- reset() {
53
- this.result = undefined;
54
- this.previousStates = [];
55
- this.currentState = Object.freeze({
56
- container: [],
57
- key: null
58
- });
59
- }
60
- write(chunk) {
61
- this.parser.write(chunk);
62
- }
63
- close() {
64
- this.parser.close();
65
- }
66
- _pushOrSet(value) {
67
- const {
68
- container,
69
- key
70
- } = this.currentState;
71
- if (key !== null) {
72
- container[key] = value;
73
- this.currentState.key = null;
74
- } else if (Array.isArray(container)) {
75
- container.push(value);
76
- } else if (container) {}
77
- }
78
- _openArray() {
79
- let newContainer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
80
- this._pushOrSet(newContainer);
81
- this.previousStates.push(this.currentState);
82
- this.currentState = {
83
- container: newContainer,
84
- isArray: true,
85
- key: null
86
- };
87
- }
88
- _closeArray() {
89
- this.currentState = this.previousStates.pop();
90
- }
91
- _openObject() {
92
- let newContainer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
93
- this._pushOrSet(newContainer);
94
- this.previousStates.push(this.currentState);
95
- this.currentState = {
96
- container: newContainer,
97
- isArray: false,
98
- key: null
99
- };
100
- }
101
- _closeObject() {
102
- this.currentState = this.previousStates.pop();
103
- }
70
+ else if (Array.isArray(container)) {
71
+ container.push(value);
72
+ }
73
+ else if (container) {
74
+ // break for debug
75
+ }
76
+ }
77
+ _openArray(newContainer = []) {
78
+ // this.jsonpath.push(null);
79
+ this._pushOrSet(newContainer);
80
+ this.previousStates.push(this.currentState);
81
+ this.currentState = { container: newContainer, isArray: true, key: null };
82
+ }
83
+ _closeArray() {
84
+ // this.jsonpath.pop();
85
+ this.currentState = this.previousStates.pop();
86
+ }
87
+ _openObject(newContainer = {}) {
88
+ // this.jsonpath.push(null);
89
+ this._pushOrSet(newContainer);
90
+ this.previousStates.push(this.currentState);
91
+ this.currentState = { container: newContainer, isArray: false, key: null };
92
+ }
93
+ _closeObject() {
94
+ // this.jsonpath.pop();
95
+ this.currentState = this.previousStates.pop();
96
+ }
104
97
  }
105
- //# sourceMappingURL=streaming-xml-parser.js.map
@@ -1,17 +1,29 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ /**
5
+ * Uncapitalize first letter of a string
6
+ * @param str
7
+ * @returns
8
+ */
1
9
  export function uncapitalize(str) {
2
- return typeof str === 'string' ? str.charAt(0).toLowerCase() + str.slice(1) : str;
10
+ return typeof str === 'string' ? str.charAt(0).toLowerCase() + str.slice(1) : str;
3
11
  }
12
+ /**
13
+ * Recursively uncapitalize all keys in a nested object
14
+ * @param object
15
+ * @returns
16
+ */
4
17
  export function uncapitalizeKeys(object) {
5
- if (Array.isArray(object)) {
6
- return object.map(element => uncapitalizeKeys(element));
7
- }
8
- if (object && typeof object === 'object') {
9
- const newObject = {};
10
- for (const [key, value] of Object.entries(object)) {
11
- newObject[uncapitalize(key)] = uncapitalizeKeys(value);
18
+ if (Array.isArray(object)) {
19
+ return object.map((element) => uncapitalizeKeys(element));
12
20
  }
13
- return newObject;
14
- }
15
- return object;
21
+ if (object && typeof object === 'object') {
22
+ const newObject = {};
23
+ for (const [key, value] of Object.entries(object)) {
24
+ newObject[uncapitalize(key)] = uncapitalizeKeys(value);
25
+ }
26
+ return newObject;
27
+ }
28
+ return object;
16
29
  }
17
- //# sourceMappingURL=uncapitalize.js.map
@@ -1,14 +1,28 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // TODO - these utilities could be moved to the XML parser.
5
+ // uncapitalizeKeys could be an XMLLoader option
6
+ /**
7
+ * Extracts a value or array and always return an array
8
+ * Useful since XML parses to object instead of array when only a single value is provided
9
+ */
1
10
  export function convertXMLValueToArray(xmlValue) {
2
- if (Array.isArray(xmlValue)) {
3
- return xmlValue;
4
- }
5
- if (xmlValue && typeof xmlValue === 'object' && xmlValue['0']) {}
6
- if (xmlValue) {
7
- return [xmlValue];
8
- }
9
- return [];
11
+ if (Array.isArray(xmlValue)) {
12
+ return xmlValue;
13
+ }
14
+ if (xmlValue && typeof xmlValue === 'object' && xmlValue['0']) {
15
+ // Error this is an objectified array
16
+ }
17
+ if (xmlValue) {
18
+ return [xmlValue];
19
+ }
20
+ return [];
10
21
  }
22
+ /**
23
+ * Mutates a field in place, converting it to array
24
+ * Useful since XML parses to object instead of array when only a single value is provided
25
+ */
11
26
  export function convertXMLFieldToArrayInPlace(xml, key) {
12
- xml[key] = convertXMLValueToArray(xml[key]);
27
+ xml[key] = convertXMLValueToArray(xml[key]);
13
28
  }
14
- //# sourceMappingURL=xml-utils.js.map