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