33 #include <boost/algorithm/string.hpp>
44 m_parser = XML_ParserCreate(
"US-ASCII");
51 const std::initializer_list<std::string> &sub_parsers) :
66 if (XML_GetErrorCode(
m_parser) == XML_ERROR_TAG_MISMATCH)
68 (
const char *)XML_ErrorString(XML_GetErrorCode(
m_parser)),
72 const char *end = base;
77 (
const char *)XML_ErrorString(XML_GetErrorCode(
m_parser)),
78 string(base, end-base).c_str());
91 const std::string &content) {
94 "Empty content for %s", name.c_str());
97 int64_t val = strtoll(content.c_str(), &end, 10);
100 "Invalid value for %s (%s)", name.c_str(), content.c_str());
105 const std::string &content) {
107 if (val64 > (int64_t)numeric_limits<int32_t>::max() ||
108 val64 < (int64_t)numeric_limits<int32_t>::min())
110 "Invalid value for %s (%s)", name.c_str(), content.c_str());
111 return (int32_t)val64;
115 const std::string &content) {
117 if (val64 > (int64_t)numeric_limits<int16_t>::max() ||
118 val64 < (int64_t)numeric_limits<int16_t>::min())
120 "Invalid value for %s (%s)", name.c_str(), content.c_str());
121 return (int16_t)val64;
125 const std::string &content) {
128 "Empty content for %s", name.c_str());
129 if (!strcasecmp(content.c_str(),
"true"))
131 else if (!strcasecmp(content.c_str(),
"false"))
134 "Invalid boolean value for %s (%s)", name.c_str(), content.c_str());
138 const std::string &content,
139 const std::initializer_list<std::string> &valid) {
142 "Empty content for %s", name.c_str());
143 for (
auto &v : valid) {
144 if (!strcasecmp(v.c_str(), content.c_str()))
148 "Invalid value for %s (%s)", name.c_str(), content.c_str());
159 if (!strcasecmp(sp.c_str(), name)) {
172 int offset = XML_GetCurrentByteIndex(
m_parser);
173 for (; offset <
m_length; ++offset) {
174 if (
m_base[offset] ==
'>') {
177 offset - m_sub_parse_base_offset);
183 "Unable to find '>' in close tag '%s'", name);
209 const XML_Char **atts) {
223 boost::trim(content);
std::string m_collected_text
Collected element text.
const char * m_base
Pointer to buffer holding content to be parsed.
std::stack< std::string > m_element_stack
Element stack.
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
int m_sub_parse_base_offset
Raw text offset (from m_base) of beginning of sub parse.
static bool content_to_bool(const std::string &name, const std::string &content)
Helper function to convert content to a boolean value.
static void end_element_handler(void *userdata, const XML_Char *name)
eXpat end element handler.
virtual void end_element(const XML_Char *name, const std::string &content)
End element callback member function.
static int32_t content_to_i32(const std::string &name, const std::string &content)
Helper function to convert content to an int32_t value.
bool open_element(const XML_Char *name)
Determines if element should be parsed or included in a sub parse.
void pop_element()
Pops element from element stack.
void push_element(const XML_Char *name)
Push element onto element stack.
Definitions for XmlParser.
static void character_data_handler(void *userdata, const XML_Char *s, int len)
eXpat character data handler add_text() is called to add len characters starting at s to collected te...
Logging routines and macros.
Compatibility Macros for C/C++.
virtual ~XmlParser()
Destructor.
bool close_element(const XML_Char *name)
Checks for and performs sub parse.
int m_length
Length of data at m_base to be parsed.
XmlParser(const char *base, int len)
Constructor.
static int64_t content_to_i64(const std::string &name, const std::string &content)
Helper function to convert content to an int64_t value.
void add_text(const XML_Char *s, int len)
Collects text.
int m_sub_parse_toplevel
Toplevel element of current sub parse.
static int16_t content_to_i16(const std::string &name, const std::string &content)
Helper function to convert content to an int16_t value.
const std::string collected_text()
Returns collected text.
std::vector< std::string > m_sub_parsers
List of element names for which there is a sub-parser.
Base class for XML document parsers.
static const std::string content_to_text(const std::string &name, const std::string &content, const std::initializer_list< std::string > &valid)
Helper function to convert content to one of a set of valid text values.
virtual void start_element(const XML_Char *name, const XML_Char **atts)
Start element callback member function.
#define HT_THROWF(_code_, _fmt_,...)
XML_Parser m_parser
eXpat parser
This is a generic exception class for Hypertable.
static void start_element_handler(void *userdata, const XML_Char *name, const XML_Char **atts)
eXpat start element handler.
virtual void sub_parse(const XML_Char *name, const char *base, int len)
Performs a sub-parse.
Error codes, Exception handling, error logging.
#define HT_THROW(_code_, _msg_)
std::string m_current_element
Current element being parsed.
int code() const
Returns the error code.