xmljslib 1.0.2 → 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 -21
- package/dist/index.cjs +444 -44
- package/dist/index.d.cts +133 -2
- package/dist/index.d.ts +133 -2
- package/dist/index.js +442 -43
- package/package.json +63 -62
- package/readme.md +573 -466
- package/readme.ru.md +110 -3
package/dist/cli.js
CHANGED
|
@@ -27,13 +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;
|
|
48
|
+
if (prop === "$value") return nodeValue;
|
|
37
49
|
if (prop === "valueOf" || prop === Symbol.toPrimitive) {
|
|
38
50
|
return () => parsedValue;
|
|
39
51
|
}
|
|
@@ -120,10 +132,14 @@ var XMLParser = class _XMLParser {
|
|
|
120
132
|
static parseTreeJSON(item) {
|
|
121
133
|
const result = {};
|
|
122
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;
|
|
123
137
|
for (const attr of Object.keys(item.attributes)) {
|
|
138
|
+
if (attr === "value") {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
124
141
|
node[`$${attr}`] = item.attributes[attr];
|
|
125
142
|
}
|
|
126
|
-
node.$value = item.$value;
|
|
127
143
|
for (const child of item.children) {
|
|
128
144
|
const parsed = _XMLParser.parseTreeJSON(child);
|
|
129
145
|
const childValue = parsed[child.tag];
|
|
@@ -258,7 +274,7 @@ var XMLParser = class _XMLParser {
|
|
|
258
274
|
};
|
|
259
275
|
|
|
260
276
|
// src/lib/wsdlTypeGenerator.ts
|
|
261
|
-
var WSDLTypeGenerator = class {
|
|
277
|
+
var WSDLTypeGenerator = class _WSDLTypeGenerator {
|
|
262
278
|
typeRegistry = /* @__PURE__ */ new Map();
|
|
263
279
|
simpleTypeRegistry = /* @__PURE__ */ new Map();
|
|
264
280
|
simpleTypeDocumentationRegistry = /* @__PURE__ */ new Map();
|
|
@@ -273,6 +289,40 @@ var WSDLTypeGenerator = class {
|
|
|
273
289
|
globalElementRegistry = /* @__PURE__ */ new Map();
|
|
274
290
|
baseUrl = "";
|
|
275
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";
|
|
276
326
|
get complexTypeCount() {
|
|
277
327
|
return this.typeRegistry.size;
|
|
278
328
|
}
|
|
@@ -295,8 +345,17 @@ var WSDLTypeGenerator = class {
|
|
|
295
345
|
this.messageRegistry.clear();
|
|
296
346
|
this.globalElementRegistry.clear();
|
|
297
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();
|
|
298
355
|
console.log("\u{1F4E6} \u041F\u0430\u0440\u0441\u0438\u043D\u0433 \u0441\u0442\u0440\u0443\u043A\u0442\u0443\u0440\u044B XML...");
|
|
299
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);
|
|
300
359
|
console.log("\u{1F50D} \u0410\u043D\u0430\u043B\u0438\u0437 WSDL \u0441\u043E\u043E\u0431\u0449\u0435\u043D\u0438\u0439...");
|
|
301
360
|
this.extractMessages(wsdl);
|
|
302
361
|
console.log("\u{1F50D} \u0410\u043D\u0430\u043B\u0438\u0437 WSDL \u043E\u043F\u0435\u0440\u0430\u0446\u0438\u0439...");
|
|
@@ -339,6 +398,106 @@ var WSDLTypeGenerator = class {
|
|
|
339
398
|
const primitive = typeof value.valueOf === "function" ? value.valueOf() : value;
|
|
340
399
|
return String(primitive);
|
|
341
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
|
+
}
|
|
342
501
|
/**
|
|
343
502
|
* Извлекает текст документации из XML-узла.
|
|
344
503
|
*
|
|
@@ -576,6 +735,8 @@ ${indent} */
|
|
|
576
735
|
if (!schema) {
|
|
577
736
|
return;
|
|
578
737
|
}
|
|
738
|
+
this.collectNamespaceDeclarations(schema);
|
|
739
|
+
const context = this.createSchemaContext(schema);
|
|
579
740
|
for (const key of Object.keys(schema)) {
|
|
580
741
|
const cleanKey = this.cleanPrefix(key);
|
|
581
742
|
if (["import", "include"].includes(cleanKey)) {
|
|
@@ -597,7 +758,7 @@ ${indent} */
|
|
|
597
758
|
if (!ct?.$name) {
|
|
598
759
|
continue;
|
|
599
760
|
}
|
|
600
|
-
this.parseComplexType(ct.$name, ct);
|
|
761
|
+
this.parseComplexType(ct.$name, ct, context);
|
|
601
762
|
}
|
|
602
763
|
}
|
|
603
764
|
for (const key of Object.keys(schema)) {
|
|
@@ -621,10 +782,51 @@ ${indent} */
|
|
|
621
782
|
if (!element?.$name) {
|
|
622
783
|
continue;
|
|
623
784
|
}
|
|
624
|
-
this.parseGlobalElement(element);
|
|
785
|
+
this.parseGlobalElement(element, context);
|
|
625
786
|
}
|
|
626
787
|
}
|
|
627
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
|
+
}
|
|
628
830
|
/**
|
|
629
831
|
* Регистрирует глобальный XSD element.
|
|
630
832
|
*
|
|
@@ -636,11 +838,12 @@ ${indent} */
|
|
|
636
838
|
*
|
|
637
839
|
* FetchBooking -> FetchBookingRequest
|
|
638
840
|
*/
|
|
639
|
-
parseGlobalElement(element) {
|
|
841
|
+
parseGlobalElement(element, context) {
|
|
640
842
|
const name = element.$name;
|
|
641
843
|
if (!name) {
|
|
642
844
|
return;
|
|
643
845
|
}
|
|
846
|
+
this.registerElementNamespace(name, this.resolveElementPrefix(element, context, true));
|
|
644
847
|
let typeName;
|
|
645
848
|
if (element.$type) {
|
|
646
849
|
typeName = this.mapXSDTypeToTS(element.$type);
|
|
@@ -649,7 +852,7 @@ ${indent} */
|
|
|
649
852
|
const inlineSimpleType = this.getChildNode(element, "simpleType");
|
|
650
853
|
if (inlineComplexType) {
|
|
651
854
|
typeName = `${name}Type`;
|
|
652
|
-
this.parseComplexType(typeName, inlineComplexType);
|
|
855
|
+
this.parseComplexType(typeName, inlineComplexType, context);
|
|
653
856
|
} else if (inlineSimpleType) {
|
|
654
857
|
typeName = `${name}Type`;
|
|
655
858
|
this.parseSimpleType(typeName, inlineSimpleType);
|
|
@@ -658,6 +861,7 @@ ${indent} */
|
|
|
658
861
|
}
|
|
659
862
|
}
|
|
660
863
|
this.globalElementRegistry.set(name, typeName);
|
|
864
|
+
this.elementTypeRegistry.set(name, typeName);
|
|
661
865
|
}
|
|
662
866
|
/**
|
|
663
867
|
* Загружает внешнюю XSD-схему.
|
|
@@ -726,13 +930,16 @@ ${indent} */
|
|
|
726
930
|
/**
|
|
727
931
|
* Разбирает XSD complexType.
|
|
728
932
|
*/
|
|
729
|
-
parseComplexType(name, ctNode) {
|
|
933
|
+
parseComplexType(name, ctNode, context) {
|
|
730
934
|
const complexType = {
|
|
731
935
|
name,
|
|
732
936
|
elements: [],
|
|
733
937
|
attributes: [],
|
|
734
938
|
documentation: this.extractDocumentation(ctNode)
|
|
735
939
|
};
|
|
940
|
+
if (context.prefix) {
|
|
941
|
+
this.typeNamespacePrefix.set(name, context.prefix);
|
|
942
|
+
}
|
|
736
943
|
let baseType;
|
|
737
944
|
const findElementsAndAttributes = (node) => {
|
|
738
945
|
if (!node || typeof node !== "object") {
|
|
@@ -760,23 +967,28 @@ ${indent} */
|
|
|
760
967
|
if (!fieldName) {
|
|
761
968
|
continue;
|
|
762
969
|
}
|
|
970
|
+
const elementName = this.cleanPrefix(fieldName);
|
|
971
|
+
const elementNamespace = this.resolveElementPrefix(element, context, false);
|
|
972
|
+
this.registerElementNamespace(elementName, elementNamespace);
|
|
763
973
|
const inlineComplexType = this.getChildNode(element, "complexType");
|
|
764
974
|
const inlineSimpleType = this.getChildNode(element, "simpleType");
|
|
765
975
|
let elementType = element.$type || element.$ref;
|
|
766
976
|
if (inlineComplexType) {
|
|
767
977
|
elementType = `${this.cleanPrefix(fieldName)}Type`;
|
|
768
|
-
this.parseComplexType(elementType, inlineComplexType);
|
|
978
|
+
this.parseComplexType(elementType, inlineComplexType, context);
|
|
769
979
|
} else if (inlineSimpleType) {
|
|
770
980
|
elementType = `${this.cleanPrefix(fieldName)}Type`;
|
|
771
981
|
this.parseSimpleType(elementType, inlineSimpleType);
|
|
772
982
|
}
|
|
983
|
+
this.elementTypeRegistry.set(elementName, this.cleanPrefix(elementType || "string"));
|
|
773
984
|
complexType.elements.push({
|
|
774
|
-
name:
|
|
985
|
+
name: elementName,
|
|
775
986
|
type: this.mapXSDTypeToTS(elementType || "string"),
|
|
776
987
|
isArray: element.$maxOccurs === "unbounded" || Number(element.$maxOccurs) > 1,
|
|
777
988
|
isOptional: element.$minOccurs === "0" || !element.$minOccurs,
|
|
778
989
|
attributes: this.extractAttributes(element),
|
|
779
|
-
documentation: this.extractDocumentation(element)
|
|
990
|
+
documentation: this.extractDocumentation(element),
|
|
991
|
+
namespace: elementNamespace
|
|
780
992
|
});
|
|
781
993
|
}
|
|
782
994
|
}
|
|
@@ -950,18 +1162,22 @@ ${indent} */
|
|
|
950
1162
|
for (const [name, complexType] of this.typeRegistry.entries()) {
|
|
951
1163
|
code += this.formatDocumentation(complexType.documentation);
|
|
952
1164
|
const baseType = complexType.base;
|
|
1165
|
+
const namespaceField = this.compileNamespaceField(complexType);
|
|
953
1166
|
if (baseType && builtInPrimitives.includes(baseType)) {
|
|
954
|
-
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("; ")} }` : "{}";
|
|
955
1173
|
code += `export type ${name} = XMLLeaf<${baseType}, ${attrsObj}>;
|
|
956
1174
|
|
|
957
1175
|
`;
|
|
958
1176
|
continue;
|
|
959
1177
|
}
|
|
960
|
-
|
|
961
|
-
code += `export interface ${name}${extendsStr} {
|
|
962
|
-
`;
|
|
1178
|
+
let fields = namespaceField;
|
|
963
1179
|
for (const element of complexType.elements) {
|
|
964
|
-
|
|
1180
|
+
fields += this.formatDocumentation(element.documentation, " ");
|
|
965
1181
|
const optional = element.isOptional ? "?" : "";
|
|
966
1182
|
let type = element.type;
|
|
967
1183
|
const hasAttributes = element.attributes.length > 0;
|
|
@@ -971,17 +1187,134 @@ ${indent} */
|
|
|
971
1187
|
type = isPrimitiveLike ? `XMLLeaf<${type}, ${attributesType}>` : `XMLBranch<${type}, ${attributesType}>`;
|
|
972
1188
|
}
|
|
973
1189
|
const finalType = element.isArray ? `canBeArray<${type}>` : type;
|
|
974
|
-
|
|
1190
|
+
fields += ` ${element.name}${optional}: ${finalType};
|
|
975
1191
|
`;
|
|
976
1192
|
}
|
|
977
1193
|
for (const attribute of complexType.attributes) {
|
|
978
|
-
|
|
979
|
-
|
|
1194
|
+
fields += this.formatDocumentation(attribute.documentation, " ");
|
|
1195
|
+
fields += ` ${attribute.name}?: ${attribute.type};
|
|
980
1196
|
`;
|
|
981
1197
|
}
|
|
982
|
-
|
|
983
|
-
|
|
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";
|
|
984
1316
|
}
|
|
1317
|
+
code += "} as const;\n\n";
|
|
985
1318
|
return code;
|
|
986
1319
|
}
|
|
987
1320
|
};
|