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 
48 class ORCUS_PSR_DLLPUBLIC sax_token_handler_wrapper_base
49 {
50 protected:
51  xml_token_element_t m_elem;
52  const tokens& m_tokens;
53 
54  xml_token_t tokenize(const pstring& name) const;
55  void set_element(const sax_ns_parser_element& elem);
56 
57 public:
58  sax_token_handler_wrapper_base(const tokens& _tokens);
59 
60  void attribute(const pstring& name, const pstring& val);
61  void attribute(const sax_ns_parser_attribute& attr);
62 };
63 
67 template<typename _Handler>
69 {
70 public:
71  typedef _Handler handler_type;
72 
74  const char* content, const size_t size, const tokens& _tokens,
75  xmlns_context& ns_cxt, handler_type& handler);
76 
78 
79  void parse();
80 
81 private:
82 
87  class handler_wrapper : public sax_token_handler_wrapper_base
88  {
89  handler_type& m_handler;
90 
91  public:
92  handler_wrapper(const tokens& _tokens, handler_type& handler) :
93  sax_token_handler_wrapper_base(_tokens), m_handler(handler) {}
94 
95  void doctype(const sax::doctype_declaration&) {}
96 
97  void start_declaration(const pstring&) {}
98 
99  void end_declaration(const pstring&)
100  {
101  m_elem.attrs.clear();
102  }
103 
104  void start_element(const sax_ns_parser_element& elem)
105  {
106  set_element(elem);
107  m_handler.start_element(m_elem);
108  m_elem.attrs.clear();
109  }
110 
111  void end_element(const sax_ns_parser_element& elem)
112  {
113  set_element(elem);
114  m_handler.end_element(m_elem);
115  }
116 
117  void characters(const pstring& val, bool transient)
118  {
119  m_handler.characters(val, transient);
120  }
121  };
122 
123 private:
124  handler_wrapper m_wrapper;
126 };
127 
128 template<typename _Handler>
130  const char* content, const size_t size, const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
131  m_wrapper(_tokens, handler),
132  m_parser(content, size, ns_cxt, m_wrapper)
133 {
134 }
135 
136 template<typename _Handler>
138 {
139 }
140 
141 template<typename _Handler>
143 {
144  m_parser.parse();
145 }
146 
147 }
148 
149 #endif
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: sax_token_parser.hpp:68
Definition: pstring.hpp:24
Definition: sax_ns_parser.hpp:31
Definition: xml_namespace.hpp:80
Definition: types.hpp:67
Definition: sax_ns_parser.hpp:22
Definition: sax_token_parser.hpp:48
Definition: tokens.hpp:21
Definition: sax_parser_base.hpp:45
Definition: base64.hpp:15