@xapi-js/core 1.2.0 → 1.3.0

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.
package/dist/index.cjs CHANGED
@@ -1,30 +1,3 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
- key = keys[i];
11
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
- get: ((k) => from[k]).bind(null, key),
13
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
- });
15
- }
16
- return to;
17
- };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
- value: mod,
20
- enumerable: true
21
- }) : target, mod));
22
-
23
- //#endregion
24
- let stax_xml = require("stax-xml");
25
- stax_xml = __toESM(stax_xml);
26
- let txml = require("txml");
27
- txml = __toESM(txml);
28
1
 
29
2
  //#region src/types.ts
30
3
  /**
@@ -163,18 +136,18 @@ var XapiRoot = class {
163
136
  return this.datasets.length;
164
137
  }
165
138
  /**
166
- * Iterates over the parameters.
167
- * @returns An IterableIterator for parameters.
139
+ * Returns all parameters as an array.
140
+ * @returns An array of parameters.
168
141
  */
169
- *iterParameters() {
170
- for (const param of this.parameters.params) yield param;
142
+ getParameters() {
143
+ return this.parameters.params;
171
144
  }
172
145
  /**
173
- * Iterates over the datasets.
174
- * @returns An IterableIterator for datasets.
146
+ * Returns all datasets as an array.
147
+ * @returns An array of datasets.
175
148
  */
176
- *iterDatasets() {
177
- for (const dataset of this.datasets) yield dataset;
149
+ getDatasets() {
150
+ return this.datasets;
178
151
  }
179
152
  };
180
153
  /**
@@ -302,25 +275,25 @@ var Dataset = class {
302
275
  } else throw new Error(`Row index ${rowIdx} out of bounds in dataset ${this.id}`);
303
276
  }
304
277
  /**
305
- * Iterates over the constant column definitions.
306
- * @returns An IterableIterator for constant columns.
278
+ * Returns all constant column definitions as an array.
279
+ * @returns An array of constant columns.
307
280
  */
308
- *iterConstColumns() {
309
- for (const column of this.constColumns) yield column;
281
+ getConstColumns() {
282
+ return this.constColumns;
310
283
  }
311
284
  /**
312
- * Iterates over the column definitions.
313
- * @returns An IterableIterator for columns.
285
+ * Returns all column definitions as an array.
286
+ * @returns An array of columns.
314
287
  */
315
- *iterColumns() {
316
- for (const column of this.columns) yield column;
288
+ getColumns() {
289
+ return this.columns;
317
290
  }
318
291
  /**
319
- * Iterates over the rows in the dataset.
320
- * @returns An IterableIterator for rows.
292
+ * Returns all rows in the dataset as an array.
293
+ * @returns An array of rows.
321
294
  */
322
- *iterRows() {
323
- for (const row of this.rows) yield row;
295
+ getRows() {
296
+ return this.rows;
324
297
  }
325
298
  /**
326
299
  * Returns the number of column definitions.
@@ -363,18 +336,6 @@ function makeParseEntities() {
363
336
  return entities$1;
364
337
  }
365
338
  /**
366
- * Creates an array of entities for writing XML, including control characters.
367
- * @returns An array of objects, each with an `entity` (e.g., "&#1;") and its `value` (e.g., "\x01").
368
- */
369
- function makeWriterEntities() {
370
- const entities$1 = [];
371
- for (let i = 1; i <= 32; i++) entities$1.push({
372
- entity: `&#${i};`,
373
- value: String.fromCharCode(i)
374
- });
375
- return entities$1;
376
- }
377
- /**
378
339
  * Converts a Base64 string to a Uint8Array.
379
340
  * @param base64String - The Base64 string to convert.
380
341
  * @returns A Uint8Array.
@@ -545,15 +506,291 @@ function convertToString(value, type) {
545
506
  const entities = makeParseEntities();
546
507
  function _unescapeXml(str) {
547
508
  if (!str) return str;
509
+ let result = str.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&apos;/g, "'").replace(/&amp;/g, "&");
548
510
  const regex = new RegExp(entities.map((e) => e.entity).join("|"), "g");
549
- return str.replace(regex, (match) => {
511
+ result = result.replace(regex, (match) => {
550
512
  const entity = entities.find((e) => e.entity === match);
551
513
  return entity ? entity.value : match;
552
514
  });
515
+ return result;
553
516
  }
554
517
  function isXapiRoot(value) {
555
518
  return value instanceof XapiRoot;
556
519
  }
520
+ /**
521
+ * Escapes XML special characters in a string.
522
+ * @param str - The string to escape.
523
+ * @returns The escaped string.
524
+ */
525
+ function escapeXml(str) {
526
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
527
+ }
528
+ /**
529
+ * Encodes control characters as XML entities, excluding standard whitespace.
530
+ * Encodes 0x01-0x08, 0x0B-0x0C, 0x0E-0x1F but NOT 0x09 (tab), 0x0A (LF), 0x0D (CR), 0x20 (space).
531
+ * @param str - The string to encode.
532
+ * @returns The encoded string.
533
+ */
534
+ function encodeControlChars(str) {
535
+ let result = "";
536
+ for (let i = 0; i < str.length; i++) {
537
+ const charCode = str.charCodeAt(i);
538
+ if (charCode >= 1 && charCode <= 8 || charCode >= 11 && charCode <= 12 || charCode >= 14 && charCode <= 31) result += `&#${charCode};`;
539
+ else result += str[i];
540
+ }
541
+ return result;
542
+ }
543
+ /**
544
+ * A string builder for generating formatted XML.
545
+ */
546
+ var XmlStringBuilder = class {
547
+ lines = [];
548
+ indentLevel = 0;
549
+ indentString = " ";
550
+ /**
551
+ * Writes the XML declaration.
552
+ * @param version - The XML version (default: "1.0").
553
+ * @param encoding - The encoding (default: "UTF-8").
554
+ */
555
+ writeDeclaration(version = "1.0", encoding = "UTF-8") {
556
+ this.lines.push(`<?xml version="${version}" encoding="${encoding}"?>`);
557
+ }
558
+ /**
559
+ * Writes a start element tag.
560
+ * @param name - The element name.
561
+ * @param attributes - Optional attributes object.
562
+ * @param selfClosing - Whether to create a self-closing tag.
563
+ */
564
+ writeStartElement(name, attributes, selfClosing) {
565
+ let tag = `${this.indentString.repeat(this.indentLevel)}<${name}`;
566
+ if (attributes) for (const [key, value] of Object.entries(attributes)) {
567
+ const encodedValue = encodeControlChars(escapeXml(value));
568
+ tag += ` ${key}="${encodedValue}"`;
569
+ }
570
+ if (selfClosing) {
571
+ tag += "/>";
572
+ this.lines.push(tag);
573
+ } else {
574
+ tag += ">";
575
+ this.lines.push(tag);
576
+ this.indentLevel++;
577
+ }
578
+ }
579
+ /**
580
+ * Writes an end element tag.
581
+ * @param name - The element name.
582
+ */
583
+ writeEndElement(name) {
584
+ this.indentLevel--;
585
+ const indent = this.indentString.repeat(this.indentLevel);
586
+ this.lines.push(`${indent}</${name}>`);
587
+ }
588
+ /**
589
+ * Writes an element with text content.
590
+ * @param name - The element name.
591
+ * @param attributes - Optional attributes object.
592
+ * @param text - The text content.
593
+ */
594
+ writeElementWithText(name, attributes, text) {
595
+ let tag = `${this.indentString.repeat(this.indentLevel)}<${name}`;
596
+ if (attributes) for (const [key, value] of Object.entries(attributes)) {
597
+ const encodedValue = encodeControlChars(escapeXml(value));
598
+ tag += ` ${key}="${encodedValue}"`;
599
+ }
600
+ const encodedText = encodeControlChars(escapeXml(text));
601
+ tag += `>${encodedText}</${name}>`;
602
+ this.lines.push(tag);
603
+ }
604
+ /**
605
+ * Returns the generated XML string.
606
+ * @returns The complete XML string with line breaks.
607
+ */
608
+ toString() {
609
+ return this.lines.join("\n") + "\n";
610
+ }
611
+ };
612
+ /**
613
+ * Parses XML string into an XML node structure.
614
+ * This is a lightweight parser optimized for X-API XML structure.
615
+ *
616
+ * @param xml - The XML string to parse.
617
+ * @returns An array of parsed XML nodes.
618
+ */
619
+ function parseXml(xml) {
620
+ if (!xml || xml.trim() === "") return [];
621
+ return new XapiXmlParser(xml).parse();
622
+ }
623
+ /**
624
+ * A lightweight XML parser optimized for X-API structure.
625
+ */
626
+ var XapiXmlParser = class {
627
+ xml;
628
+ pos = 0;
629
+ length;
630
+ constructor(xml) {
631
+ this.xml = xml;
632
+ this.length = xml.length;
633
+ }
634
+ parse() {
635
+ const nodes = [];
636
+ while (this.pos < this.length) {
637
+ this.skipWhitespace();
638
+ if (this.pos >= this.length) break;
639
+ if (this.peek(5) === "<?xml") {
640
+ this.skipUntil("?>");
641
+ this.pos += 2;
642
+ continue;
643
+ }
644
+ if (this.peek(4) === "<!--") {
645
+ this.skipUntil("-->");
646
+ this.pos += 3;
647
+ continue;
648
+ }
649
+ if (this.current() === "<") {
650
+ const node = this.parseElement();
651
+ if (node) nodes.push(node);
652
+ } else this.pos++;
653
+ }
654
+ return nodes;
655
+ }
656
+ parseElement() {
657
+ if (this.current() !== "<") return null;
658
+ this.pos++;
659
+ if (this.current() === "/") return null;
660
+ const tagName = this.parseTagName();
661
+ if (!tagName) return null;
662
+ const node = { tagName };
663
+ const attributes = this.parseAttributes();
664
+ if (attributes && Object.keys(attributes).length > 0) node.attributes = attributes;
665
+ this.skipWhitespace();
666
+ if (this.peek(2) === "/>") {
667
+ this.pos += 2;
668
+ return node;
669
+ }
670
+ if (this.current() === ">") this.pos++;
671
+ const children = [];
672
+ let textContent = "";
673
+ while (this.pos < this.length) {
674
+ if (this.peek(9) === "<![CDATA[") {
675
+ const cdataText = this.parseCDATA();
676
+ if (cdataText !== null) textContent += cdataText;
677
+ continue;
678
+ }
679
+ if (this.peek(2) === "</") {
680
+ if (textContent) children.push(textContent);
681
+ this.pos += 2;
682
+ this.skipUntil(">");
683
+ this.pos++;
684
+ break;
685
+ }
686
+ if (this.current() === "<") {
687
+ if (textContent.trim()) {
688
+ children.push(textContent);
689
+ textContent = "";
690
+ }
691
+ const child = this.parseElement();
692
+ if (child) children.push(child);
693
+ } else {
694
+ textContent += this.current();
695
+ this.pos++;
696
+ }
697
+ }
698
+ if (children.length > 0) node.children = children;
699
+ return node;
700
+ }
701
+ parseTagName() {
702
+ let name = "";
703
+ while (this.pos < this.length) {
704
+ const ch = this.current();
705
+ if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === ">" || ch === "/") break;
706
+ name += ch;
707
+ this.pos++;
708
+ }
709
+ return name;
710
+ }
711
+ parseAttributes() {
712
+ const attrs = {};
713
+ while (this.pos < this.length) {
714
+ this.skipWhitespace();
715
+ const ch = this.current();
716
+ if (ch === ">" || ch === "/" || this.peek(2) === "/>") break;
717
+ const attrName = this.parseAttributeName();
718
+ if (!attrName) break;
719
+ this.skipWhitespace();
720
+ if (this.current() === "=") this.pos++;
721
+ this.skipWhitespace();
722
+ attrs[attrName] = this.parseAttributeValue();
723
+ }
724
+ return Object.keys(attrs).length > 0 ? attrs : void 0;
725
+ }
726
+ parseAttributeName() {
727
+ let name = "";
728
+ while (this.pos < this.length) {
729
+ const ch = this.current();
730
+ if (ch === "=" || ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === ">" || ch === "/") break;
731
+ name += ch;
732
+ this.pos++;
733
+ }
734
+ return name;
735
+ }
736
+ parseAttributeValue() {
737
+ let value = "";
738
+ const quote = this.current();
739
+ if (quote !== "\"" && quote !== "'") {
740
+ while (this.pos < this.length) {
741
+ const ch = this.current();
742
+ if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === ">" || ch === "/") break;
743
+ value += ch;
744
+ this.pos++;
745
+ }
746
+ return value;
747
+ }
748
+ this.pos++;
749
+ while (this.pos < this.length) {
750
+ const ch = this.current();
751
+ if (ch === quote) {
752
+ this.pos++;
753
+ break;
754
+ }
755
+ value += ch;
756
+ this.pos++;
757
+ }
758
+ return value;
759
+ }
760
+ parseCDATA() {
761
+ if (this.peek(9) !== "<![CDATA[") return null;
762
+ this.pos += 9;
763
+ let content = "";
764
+ while (this.pos < this.length) {
765
+ if (this.peek(3) === "]]>") {
766
+ this.pos += 3;
767
+ break;
768
+ }
769
+ content += this.current();
770
+ this.pos++;
771
+ }
772
+ return content;
773
+ }
774
+ skipWhitespace() {
775
+ while (this.pos < this.length) {
776
+ const ch = this.current();
777
+ if (ch !== " " && ch !== " " && ch !== "\n" && ch !== "\r") break;
778
+ this.pos++;
779
+ }
780
+ }
781
+ skipUntil(target) {
782
+ while (this.pos < this.length) {
783
+ if (this.peek(target.length) === target) break;
784
+ this.pos++;
785
+ }
786
+ }
787
+ current() {
788
+ return this.xml[this.pos];
789
+ }
790
+ peek(length) {
791
+ return this.xml.substring(this.pos, this.pos + length);
792
+ }
793
+ };
557
794
 
558
795
  //#endregion
559
796
  //#region src/handler.ts
@@ -570,12 +807,12 @@ function initXapi(options) {
570
807
  _options = { ...options };
571
808
  }
572
809
  /**
573
- * Parses an XML string into an XapiRoot object using txml.
810
+ * Parses an XML string into an XapiRoot object using custom parser.
574
811
  * @param xml - The string containing the XML data.
575
- * @returns A Promise that resolves to an XapiRoot object.
812
+ * @returns An XapiRoot object.
576
813
  */
577
814
  function parse(xml) {
578
- const parsedXml = txml.parse(xml);
815
+ const parsedXml = parseXml(xml);
579
816
  const xapiRoot = new XapiRoot();
580
817
  let rootElement;
581
818
  for (let i = 0; i < parsedXml.length; i++) if (parsedXml[i].tagName === "Root") {
@@ -586,6 +823,7 @@ function parse(xml) {
586
823
  const rootChildren = rootElement.children;
587
824
  if (rootChildren && rootChildren.length > 0) for (let i = 0; i < rootChildren.length; i++) {
588
825
  const child = rootChildren[i];
826
+ if (typeof child === "string") continue;
589
827
  const tagName = child.tagName;
590
828
  if (tagName === "Parameters") parseParameters(child, xapiRoot);
591
829
  else if (tagName === "Dataset") {
@@ -598,6 +836,7 @@ function parse(xml) {
598
836
  let rowsElement;
599
837
  if (datasetChildren) for (let j = 0; j < datasetChildren.length; j++) {
600
838
  const datasetChild = datasetChildren[j];
839
+ if (typeof datasetChild === "string") continue;
601
840
  const childTagName = datasetChild.tagName;
602
841
  if (childTagName === "ColumnInfo") columnInfoElement = datasetChild;
603
842
  else if (childTagName === "Rows") rowsElement = datasetChild;
@@ -614,89 +853,93 @@ function parseValue(value, type = "STRING") {
614
853
  return _options.parseToTypes ? convertToColumnType(value, type) : value;
615
854
  }
616
855
  function parseColumnInfo(columnInfoElement, dataset) {
617
- if (!columnInfoElement) throw new InvalidXmlError("ColumnInfo element is missing in the dataset");
856
+ if (!columnInfoElement || typeof columnInfoElement === "string") throw new InvalidXmlError("ColumnInfo element is missing in the dataset");
618
857
  const children = columnInfoElement.children;
619
858
  if (!children) return;
620
859
  for (let i = 0; i < children.length; i++) {
621
860
  const colInfo = children[i];
622
- const tagName = colInfo.tagName;
623
- if (tagName === "ConstColumn") {
624
- const attrs = colInfo.attributes;
625
- const sizeStr = attrs?.size;
626
- dataset.addConstColumn({
627
- id: attrs?.id,
628
- size: sizeStr ? parseInt(sizeStr, 10) : 0,
629
- type: attrs?.type || "STRING",
630
- value: parseValue(attrs?.value, attrs?.type)
631
- });
632
- } else if (tagName === "Column") {
633
- const attrs = colInfo.attributes;
634
- const sizeStr = attrs?.size;
635
- dataset.addColumn({
636
- id: attrs?.id,
637
- size: sizeStr ? parseInt(sizeStr, 10) : 0,
638
- type: attrs?.type || "STRING"
639
- });
861
+ if (typeof colInfo !== "string") {
862
+ const tagName = colInfo.tagName;
863
+ if (tagName === "ConstColumn") {
864
+ const attrs = colInfo.attributes;
865
+ const sizeStr = attrs?.size;
866
+ dataset.addConstColumn({
867
+ id: attrs?.id,
868
+ size: sizeStr ? parseInt(sizeStr, 10) : 0,
869
+ type: attrs?.type || "STRING",
870
+ value: parseValue(attrs?.value, attrs?.type)
871
+ });
872
+ } else if (tagName === "Column") {
873
+ const attrs = colInfo.attributes;
874
+ const sizeStr = attrs?.size;
875
+ dataset.addColumn({
876
+ id: attrs?.id,
877
+ size: sizeStr ? parseInt(sizeStr, 10) : 0,
878
+ type: attrs?.type || "STRING"
879
+ });
880
+ }
640
881
  }
641
882
  }
642
883
  }
643
884
  function parseRows(rowsElement, dataset) {
885
+ if (typeof rowsElement === "string") return;
644
886
  const rows = rowsElement?.children;
645
887
  if (!rows) return;
646
888
  const datasetRows = dataset.rows;
647
889
  for (let i = 0; i < rows.length; i++) {
648
890
  const r = rows[i];
649
- const tagName = r.tagName;
650
- if (tagName === "Row") {
651
- const rowIndex = dataset.newRow();
652
- const currentRow = datasetRows[rowIndex];
653
- currentRow.type = r.attributes?.type || void 0;
654
- const cols = r.children;
655
- if (!cols) continue;
656
- for (let j = 0; j < cols.length; j++) {
657
- const col = cols[j];
658
- const colTagName = col.tagName;
659
- if (colTagName === "Col") {
660
- const colId = col.attributes?.id;
661
- const value = col.children?.[0];
662
- const columnInfo = dataset.getColumnInfo(colId);
663
- if (!columnInfo) throw new InvalidXmlError(`Column with id ${colId} not found in dataset ${dataset.id}`);
664
- const castedValue = parseValue(value, columnInfo.type);
665
- currentRow.cols.push({
666
- id: colId,
667
- value: castedValue
668
- });
669
- } else if (colTagName === "OrgRow") {
670
- const orgRow = [];
671
- currentRow.orgRow = orgRow;
672
- const orgCols = col.children;
673
- if (!orgCols) continue;
674
- for (let k = 0; k < orgCols.length; k++) {
675
- const orgCol = orgCols[k];
676
- if (orgCol.tagName === "Col") {
677
- const colId = orgCol.attributes?.id;
678
- const value = orgCol.children?.[0];
891
+ if (typeof r !== "string") {
892
+ const tagName = r.tagName;
893
+ if (tagName === "Row") {
894
+ const currentRow = datasetRows[dataset.newRow()];
895
+ currentRow.type = r.attributes?.type || void 0;
896
+ const cols = r.children;
897
+ if (cols) for (let j = 0; j < cols.length; j++) {
898
+ const col = cols[j];
899
+ if (typeof col !== "string") {
900
+ const colTagName = col.tagName;
901
+ if (colTagName === "Col") {
902
+ const colId = col.attributes?.id;
903
+ const value = col.children?.[0];
679
904
  const columnInfo = dataset.getColumnInfo(colId);
905
+ if (!columnInfo) throw new InvalidXmlError(`Column with id ${colId} not found in dataset ${dataset.id}`);
680
906
  const castedValue = parseValue(value, columnInfo.type);
681
- orgRow.push({
907
+ currentRow.cols.push({
682
908
  id: colId,
683
909
  value: castedValue
684
910
  });
911
+ } else if (colTagName === "OrgRow") {
912
+ const orgRow = [];
913
+ currentRow.orgRow = orgRow;
914
+ const orgCols = col.children;
915
+ if (orgCols) for (let k = 0; k < orgCols.length; k++) {
916
+ const orgCol = orgCols[k];
917
+ if (typeof orgCol !== "string" && orgCol.tagName === "Col") {
918
+ const colId = orgCol.attributes?.id;
919
+ const value = orgCol.children?.[0];
920
+ const castedValue = parseValue(value, dataset.getColumnInfo(colId).type);
921
+ orgRow.push({
922
+ id: colId,
923
+ value: castedValue
924
+ });
925
+ }
926
+ }
685
927
  }
686
928
  }
687
929
  }
688
- }
689
- } else if (tagName === "Col") throw new InvalidXmlError("Row must be defined before Col");
690
- else if (tagName === "OrgRow") throw new InvalidXmlError("Row must be defined before OrgRow");
930
+ } else if (tagName === "Col") throw new InvalidXmlError("Row must be defined before Col");
931
+ else if (tagName === "OrgRow") throw new InvalidXmlError("Row must be defined before OrgRow");
932
+ }
691
933
  }
692
934
  }
693
935
  function parseParameters(parametersElement, xapiRoot) {
936
+ if (typeof parametersElement === "string") return;
694
937
  if (!parametersElement) return;
695
938
  const children = parametersElement.children;
696
939
  if (!children) return;
697
940
  for (let i = 0; i < children.length; i++) {
698
941
  const p = children[i];
699
- if (p.tagName === "Parameter") {
942
+ if (typeof p !== "string" && p.tagName === "Parameter") {
700
943
  const attrs = p.attributes;
701
944
  const id = attrs?.id;
702
945
  const type = attrs?.type || "STRING";
@@ -710,89 +953,73 @@ function parseParameters(parametersElement, xapiRoot) {
710
953
  }
711
954
  }
712
955
  function write(root) {
713
- const writer = new stax_xml.StaxXmlWriterSync({
714
- addEntities: makeWriterEntities(),
715
- encoding: "UTF-8",
716
- indentString: " ",
717
- prettyPrint: true
718
- });
719
- writer.writeStartDocument("1.0", "UTF-8");
720
- writer.writeStartElement("Root", { attributes: {
956
+ const builder = new XmlStringBuilder();
957
+ builder.writeDeclaration("1.0", "UTF-8");
958
+ builder.writeStartElement("Root", {
721
959
  xmlns: _options.xapiVersion?.xmlns || NexaVersion.xmlns,
722
960
  version: _options.xapiVersion?.version || NexaVersion.version
723
- } });
724
- if (root.parameterSize() > 0) writeParameters(writer, root.iterParameters());
961
+ });
962
+ if (root.parameterSize() > 0) writeParameters(builder, root.getParameters());
725
963
  if (root.datasetSize() > 0) {
726
- writer.writeStartElement("Datasets");
727
- for (const dataset of root.iterDatasets()) writeDataset(writer, dataset);
728
- writer.writeEndElement();
964
+ builder.writeStartElement("Datasets");
965
+ for (const dataset of root.getDatasets()) writeDataset(builder, dataset);
966
+ builder.writeEndElement("Datasets");
729
967
  }
730
- writer.writeEndElement();
731
- return writer.getXmlString();
968
+ builder.writeEndElement("Root");
969
+ return builder.toString();
732
970
  }
733
- function writeParameters(writer, iterator) {
734
- if (iterator) {
735
- writer.writeStartElement("Parameters");
736
- for (const parameter of iterator) {
737
- writer.writeStartElement("Parameter", { attributes: { id: parameter.id } });
738
- if (parameter.type !== void 0) writer.writeAttribute("type", parameter.type);
739
- if (parameter.value !== void 0) if (typeof parameter.value === "string") writer.writeAttribute("value", parameter.value);
740
- else if (parameter.value instanceof Date) writer.writeAttribute("value", dateToString(parameter.value, parameter.type));
741
- else if (parameter.value instanceof Uint8Array) writer.writeAttribute("value", uint8ArrayToBase64(parameter.value));
742
- else writer.writeAttribute("value", String(parameter.value));
743
- writer.writeEndElement();
971
+ function writeParameters(builder, parameters) {
972
+ if (parameters) {
973
+ builder.writeStartElement("Parameters");
974
+ for (const parameter of parameters) {
975
+ const attrs = { id: parameter.id };
976
+ if (parameter.type !== void 0) attrs.type = parameter.type;
977
+ if (parameter.value !== void 0) if (typeof parameter.value === "string") attrs.value = parameter.value;
978
+ else if (parameter.value instanceof Date) attrs.value = dateToString(parameter.value, parameter.type);
979
+ else if (parameter.value instanceof Uint8Array) attrs.value = uint8ArrayToBase64(parameter.value);
980
+ else attrs.value = String(parameter.value);
981
+ builder.writeStartElement("Parameter", attrs, true);
744
982
  }
745
- writer.writeEndElement();
983
+ builder.writeEndElement("Parameters");
746
984
  }
747
985
  }
748
- function writeDataset(writer, dataset) {
749
- writer.writeStartElement("Dataset", { attributes: { id: dataset.id } });
986
+ function writeDataset(builder, dataset) {
987
+ builder.writeStartElement("Dataset", { id: dataset.id });
750
988
  if (dataset.constColumnSize() > 0 || dataset.columnSize() > 0) {
751
- writer.writeStartElement("ColumnInfo");
752
- for (const constCol of dataset.iterConstColumns()) writer.writeStartElement("ConstColumn", {
753
- attributes: {
754
- id: constCol.id,
755
- size: String(constCol.size),
756
- type: constCol.type,
757
- value: constCol.value !== void 0 ? String(constCol.value) : ""
758
- },
759
- selfClosing: true
760
- });
761
- for (const col of dataset.iterColumns()) writer.writeStartElement("Column", {
762
- attributes: {
763
- id: col.id,
764
- size: String(col.size),
765
- type: col.type
766
- },
767
- selfClosing: true
768
- });
769
- writer.writeEndElement();
989
+ builder.writeStartElement("ColumnInfo");
990
+ for (const constCol of dataset.getConstColumns()) builder.writeStartElement("ConstColumn", {
991
+ id: constCol.id,
992
+ size: String(constCol.size),
993
+ type: constCol.type,
994
+ value: constCol.value !== void 0 ? String(constCol.value) : ""
995
+ }, true);
996
+ for (const col of dataset.getColumns()) builder.writeStartElement("Column", {
997
+ id: col.id,
998
+ size: String(col.size),
999
+ type: col.type
1000
+ }, true);
1001
+ builder.writeEndElement("ColumnInfo");
770
1002
  }
771
- writer.writeStartElement("Rows");
772
- for (const row of dataset.iterRows()) {
773
- if (row.type) writer.writeStartElement("Row", { attributes: { type: row.type } });
774
- else writer.writeStartElement("Row");
775
- for (const col of row.cols) writeColumn(writer, dataset, col);
1003
+ builder.writeStartElement("Rows");
1004
+ for (const row of dataset.getRows()) {
1005
+ if (row.type) builder.writeStartElement("Row", { type: row.type });
1006
+ else builder.writeStartElement("Row");
1007
+ for (const col of row.cols) writeColumn(builder, dataset, col);
776
1008
  if (row.orgRow && row.orgRow.length > 0) {
777
- writer.writeStartElement("OrgRow");
778
- for (const orgCol of row.orgRow) writeColumn(writer, dataset, orgCol);
779
- writer.writeEndElement();
1009
+ builder.writeStartElement("OrgRow");
1010
+ for (const orgCol of row.orgRow) writeColumn(builder, dataset, orgCol);
1011
+ builder.writeEndElement("OrgRow");
780
1012
  }
781
- writer.writeEndElement();
1013
+ builder.writeEndElement("Row");
782
1014
  }
783
- writer.writeEndElement();
784
- writer.writeEndElement();
1015
+ builder.writeEndElement("Rows");
1016
+ builder.writeEndElement("Dataset");
785
1017
  }
786
- function writeColumn(writer, dataset, col) {
1018
+ function writeColumn(builder, dataset, col) {
787
1019
  if (col.value !== void 0 && col.value !== null) {
788
1020
  const colInfo = dataset.getColumnInfo(col.id);
789
- writer.writeStartElement("Col", { attributes: { id: col.id } });
790
- writer.writeCharacters(convertToString(col.value, colInfo.type));
791
- writer.writeEndElement();
792
- } else writer.writeStartElement("Col", {
793
- attributes: { id: col.id },
794
- selfClosing: true
795
- });
1021
+ builder.writeElementWithText("Col", { id: col.id }, convertToString(col.value, colInfo.type));
1022
+ } else builder.writeStartElement("Col", { id: col.id }, true);
796
1023
  }
797
1024
 
798
1025
  //#endregion
@@ -802,6 +1029,7 @@ exports.InvalidXmlError = InvalidXmlError;
802
1029
  exports.NexaVersion = NexaVersion;
803
1030
  exports.StringWritableStream = StringWritableStream;
804
1031
  exports.XapiRoot = XapiRoot;
1032
+ exports.XmlStringBuilder = XmlStringBuilder;
805
1033
  exports.XplatformVersion = XplatformVersion;
806
1034
  exports._unescapeXml = _unescapeXml;
807
1035
  exports.arrayBufferToString = arrayBufferToString;
@@ -810,11 +1038,13 @@ exports.columnType = columnType;
810
1038
  exports.convertToColumnType = convertToColumnType;
811
1039
  exports.convertToString = convertToString;
812
1040
  exports.dateToString = dateToString;
1041
+ exports.encodeControlChars = encodeControlChars;
1042
+ exports.escapeXml = escapeXml;
813
1043
  exports.initXapi = initXapi;
814
1044
  exports.isXapiRoot = isXapiRoot;
815
1045
  exports.makeParseEntities = makeParseEntities;
816
- exports.makeWriterEntities = makeWriterEntities;
817
1046
  exports.parse = parse;
1047
+ exports.parseXml = parseXml;
818
1048
  exports.rowType = rowType;
819
1049
  exports.stringToDate = stringToDate;
820
1050
  exports.stringToReadableStream = stringToReadableStream;