xml-toolkit 1.0.17 → 1.0.19

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/README.md CHANGED
@@ -82,7 +82,7 @@ const data = {ExportDebtRequestsResponse: {
82
82
  }
83
83
  }
84
84
 
85
- const xs = await XMLSchemata.fromFile ('xs.xsd')
85
+ const xs = new XMLSchemata ('xs.xsd')
86
86
 
87
87
  const xml = xs.stringify (data)
88
88
 
@@ -99,7 +99,7 @@ const xml = xs.stringify (data)
99
99
  const http = require ('http')
100
100
  const {SOAP11} = require ('xml-toolkit')
101
101
 
102
- const soap = await SOAP11.fromFile ('their.wsdl')
102
+ const soap = new SOAP11 ('their.wsdl')
103
103
 
104
104
  const {method, headers, body} = soap.http ({RequestElementNameOfTheirs: {amount: '0.01'}})
105
105
 
package/lib/SOAP11.js CHANGED
@@ -7,9 +7,27 @@ const SOAPHTTP = require ('./SOAPHTTP.js')
7
7
 
8
8
  const SOAP11 = class {
9
9
 
10
+ constructor (fn) {
11
+
12
+ this.definitions = []
13
+
14
+ if (fn) {
15
+
16
+ this.xs = new XMLSchemata (fn)
17
+
18
+ for (const doc of this.xs.documents)
19
+
20
+ if (doc.localName === 'definitions' && doc.namespaceURI === 'http://schemas.xmlsoap.org/wsdl/')
21
+
22
+ this.definitions.push (doc)
23
+
24
+ }
25
+
26
+ }
27
+
10
28
  getMessageLocalNameByElementLocalName (elementLocalName) {
11
29
 
12
- for (const m of this.definitions.children) if (m.localName === 'message')
30
+ for (const d of this.definitions) for (const m of d.children) if (m.localName === 'message')
13
31
 
14
32
  for (const p of m.children) if (p.localName === 'part')
15
33
 
@@ -21,7 +39,7 @@ const SOAP11 = class {
21
39
 
22
40
  getOperationNameByMessageLocalName (messageLocalName) {
23
41
 
24
- for (const p of this.definitions.children) if (p.localName === 'portType')
42
+ for (const d of this.definitions) for (const p of d.children) if (p.localName === 'portType')
25
43
 
26
44
  for (const o of p.children) if (o.localName === 'operation')
27
45
 
@@ -36,7 +54,7 @@ const SOAP11 = class {
36
54
 
37
55
  getSoapActionByOperationName (operationName) {
38
56
 
39
- for (const b of this.definitions.children) if (b.localName === 'binding')
57
+ for (const d of this.definitions) for (const b of d.children) if (b.localName === 'binding')
40
58
 
41
59
  for (const o of b.children) if (o.localName === 'operation' && o.attributes.get ('name') === operationName)
42
60
 
@@ -97,11 +115,13 @@ SOAP11.fromFile = async function (fn, options = {}) {
97
115
 
98
116
  s.xs = await XMLSchemata.fromFile (fn)
99
117
 
100
- s.definitions = await new XMLReader ({
118
+ const nodes = new XMLReader ({
101
119
  filterElements: e =>
102
120
  e.namespaceURI === WSDL.namespaceURI &&
103
121
  e.localName === 'definitions'
104
- }).process (fs.createReadStream (fn)).findFirst ()
122
+ }).process (fs.createReadStream (fn))
123
+
124
+ for await (const node of nodes) s.definitions.push (node)
105
125
 
106
126
  return s
107
127
 
package/lib/XMLParser.js CHANGED
@@ -60,8 +60,8 @@ const XMLReader = class {
60
60
  case SAXEvent.TYPES.START_ELEMENT:
61
61
 
62
62
  node.children = []
63
- if (this.useNamespaces) node.readNamespaces ()
64
63
  node.parent = this.element
64
+ if (this.useNamespaces) node.readNamespaces ()
65
65
  this.element = node
66
66
  if (this.document === null) this.document = node
67
67
  break
package/lib/XMLSchema.js CHANGED
@@ -47,15 +47,23 @@ const XMLSchema = class extends Map {
47
47
 
48
48
  }
49
49
 
50
- XMLSchema.adjustNode = node => {
50
+ XMLSchema.adjustNode = (node, recurse) => {
51
51
 
52
- if (node.children) node.children = node.children.filter (i => i.localName !== 'annotation')
52
+ if (node.children) {
53
53
 
54
+ node.children = node.children.filter (i => i.localName !== 'annotation')
55
+
56
+ if (recurse) node.children = node.children.map (node => XMLSchema.adjustNode (node, true))
57
+
58
+ }
59
+
54
60
  const {attributes, namespacesMap} = node, splitNs = name => {
55
61
 
56
62
  if (!attributes.has (name)) return
57
-
58
- const [local, prefix] = attributes.get (name).split (':').reverse ()
63
+
64
+ const v = attributes.get (name); if (Array.isArray (v)) return
65
+
66
+ const [local, prefix] = v.split (':').reverse ()
59
67
 
60
68
  attributes.set (name, [local, namespacesMap.get (prefix || '')])
61
69
 
@@ -3,6 +3,7 @@ const assert = require ('assert')
3
3
  const fs = require ('fs')
4
4
  const path = require ('path')
5
5
 
6
+ const XMLParser = require ('./XMLParser.js')
6
7
  const XMLReader = require ('./XMLReader.js')
7
8
  const XMLNode = require ('./XMLNode.js')
8
9
  const XMLSchema = require ('./XMLSchema.js'), {adjustNode} = XMLSchema
@@ -13,11 +14,19 @@ const IDX = Symbol ('_index')
13
14
 
14
15
  const XMLSchemata = class extends Map {
15
16
 
16
- constructor () {
17
+ constructor (fn) {
17
18
 
18
19
  super ()
19
20
 
20
21
  this [IDX] = new Map ()
22
+
23
+ this.documents = []
24
+
25
+ if (fn) {
26
+ this.parser = new XMLParser ()
27
+ this.addFileSync (fn)
28
+ delete this.parser
29
+ }
21
30
 
22
31
  }
23
32
 
@@ -126,6 +135,50 @@ const XMLSchemata = class extends Map {
126
135
 
127
136
  }
128
137
 
138
+ addSchemaSync (node, options = {}) {
139
+
140
+ if (node.localName !== 'schema' || node.namespaceURI !== XMLSchema.namespaceURI) {
141
+
142
+ for (const i of node.children) this.addSchemaSync (i, options)
143
+
144
+ return
145
+
146
+ }
147
+
148
+ node = adjustNode (node, true).detach ()
149
+
150
+ let targetNamespace = node.attributes.targetNamespace || options.targetNamespace
151
+
152
+ if (!this.has (targetNamespace)) this.set (targetNamespace, new XMLSchema (this, targetNamespace))
153
+
154
+ const {addLocation} = options; for (const {localName, namespaceURI, attributes} of node.children)
155
+
156
+ if (localName === 'import' && namespaceURI === XMLSchema.namespaceURI)
157
+
158
+ addLocation (attributes.schemaLocation, attributes.namespace)
159
+
160
+ this.get (targetNamespace).add (node)
161
+
162
+ }
163
+
164
+ addFileSync (fn, options = {}) {
165
+
166
+ const dirname = path.dirname (fn), that = this
167
+
168
+ options.addLocation = function (schemaLocation, namespace) {
169
+
170
+ that.addFileSync (path.join (dirname, schemaLocation), {targetNamespace: namespace})
171
+
172
+ }
173
+
174
+ const document = this.parser.process (fs.readFileSync (fn, 'utf-8'))
175
+
176
+ this.documents.push (document)
177
+
178
+ this.addSchemaSync (document, options)
179
+
180
+ }
181
+
129
182
  }
130
183
 
131
184
  XMLSchemata.fromFile = async function (fn, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "Collection of classes for dealing with XML",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/test.js CHANGED
@@ -113,6 +113,7 @@ console.log (xml)
113
113
  async function test_004_schemata (fn) {
114
114
 
115
115
  let xs = await XMLSchemata.fromFile ('test/dom-gosuslugi-ru-smev3-debt-responses.xsd')
116
+ let xss = new XMLSchemata ('test/dom-gosuslugi-ru-smev3-debt-responses.xsd')
116
117
  /*
117
118
  const localName = 'ImportDebtRequestsRequest'
118
119
 
@@ -127,7 +128,7 @@ async function test_004_schemata (fn) {
127
128
  }))
128
129
  */
129
130
 
130
- console.log (xs.stringify ({
131
+ const d = {
131
132
  "ExportDebtRequestsResponse": {
132
133
  "request-data": {
133
134
  "request-id": "bac4c940-6ad3-11eb-9439-0242ac130002",
@@ -212,7 +213,9 @@ async function test_004_schemata (fn) {
212
213
  }
213
214
  }
214
215
  }
215
- }))
216
+ }
217
+ console.log (xs.stringify (d))
218
+ console.log (xss.stringify (d))
216
219
 
217
220
  //getNamespacePrefixesMap
218
221
  /*
@@ -226,6 +229,9 @@ async function test_004_schemata (fn) {
226
229
  console.log (xs.stringify ({
227
230
  "ExportDebtRequestsRequest": {Id: 1}
228
231
  }))
232
+ console.log (xss.stringify ({
233
+ "ExportDebtRequestsRequest": {Id: 1}
234
+ }))
229
235
 
230
236
  // console.log (xs.get ('urn:dom.gosuslugi.ru/debt-responses/1.0.0').get ('AttachmentType'))
231
237
 
@@ -234,6 +240,7 @@ async function test_004_schemata (fn) {
234
240
  async function test_005_schemata (fn) {
235
241
 
236
242
  const xs = await XMLSchemata.fromFile ('test/20040.wsdl')
243
+ const xss = new XMLSchemata ('test/20040.wsdl')
237
244
 
238
245
  const data = {
239
246
 
@@ -274,26 +281,26 @@ async function test_005_schemata (fn) {
274
281
 
275
282
  // const m = xs.createMarshaller ('AppDataChildDotation', 'http://smev.gosuslugi.ru/rev111111')
276
283
 
277
- console.log (xs.stringify (
278
-
279
- {childDotation2Request: {
280
- Message: {
281
- TestMsg: "Тестовый запроc",
282
- },
283
- MessageData: {
284
- AppData: {
285
- info
286
- }
284
+ const d = {childDotation2Request: {
285
+ Message: {
286
+ TestMsg: "Тестовый запроc",
287
+ },
288
+ MessageData: {
289
+ AppData: {
290
+ info
287
291
  }
288
- }}
289
-
290
- ))
292
+ }
293
+ }}
294
+
295
+ console.log (xs.stringify (d))
296
+ console.log (xss.stringify (d))
291
297
 
292
298
  }
293
299
 
294
300
  async function test_006_schemata (fn) {
295
301
 
296
302
  const xs = await XMLSchemata.fromFile ('test/snils-by-additionalData-1.0.1.xsd')
303
+ const xss = new XMLSchemata ('test/snils-by-additionalData-1.0.1.xsd')
297
304
 
298
305
  const data = {
299
306
  "SnilsByAdditionalDataRequest": {
@@ -321,22 +328,28 @@ async function test_006_schemata (fn) {
321
328
  // const m = xs.createMarshaller ('AppDataChildDotation', 'http://smev.gosuslugi.ru/rev111111')
322
329
 
323
330
  console.log (xs.stringify (data))
331
+ console.log (xss.stringify (data))
324
332
 
325
333
  }
326
334
 
327
335
  async function test_007_wsdl (fn) {
328
336
 
329
337
  const soap = await SOAP11.fromFile ('test/20186.wsdl')
338
+ const soaps = new SOAP11 ('test/20186.wsdl')
339
+
340
+ const d = {GetForm9Sync: {address: {Region: {Code: 78}}}}
330
341
 
331
- console.log (soap.http ({GetForm9Sync: {address: {Region: {Code: 78}}}}))
342
+ console.log (soap.http (d))
343
+ console.log (soaps.http (d))
332
344
 
333
- console.log (soap)
345
+ // console.log (soap)
334
346
 
335
347
  }
336
348
 
337
349
  async function test_008_schemata (fn) {
338
350
 
339
351
  const xs = await XMLSchemata.fromFile ('test/30017.xsd')
352
+ const xss = new XMLSchemata ('test/30017.xsd')
340
353
 
341
354
  const data =
342
355
 
@@ -366,10 +379,11 @@ async function test_008_schemata (fn) {
366
379
  }
367
380
  }
368
381
 
369
- // console.log (xs.stringify (data))
370
-
371
382
  // console.log (xs)
372
383
 
384
+ console.log (xs.stringify (data))
385
+ console.log (xss.stringify (data))
386
+
373
387
  }
374
388
 
375
389
  function test_010_node () {
@@ -423,8 +437,8 @@ function test_012_parser (fn) {
423
437
  const parser = new XMLParser ({})
424
438
  const doc = parser.process (xml)
425
439
 
426
- for (const element of doc.detach ().children) {
427
- console.log (element.attributes)
440
+ for (const element of doc.children) {
441
+ console.log (element)
428
442
  }
429
443
 
430
444
  }
@@ -446,14 +460,14 @@ async function main () {
446
460
  // await test_004_schemata ()
447
461
  // await test_005_schemata ()
448
462
  // await test_006_schemata ()
449
- // await test_007_wsdl ()
463
+ await test_007_wsdl ()
450
464
  // await test_008_schemata ()
451
465
 
452
466
  // test_010_node ()
453
467
  // test_011_iterator ('param_types.xml')
454
468
  // test_011_iterator ('20040.wsdl')
455
469
 
456
- test_012_parser ('param_types.xml')
470
+ // test_012_parser ('param_types.xml')
457
471
  // test_012_parser ('20040.wsdl')
458
472
 
459
473
  }