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 namespace sax {
23 
24 #if ORCUS_DEBUG_SAX_PARSER
25 template<typename _Attr, typename _Tokens>
26 class attr_printer
27 {
28 public:
29  attr_printer(const _Tokens& tokens, const ::std::string& indent) :
30  m_tokens(tokens), m_indent(indent) {}
31 
32  void operator() (const _Attr& attr) const
33  {
34  using namespace std;
35  cout << m_indent << " attribute: "
36  << attr.ns << ":"
37  << m_tokens.get_token_name(attr.name) << "=\""
38  << attr.value.str() << "\"" << endl;
39  }
40 private:
41  const _Tokens& m_tokens;
42  ::std::string m_indent;
43 };
44 #endif
45 
46 }
47 
48 class ORCUS_PSR_DLLPUBLIC sax_token_handler_wrapper_base
49 {
50 protected:
51  xml_declaration_t m_declaration;
52  xml_token_element_t m_elem;
53  const tokens& m_tokens;
54 
55  xml_token_t tokenize(std::string_view name) const;
56  void set_element(const sax_ns_parser_element& elem);
57 
58 public:
59  sax_token_handler_wrapper_base(const tokens& _tokens);
60 
61  void attribute(std::string_view name, std::string_view val);
62  void attribute(const sax_ns_parser_attribute& attr);
63 };
64 
66 {
67 public:
68 
75  {
76  (void)decl;
77  }
78 
86  {
87  (void)elem;
88  }
89 
97  {
98  (void)elem;
99  }
100 
115  void characters(std::string_view val, bool transient)
116  {
117  (void)val; (void)transient;
118  }
119 };
120 
124 template<typename _Handler>
126 {
127 public:
128  typedef _Handler handler_type;
129 
131  const char* content, const size_t size, const tokens& _tokens,
132  xmlns_context& ns_cxt, handler_type& handler);
133 
135  const char* content, const size_t size, bool transient_stream,
136  const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler);
137 
138  ~sax_token_parser();
139 
140  void parse();
141 
142 private:
143 
148  class handler_wrapper : public sax_token_handler_wrapper_base
149  {
150  handler_type& m_handler;
151 
152  public:
153  handler_wrapper(const tokens& _tokens, handler_type& handler) :
154  sax_token_handler_wrapper_base(_tokens), m_handler(handler) {}
155 
156  void doctype(const sax::doctype_declaration&) {}
157 
158  void start_declaration(std::string_view) {}
159 
160  void end_declaration(std::string_view)
161  {
162  m_handler.declaration(m_declaration);
163  m_elem.attrs.clear();
164  }
165 
166  void start_element(const sax_ns_parser_element& elem)
167  {
168  set_element(elem);
169  m_handler.start_element(m_elem);
170  m_elem.attrs.clear();
171  }
172 
173  void end_element(const sax_ns_parser_element& elem)
174  {
175  set_element(elem);
176  m_handler.end_element(m_elem);
177  }
178 
179  void characters(std::string_view val, bool transient)
180  {
181  m_handler.characters(val, transient);
182  }
183  };
184 
185 private:
186  handler_wrapper m_wrapper;
188 };
189 
190 template<typename _Handler>
192  const char* content, const size_t size, const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
193  m_wrapper(_tokens, handler),
194  m_parser(content, size, ns_cxt, m_wrapper)
195 {
196 }
197 
198 template<typename _Handler>
199 sax_token_parser<_Handler>::sax_token_parser(
200  const char* content, const size_t size, bool transient_stream,
201  const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
202  m_wrapper(_tokens, handler),
203  m_parser(content, size, transient_stream, ns_cxt, m_wrapper)
204 {
205 }
206 
207 template<typename _Handler>
208 sax_token_parser<_Handler>::~sax_token_parser()
209 {
210 }
211 
212 template<typename _Handler>
213 void sax_token_parser<_Handler>::parse()
214 {
215  m_parser.parse();
216 }
217 
218 }
219 
220 #endif
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: sax_token_parser.hpp:49
Definition: sax_token_parser.hpp:66
void declaration(const orcus::xml_declaration_t &decl)
Definition: sax_token_parser.hpp:74
void start_element(const orcus::xml_token_element_t &elem)
Definition: sax_token_parser.hpp:85
void end_element(const orcus::xml_token_element_t &elem)
Definition: sax_token_parser.hpp:96
void characters(std::string_view val, bool transient)
Definition: sax_token_parser.hpp:115
Definition: sax_token_parser.hpp:126
Definition: tokens.hpp:19
Definition: xml_namespace.hpp:82
Definition: sax_parser_base.hpp:45
Definition: sax_ns_parser.hpp:32
Definition: sax_ns_parser.hpp:23
Definition: types.hpp:390
Definition: types.hpp:108