builder.js
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var util = require('../util');
var XmlNode = require('./xml-node').XmlNode;
var XmlText = require('./xml-text').XmlText;
function XmlBuilder() { }
XmlBuilder.prototype.toXML = function(params, shape, rootElement, noEmpty) {
var xml = new XmlNode(rootElement);
applyNamespaces(xml, shape, true);
serialize(xml, params, shape);
return xml.children.length > 0 || noEmpty ? xml.toString() : '';
};
function serialize(xml, value, shape) {
switch (shape.type) {
case 'structure': return serializeStructure(xml, value, shape);
case 'map': return serializeMap(xml, value, shape);
case 'list': return serializeList(xml, value, shape);
default: return serializeScalar(xml, value, shape);
}
}
function serializeStructure(xml, params, shape) {
util.arrayEach(shape.memberNames, function(memberName) {
var memberShape = shape.members[memberName];
if (memberShape.location !== 'body') return;
var value = params[memberName];
var name = memberShape.name;
if (value !== undefined && value !== null) {
if (memberShape.isXmlAttribute) {
xml.addAttribute(name, value);
} else if (memberShape.flattened) {
serialize(xml, value, memberShape);
} else {
var element = new XmlNode(name);
xml.addChildNode(element);
applyNamespaces(element, memberShape);
serialize(element, value, memberShape);
}
}
});
}
function serializeMap(xml, map, shape) {
var xmlKey = shape.key.name || 'key';
var xmlValue = shape.value.name || 'value';
util.each(map, function(key, value) {
var entry = new XmlNode(shape.flattened ? shape.name : 'entry');
xml.addChildNode(entry);
var entryKey = new XmlNode(xmlKey);
var entryValue = new XmlNode(xmlValue);
entry.addChildNode(entryKey);
entry.addChildNode(entryValue);
serialize(entryKey, key, shape.key);
serialize(entryValue, value, shape.value);
});
}
function serializeList(xml, list, shape) {
if (shape.flattened) {
util.arrayEach(list, function(value) {
var name = shape.member.name || shape.name;
var element = new XmlNode(name);
xml.addChildNode(element);
serialize(element, value, shape.member);
});
} else {
util.arrayEach(list, function(value) {
var name = shape.member.name || 'member';
var element = new XmlNode(name);
xml.addChildNode(element);
serialize(element, value, shape.member);
});
}
}
function serializeScalar(xml, value, shape) {
xml.addChildNode(
new XmlText(shape.toWireFormat(value))
);
}
function applyNamespaces(xml, shape, isRoot) {
var uri, prefix = 'xmlns';
if (shape.xmlNamespaceUri) {
uri = shape.xmlNamespaceUri;
if (shape.xmlNamespacePrefix) prefix += ':' + shape.xmlNamespacePrefix;
} else if (isRoot && shape.api.xmlNamespaceUri) {
uri = shape.api.xmlNamespaceUri;
}
if (uri) xml.addAttribute(prefix, uri);
}
/**
* @api private
*/
module.exports = XmlBuilder;