browser_parser.js
5.5 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var util = require('../util');
var Shape = require('../model/shape');
function DomXmlParser() { }
DomXmlParser.prototype.parse = function(xml, shape) {
if (xml.replace(/^\s+/, '') === '') return {};
var result, error;
try {
if (window.DOMParser) {
try {
var parser = new DOMParser();
result = parser.parseFromString(xml, 'text/xml');
} catch (syntaxError) {
throw util.error(new Error('Parse error in document'),
{
originalError: syntaxError,
code: 'XMLParserError',
retryable: true
});
}
if (result.documentElement === null) {
throw util.error(new Error('Cannot parse empty document.'),
{
code: 'XMLParserError',
retryable: true
});
}
var isError = result.getElementsByTagName('parsererror')[0];
if (isError && (isError.parentNode === result ||
isError.parentNode.nodeName === 'body' ||
isError.parentNode.parentNode === result ||
isError.parentNode.parentNode.nodeName === 'body')) {
var errorElement = isError.getElementsByTagName('div')[0] || isError;
throw util.error(new Error(errorElement.textContent || 'Parser error in document'),
{
code: 'XMLParserError',
retryable: true
});
}
} else if (window.ActiveXObject) {
result = new window.ActiveXObject('Microsoft.XMLDOM');
result.async = false;
if (!result.loadXML(xml)) {
throw util.error(new Error('Parse error in document'),
{
code: 'XMLParserError',
retryable: true
});
}
} else {
throw new Error('Cannot load XML parser');
}
} catch (e) {
error = e;
}
if (result && result.documentElement && !error) {
var data = parseXml(result.documentElement, shape);
var metadata = getElementByTagName(result.documentElement, 'ResponseMetadata');
if (metadata) {
data.ResponseMetadata = parseXml(metadata, {});
}
return data;
} else if (error) {
throw util.error(error || new Error(), {code: 'XMLParserError', retryable: true});
} else { // empty xml document
return {};
}
};
function getElementByTagName(xml, tag) {
var elements = xml.getElementsByTagName(tag);
for (var i = 0, iLen = elements.length; i < iLen; i++) {
if (elements[i].parentNode === xml) {
return elements[i];
}
}
}
function parseXml(xml, shape) {
if (!shape) shape = {};
switch (shape.type) {
case 'structure': return parseStructure(xml, shape);
case 'map': return parseMap(xml, shape);
case 'list': return parseList(xml, shape);
case undefined: case null: return parseUnknown(xml);
default: return parseScalar(xml, shape);
}
}
function parseStructure(xml, shape) {
var data = {};
if (xml === null) return data;
util.each(shape.members, function(memberName, memberShape) {
if (memberShape.isXmlAttribute) {
if (Object.prototype.hasOwnProperty.call(xml.attributes, memberShape.name)) {
var value = xml.attributes[memberShape.name].value;
data[memberName] = parseXml({textContent: value}, memberShape);
}
} else {
var xmlChild = memberShape.flattened ? xml :
getElementByTagName(xml, memberShape.name);
if (xmlChild) {
data[memberName] = parseXml(xmlChild, memberShape);
} else if (!memberShape.flattened && memberShape.type === 'list') {
data[memberName] = memberShape.defaultValue;
}
}
});
return data;
}
function parseMap(xml, shape) {
var data = {};
var xmlKey = shape.key.name || 'key';
var xmlValue = shape.value.name || 'value';
var tagName = shape.flattened ? shape.name : 'entry';
var child = xml.firstElementChild;
while (child) {
if (child.nodeName === tagName) {
var key = getElementByTagName(child, xmlKey).textContent;
var value = getElementByTagName(child, xmlValue);
data[key] = parseXml(value, shape.value);
}
child = child.nextElementSibling;
}
return data;
}
function parseList(xml, shape) {
var data = [];
var tagName = shape.flattened ? shape.name : (shape.member.name || 'member');
var child = xml.firstElementChild;
while (child) {
if (child.nodeName === tagName) {
data.push(parseXml(child, shape.member));
}
child = child.nextElementSibling;
}
return data;
}
function parseScalar(xml, shape) {
if (xml.getAttribute) {
var encoding = xml.getAttribute('encoding');
if (encoding === 'base64') {
shape = new Shape.create({type: encoding});
}
}
var text = xml.textContent;
if (text === '') text = null;
if (typeof shape.toType === 'function') {
return shape.toType(text);
} else {
return text;
}
}
function parseUnknown(xml) {
if (xml === undefined || xml === null) return '';
// empty object
if (!xml.firstElementChild) {
if (xml.parentNode.parentNode === null) return {};
if (xml.childNodes.length === 0) return '';
else return xml.textContent;
}
// object, parse as structure
var shape = {type: 'structure', members: {}};
var child = xml.firstElementChild;
while (child) {
var tag = child.nodeName;
if (Object.prototype.hasOwnProperty.call(shape.members, tag)) {
// multiple tags of the same name makes it a list
shape.members[tag].type = 'list';
} else {
shape.members[tag] = {name: tag};
}
child = child.nextElementSibling;
}
return parseStructure(xml, shape);
}
/**
* @api private
*/
module.exports = DomXmlParser;