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
|
(function(module, ns) {
var Responder = ns.require('responder');
if (typeof(window) === 'undefined') {
Parser.sax = require('sax');
} else {
Parser.sax = sax;
}
/**
* Creates a parser object.
*
* @param {Object} baseHandler base sax handler.
*/
function Parser(baseHandler) {
var handler;
var events = [
'ontext',
'oncdata',
'onopentag',
'onclosetag',
'onerror',
'onend'
];
if (typeof(baseHandler) !== 'undefined') {
handler = baseHandler;
} else {
handler = ns.require('sax/base');
}
this.stack = [];
this.handles = {};
this._handlerStack = [];
this.tagStack = [];
this.root = this.current = {};
this.setHandler(handler);
this._parse = Parser.sax.parser(true, {
xmlns: true,
trim: true,
normalize: false,
lowercase: true
});
events.forEach(function(event) {
this._parse[event] = this[event].bind(this);
}, this);
Responder.call(this);
}
Parser.prototype = {
__proto__: Responder.prototype,
/**
* Sets current handler, optionally adding
* previous one to the handlerStack.
*
* @param {Object} handler new handler.
* @param {Boolean} storeOriginal store old handler?
*/
setHandler: function(handler, storeOriginal) {
if (storeOriginal) {
this._handlerStack.push(this.handler);
}
this.handler = handler;
},
/**
* Sets handler to previous one in the stack.
*/
restoreHandler: function() {
if (this._handlerStack.length) {
this.handler = this._handlerStack.pop();
}
},
/**
* Registers a top level handler
*
* @param {String} tag xmlns uri/local tag name for example
* DAV:/a.
*
* @param {Object} handler new handler to use when tag is
* triggered.
*/
registerHandler: function(tag, handler) {
this.handles[tag] = handler;
},
/**
* Writes data into the parser.
*
* @param {String} chunk partial/complete chunk of xml.
*/
write: function(chunk) {
return this._parse.write(chunk);
},
close: function() {
this._parse.close();
},
get closed() {
return this._parse.closed;
},
/**
* Determines if given tagSpec has a specific handler.
*
* @param {String} tagSpec usual tag spec.
*/
getHandler: function(tagSpec) {
var handler;
var handlers = this.handler.handles;
if (!handlers) {
handlers = this.handles;
}
if (tagSpec in handlers) {
handler = handlers[tagSpec];
if (handler !== this.handler) {
return handler;
}
}
return false;
},
_fireHandler: function(event, data) {
if (typeof(this.handler[event]) === 'function') {
this.handler[event].call(this, data, this.handler);
}
},
onopentag: function(data) {
var handle;
var stackData = {
local: data.local,
name: data.name
};
//build tagSpec for others to use.
data.tagSpec = data.uri + '/' + data.local;
//add to stackData
stackData.tagSpec = data.tagSpec;
//determine if we need to switch to another
//handler object.
handle = this.getHandler(data.tagSpec);
if (handle) {
//switch to new handler object
this.setHandler(handle, true);
stackData.handler = handle;
}
this.tagStack.push(stackData);
this._fireHandler('onopentag', data);
},
//XXX: optimize later
get currentTag() {
return this.tagStack[this.tagStack.length - 1];
},
onclosetag: function(data) {
var stack, handler;
stack = this.currentTag;
if (stack.handler) {
//fire oncomplete handler if available
this._fireHandler('oncomplete');
}
//fire the onclosetag event
this._fireHandler('onclosetag', data);
if (stack.handler) {
//restore previous handler
this.restoreHandler();
}
//actually remove the stack tag
this.tagStack.pop();
},
ontext: function(data) {
this._fireHandler('ontext', data);
},
oncdata: function(data) {
this._fireHandler('oncdata', data);
},
onerror: function(data) {
//TODO: XXX implement handling of parsing errors.
//unlikely but possible if server goes down
//or there is some authentication issue that
//we miss.
this._fireHandler('onerror', data);
},
onend: function() {
this._fireHandler('onend', this.root);
}
};
module.exports = Parser;
}.apply(
this,
(this.Caldav) ?
[Caldav('sax'), Caldav] :
[module, require('./caldav')]
));
|