| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Adrien Devresse <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <davix_internal.hpp> |
| 23 |
#include "davxmlparser.hpp" |
| 24 |
|
| 25 |
namespace Davix { |
| 26 |
|
| 27 |
struct InternalDavParser{ |
| 28 |
static int dav_xml_parser_ne_xml_startelm_cb(void *userdata, int parent, |
| 29 |
const char *nspace, const char *name, |
| 30 |
const char **atts){ |
| 31 |
return static_cast<XMLSAXParser*>(userdata)->parserStartElemCb(parent, nspace, name, atts); |
| 32 |
} |
| 33 |
|
| 34 |
static int dav_xml_ne_xml_cdata_cb(void *userdata, int state, |
| 35 |
const char *cdata, size_t len){ |
| 36 |
return static_cast<XMLSAXParser*>(userdata)->parserCdataCb(state, cdata, len); |
| 37 |
} |
| 38 |
|
| 39 |
static int ne_xml_endelm_cb(void *userdata, int state, |
| 40 |
const char *nspace, const char *name){ |
| 41 |
return static_cast<XMLSAXParser*>(userdata)->parserEndElemCb(state,nspace, name); |
| 42 |
} |
| 43 |
|
| 44 |
}; |
| 45 |
|
| 46 |
int davParserNotImplemented(){ |
| 47 |
throw Davix::DavixException(davix_scope_xml_parser(), StatusCode::OperationNonSupported, "the parser callbacks are not configured properly"); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
|
| 51 |
XMLSAXParser::XMLSAXParser() : _ne_parser(ne_xml_create()) |
| 52 |
{ |
| 53 |
ne_xml_push_handler(_ne_parser, |
| 54 |
&InternalDavParser::dav_xml_parser_ne_xml_startelm_cb, |
| 55 |
&InternalDavParser::dav_xml_ne_xml_cdata_cb, |
| 56 |
&InternalDavParser::ne_xml_endelm_cb, |
| 57 |
this); |
| 58 |
} |
| 59 |
|
| 60 |
XMLSAXParser::~XMLSAXParser(){ |
| 61 |
ne_xml_destroy(_ne_parser); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
int XMLSAXParser::parseChunk(const char *partial_string, dav_size_t length){ |
| 66 |
int ret = ne_xml_parse(_ne_parser,partial_string,length); |
| 67 |
if(ret != 0){ |
| 68 |
if(ret > 0){ |
| 69 |
ret =-1; |
| 70 |
const char* ne_parser_err = ne_xml_get_error(_ne_parser); |
| 71 |
throw Davix::DavixException(davix_scope_xml_parser(), StatusCode::WebDavPropertiesParsingError, "XML Parsing Error: " + std::string((ne_parser_err)?ne_parser_err:"Unknown ne error")); |
| 72 |
} |
| 73 |
throw Davix::DavixException(davix_scope_xml_parser(), StatusCode::WebDavPropertiesParsingError, "Unknown XML parsing error "); |
| 74 |
} |
| 75 |
return ret; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
int XMLSAXParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){ |
| 80 |
(void) parent; |
| 81 |
(void) nspace; |
| 82 |
(void) name; |
| 83 |
(void) atts; |
| 84 |
return davParserNotImplemented(); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
int XMLSAXParser::parserCdataCb(int state, const char *cdata, size_t len){ |
| 89 |
(void) state; |
| 90 |
(void) cdata; |
| 91 |
(void) len; |
| 92 |
return davParserNotImplemented(); |
| 93 |
} |
| 94 |
|
| 95 |
int XMLSAXParser::parserEndElemCb(int state, const char *nspace, const char *name){ |
| 96 |
(void) state; |
| 97 |
(void) nspace; |
| 98 |
(void) name; |
| 99 |
return davParserNotImplemented(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
int XMLSAXParser::startElemCb(const Chunk & data, |
| 104 |
const std::vector<XMLSAXParser::Chunk> & attrs){ |
| 105 |
(void) data; |
| 106 |
(void) attrs; |
| 107 |
return -1; |
| 108 |
} |
| 109 |
|
| 110 |
int XMLSAXParser::cdataCb(const Chunk & data){ |
| 111 |
(void) data; |
| 112 |
return -1; |
| 113 |
} |
| 114 |
|
| 115 |
int XMLSAXParser::endElemCb(const Chunk & data){ |
| 116 |
(void) data; |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
int XMLSAXParser::commentCb(const Chunk & data){ |
| 121 |
(void) data; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
} // namespace Davix |