xmljslib 1.0.3 → 1.0.4
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/cli.js +354 -22
- package/dist/index.cjs +444 -45
- package/dist/index.d.cts +133 -2
- package/dist/index.d.ts +133 -2
- package/dist/index.js +442 -44
- package/package.json +63 -62
- package/readme.md +573 -466
- package/readme.ru.md +110 -3
package/dist/cli.js
CHANGED
|
@@ -27,14 +27,25 @@ function createXMLNode(value, attributes = {}, isLeaf = false) {
|
|
|
27
27
|
if (!hasAttributes) {
|
|
28
28
|
return parsedValue;
|
|
29
29
|
}
|
|
30
|
-
const
|
|
30
|
+
const attributeValue = Object.prototype.hasOwnProperty.call(attributes, "value") ? parsePrimitive(attributes.value) : void 0;
|
|
31
|
+
const nodeValue = parsedValue !== void 0 ? parsedValue : attributeValue;
|
|
32
|
+
const targetObj = Object(nodeValue !== void 0 ? nodeValue : "");
|
|
31
33
|
for (const [key, attrVal] of Object.entries(attributes)) {
|
|
34
|
+
if (key === "value") {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
32
37
|
targetObj[`$${key}`] = parsePrimitive(attrVal);
|
|
33
38
|
}
|
|
39
|
+
Object.defineProperty(targetObj, "$value", {
|
|
40
|
+
value: nodeValue,
|
|
41
|
+
writable: true,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true
|
|
44
|
+
});
|
|
34
45
|
return new Proxy(targetObj, {
|
|
35
46
|
get(target, prop) {
|
|
36
47
|
if (prop === "__isXMLNode") return true;
|
|
37
|
-
if (prop === "$value") return
|
|
48
|
+
if (prop === "$value") return nodeValue;
|
|
38
49
|
if (prop === "valueOf" || prop === Symbol.toPrimitive) {
|
|
39
50
|
return () => parsedValue;
|
|
40
51
|
}
|
|
@@ -121,10 +132,14 @@ var XMLParser = class _XMLParser {
|
|
|
121
132
|
static parseTreeJSON(item) {
|
|
122
133
|
const result = {};
|
|
123
134
|
const node = {};
|
|
135
|
+
const attributeValue = Object.prototype.hasOwnProperty.call(item.attributes, "value") ? item.attributes.value : void 0;
|
|
136
|
+
node.$value = item.$value !== null ? item.$value : attributeValue ?? null;
|
|
124
137
|
for (const attr of Object.keys(item.attributes)) {
|
|
138
|
+
if (attr === "value") {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
125
141
|
node[`$${attr}`] = item.attributes[attr];
|
|
126
142
|
}
|
|
127
|
-
node.$value = item.$value;
|
|
128
143
|
for (const child of item.children) {
|
|
129
144
|
const parsed = _XMLParser.parseTreeJSON(child);
|
|
130
145
|
const childValue = parsed[child.tag];
|
|
@@ -259,7 +274,7 @@ var XMLParser = class _XMLParser {
|
|
|
259
274
|
};
|
|
260
275
|
|
|
261
276
|
// src/lib/wsdlTypeGenerator.ts
|
|
262
|
-
var WSDLTypeGenerator = class {
|
|
277
|
+
var WSDLTypeGenerator = class _WSDLTypeGenerator {
|
|
263
278
|
typeRegistry = /* @__PURE__ */ new Map();
|
|
264
279
|
simpleTypeRegistry = /* @__PURE__ */ new Map();
|
|
265
280
|
simpleTypeDocumentationRegistry = /* @__PURE__ */ new Map();
|
|
@@ -274,6 +289,40 @@ var WSDLTypeGenerator = class {
|
|
|
274
289
|
globalElementRegistry = /* @__PURE__ */ new Map();
|
|
275
290
|
baseUrl = "";
|
|
276
291
|
loadedUrls = /* @__PURE__ */ new Set();
|
|
292
|
+
/**
|
|
293
|
+
* Пространства имён, объявленные в документе (`xmlns:*`).
|
|
294
|
+
* Используются и для автоподстановки префиксов, и для объявления `xmlns` в XML.
|
|
295
|
+
*/
|
|
296
|
+
namespaceUriByPrefix = /* @__PURE__ */ new Map();
|
|
297
|
+
namespacePrefixByUri = /* @__PURE__ */ new Map();
|
|
298
|
+
/**
|
|
299
|
+
* Пространства имён элементов: локальное имя элемента -> префикс.
|
|
300
|
+
*
|
|
301
|
+
* Квалифицированные элементы хранятся в `qualifiedElements`,
|
|
302
|
+
* неквалифицированные — в `unqualifiedElements`, чтобы одноимённые элементы
|
|
303
|
+
* из разных схем не конфликтовали: квалифицированный всегда выигрывает.
|
|
304
|
+
*/
|
|
305
|
+
qualifiedElements = /* @__PURE__ */ new Map();
|
|
306
|
+
unqualifiedElements = /* @__PURE__ */ new Set();
|
|
307
|
+
/**
|
|
308
|
+
* Имена элементов, объявленных в нескольких пространствах имён.
|
|
309
|
+
* Для таких элементов одного префикса «по имени» недостаточно —
|
|
310
|
+
* нужен контекст родителя.
|
|
311
|
+
*/
|
|
312
|
+
ambiguousElements = /* @__PURE__ */ new Set();
|
|
313
|
+
/**
|
|
314
|
+
* Тип каждого элемента: имя элемента -> имя типа в реестре complexType.
|
|
315
|
+
* Нужен, чтобы знать состав дочерних элементов.
|
|
316
|
+
*/
|
|
317
|
+
elementTypeRegistry = /* @__PURE__ */ new Map();
|
|
318
|
+
/**
|
|
319
|
+
* Пространства имён типов: имя complexType -> префикс.
|
|
320
|
+
*/
|
|
321
|
+
typeNamespacePrefix = /* @__PURE__ */ new Map();
|
|
322
|
+
/**
|
|
323
|
+
* Пространство имён XML Schema. Не имеет отношения к данным сервиса.
|
|
324
|
+
*/
|
|
325
|
+
static XML_SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
|
|
277
326
|
get complexTypeCount() {
|
|
278
327
|
return this.typeRegistry.size;
|
|
279
328
|
}
|
|
@@ -296,8 +345,17 @@ var WSDLTypeGenerator = class {
|
|
|
296
345
|
this.messageRegistry.clear();
|
|
297
346
|
this.globalElementRegistry.clear();
|
|
298
347
|
this.loadedUrls.clear();
|
|
348
|
+
this.namespaceUriByPrefix.clear();
|
|
349
|
+
this.namespacePrefixByUri.clear();
|
|
350
|
+
this.qualifiedElements.clear();
|
|
351
|
+
this.unqualifiedElements.clear();
|
|
352
|
+
this.ambiguousElements.clear();
|
|
353
|
+
this.elementTypeRegistry.clear();
|
|
354
|
+
this.typeNamespacePrefix.clear();
|
|
299
355
|
console.log("\u{1F4E6} \u041F\u0430\u0440\u0441\u0438\u043D\u0433 \u0441\u0442\u0440\u0443\u043A\u0442\u0443\u0440\u044B XML...");
|
|
300
356
|
const wsdl = new XMLParser(xmlContent, config);
|
|
357
|
+
console.log("\u{1F50D} \u0410\u043D\u0430\u043B\u0438\u0437 \u043E\u0431\u044A\u044F\u0432\u043B\u0435\u043D\u0438\u0439 \u043F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u0441\u0442\u0432 \u0438\u043C\u0451\u043D...");
|
|
358
|
+
this.collectNamespaceDeclarations(wsdl);
|
|
301
359
|
console.log("\u{1F50D} \u0410\u043D\u0430\u043B\u0438\u0437 WSDL \u0441\u043E\u043E\u0431\u0449\u0435\u043D\u0438\u0439...");
|
|
302
360
|
this.extractMessages(wsdl);
|
|
303
361
|
console.log("\u{1F50D} \u0410\u043D\u0430\u043B\u0438\u0437 WSDL \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u0439...");
|
|
@@ -340,6 +398,106 @@ var WSDLTypeGenerator = class {
|
|
|
340
398
|
const primitive = typeof value.valueOf === "function" ? value.valueOf() : value;
|
|
341
399
|
return String(primitive);
|
|
342
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Приводит значение узла к строке.
|
|
403
|
+
*/
|
|
404
|
+
toPrimitiveString(value) {
|
|
405
|
+
if (value === null || value === void 0) {
|
|
406
|
+
return void 0;
|
|
407
|
+
}
|
|
408
|
+
const primitive = typeof value.valueOf === "function" ? value.valueOf() : value;
|
|
409
|
+
if (primitive === null || primitive === void 0) {
|
|
410
|
+
return void 0;
|
|
411
|
+
}
|
|
412
|
+
const text = String(primitive).trim();
|
|
413
|
+
return text.length > 0 ? text : void 0;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Регистрирует связь префикс <-> URI пространства имён.
|
|
417
|
+
* Первые объявления имеют приоритет: префикс не переопределяется.
|
|
418
|
+
*/
|
|
419
|
+
registerNamespacePrefix(prefix, uri) {
|
|
420
|
+
if (!this.namespaceUriByPrefix.has(prefix)) {
|
|
421
|
+
this.namespaceUriByPrefix.set(prefix, uri);
|
|
422
|
+
}
|
|
423
|
+
if (!this.namespacePrefixByUri.has(uri)) {
|
|
424
|
+
this.namespacePrefixByUri.set(uri, prefix);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Собирает объявления пространств имён (`xmlns:*`) из всего документа.
|
|
429
|
+
*/
|
|
430
|
+
collectNamespaceDeclarations(node) {
|
|
431
|
+
if (!node || typeof node !== "object") {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (Array.isArray(node)) {
|
|
435
|
+
for (const item of node) {
|
|
436
|
+
this.collectNamespaceDeclarations(item);
|
|
437
|
+
}
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
for (const [key, value] of Object.entries(node)) {
|
|
441
|
+
if (key.startsWith("$xmlns:")) {
|
|
442
|
+
const prefix = key.slice("$xmlns:".length);
|
|
443
|
+
const uri = this.toPrimitiveString(value);
|
|
444
|
+
if (prefix && uri && uri !== _WSDLTypeGenerator.XML_SCHEMA_NAMESPACE) {
|
|
445
|
+
this.registerNamespacePrefix(prefix, uri);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
this.collectNamespaceDeclarations(value);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Определяет префикс для URI пространства имён.
|
|
453
|
+
*
|
|
454
|
+
* Если префикс не объявлен в документе, подбирается свободный
|
|
455
|
+
* (`tns` или `ns1`, `ns2`, ...), чтобы префикс всегда можно было объявить.
|
|
456
|
+
*/
|
|
457
|
+
resolveNamespacePrefix(uri) {
|
|
458
|
+
if (!uri) {
|
|
459
|
+
return void 0;
|
|
460
|
+
}
|
|
461
|
+
const declared = this.namespacePrefixByUri.get(uri);
|
|
462
|
+
if (declared) {
|
|
463
|
+
return declared;
|
|
464
|
+
}
|
|
465
|
+
if (!this.namespaceUriByPrefix.has("tns")) {
|
|
466
|
+
this.registerNamespacePrefix("tns", uri);
|
|
467
|
+
return "tns";
|
|
468
|
+
}
|
|
469
|
+
let index = 1;
|
|
470
|
+
while (this.namespaceUriByPrefix.has(`ns${index}`)) {
|
|
471
|
+
index++;
|
|
472
|
+
}
|
|
473
|
+
const prefix = `ns${index}`;
|
|
474
|
+
this.registerNamespacePrefix(prefix, uri);
|
|
475
|
+
return prefix;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Регистрирует пространство имён элемента.
|
|
479
|
+
*
|
|
480
|
+
* Квалифицированный элемент всегда выигрывает у неквалифицированного
|
|
481
|
+
* с тем же локальным именем.
|
|
482
|
+
*/
|
|
483
|
+
registerElementNamespace(name, prefix) {
|
|
484
|
+
if (!name) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (prefix) {
|
|
488
|
+
const known = this.qualifiedElements.get(name);
|
|
489
|
+
if (known === void 0) {
|
|
490
|
+
this.qualifiedElements.set(name, prefix);
|
|
491
|
+
} else if (known !== prefix) {
|
|
492
|
+
this.ambiguousElements.add(name);
|
|
493
|
+
}
|
|
494
|
+
this.unqualifiedElements.delete(name);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (!this.qualifiedElements.has(name)) {
|
|
498
|
+
this.unqualifiedElements.add(name);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
343
501
|
/**
|
|
344
502
|
* Извлекает текст документации из XML-узла.
|
|
345
503
|
*
|
|
@@ -577,6 +735,8 @@ ${indent} */
|
|
|
577
735
|
if (!schema) {
|
|
578
736
|
return;
|
|
579
737
|
}
|
|
738
|
+
this.collectNamespaceDeclarations(schema);
|
|
739
|
+
const context = this.createSchemaContext(schema);
|
|
580
740
|
for (const key of Object.keys(schema)) {
|
|
581
741
|
const cleanKey = this.cleanPrefix(key);
|
|
582
742
|
if (["import", "include"].includes(cleanKey)) {
|
|
@@ -598,7 +758,7 @@ ${indent} */
|
|
|
598
758
|
if (!ct?.$name) {
|
|
599
759
|
continue;
|
|
600
760
|
}
|
|
601
|
-
this.parseComplexType(ct.$name, ct);
|
|
761
|
+
this.parseComplexType(ct.$name, ct, context);
|
|
602
762
|
}
|
|
603
763
|
}
|
|
604
764
|
for (const key of Object.keys(schema)) {
|
|
@@ -622,10 +782,51 @@ ${indent} */
|
|
|
622
782
|
if (!element?.$name) {
|
|
623
783
|
continue;
|
|
624
784
|
}
|
|
625
|
-
this.parseGlobalElement(element);
|
|
785
|
+
this.parseGlobalElement(element, context);
|
|
626
786
|
}
|
|
627
787
|
}
|
|
628
788
|
}
|
|
789
|
+
/**
|
|
790
|
+
* Формирует контекст схемы: целевое пространство имён и правила квалификации.
|
|
791
|
+
*/
|
|
792
|
+
createSchemaContext(schema) {
|
|
793
|
+
const targetNamespace = this.getAttribute(schema, "targetNamespace");
|
|
794
|
+
const formDefault = (this.getAttribute(schema, "elementFormDefault") ?? "unqualified").toLowerCase();
|
|
795
|
+
return {
|
|
796
|
+
targetNamespace,
|
|
797
|
+
prefix: this.resolveNamespacePrefix(targetNamespace),
|
|
798
|
+
elementFormDefault: formDefault === "qualified" ? "qualified" : "unqualified"
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Определяет, принадлежит ли элемент целевому пространству имён.
|
|
803
|
+
*
|
|
804
|
+
* Локальные элементы подчиняются `elementFormDefault`,
|
|
805
|
+
* глобальные — всегда квалифицированы.
|
|
806
|
+
*
|
|
807
|
+
* Для ссылок (`ref="pfx:Name"`) берётся пространство имён,
|
|
808
|
+
* объявленное в самом документе, а не пространство текущей схемы.
|
|
809
|
+
*/
|
|
810
|
+
resolveElementPrefix(element, context, isGlobal) {
|
|
811
|
+
const ref = this.getAttribute(element, "ref");
|
|
812
|
+
if (ref) {
|
|
813
|
+
const refPrefix = ref.includes(":") ? ref.split(":")[0] : "";
|
|
814
|
+
const uri = this.namespaceUriByPrefix.get(refPrefix);
|
|
815
|
+
const resolved = uri ? this.namespacePrefixByUri.get(uri) : void 0;
|
|
816
|
+
return resolved ?? context.prefix;
|
|
817
|
+
}
|
|
818
|
+
if (!context.prefix || !context.targetNamespace) {
|
|
819
|
+
return void 0;
|
|
820
|
+
}
|
|
821
|
+
if (isGlobal) {
|
|
822
|
+
return context.prefix;
|
|
823
|
+
}
|
|
824
|
+
const form = this.getAttribute(element, "form");
|
|
825
|
+
if (form) {
|
|
826
|
+
return form.toLowerCase() === "qualified" ? context.prefix : void 0;
|
|
827
|
+
}
|
|
828
|
+
return context.elementFormDefault === "qualified" ? context.prefix : void 0;
|
|
829
|
+
}
|
|
629
830
|
/**
|
|
630
831
|
* Регистрирует глобальный XSD element.
|
|
631
832
|
*
|
|
@@ -637,11 +838,12 @@ ${indent} */
|
|
|
637
838
|
*
|
|
638
839
|
* FetchBooking -> FetchBookingRequest
|
|
639
840
|
*/
|
|
640
|
-
parseGlobalElement(element) {
|
|
841
|
+
parseGlobalElement(element, context) {
|
|
641
842
|
const name = element.$name;
|
|
642
843
|
if (!name) {
|
|
643
844
|
return;
|
|
644
845
|
}
|
|
846
|
+
this.registerElementNamespace(name, this.resolveElementPrefix(element, context, true));
|
|
645
847
|
let typeName;
|
|
646
848
|
if (element.$type) {
|
|
647
849
|
typeName = this.mapXSDTypeToTS(element.$type);
|
|
@@ -650,7 +852,7 @@ ${indent} */
|
|
|
650
852
|
const inlineSimpleType = this.getChildNode(element, "simpleType");
|
|
651
853
|
if (inlineComplexType) {
|
|
652
854
|
typeName = `${name}Type`;
|
|
653
|
-
this.parseComplexType(typeName, inlineComplexType);
|
|
855
|
+
this.parseComplexType(typeName, inlineComplexType, context);
|
|
654
856
|
} else if (inlineSimpleType) {
|
|
655
857
|
typeName = `${name}Type`;
|
|
656
858
|
this.parseSimpleType(typeName, inlineSimpleType);
|
|
@@ -659,6 +861,7 @@ ${indent} */
|
|
|
659
861
|
}
|
|
660
862
|
}
|
|
661
863
|
this.globalElementRegistry.set(name, typeName);
|
|
864
|
+
this.elementTypeRegistry.set(name, typeName);
|
|
662
865
|
}
|
|
663
866
|
/**
|
|
664
867
|
* Загружает внешнюю XSD-схему.
|
|
@@ -727,13 +930,16 @@ ${indent} */
|
|
|
727
930
|
/**
|
|
728
931
|
* Разбирает XSD complexType.
|
|
729
932
|
*/
|
|
730
|
-
parseComplexType(name, ctNode) {
|
|
933
|
+
parseComplexType(name, ctNode, context) {
|
|
731
934
|
const complexType = {
|
|
732
935
|
name,
|
|
733
936
|
elements: [],
|
|
734
937
|
attributes: [],
|
|
735
938
|
documentation: this.extractDocumentation(ctNode)
|
|
736
939
|
};
|
|
940
|
+
if (context.prefix) {
|
|
941
|
+
this.typeNamespacePrefix.set(name, context.prefix);
|
|
942
|
+
}
|
|
737
943
|
let baseType;
|
|
738
944
|
const findElementsAndAttributes = (node) => {
|
|
739
945
|
if (!node || typeof node !== "object") {
|
|
@@ -761,23 +967,28 @@ ${indent} */
|
|
|
761
967
|
if (!fieldName) {
|
|
762
968
|
continue;
|
|
763
969
|
}
|
|
970
|
+
const elementName = this.cleanPrefix(fieldName);
|
|
971
|
+
const elementNamespace = this.resolveElementPrefix(element, context, false);
|
|
972
|
+
this.registerElementNamespace(elementName, elementNamespace);
|
|
764
973
|
const inlineComplexType = this.getChildNode(element, "complexType");
|
|
765
974
|
const inlineSimpleType = this.getChildNode(element, "simpleType");
|
|
766
975
|
let elementType = element.$type || element.$ref;
|
|
767
976
|
if (inlineComplexType) {
|
|
768
977
|
elementType = `${this.cleanPrefix(fieldName)}Type`;
|
|
769
|
-
this.parseComplexType(elementType, inlineComplexType);
|
|
978
|
+
this.parseComplexType(elementType, inlineComplexType, context);
|
|
770
979
|
} else if (inlineSimpleType) {
|
|
771
980
|
elementType = `${this.cleanPrefix(fieldName)}Type`;
|
|
772
981
|
this.parseSimpleType(elementType, inlineSimpleType);
|
|
773
982
|
}
|
|
983
|
+
this.elementTypeRegistry.set(elementName, this.cleanPrefix(elementType || "string"));
|
|
774
984
|
complexType.elements.push({
|
|
775
|
-
name:
|
|
985
|
+
name: elementName,
|
|
776
986
|
type: this.mapXSDTypeToTS(elementType || "string"),
|
|
777
987
|
isArray: element.$maxOccurs === "unbounded" || Number(element.$maxOccurs) > 1,
|
|
778
988
|
isOptional: element.$minOccurs === "0" || !element.$minOccurs,
|
|
779
989
|
attributes: this.extractAttributes(element),
|
|
780
|
-
documentation: this.extractDocumentation(element)
|
|
990
|
+
documentation: this.extractDocumentation(element),
|
|
991
|
+
namespace: elementNamespace
|
|
781
992
|
});
|
|
782
993
|
}
|
|
783
994
|
}
|
|
@@ -951,18 +1162,22 @@ ${indent} */
|
|
|
951
1162
|
for (const [name, complexType] of this.typeRegistry.entries()) {
|
|
952
1163
|
code += this.formatDocumentation(complexType.documentation);
|
|
953
1164
|
const baseType = complexType.base;
|
|
1165
|
+
const namespaceField = this.compileNamespaceField(complexType);
|
|
954
1166
|
if (baseType && builtInPrimitives.includes(baseType)) {
|
|
955
|
-
const
|
|
1167
|
+
const attrs = [...complexType.attributes.map((attribute) => `${attribute.name}?: ${attribute.type}`)];
|
|
1168
|
+
const namespaceAttribute = namespaceField.trim();
|
|
1169
|
+
if (namespaceAttribute) {
|
|
1170
|
+
attrs.push(namespaceAttribute);
|
|
1171
|
+
}
|
|
1172
|
+
const attrsObj = attrs.length > 0 ? `{ ${attrs.join("; ")} }` : "{}";
|
|
956
1173
|
code += `export type ${name} = XMLLeaf<${baseType}, ${attrsObj}>;
|
|
957
1174
|
|
|
958
1175
|
`;
|
|
959
1176
|
continue;
|
|
960
1177
|
}
|
|
961
|
-
|
|
962
|
-
code += `export interface ${name}${extendsStr} {
|
|
963
|
-
`;
|
|
1178
|
+
let fields = namespaceField;
|
|
964
1179
|
for (const element of complexType.elements) {
|
|
965
|
-
|
|
1180
|
+
fields += this.formatDocumentation(element.documentation, " ");
|
|
966
1181
|
const optional = element.isOptional ? "?" : "";
|
|
967
1182
|
let type = element.type;
|
|
968
1183
|
const hasAttributes = element.attributes.length > 0;
|
|
@@ -972,17 +1187,134 @@ ${indent} */
|
|
|
972
1187
|
type = isPrimitiveLike ? `XMLLeaf<${type}, ${attributesType}>` : `XMLBranch<${type}, ${attributesType}>`;
|
|
973
1188
|
}
|
|
974
1189
|
const finalType = element.isArray ? `canBeArray<${type}>` : type;
|
|
975
|
-
|
|
1190
|
+
fields += ` ${element.name}${optional}: ${finalType};
|
|
976
1191
|
`;
|
|
977
1192
|
}
|
|
978
1193
|
for (const attribute of complexType.attributes) {
|
|
979
|
-
|
|
980
|
-
|
|
1194
|
+
fields += this.formatDocumentation(attribute.documentation, " ");
|
|
1195
|
+
fields += ` ${attribute.name}?: ${attribute.type};
|
|
981
1196
|
`;
|
|
982
1197
|
}
|
|
983
|
-
|
|
984
|
-
|
|
1198
|
+
const body = baseType ? `${baseType} & {
|
|
1199
|
+
${fields}}` : `{
|
|
1200
|
+
${fields}}`;
|
|
1201
|
+
code += `export type ${name} = XMLBranch<${body}>;
|
|
1202
|
+
|
|
1203
|
+
`;
|
|
1204
|
+
}
|
|
1205
|
+
code += this.compileNamespaceRegistry();
|
|
1206
|
+
return code;
|
|
1207
|
+
}
|
|
1208
|
+
/**
|
|
1209
|
+
* Поле `$namespace` для типа: префикс пространства имён, в котором объявлен тип.
|
|
1210
|
+
*
|
|
1211
|
+
* Поле пропускается, если в типе уже есть XSD-атрибут с именем `namespace`:
|
|
1212
|
+
* иначе возник бы конфликт имён.
|
|
1213
|
+
*/
|
|
1214
|
+
compileNamespaceField(complexType) {
|
|
1215
|
+
const prefix = this.typeNamespacePrefix.get(complexType.name);
|
|
1216
|
+
if (!prefix) {
|
|
1217
|
+
return "";
|
|
1218
|
+
}
|
|
1219
|
+
if (complexType.attributes.some((attribute) => attribute.name === "$namespace")) {
|
|
1220
|
+
return "";
|
|
1221
|
+
}
|
|
1222
|
+
return ` $namespace?: '${prefix}';
|
|
1223
|
+
`;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Генерирует реестр пространств имён для XMLBuilder.
|
|
1227
|
+
*
|
|
1228
|
+
* Реестр сопоставляет локальные имена элементов с префиксами пространств имён,
|
|
1229
|
+
* поэтому XMLBuilder может подставлять префиксы и объявлять `xmlns` автоматически.
|
|
1230
|
+
*/
|
|
1231
|
+
compileNamespaceRegistry() {
|
|
1232
|
+
if (this.qualifiedElements.size === 0 && this.namespaceUriByPrefix.size === 0) {
|
|
1233
|
+
return "";
|
|
1234
|
+
}
|
|
1235
|
+
let code = "";
|
|
1236
|
+
code += "/**\n";
|
|
1237
|
+
code += " * \u041F\u0420\u041E\u0421\u0422\u0420\u0410\u041D\u0421\u0422\u0412\u0410 \u0418\u041C\u0401\u041D, \u0418\u0417\u0412\u041B\u0415\u0427\u0401\u041D\u041D\u042B\u0415 \u0418\u0417 WSDL/XSD\n";
|
|
1238
|
+
code += " *\n";
|
|
1239
|
+
code += " * \u0420\u0435\u0435\u0441\u0442\u0440 \u043C\u043E\u0436\u043D\u043E \u043F\u0435\u0440\u0435\u0434\u0430\u0442\u044C \u0432 XMLBuilder, \u0438 \u0442\u043E\u0433\u0434\u0430 \u043F\u0440\u0435\u0444\u0438\u043A\u0441\u044B \u043F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u0441\u0442\u0432 \u0438\u043C\u0451\u043D\n";
|
|
1240
|
+
code += " * \u0431\u0443\u0434\u0443\u0442 \u043F\u043E\u0434\u0441\u0442\u0430\u0432\u043B\u044F\u0442\u044C\u0441\u044F \u0430\u0432\u0442\u043E\u043C\u0430\u0442\u0438\u0447\u0435\u0441\u043A\u0438, \u0430 \u043E\u0431\u044A\u044F\u0432\u043B\u0435\u043D\u0438\u044F `xmlns:*` \u2014 \u0434\u043E\u0431\u0430\u0432\u043B\u044F\u0442\u044C\u0441\u044F\n";
|
|
1241
|
+
code += " * \u0432 \u043A\u043E\u0440\u043D\u0435\u0432\u043E\u0439 \u044D\u043B\u0435\u043C\u0435\u043D\u0442:\n";
|
|
1242
|
+
code += " *\n";
|
|
1243
|
+
code += " * new XMLBuilder(request, { namespaces: XMLNamespaceRegistry }).toXML();\n";
|
|
1244
|
+
code += " */\n\n";
|
|
1245
|
+
code += "export const XMLNamespacePrefixes = {\n";
|
|
1246
|
+
for (const [prefix, uri] of this.namespaceUriByPrefix.entries()) {
|
|
1247
|
+
code += ` ${JSON.stringify(prefix)}: ${JSON.stringify(uri)},
|
|
1248
|
+
`;
|
|
1249
|
+
}
|
|
1250
|
+
code += "} as const;\n\n";
|
|
1251
|
+
code += "export const XMLNamespaceElements = {\n";
|
|
1252
|
+
for (const [element, prefix] of this.qualifiedElements.entries()) {
|
|
1253
|
+
code += ` ${JSON.stringify(element)}: ${JSON.stringify(prefix)},
|
|
1254
|
+
`;
|
|
1255
|
+
}
|
|
1256
|
+
code += "} as const;\n\n";
|
|
1257
|
+
const children = this.compileNamespaceChildren();
|
|
1258
|
+
if (children) {
|
|
1259
|
+
code += "/**\n";
|
|
1260
|
+
code += " * \u041F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u0441\u0442\u0432\u0430 \u0438\u043C\u0451\u043D \u0434\u043E\u0447\u0435\u0440\u043D\u0438\u0445 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432: \u0440\u043E\u0434\u0438\u0442\u0435\u043B\u044C -> { \u044D\u043B\u0435\u043C\u0435\u043D\u0442 -> \u043F\u0440\u0435\u0444\u0438\u043A\u0441 }.\n";
|
|
1261
|
+
code += " *\n";
|
|
1262
|
+
code += " * \u0417\u0430\u043F\u043E\u043B\u043D\u044F\u0435\u0442\u0441\u044F \u0434\u043B\u044F \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u043E\u0432, \u043E\u0431\u044A\u044F\u0432\u043B\u0435\u043D\u043D\u044B\u0445 \u0441\u0440\u0430\u0437\u0443 \u0432 \u043D\u0435\u0441\u043A\u043E\u043B\u044C\u043A\u0438\u0445 \u043F\u0440\u043E\u0441\u0442\u0440\u0430\u043D\u0441\u0442\u0432\u0430\u0445 \u0438\u043C\u0451\u043D:\n";
|
|
1263
|
+
code += " * \u043F\u043E \u043E\u0434\u043D\u043E\u043C\u0443 \u0438\u043C\u0435\u043D\u0438 \u044D\u043B\u0435\u043C\u0435\u043D\u0442\u0430 \u043F\u0440\u0435\u0444\u0438\u043A\u0441 \u043E\u043F\u0440\u0435\u0434\u0435\u043B\u0438\u0442\u044C \u043E\u0434\u043D\u043E\u0437\u043D\u0430\u0447\u043D\u043E \u043D\u0435\u043B\u044C\u0437\u044F,\n";
|
|
1264
|
+
code += " * \u043F\u043E\u044D\u0442\u043E\u043C\u0443 \u0443\u0447\u0438\u0442\u044B\u0432\u0430\u0435\u0442\u0441\u044F \u043A\u043E\u043D\u0442\u0435\u043A\u0441\u0442 \u0440\u043E\u0434\u0438\u0442\u0435\u043B\u044F.\n";
|
|
1265
|
+
code += " */\n\n";
|
|
1266
|
+
code += children;
|
|
1267
|
+
}
|
|
1268
|
+
code += "export const XMLNamespaceRegistry = {\n";
|
|
1269
|
+
code += " prefixes: XMLNamespacePrefixes,\n";
|
|
1270
|
+
code += " elements: XMLNamespaceElements,\n";
|
|
1271
|
+
if (children) {
|
|
1272
|
+
code += " children: XMLNamespaceChildren,\n";
|
|
1273
|
+
}
|
|
1274
|
+
code += "} as const;\n\n";
|
|
1275
|
+
return code;
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Собирает карту «родитель -> { дочерний элемент -> префикс }».
|
|
1279
|
+
*
|
|
1280
|
+
* В карту попадают только неоднозначные элементы — те, что объявлены
|
|
1281
|
+
* в нескольких пространствах имён. Для остальных достаточно общего реестра.
|
|
1282
|
+
*/
|
|
1283
|
+
compileNamespaceChildren() {
|
|
1284
|
+
const children = {};
|
|
1285
|
+
for (const [elementName, typeName] of this.elementTypeRegistry.entries()) {
|
|
1286
|
+
const type = this.typeRegistry.get(typeName);
|
|
1287
|
+
if (!type) {
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
const map = {};
|
|
1291
|
+
for (const element of type.elements) {
|
|
1292
|
+
if (!element.namespace) {
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
if (!this.ambiguousElements.has(element.name)) {
|
|
1296
|
+
continue;
|
|
1297
|
+
}
|
|
1298
|
+
map[element.name] = element.namespace;
|
|
1299
|
+
}
|
|
1300
|
+
if (Object.keys(map).length > 0) {
|
|
1301
|
+
children[elementName] = map;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
if (Object.keys(children).length === 0) {
|
|
1305
|
+
return "";
|
|
1306
|
+
}
|
|
1307
|
+
let code = "export const XMLNamespaceChildren = {\n";
|
|
1308
|
+
for (const [parent, map] of Object.entries(children)) {
|
|
1309
|
+
code += ` ${JSON.stringify(parent)}: {
|
|
1310
|
+
`;
|
|
1311
|
+
for (const [child, prefix] of Object.entries(map)) {
|
|
1312
|
+
code += ` ${JSON.stringify(child)}: ${JSON.stringify(prefix)},
|
|
1313
|
+
`;
|
|
1314
|
+
}
|
|
1315
|
+
code += " },\n";
|
|
985
1316
|
}
|
|
1317
|
+
code += "} as const;\n\n";
|
|
986
1318
|
return code;
|
|
987
1319
|
}
|
|
988
1320
|
};
|