forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.ts
More file actions
245 lines (197 loc) · 7.5 KB
/
xml.ts
File metadata and controls
245 lines (197 loc) · 7.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import definition = require("xml");
import easysax = require("js-libs/easysax");
export class ParserEventType implements definition.ParserEventType {
static StartElement = "StartElement";
static EndElement = "EndElement";
static Text = "Text";
static CDATA = "CDATA";
static Comment = "Comment";
}
export class ParserEvent implements definition.ParserEvent {
private _eventType: string;
private _prefix: string;
private _namespace: string;
private _elementName: string;
private _attributes: Object;
private _data: string;
constructor(eventType: string, prefix?: string, namespace?: string, elementName?: string, attributes?: Object, data?: string) {
this._eventType = eventType;
this._prefix = prefix;
this._namespace = namespace;
this._elementName = elementName;
this._attributes = attributes;
this._data = data;
}
public toString(): string {
return JSON.stringify({
eventType: this.eventType,
prefix: this.prefix,
namespace: this.namespace,
elementName: this.elementName,
attributes: this.attributes,
data: this.data
});
}
public get eventType(): string {
return this._eventType;
}
public get prefix(): string {
return this._prefix;
}
public get namespace(): string {
return this._namespace;
}
public get elementName(): string {
return this._elementName;
}
public get attributes(): Object {
return this._attributes;
}
public get data(): string {
return this._data;
}
}
export class XmlParser implements definition.XmlParser {
//TODO: Add option to configure whether the parser should report ignorable whitespace, i.e. document formatting whitespace.
private _parser: easysax.EasySAXParser;
private _processNamespaces: boolean;
private _namespaceStack: Array<any>;
constructor(onEvent: (event: definition.ParserEvent) => void, onError?: (error: Error) => void, processNamespaces?: boolean) {
this._processNamespaces = processNamespaces;
this._parser = new easysax.EasySAXParser();
var that = this;
this._parser.on('startNode', function (elem, attr, uq, str, tagend) {
var attributes = attr();
if (attributes === true) {//HACK: For some reason easysax returns the true literal when an element has no attributes.
attributes = undefined;
}
if (attributes) {
var key;
for (key in attributes) {
if (attributes.hasOwnProperty(key)) {
// Convert entities such as > to >
attributes[key] = XmlParser._dereferenceEntities(attributes[key]);
}
}
}
var prefix = undefined;
var namespace = undefined;
var name = elem;
if (that._processNamespaces) {
var stackEntry = XmlParser._getNamespacesStackEntry(attributes);
that._namespaceStack.push(stackEntry);
var resolved = that._resolveNamespace(name);
prefix = resolved.prefix;
namespace = resolved.namespace;
name = resolved.name;
}
onEvent(new ParserEvent(ParserEventType.StartElement, prefix, namespace, name, attributes, undefined));
});
this._parser.on('textNode', function (text, uq) {
var data = uq(text);// Decode entity references such as < and >
onEvent(new ParserEvent(ParserEventType.Text, undefined, undefined, undefined, undefined, data));
});
this._parser.on('endNode', function (elem, uq, tagstart, str) {
var prefix = undefined;
var namespace = undefined;
var name = elem;
if (that._processNamespaces) {
var resolved = that._resolveNamespace(name);
prefix = resolved.prefix;
namespace = resolved.namespace;
name = resolved.name;
}
onEvent(new ParserEvent(ParserEventType.EndElement, prefix, namespace, name, undefined, undefined));
if (that._processNamespaces) {
that._namespaceStack.pop();
}
});
this._parser.on('cdata', function (data) {
onEvent(new ParserEvent(ParserEventType.CDATA, undefined, undefined, undefined, undefined, data));
});
this._parser.on('comment', function (text) {
onEvent(new ParserEvent(ParserEventType.Comment, undefined, undefined, undefined, undefined, text));
});
if (onError) {
this._parser.on('error', function (msg) {
onError(new Error(msg));
});
}
}
public parse(xmlString: string): void {
if (this._processNamespaces) {
this._namespaceStack = [];
}
this._parser.parse(xmlString);
}
private static _getNamespacesStackEntry(attributes: any): any {
var stackEntry = {};
if (!attributes) {
return stackEntry;
}
for (var key in attributes) {
if (!attributes.hasOwnProperty(key)) {
continue;
}
var attributeName = <string>key;
if (attributeName.indexOf("xmlns") !== 0) {
// This is a normal attribute, so go on.
continue;
}
var namespacePrefix = "";
if (attributeName.indexOf(":") !== -1) {
namespacePrefix = attributeName.split(":")[1];
}
stackEntry[namespacePrefix] = attributes[key];
}
return stackEntry;
}
private _resolveNamespace(fullName: string): { prefix: string; namespace: string; name: string; } {
var result: { prefix: string; namespace: string; name: string; } = { prefix: undefined, namespace: undefined, name: undefined }
result.prefix = "";
if (fullName.indexOf(":") !== -1) {
var split = fullName.split(":");
result.prefix = split[0];
result.name = split[1];
}
else {
result.name = fullName;
}
var i;
var stackEntry;
for (i = this._namespaceStack.length - 1; i >= 0; i--) {
stackEntry = this._namespaceStack[i];
for (var key in stackEntry) {
if (!stackEntry.hasOwnProperty(key)) {
continue;
}
if (result.prefix === key) {
result.namespace = stackEntry[key];
return result;
}
}
}
return result;
}
private static _dereferenceEntities(s: string): string {
s = String(s);
if (s.length > 3 && s.indexOf('&') !== -1) {
if (s.indexOf('<') !== -1) {
s = s.replace(/</g, '<');
}
if (s.indexOf('>') !== -1) {
s = s.replace(/>/g, '>');
}
if (s.indexOf('&') !== -1) {
s = s.replace(/&/g, '&');
}
if (s.indexOf(''') !== -1) {
s = s.replace(/'/g, "'");
}
if (s.indexOf('"') !== -1) {
s = s.replace(/"/g, '"');
}
};
return s;
}
}