xml-toolkit 1.0.37 → 1.0.39
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/index.js +1 -0
- package/lib/SOAPEncoding.js +79 -0
- package/lib/XMLNode.js +14 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const util = require ('util')
|
|
2
|
+
|
|
3
|
+
const NS_XSD = 'http://www.w3.org/2001/XMLSchema'
|
|
4
|
+
const NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance'
|
|
5
|
+
const NS_SOAP = 'http://xml.apache.org/xml-soap'
|
|
6
|
+
const NS_SOAPENC = 'http://schemas.xmlsoap.org/soap/encoding/'
|
|
7
|
+
|
|
8
|
+
const SOAPEncoding = class {
|
|
9
|
+
|
|
10
|
+
constructor (options = {}) {
|
|
11
|
+
|
|
12
|
+
this.emptyScalar = options.emptyScalar
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
decodeMap ({children}) {
|
|
17
|
+
|
|
18
|
+
const result = {}
|
|
19
|
+
|
|
20
|
+
for (const {children: [{children: [{src}]}, value]} of children) {
|
|
21
|
+
|
|
22
|
+
const v = this.decode (value)
|
|
23
|
+
|
|
24
|
+
if (v === undefined) continue
|
|
25
|
+
|
|
26
|
+
result [src] = v
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
decodeArray ({children}) {
|
|
35
|
+
|
|
36
|
+
return children.map (value => this.decode (value))
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
decodeScalar ({children}) {
|
|
41
|
+
|
|
42
|
+
if (children.length === 0) return this.emptyScalar
|
|
43
|
+
|
|
44
|
+
return children [0].src
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
decode (node) {
|
|
49
|
+
|
|
50
|
+
const {attributes} = node; if (!attributes) throw Error ('no attributes at all: ' + util.inspect(node))
|
|
51
|
+
|
|
52
|
+
const nil = attributes.get ('nil', NS_XSI); if (nil === 'true' || nil === '1') return null
|
|
53
|
+
|
|
54
|
+
const type = attributes.get ('type', NS_XSI); if (!type) throw Error ('xsi:type not defined: ' + util.inspect(node))
|
|
55
|
+
|
|
56
|
+
const [namespacePrefix, localName] = type.split (':'), namespaceURI = attributes._nsMap.get (namespacePrefix)
|
|
57
|
+
|
|
58
|
+
switch (namespaceURI) {
|
|
59
|
+
|
|
60
|
+
case NS_XSD: return this.decodeScalar (node)
|
|
61
|
+
|
|
62
|
+
case NS_SOAP:
|
|
63
|
+
if (localName !== 'Map') throw Error (`Don't know how to decode ${localName} from ${namespaceURI}`)
|
|
64
|
+
return this.decodeMap (node)
|
|
65
|
+
|
|
66
|
+
case NS_SOAPENC:
|
|
67
|
+
if (localName !== 'Array') throw Error (`Don't know how to decode ${localName} from ${namespaceURI}`)
|
|
68
|
+
return this.decodeArray (node)
|
|
69
|
+
|
|
70
|
+
default:
|
|
71
|
+
throw Error (`Don't know how to decode ${localName} from ${namespaceURI}`)
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = SOAPEncoding
|
package/lib/XMLNode.js
CHANGED
|
@@ -126,6 +126,20 @@ const XMLNode = class extends SAXEvent {
|
|
|
126
126
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
+
toString () {
|
|
130
|
+
|
|
131
|
+
const {src} = this; if (this.type !== SAXEvent.TYPES.END_ELEMENT || this.isSelfEnclosed) return src
|
|
132
|
+
|
|
133
|
+
let s = this.src
|
|
134
|
+
|
|
135
|
+
for (const child of this.children) s += child
|
|
136
|
+
|
|
137
|
+
s += this.xml
|
|
138
|
+
|
|
139
|
+
return s
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
129
143
|
get text () {
|
|
130
144
|
|
|
131
145
|
let s = super.text
|