xml-toolkit 1.0.17 → 1.0.18

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
 
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,17 @@ 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
+ if (fn) {
24
+ this.parser = new XMLParser ()
25
+ this.addFileSync (fn)
26
+ delete this.parser
27
+ }
21
28
 
22
29
  }
23
30
 
@@ -126,6 +133,46 @@ const XMLSchemata = class extends Map {
126
133
 
127
134
  }
128
135
 
136
+ addSchemaSync (node, options = {}) {
137
+
138
+ if (node.localName !== 'schema' || node.namespaceURI !== XMLSchema.namespaceURI) {
139
+
140
+ for (const i of node.children) this.addSchemaSync (i, options)
141
+
142
+ return
143
+
144
+ }
145
+
146
+ node = adjustNode (node, true).detach ()
147
+
148
+ let targetNamespace = node.attributes.targetNamespace || options.targetNamespace
149
+
150
+ if (!this.has (targetNamespace)) this.set (targetNamespace, new XMLSchema (this, targetNamespace))
151
+
152
+ const {addLocation} = options; for (const {localName, namespaceURI, attributes} of node.children)
153
+
154
+ if (localName === 'import' && namespaceURI === XMLSchema.namespaceURI)
155
+
156
+ addLocation (attributes.schemaLocation, attributes.namespace)
157
+
158
+ this.get (targetNamespace).add (node)
159
+
160
+ }
161
+
162
+ addFileSync (fn, options = {}) {
163
+
164
+ const dirname = path.dirname (fn), that = this
165
+
166
+ options.addLocation = function (schemaLocation, namespace) {
167
+
168
+ that.addFileSync (path.join (dirname, schemaLocation), {targetNamespace: namespace})
169
+
170
+ }
171
+
172
+ this.addSchemaSync (this.parser.process (fs.readFileSync (fn, 'utf-8')), options)
173
+
174
+ }
175
+
129
176
  }
130
177
 
131
178
  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.18",
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,6 +328,7 @@ 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
 
@@ -337,6 +345,7 @@ async function test_007_wsdl (fn) {
337
345
  async function test_008_schemata (fn) {
338
346
 
339
347
  const xs = await XMLSchemata.fromFile ('test/30017.xsd')
348
+ const xss = new XMLSchemata ('test/30017.xsd')
340
349
 
341
350
  const data =
342
351
 
@@ -366,10 +375,11 @@ async function test_008_schemata (fn) {
366
375
  }
367
376
  }
368
377
 
369
- // console.log (xs.stringify (data))
370
-
371
378
  // console.log (xs)
372
379
 
380
+ console.log (xs.stringify (data))
381
+ console.log (xss.stringify (data))
382
+
373
383
  }
374
384
 
375
385
  function test_010_node () {
@@ -423,8 +433,8 @@ function test_012_parser (fn) {
423
433
  const parser = new XMLParser ({})
424
434
  const doc = parser.process (xml)
425
435
 
426
- for (const element of doc.detach ().children) {
427
- console.log (element.attributes)
436
+ for (const element of doc.children) {
437
+ console.log (element)
428
438
  }
429
439
 
430
440
  }
@@ -443,7 +453,7 @@ async function main () {
443
453
  // await test_003_emitter_sync ('not-sa01.xml')
444
454
  // await test_003_emitter_sync ('ent.xml')
445
455
  // await test_003_emitter_sync ('soap.xml')
446
- // await test_004_schemata ()
456
+ await test_004_schemata ()
447
457
  // await test_005_schemata ()
448
458
  // await test_006_schemata ()
449
459
  // await test_007_wsdl ()
@@ -453,7 +463,7 @@ async function main () {
453
463
  // test_011_iterator ('param_types.xml')
454
464
  // test_011_iterator ('20040.wsdl')
455
465
 
456
- test_012_parser ('param_types.xml')
466
+ // test_012_parser ('param_types.xml')
457
467
  // test_012_parser ('20040.wsdl')
458
468
 
459
469
  }