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 <vector>
12 #include <algorithm>
13 #include <functional>
14 
15 #include "types.hpp"
16 #include "sax_ns_parser.hpp"
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 : public ::std::unary_function<_Attr, void>
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 
53 {
54  xmlns_id_t ns;
55  xml_token_t name;
56  pstring raw_name;
57  std::vector<xml_token_attr_t> attrs;
58 };
59 
60 class ORCUS_PSR_DLLPUBLIC sax_token_handler_wrapper_base
61 {
62 protected:
64  const tokens& m_tokens;
65 
66  xml_token_t tokenize(const pstring& name) const;
67  void set_element(const sax_ns_parser_element& elem);
68 
69 public:
70  sax_token_handler_wrapper_base(const tokens& _tokens);
71 
72  void attribute(const pstring& name, const pstring& val);
73  void attribute(const sax_ns_parser_attribute& attr);
74 };
75 
79 template<typename _Handler>
81 {
82 public:
83  typedef _Handler handler_type;
84 
86  const char* content, const size_t size, const tokens& _tokens,
87  xmlns_context& ns_cxt, handler_type& handler);
88 
90 
91  void parse();
92 
93 private:
94 
99  class handler_wrapper : public sax_token_handler_wrapper_base
100  {
101  handler_type& m_handler;
102 
103  public:
104  handler_wrapper(const tokens& _tokens, handler_type& handler) :
105  sax_token_handler_wrapper_base(_tokens), m_handler(handler) {}
106 
107  void doctype(const sax::doctype_declaration&) {}
108 
109  void start_declaration(const pstring&) {}
110 
111  void end_declaration(const pstring&)
112  {
113  m_elem.attrs.clear();
114  }
115 
116  void start_element(const sax_ns_parser_element& elem)
117  {
118  set_element(elem);
119  m_handler.start_element(m_elem);
120  m_elem.attrs.clear();
121  }
122 
123  void end_element(const sax_ns_parser_element& elem)
124  {
125  set_element(elem);
126  m_handler.end_element(m_elem);
127  }
128 
129  void characters(const pstring& val, bool transient)
130  {
131  m_handler.characters(val, transient);
132  }
133  };
134 
135 private:
136  handler_wrapper m_wrapper;
138 };
139 
140 template<typename _Handler>
142  const char* content, const size_t size, const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
143  m_wrapper(_tokens, handler),
144  m_parser(content, size, ns_cxt, m_wrapper)
145 {
146 }
147 
148 template<typename _Handler>
150 {
151 }
152 
153 template<typename _Handler>
155 {
156  m_parser.parse();
157 }
158 
159 }
160 
161 #endif
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: sax_token_parser.hpp:80
Definition: pstring.hpp:24
Definition: sax_token_parser.hpp:52
Definition: sax_ns_parser.hpp:31
Definition: xml_namespace.hpp:80
Definition: sax_ns_parser.hpp:22
Definition: sax_token_parser.hpp:60
Definition: tokens.hpp:21
Definition: sax_parser_base.hpp:45
Definition: base64.hpp:15