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.
19 #include "DAVMultiStatus.hxx"
27 #include <boost/foreach.hpp>
29 #include "simgear/debug/logstream.hxx"
30 #include "simgear/xml/xmlparse.h"
31 #include "simgear/misc/strutils.hxx"
32 #include "simgear/structure/exception.hxx"
36 using namespace simgear;
38 #define DAV_NS "DAV::"
39 #define SUBVERSION_DAV_NS "http://subversion.tigris.org/xmlns/dav/"
41 const char* DAV_MULTISTATUS_TAG = DAV_NS "multistatus";
42 const char* DAV_RESPONSE_TAG = DAV_NS "response";
43 const char* DAV_PROPSTAT_TAG = DAV_NS "propstat";
44 const char* DAV_PROP_TAG = DAV_NS "prop";
46 const char* DAV_HREF_TAG = DAV_NS "href";
47 const char* DAV_RESOURCE_TYPE_TAG = DAV_NS "resourcetype";
48 const char* DAV_CONTENT_TYPE_TAG = DAV_NS "getcontenttype";
49 const char* DAV_CONTENT_LENGTH_TAG = DAV_NS "getcontentlength";
50 const char* DAV_VERSIONNAME_TAG = DAV_NS "version-name";
51 const char* DAV_COLLECTION_TAG = DAV_NS "collection";
52 const char* DAV_VCC_TAG = DAV_NS "version-controlled-configuration";
54 const char* SUBVERSION_MD5_CHECKSUM_TAG = SUBVERSION_DAV_NS ":md5-checksum";
56 DAVResource::DAVResource(const string& href) :
61 assert(!href.empty());
64 void DAVResource::setVersionName(const std::string& aVersion)
66 _versionName = aVersion;
69 void DAVResource::setVersionControlledConfiguration(const std::string& vcc)
74 void DAVResource::setMD5(const std::string& md5Hex)
79 std::string DAVResource::name() const
81 string::size_type index = _url.rfind('/');
82 if (index != string::npos) {
83 return _url.substr(index + 1);
86 throw sg_exception("bad DAV resource HREF:" + _url);
89 ////////////////////////////////////////////////////////////////////////////
91 DAVCollection::DAVCollection(const string& href) :
94 _type = DAVResource::Collection;
97 DAVCollection::~DAVCollection()
99 BOOST_FOREACH(DAVResource* c, _contents) {
104 void DAVCollection::addChild(DAVResource *res)
107 if (res->container() == this) {
111 assert(res->container() == NULL);
112 assert(std::find(_contents.begin(), _contents.end(), res) == _contents.end());
113 assert(strutils::starts_with(res->url(), _url));
114 assert(childWithUrl(res->url()) == NULL);
116 res->_container = this;
117 _contents.push_back(res);
120 void DAVCollection::removeChild(DAVResource* res)
123 assert(res->container() == this);
125 res->_container = NULL;
126 DAVResourceList::iterator it = std::find(_contents.begin(), _contents.end(), res);
127 assert(it != _contents.end());
132 DAVCollection::createChildCollection(const std::string& name)
134 DAVCollection* child = new DAVCollection(urlForChildWithName(name));
139 DAVResourceList DAVCollection::contents() const
144 DAVResource* DAVCollection::childWithUrl(const string& url) const
149 BOOST_FOREACH(DAVResource* c, _contents) {
150 if (c->url() == url) {
158 DAVResource* DAVCollection::childWithName(const string& name) const
160 return childWithUrl(urlForChildWithName(name));
163 std::string DAVCollection::urlForChildWithName(const std::string& name) const
165 return url() + "/" + name;
168 ///////////////////////////////////////////////////////////////////////////////
170 class DAVMultiStatus::DAVMultiStatusPrivate
173 DAVMultiStatusPrivate() :
179 void startElement (const char * name)
181 if (tagStack.empty()) {
182 if (strcmp(name, DAV_MULTISTATUS_TAG)) {
183 SG_LOG(SG_IO, SG_WARN, "root element is not " <<
184 DAV_MULTISTATUS_TAG << ", got:" << name);
189 // not at the root element
190 if (tagStack.back() == DAV_MULTISTATUS_TAG) {
191 if (strcmp(name, DAV_RESPONSE_TAG)) {
192 SG_LOG(SG_IO, SG_WARN, "multistatus child is not response: saw:"
197 if (tagStack.back() == DAV_RESOURCE_TYPE_TAG) {
198 if (!strcmp(name, DAV_COLLECTION_TAG)) {
199 currentElementType = DAVResource::Collection;
201 currentElementType = DAVResource::Unknown;
206 tagStack.push_back(name);
207 if (!strcmp(name, DAV_RESPONSE_TAG)) {
208 currentElementType = DAVResource::Unknown;
209 currentElementUrl.clear();
210 currentElementMD5.clear();
211 currentVersionName.clear();
216 void endElement (const char * name)
218 assert(tagStack.back() == name);
221 if (!strcmp(name, DAV_RESPONSE_TAG)) {
222 // finish complete response
223 currentElementUrl = strutils::strip(currentElementUrl);
225 DAVResource* res = NULL;
226 if (currentElementType == DAVResource::Collection) {
227 DAVCollection* col = new DAVCollection(currentElementUrl);
230 res = new DAVResource(currentElementUrl);
233 res->setVersionName(strutils::strip(currentVersionName));
234 res->setVersionControlledConfiguration(currentVCC);
236 strutils::starts_with(currentElementUrl, rootResource->url()))
238 static_cast<DAVCollection*>(rootResource)->addChild(res);
247 void data (const char * s, int length)
249 if (tagStack.back() == DAV_HREF_TAG) {
250 if (tagN(1) == DAV_RESPONSE_TAG) {
251 currentElementUrl += string(s, length);
252 } else if (tagN(1) == DAV_VCC_TAG) {
253 currentVCC += string(s, length);
255 } else if (tagStack.back() == SUBVERSION_MD5_CHECKSUM_TAG) {
256 currentElementMD5 = string(s, length);
257 } else if (tagStack.back() == DAV_VERSIONNAME_TAG) {
258 currentVersionName = string(s, length);
259 } else if (tagStack.back() == DAV_CONTENT_LENGTH_TAG) {
260 std::istringstream is(string(s, length));
261 is >> currentElementLength;
265 void pi (const char * target, const char * data) {}
267 string tagN(const unsigned int n) const
269 size_t sz = tagStack.size();
274 return tagStack[sz - (1 + n)];
278 XML_Parser xmlParser;
279 DAVResource* rootResource;
282 string_list tagStack;
283 DAVResource::Type currentElementType;
284 string currentElementUrl,
287 int currentElementLength;
288 string currentElementMD5;
292 ////////////////////////////////////////////////////////////////////////
293 // Static callback functions for Expat.
294 ////////////////////////////////////////////////////////////////////////
296 #define VISITOR static_cast<DAVMultiStatus::DAVMultiStatusPrivate *>(userData)
299 start_element (void * userData, const char * name, const char ** atts)
301 VISITOR->startElement(name);
305 end_element (void * userData, const char * name)
307 VISITOR->endElement(name);
311 character_data (void * userData, const char * s, int len)
313 VISITOR->data(s, len);
317 processing_instruction (void * userData,
321 VISITOR->pi(target, data);
326 ///////////////////////////////////////////////////////////////////////////////
328 DAVMultiStatus::DAVMultiStatus() :
329 _d(new DAVMultiStatusPrivate)
334 DAVMultiStatus::~DAVMultiStatus()
339 void DAVMultiStatus::parseXML(const char* data, int size)
341 if (!_d->parserInited) {
342 _d->xmlParser = XML_ParserCreateNS(0, ':');
343 XML_SetUserData(_d->xmlParser, _d.get());
344 XML_SetElementHandler(_d->xmlParser, start_element, end_element);
345 XML_SetCharacterDataHandler(_d->xmlParser, character_data);
346 XML_SetProcessingInstructionHandler(_d->xmlParser, processing_instruction);
347 _d->parserInited = true;
350 if (!XML_Parse(_d->xmlParser, data, size, false)) {
351 SG_LOG(SG_IO, SG_WARN, "DAV parse error:" << XML_ErrorString(XML_GetErrorCode(_d->xmlParser))
352 << " at line:" << XML_GetCurrentLineNumber(_d->xmlParser)
353 << " column " << XML_GetCurrentColumnNumber(_d->xmlParser));
355 XML_ParserFree(_d->xmlParser);
356 _d->parserInited = false;
360 void DAVMultiStatus::finishParse()
362 if (_d->parserInited) {
363 XML_Parse(_d->xmlParser, NULL, 0, true);
364 XML_ParserFree(_d->xmlParser);
367 _d->parserInited = false;
370 DAVResource* DAVMultiStatus::resource()
372 return _d->rootResource;