xml-toolkit 1.0.9 → 1.0.10

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.
@@ -2,51 +2,33 @@ const assert = require ('assert')
2
2
 
3
3
  const NamespacePrefixesMap = class extends Map {
4
4
 
5
- constructor (schemata, node) {
5
+ constructor (schemata) {
6
6
 
7
7
  super ()
8
8
 
9
- this.schemata = schemata
9
+ for (const uri of schemata.keys ())
10
10
 
11
- this.checkNode (node)
12
-
13
- }
14
-
15
- checkNode ({targetNamespace, attributes, children}) {
16
-
17
- this.checkURI (targetNamespace)
18
-
19
- for (const a of Object.values (attributes))
11
+ this.set (uri, 'ns' + this.size)
20
12
 
21
- if (Array.isArray (a) && a.length === 2)
22
-
23
- this.checkURI (a [1])
24
-
25
- for (const i of children) this.checkNode (i)
26
-
27
13
  }
28
-
29
- checkURI (uri) {
30
14
 
31
- if (this.has (uri)) return
15
+ QName (localName, namespaceURI) {
32
16
 
33
- if (!this.schemata.has (uri)) return
17
+ if (namespaceURI == null) return localName
34
18
 
35
- this.set (uri, 'ns' + this.size)
19
+ assert (this.has (namespaceURI), 'Unknown target namespace: ' + namespaceURI)
36
20
 
37
- }
38
-
39
- QName (localName, namespaceURI) {
40
-
41
- if (namespaceURI == null || !this.has (namespaceURI)) return localName
42
-
43
21
  return this.get (namespaceURI) + ':' + localName
44
22
 
45
23
  }
46
24
 
47
- appendTo (buf) {
25
+ toString () {
26
+
27
+ let s = ''
48
28
 
49
- for (let [k, v] of this.entries ()) buf [0] += ' xmlns:' + v + '="' + k + '"'
29
+ for (let [k, v] of this.entries ()) s += ' xmlns:' + v + '="' + k + '"'
30
+
31
+ return s
50
32
 
51
33
  }
52
34
 
@@ -1,5 +1,6 @@
1
1
  const assert = require ('assert')
2
2
  const stringEscape = require ('string-escape-map')
3
+ const NamespacePrefixesMap = require ('./NamespacePrefixesMap.js')
3
4
 
4
5
  let esc = [
5
6
  ['<', '&lt;'],
@@ -33,33 +34,65 @@ const XMLMarshaller = class {
33
34
 
34
35
  this.schemaElement = this.schema.get (localName); assert (this.schemaElement, 'No schema element found for namespaceURI = ' + namespaceURI + ', localName = ' + localName)
35
36
 
36
- this.ns = this.xs.getNamespacePrefixesMap (this.schemaElement)
37
+ this.ns = new NamespacePrefixesMap (xs)
38
+
39
+ this.isNsDumped = false
40
+
41
+ this.qNames = []
37
42
 
38
43
  }
39
44
 
40
45
  stringify (data, name) {
41
-
46
+
42
47
  const {schemaElement} = this, {targetNamespace, attributes, children} = schemaElement
43
48
 
44
- const qName = this.ns.QName (name || attributes.name, targetNamespace)
49
+ this.buf = ''
50
+
51
+ this.appendElement (schemaElement, data, this.ns.QName (name || attributes.name, targetNamespace))
52
+
53
+ let xml = this.buf
54
+
55
+ delete this.buf
45
56
 
46
- this.buf = ['<' + qName]
57
+ return xml
58
+
59
+ }
60
+
61
+ appendElement (node, data, qName) {
62
+
63
+ this.appendStartTag (node, data, qName)
47
64
 
48
- this.ns.appendTo (this.buf)
65
+ this.appendElementBody (node, data)
49
66
 
50
- this.appendAttributes (schemaElement, data)
67
+ this.appendEndTag ()
51
68
 
52
- this.buf [0] += '>'
69
+ }
70
+
71
+ appendStartTag (node, data, qName) {
72
+
73
+ this.qNames.push (qName)
74
+
75
+ this.buf += '<' + qName
53
76
 
54
- this.appendElementBody (schemaElement, data)
77
+ if (!this.isNsDumped) {
55
78
 
56
- const xml = this.buf [0] + '</' + qName + '>'
79
+ this.buf += this.ns
80
+
81
+ this.isNsDumped = true
57
82
 
58
- delete this.buf
83
+ }
59
84
 
60
- return xml
85
+ this.appendAttributes (node, data)
61
86
 
62
- }
87
+ this.buf += '>'
88
+
89
+ }
90
+
91
+ appendEndTag () {
92
+
93
+ this.buf += '</' + this.qNames.pop () + '>'
94
+
95
+ }
63
96
 
64
97
  appendNullElement (node) {
65
98
 
@@ -69,7 +102,7 @@ const XMLMarshaller = class {
69
102
 
70
103
  const qName = this.ns.QName (name, targetNamespace)
71
104
 
72
- this.buf [0] += `<${qName} xsi:nil="true" />`
105
+ this.buf += `<${qName} xsi:nil="true" />`
73
106
 
74
107
  }
75
108
 
@@ -87,7 +120,7 @@ const XMLMarshaller = class {
87
120
 
88
121
  }
89
122
 
90
- this.buf [0] += this.to_string (data, node.attributes.name, restriction)
123
+ this.buf += this.to_string (data, node.attributes.name, restriction)
91
124
 
92
125
  }
93
126
 
@@ -115,11 +148,11 @@ const XMLMarshaller = class {
115
148
 
116
149
  let v = data [name]; if (v == null) return
117
150
 
118
- this.buf [0] += ' ' + this.ns.QName (name, targetNamespace) + '="'
151
+ this.buf += ' ' + this.ns.QName (name, targetNamespace) + '="'
119
152
 
120
153
  this.appendScalar (this.xs.getByReference (type), v)
121
154
 
122
- this.buf [0] += '"'
155
+ this.buf += '"'
123
156
 
124
157
  break
125
158
 
@@ -160,24 +193,12 @@ const XMLMarshaller = class {
160
193
  if (!(name in data)) return
161
194
 
162
195
  let v = data [name]; if (v == null) return this.appendNullElement (node)
163
-
164
- if (!Array.isArray (v)) v = [v]
165
196
 
166
197
  const qName = this.ns.QName (name, targetNamespace)
167
198
 
168
- for (const d of Array.isArray (v) ? v : [v]) {
169
-
170
- this.buf [0] += '<' + qName
171
-
172
- this.appendAttributes (node, d)
173
-
174
- this.buf [0] += '>'
175
-
176
- this.appendElementBody (node, d)
177
-
178
- this.buf [0] += '</' + qName + '>'
199
+ if (!Array.isArray (v)) return this.appendElement (node, v, qName)
179
200
 
180
- }
201
+ for (const d of v) this.appendElement (node, d, qName)
181
202
 
182
203
  break
183
204
 
package/lib/XMLNode.js CHANGED
@@ -10,6 +10,10 @@ const PARENT = Symbol ('_parent')
10
10
  const LEVEL = Symbol ('_level')
11
11
  const NS_MAP = Symbol ('_ns_map')
12
12
 
13
+ const m2o =
14
+ Object.fromEntries ? m => Object.fromEntries (m.entries ()) :
15
+ m => {let o = {}; for (const [k, v] of m.entries ()) o [k] = v; return o}
16
+
13
17
  const XMLNode = class extends SAXEvent {
14
18
 
15
19
  constructor (src, xmlReader, _type) {
@@ -142,8 +146,6 @@ const XMLNode = class extends SAXEvent {
142
146
 
143
147
  default:
144
148
 
145
- const m2o = m => Object.fromEntries (m.entries ())
146
-
147
149
  const {localName, namespaceURI, attributes, children, namespacesMap} = this
148
150
 
149
151
  let r = {
@@ -59,12 +59,6 @@ const XMLSchemata = class extends Map {
59
59
  for (const uri of ns) return this.get (uri)
60
60
 
61
61
  }
62
-
63
- getNamespacePrefixesMap (o) {
64
-
65
- return new NamespacePrefixesMap (this, o)
66
-
67
- }
68
62
 
69
63
  createMarshaller (localName, namespaceURI) {
70
64
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Collection of classes for dealing with XML",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/test.js CHANGED
@@ -219,6 +219,8 @@ async function test_004_schemata (fn) {
219
219
  console.log (xs.stringify ({
220
220
  "ExportDebtRequestsRequest": {Id: 1}
221
221
  }))
222
+
223
+ // console.log (xs.get ('urn:dom.gosuslugi.ru/debt-responses/1.0.0').get ('AttachmentType'))
222
224
 
223
225
  }
224
226
 
@@ -330,8 +332,8 @@ async function main () {
330
332
  // await test_003_emitter_sync ('ent.xml')
331
333
  // await test_003_emitter_sync ('soap.xml')
332
334
  await test_004_schemata ()
333
- // await test_005_schemata ()
334
- // await test_006_schemata ()
335
+ await test_005_schemata ()
336
+ await test_006_schemata ()
335
337
 
336
338
  }
337
339