0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
XmlParser.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #ifndef Common_XmlParser_h
28 #define Common_XmlParser_h
29 
30 extern "C" {
31 #include <expat.h>
32 #include <strings.h>
33 }
34 
35 #include <cstdint>
36 #include <initializer_list>
37 #include <stack>
38 #include <string>
39 #include <vector>
40 
41 namespace Hypertable {
42 
45 
94  class XmlParser {
95  public:
96 
105  XmlParser(const char *base, int len);
106 
113  XmlParser(const char *base, int len,
114  const std::initializer_list<std::string> &sub_parsers);
115 
118  virtual ~XmlParser();
119 
120  void parse();
121 
128  virtual void start_element(const XML_Char *name, const XML_Char **atts) {};
129 
136  virtual void end_element(const XML_Char *name, const std::string &content) {};
137 
149  virtual void sub_parse(const XML_Char *name, const char *base, int len) {};
150 
158  static int64_t content_to_i64(const std::string &name, const std::string &content);
159 
167  static int32_t content_to_i32(const std::string &name, const std::string &content);
168 
176  static int16_t content_to_i16(const std::string &name, const std::string &content);
177 
187  static bool content_to_bool(const std::string &name, const std::string &content);
188 
199  static const std::string
200  content_to_text(const std::string &name, const std::string &content,
201  const std::initializer_list<std::string> &valid);
202 
203  protected:
204 
206  XML_Parser m_parser;
207 
209  const char *m_base;
210 
212  int m_length;
213 
215  std::stack<std::string> m_element_stack;
216 
217  private:
218 
233  bool open_element(const XML_Char *name);
234 
245  bool close_element(const XML_Char *name);
246 
252  void add_text(const XML_Char *s, int len);
253 
258  void push_element(const XML_Char *name);
259 
264  void pop_element();
265 
269  const std::string collected_text() { return m_collected_text; }
270 
280  static void start_element_handler(void *userdata, const XML_Char *name,
281  const XML_Char **atts);
282 
291  static void end_element_handler(void *userdata, const XML_Char *name);
292 
299  static void character_data_handler(void *userdata, const XML_Char *s, int len);
300 
302  std::string m_collected_text;
303 
305  std::vector<std::string> m_sub_parsers;
306 
308  std::string m_current_element;
309 
312 
315  };
316 
318 }
319 
320 #endif // Common_XmlParser_h
std::string m_collected_text
Collected element text.
Definition: XmlParser.h:302
const char * m_base
Pointer to buffer holding content to be parsed.
Definition: XmlParser.h:209
std::stack< std::string > m_element_stack
Element stack.
Definition: XmlParser.h:215
int m_sub_parse_base_offset
Raw text offset (from m_base) of beginning of sub parse.
Definition: XmlParser.h:314
static bool content_to_bool(const std::string &name, const std::string &content)
Helper function to convert content to a boolean value.
Definition: XmlParser.cc:124
static void end_element_handler(void *userdata, const XML_Char *name)
eXpat end element handler.
Definition: XmlParser.cc:217
virtual void end_element(const XML_Char *name, const std::string &content)
End element callback member function.
Definition: XmlParser.h:136
static int32_t content_to_i32(const std::string &name, const std::string &content)
Helper function to convert content to an int32_t value.
Definition: XmlParser.cc:104
bool open_element(const XML_Char *name)
Determines if element should be parsed or included in a sub parse.
Definition: XmlParser.cc:152
void pop_element()
Pops element from element stack.
Definition: XmlParser.cc:201
void push_element(const XML_Char *name)
Push element onto element stack.
Definition: XmlParser.cc:194
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...
Definition: XmlParser.cc:227
virtual ~XmlParser()
Destructor.
Definition: XmlParser.cc:57
bool close_element(const XML_Char *name)
Checks for and performs sub parse.
Definition: XmlParser.cc:169
int m_length
Length of data at m_base to be parsed.
Definition: XmlParser.h:212
XmlParser(const char *base, int len)
Constructor.
Definition: XmlParser.cc:43
static int64_t content_to_i64(const std::string &name, const std::string &content)
Helper function to convert content to an int64_t value.
Definition: XmlParser.cc:90
Hypertable definitions
void add_text(const XML_Char *s, int len)
Collects text.
Definition: XmlParser.cc:190
int m_sub_parse_toplevel
Toplevel element of current sub parse.
Definition: XmlParser.h:311
static int16_t content_to_i16(const std::string &name, const std::string &content)
Helper function to convert content to an int16_t value.
Definition: XmlParser.cc:114
const std::string collected_text()
Returns collected text.
Definition: XmlParser.h:269
std::vector< std::string > m_sub_parsers
List of element names for which there is a sub-parser.
Definition: XmlParser.h:305
Base class for XML document parsers.
Definition: XmlParser.h:94
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.
Definition: XmlParser.cc:137
virtual void start_element(const XML_Char *name, const XML_Char **atts)
Start element callback member function.
Definition: XmlParser.h:128
XML_Parser m_parser
eXpat parser
Definition: XmlParser.h:206
static void start_element_handler(void *userdata, const XML_Char *name, const XML_Char **atts)
eXpat start element handler.
Definition: XmlParser.cc:208
virtual void sub_parse(const XML_Char *name, const char *base, int len)
Performs a sub-parse.
Definition: XmlParser.h:149
std::string m_current_element
Current element being parsed.
Definition: XmlParser.h:308