Orcus
sax_token_parser.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #ifndef INCLUDED_ORCUS_SAX_TOKEN_PARSER_HPP
9 #define INCLUDED_ORCUS_SAX_TOKEN_PARSER_HPP
10 
11 #include "sax_ns_parser.hpp"
12 #include "types.hpp"
13 
14 #include <vector>
15 #include <algorithm>
16 #include <functional>
17 
18 namespace orcus {
19 
20 class tokens;
21 
22 class ORCUS_PSR_DLLPUBLIC sax_token_handler_wrapper_base
23 {
24 protected:
25  xml_declaration_t m_declaration;
26  xml_token_element_t m_elem;
27  const tokens& m_tokens;
28 
29  xml_token_t tokenize(std::string_view name) const;
30  void set_element(const sax_ns_parser_element& elem);
31 
32 public:
33  sax_token_handler_wrapper_base(const tokens& _tokens);
34 
35  void attribute(std::string_view name, std::string_view val);
36  void attribute(const sax_ns_parser_attribute& attr);
37 };
38 
40 {
41 public:
42 
49  {
50  (void)decl;
51  }
52 
60  {
61  (void)elem;
62  }
63 
71  {
72  (void)elem;
73  }
74 
89  void characters(std::string_view val, bool transient)
90  {
91  (void)val; (void)transient;
92  }
93 };
94 
107 template<typename HandlerT>
109 {
110 public:
111  typedef HandlerT handler_type;
112 
114  std::string_view content, const tokens& _tokens,
115  xmlns_context& ns_cxt, handler_type& handler);
116 
117  ~sax_token_parser() = default;
118 
119  void parse();
120 
121 private:
122 
127  class handler_wrapper : public sax_token_handler_wrapper_base
128  {
129  handler_type& m_handler;
130 
131  public:
132  handler_wrapper(const tokens& _tokens, handler_type& handler) :
133  sax_token_handler_wrapper_base(_tokens), m_handler(handler) {}
134 
135  void doctype(const sax::doctype_declaration&) {}
136 
137  void start_declaration(std::string_view) {}
138 
139  void end_declaration(std::string_view)
140  {
141  m_handler.declaration(m_declaration);
142  m_elem.attrs.clear();
143  }
144 
145  void start_element(const sax_ns_parser_element& elem)
146  {
147  set_element(elem);
148  m_handler.start_element(m_elem);
149  m_elem.attrs.clear();
150  }
151 
152  void end_element(const sax_ns_parser_element& elem)
153  {
154  set_element(elem);
155  m_handler.end_element(m_elem);
156  }
157 
158  void characters(std::string_view val, bool transient)
159  {
160  m_handler.characters(val, transient);
161  }
162  };
163 
164 private:
165  handler_wrapper m_wrapper;
167 };
168 
169 template<typename HandlerT>
171  std::string_view content, const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
172  m_wrapper(_tokens, handler),
173  m_parser(content, ns_cxt, m_wrapper)
174 {
175 }
176 
177 template<typename HandlerT>
178 void sax_token_parser<HandlerT>::parse()
179 {
180  m_parser.parse();
181 }
182 
183 } // namespace orcus
184 
185 #endif
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: sax_token_parser.hpp:23
Definition: sax_token_parser.hpp:40
void declaration(const orcus::xml_declaration_t &decl)
Definition: sax_token_parser.hpp:48
void start_element(const orcus::xml_token_element_t &elem)
Definition: sax_token_parser.hpp:59
void end_element(const orcus::xml_token_element_t &elem)
Definition: sax_token_parser.hpp:70
void characters(std::string_view val, bool transient)
Definition: sax_token_parser.hpp:89
Definition: sax_token_parser.hpp:109
Definition: tokens.hpp:30
Definition: xml_namespace.hpp:100
Definition: sax_parser_base.hpp:37
Definition: sax_ns_parser.hpp:35
Definition: sax_ns_parser.hpp:21
Definition: types.hpp:434
Definition: types.hpp:148