Skip to content

Instantly share code, notes, and snippets.

@lightsofapollo
Created June 18, 2012 20:54
Show Gist options
  • Select an option

  • Save lightsofapollo/2950649 to your computer and use it in GitHub Desktop.

Select an option

Save lightsofapollo/2950649 to your computer and use it in GitHub Desktop.
/*
<D:multistatus xmlns:D="DAV:">
<D:response>
<D:href>/calendar/dav/calmozilla1%40gmail.com/user/</D:href>
<D:propstat>
<D:status>HTTP/1.1 200 OK</D:status>
<D:prop>
<D:principal-URL>
<D:href>/calendar/dav/calmozilla1@gmail.com/user/</D:href>
</D:principal-URL>
<D:resourcetype>
<D:principal />
<D:collection />
</D:resourcetype>
</D:prop>
</D:propstat>
<D:propstat>
<D:status>HTTP/1.1 404 Not Found</D:status>
<D:prop>
<A:current-user-principal xmlns:A="DAV:" />
</D:prop>
</D:propstat>
</D:response>
</D:multistatus>
*/
/**
Each sub parser should be static information
about state should be kept in the main parser
so we don't duplicate stuff all over.
*/
var MultiStatus = {
tagName: 'multistatus',
xmlns: 'DAV:',
/*
A handler is invoked when its tag is
used and the sax paser will yield to new handler.
This will occur from all handlers.
*/
handlers: [
Response
]
};
var Response = {
tagName: 'response',
xmlns: 'DAV:',
handlers: [ResourceTypes]
ontagopen: function(parser, data) {
//tag name without ns
},
ontagclose: function(parser, data) {
var current;
if (data.local == 'status') {
//value is the text of the element
parser.current = parser.current.value.match(/([0-9]{3,3})/)[1]
}
}
}
var ResourceTypes = {
tagName: 'resourcetype',
xmlns: 'DAV:',
onstart: function() {
parser.current = [];
}
//override default
ontagtext: function(){},
ontagclose: function(tagname) {},
ontagopen: function(parser, data) {
parser.current.push(data.local)
}
}
/*
Parser will take an xml blob
and parse it into a object in the
same structure as the XML by default.
'Handlers' can be added to process
tags with additional logic where wanted.
We can 'watch' for data
so we can process each tag when its
ready rather then wait for the whole xml tree
to be finished parsing. I imagine for options (propfind)
a non-evented option is totally fine. When recieving
calendar events with ICS blobs we need evented processing
so we can show the user activity.
*/
parse = new Parser();
/**
Even if we get some random ass element
back it will still parse the whole tree.
*/
parse.handle(MultiStatus, ErrorHandler);
parse.watch([
//this is stack based so it will only match top level response elements.
{ tagName: 'response', xmlns: 'DAV:' }
//you can add more for filtering
], function(data) {
//data parsed object
});
parse.oncomplete = function(data) {
//data parsed object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment