1 // DAVMultiStatus.cxx -- parser for WebDAV MultiStatus XML data
3 // Copyright (C) 2012 James Turner <zakalawe@mac.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # include <simgear_config.h>
23 #include "DAVMultiStatus.hxx"
31 #include <boost/foreach.hpp>
33 #include "simgear/debug/logstream.hxx"
34 #include "simgear/misc/strutils.hxx"
35 #include "simgear/structure/exception.hxx"
40 # include "sg_expat.h"
45 using namespace simgear;
47 #define DAV_NS "DAV::"
48 #define SUBVERSION_DAV_NS "http://subversion.tigris.org/xmlns/dav/"
50 const char* DAV_MULTISTATUS_TAG = DAV_NS "multistatus";
51 const char* DAV_RESPONSE_TAG = DAV_NS "response";
52 const char* DAV_PROPSTAT_TAG = DAV_NS "propstat";
53 const char* DAV_PROP_TAG = DAV_NS "prop";
55 const char* DAV_HREF_TAG = DAV_NS "href";
56 const char* DAV_RESOURCE_TYPE_TAG = DAV_NS "resourcetype";
57 const char* DAV_CONTENT_TYPE_TAG = DAV_NS "getcontenttype";
58 const char* DAV_CONTENT_LENGTH_TAG = DAV_NS "getcontentlength";
59 const char* DAV_VERSIONNAME_TAG = DAV_NS "version-name";
60 const char* DAV_COLLECTION_TAG = DAV_NS "collection";
61 const char* DAV_VCC_TAG = DAV_NS "version-controlled-configuration";
63 const char* SUBVERSION_MD5_CHECKSUM_TAG = SUBVERSION_DAV_NS ":md5-checksum";
65 DAVResource::DAVResource(const string& href) :
70 assert(!href.empty());
71 if (strutils::ends_with(href, "/")) {
72 _url = href.substr(0, _url.size() - 1);
76 void DAVResource::setVersionName(const std::string& aVersion)
78 _versionName = aVersion;
81 void DAVResource::setVersionControlledConfiguration(const std::string& vcc)
86 void DAVResource::setMD5(const std::string& md5Hex)
91 std::string DAVResource::name() const
93 string::size_type index = _url.rfind('/');
94 if (index != string::npos) {
95 return _url.substr(index + 1);
98 throw sg_exception("bad DAV resource HREF:" + _url);
101 ////////////////////////////////////////////////////////////////////////////
103 DAVCollection::DAVCollection(const string& href) :
106 _type = DAVResource::Collection;
109 DAVCollection::~DAVCollection()
111 BOOST_FOREACH(DAVResource* c, _contents) {
116 void DAVCollection::addChild(DAVResource *res)
119 if (res->container() == this) {
123 assert(res->container() == NULL);
124 assert(std::find(_contents.begin(), _contents.end(), res) == _contents.end());
125 assert(strutils::starts_with(res->url(), _url));
126 assert(childWithUrl(res->url()) == NULL);
128 res->_container = this;
129 _contents.push_back(res);
132 void DAVCollection::removeChild(DAVResource* res)
135 assert(res->container() == this);
137 res->_container = NULL;
138 DAVResourceList::iterator it = std::find(_contents.begin(), _contents.end(), res);
139 assert(it != _contents.end());
144 DAVCollection::createChildCollection(const std::string& name)
146 DAVCollection* child = new DAVCollection(urlForChildWithName(name));
151 DAVResourceList DAVCollection::contents() const
156 DAVResource* DAVCollection::childWithUrl(const string& url) const
161 BOOST_FOREACH(DAVResource* c, _contents) {
162 if (c->url() == url) {
170 DAVResource* DAVCollection::childWithName(const string& name) const
172 return childWithUrl(urlForChildWithName(name));
175 std::string DAVCollection::urlForChildWithName(const std::string& name) const
177 return url() + "/" + name;
180 ///////////////////////////////////////////////////////////////////////////////
182 class DAVMultiStatus::DAVMultiStatusPrivate
185 DAVMultiStatusPrivate() :
191 void startElement (const char * name)
193 if (tagStack.empty()) {
194 if (strcmp(name, DAV_MULTISTATUS_TAG)) {
195 SG_LOG(SG_IO, SG_WARN, "root element is not " <<
196 DAV_MULTISTATUS_TAG << ", got:" << name);
201 // not at the root element
202 if (tagStack.back() == DAV_MULTISTATUS_TAG) {
203 if (strcmp(name, DAV_RESPONSE_TAG)) {
204 SG_LOG(SG_IO, SG_WARN, "multistatus child is not response: saw:"
209 if (tagStack.back() == DAV_RESOURCE_TYPE_TAG) {
210 if (!strcmp(name, DAV_COLLECTION_TAG)) {
211 currentElementType = DAVResource::Collection;
213 currentElementType = DAVResource::Unknown;
218 tagStack.push_back(name);
219 if (!strcmp(name, DAV_RESPONSE_TAG)) {
220 currentElementType = DAVResource::Unknown;
221 currentElementUrl.clear();
222 currentElementMD5.clear();
223 currentVersionName.clear();
228 void endElement (const char * name)
230 assert(tagStack.back() == name);
233 if (!strcmp(name, DAV_RESPONSE_TAG)) {
234 // finish complete response
235 currentElementUrl = strutils::strip(currentElementUrl);
237 DAVResource* res = NULL;
238 if (currentElementType == DAVResource::Collection) {
239 DAVCollection* col = new DAVCollection(currentElementUrl);
242 res = new DAVResource(currentElementUrl);
245 res->setVersionName(strutils::strip(currentVersionName));
246 res->setVersionControlledConfiguration(currentVCC);
248 strutils::starts_with(currentElementUrl, rootResource->url()))
250 static_cast<DAVCollection*>(rootResource)->addChild(res);
259 void data (const char * s, int length)
261 if (tagStack.back() == DAV_HREF_TAG) {
262 if (tagN(1) == DAV_RESPONSE_TAG) {
263 currentElementUrl += string(s, length);
264 } else if (tagN(1) == DAV_VCC_TAG) {
265 currentVCC += string(s, length);
267 } else if (tagStack.back() == SUBVERSION_MD5_CHECKSUM_TAG) {
268 currentElementMD5 = string(s, length);
269 } else if (tagStack.back() == DAV_VERSIONNAME_TAG) {
270 currentVersionName = string(s, length);
271 } else if (tagStack.back() == DAV_CONTENT_LENGTH_TAG) {
272 std::istringstream is(string(s, length));
273 is >> currentElementLength;
277 void pi (const char * target, const char * data) {}
279 string tagN(const unsigned int n) const
281 size_t sz = tagStack.size();
286 return tagStack[sz - (1 + n)];
290 XML_Parser xmlParser;
291 DAVResource* rootResource;
294 string_list tagStack;
295 DAVResource::Type currentElementType;
296 string currentElementUrl,
299 int currentElementLength;
300 string currentElementMD5;
304 ////////////////////////////////////////////////////////////////////////
305 // Static callback functions for Expat.
306 ////////////////////////////////////////////////////////////////////////
308 #define VISITOR static_cast<DAVMultiStatus::DAVMultiStatusPrivate *>(userData)
311 start_element (void * userData, const char * name, const char ** atts)
313 VISITOR->startElement(name);
317 end_element (void * userData, const char * name)
319 VISITOR->endElement(name);
323 character_data (void * userData, const char * s, int len)
325 VISITOR->data(s, len);
329 processing_instruction (void * userData,
333 VISITOR->pi(target, data);
338 ///////////////////////////////////////////////////////////////////////////////
340 DAVMultiStatus::DAVMultiStatus() :
341 _d(new DAVMultiStatusPrivate)
346 DAVMultiStatus::~DAVMultiStatus()
351 void DAVMultiStatus::parseXML(const char* data, int size)
353 if (!_d->parserInited) {
354 _d->xmlParser = XML_ParserCreateNS(0, ':');
355 XML_SetUserData(_d->xmlParser, _d.get());
356 XML_SetElementHandler(_d->xmlParser, start_element, end_element);
357 XML_SetCharacterDataHandler(_d->xmlParser, character_data);
358 XML_SetProcessingInstructionHandler(_d->xmlParser, processing_instruction);
359 _d->parserInited = true;
362 if (!XML_Parse(_d->xmlParser, data, size, false)) {
363 SG_LOG(SG_IO, SG_WARN, "DAV parse error:" << XML_ErrorString(XML_GetErrorCode(_d->xmlParser))
364 << " at line:" << XML_GetCurrentLineNumber(_d->xmlParser)
365 << " column " << XML_GetCurrentColumnNumber(_d->xmlParser));
367 XML_ParserFree(_d->xmlParser);
368 _d->parserInited = false;
372 void DAVMultiStatus::finishParse()
374 if (_d->parserInited) {
375 XML_Parse(_d->xmlParser, NULL, 0, true);
376 XML_ParserFree(_d->xmlParser);
379 _d->parserInited = false;
382 DAVResource* DAVMultiStatus::resource()
384 return _d->rootResource;