]> git.mxchange.org Git - simgear.git/blob - simgear/io/DAVMultiStatus.cxx
Move bundled Expat to new home.
[simgear.git] / simgear / io / DAVMultiStatus.cxx
1 // DAVMultiStatus.cxx -- parser for WebDAV MultiStatus XML data
2 //
3 // Copyright (C) 2012  James Turner <zakalawe@mac.com>
4 //
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.
9 //
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.
14 //
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.
18
19 #ifdef HAVE_CONFIG_H
20 #  include <simgear_config.h>
21 #endif
22      
23 #include "DAVMultiStatus.hxx"
24
25 #include <iostream>
26 #include <cstring>
27 #include <cassert>
28 #include <algorithm>
29 #include <sstream>
30
31 #include <boost/foreach.hpp>
32
33 #include "simgear/debug/logstream.hxx"
34 #include "simgear/misc/strutils.hxx"
35 #include "simgear/structure/exception.hxx"
36
37 #ifdef SYSTEM_EXPAT
38 #  include <expat.h>
39 #else
40 #  include "sg_expat.h"     
41 #endif
42
43 using std::string;
44
45 using namespace simgear;
46
47 #define DAV_NS "DAV::"
48 #define SUBVERSION_DAV_NS "http://subversion.tigris.org/xmlns/dav/"
49
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";
54
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";
62
63 const char* SUBVERSION_MD5_CHECKSUM_TAG = SUBVERSION_DAV_NS ":md5-checksum";
64
65 DAVResource::DAVResource(const string& href) :
66   _type(Unknown),
67   _url(href),
68   _container(NULL)
69 {
70     assert(!href.empty());
71     if (strutils::ends_with(href, "/")) {
72         _url = href.substr(0, _url.size() - 1);
73     }
74 }
75
76 void DAVResource::setVersionName(const std::string& aVersion)
77 {
78   _versionName = aVersion;
79 }
80
81 void DAVResource::setVersionControlledConfiguration(const std::string& vcc)
82 {
83     _vcc = vcc;
84 }
85
86 void DAVResource::setMD5(const std::string& md5Hex)
87 {
88     _md5 = md5Hex;
89 }
90
91 std::string DAVResource::name() const
92 {
93     string::size_type index = _url.rfind('/');
94     if (index != string::npos) {
95         return _url.substr(index + 1);
96     }
97     
98     throw sg_exception("bad DAV resource HREF:" + _url);
99 }
100
101 ////////////////////////////////////////////////////////////////////////////
102
103 DAVCollection::DAVCollection(const string& href) :
104   DAVResource(href)
105 {
106   _type = DAVResource::Collection;
107 }
108
109 DAVCollection::~DAVCollection()
110 {
111   BOOST_FOREACH(DAVResource* c, _contents) {
112     delete c;
113   }
114 }
115
116 void DAVCollection::addChild(DAVResource *res)
117 {
118   assert(res);
119   if (res->container() == this) {
120     return;
121   }
122   
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);
127   
128   res->_container = this;
129   _contents.push_back(res);
130 }
131
132 void DAVCollection::removeChild(DAVResource* res)
133 {
134   assert(res);
135   assert(res->container() == this);
136   
137   res->_container = NULL;
138   DAVResourceList::iterator it = std::find(_contents.begin(), _contents.end(), res);
139   assert(it != _contents.end());
140   _contents.erase(it);
141 }
142
143 DAVCollection*
144 DAVCollection::createChildCollection(const std::string& name)
145 {
146     DAVCollection* child = new DAVCollection(urlForChildWithName(name));
147     addChild(child);
148     return child;
149 }
150
151 DAVResourceList DAVCollection::contents() const
152 {
153   return _contents;
154 }
155
156 DAVResource* DAVCollection::childWithUrl(const string& url) const
157 {
158   if (url.empty())
159     return NULL;
160   
161   BOOST_FOREACH(DAVResource* c, _contents) {
162     if (c->url() == url) {
163       return c;
164     }
165   }
166   
167   return NULL;
168 }
169
170 DAVResource* DAVCollection::childWithName(const string& name) const
171 {
172   return childWithUrl(urlForChildWithName(name));
173 }
174
175 std::string DAVCollection::urlForChildWithName(const std::string& name) const
176 {
177   return url() + "/" + name;
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181
182 class DAVMultiStatus::DAVMultiStatusPrivate
183 {
184 public:
185   DAVMultiStatusPrivate() :
186   parserInited(false)
187   {
188     rootResource = NULL;
189   }
190   
191   void startElement (const char * name)
192   {
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);
197       } else {
198         
199       }
200     } else {
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:"
205                  << name);
206         }
207       }
208       
209       if (tagStack.back() == DAV_RESOURCE_TYPE_TAG) {
210         if (!strcmp(name, DAV_COLLECTION_TAG)) {
211           currentElementType = DAVResource::Collection;
212         } else {
213           currentElementType = DAVResource::Unknown;
214         }
215       }
216     }
217     
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();
224       currentVCC.clear();
225     }
226   }
227   
228   void endElement (const char * name)
229   {
230     assert(tagStack.back() == name);
231     tagStack.pop_back();
232     
233     if (!strcmp(name, DAV_RESPONSE_TAG)) {
234       // finish complete response
235       currentElementUrl = strutils::strip(currentElementUrl);
236       
237       DAVResource* res = NULL;
238       if (currentElementType == DAVResource::Collection) {
239         DAVCollection* col = new DAVCollection(currentElementUrl);
240         res = col;
241       } else {
242         res = new DAVResource(currentElementUrl);
243       }
244       
245       res->setVersionName(strutils::strip(currentVersionName));
246       res->setVersionControlledConfiguration(currentVCC);
247       if (rootResource &&
248           strutils::starts_with(currentElementUrl, rootResource->url()))
249       {
250         static_cast<DAVCollection*>(rootResource)->addChild(res);
251       }
252       
253       if (!rootResource) {
254         rootResource = res;
255       }
256     }
257   }
258   
259   void data (const char * s, int length)
260   {
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);
266       }
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;
274     }
275   }
276   
277   void pi (const char * target, const char * data) {}
278   
279   string tagN(const unsigned int n) const
280   {
281     size_t sz = tagStack.size();
282     if (n >= sz) {
283       return string();
284     }
285     
286     return tagStack[sz - (1 + n)];
287   }
288   
289   bool parserInited;
290   XML_Parser xmlParser;
291   DAVResource* rootResource;
292   
293   // in-flight data
294   string_list tagStack;
295   DAVResource::Type currentElementType;
296   string currentElementUrl,
297     currentVersionName,
298     currentVCC;
299   int currentElementLength;
300   string currentElementMD5;
301 };
302
303
304 ////////////////////////////////////////////////////////////////////////
305 // Static callback functions for Expat.
306 ////////////////////////////////////////////////////////////////////////
307
308 #define VISITOR static_cast<DAVMultiStatus::DAVMultiStatusPrivate *>(userData)
309
310 static void
311 start_element (void * userData, const char * name, const char ** atts)
312 {
313   VISITOR->startElement(name);
314 }
315
316 static void
317 end_element (void * userData, const char * name)
318 {
319   VISITOR->endElement(name);
320 }
321
322 static void
323 character_data (void * userData, const char * s, int len)
324 {
325   VISITOR->data(s, len);
326 }
327
328 static void
329 processing_instruction (void * userData,
330                         const char * target,
331                         const char * data)
332 {
333   VISITOR->pi(target, data);
334 }
335
336 #undef VISITOR
337
338 ///////////////////////////////////////////////////////////////////////////////
339
340 DAVMultiStatus::DAVMultiStatus() :
341 _d(new DAVMultiStatusPrivate)
342 {
343   
344 }
345
346 DAVMultiStatus::~DAVMultiStatus()
347 {
348   
349 }
350
351 void DAVMultiStatus::parseXML(const char* data, int size)
352 {
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;
360   }
361   
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));
366     
367     XML_ParserFree(_d->xmlParser);
368     _d->parserInited = false;
369   }
370 }
371
372 void DAVMultiStatus::finishParse()
373 {
374   if (_d->parserInited) {
375     XML_Parse(_d->xmlParser, NULL, 0, true);
376     XML_ParserFree(_d->xmlParser);
377   }
378   
379   _d->parserInited = false;
380 }
381
382 DAVResource* DAVMultiStatus::resource()
383 {
384   return _d->rootResource;
385 }
386
387