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  const char* content, const size_t size, bool transient_stream,
80  const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler);
81 
83 
84  void parse();
85 
86 private:
87 
92  class handler_wrapper : public sax_token_handler_wrapper_base
93  {
94  handler_type& m_handler;
95 
96  public:
97  handler_wrapper(const tokens& _tokens, handler_type& handler) :
98  sax_token_handler_wrapper_base(_tokens), m_handler(handler) {}
99 
100  void doctype(const sax::doctype_declaration&) {}
101 
102  void start_declaration(const pstring&) {}
103 
104  void end_declaration(const pstring&)
105  {
106  m_handler.declaration(m_declaration);
107  m_elem.attrs.clear();
108  }
109 
110  void start_element(const sax_ns_parser_element& elem)
111  {
112  set_element(elem);
113  m_handler.start_element(m_elem);
114  m_elem.attrs.clear();
115  }
116 
117  void end_element(const sax_ns_parser_element& elem)
118  {
119  set_element(elem);
120  m_handler.end_element(m_elem);
121  }
122 
123  void characters(const pstring& val, bool transient)
124  {
125  m_handler.characters(val, transient);
126  }
127  };
128 
129 private:
130  handler_wrapper m_wrapper;
132 };
133 
134 template<typename _Handler>
136  const char* content, const size_t size, const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
137  m_wrapper(_tokens, handler),
138  m_parser(content, size, ns_cxt, m_wrapper)
139 {
140 }
141 
142 template<typename _Handler>
143 sax_token_parser<_Handler>::sax_token_parser(
144  const char* content, const size_t size, bool transient_stream,
145  const tokens& _tokens, xmlns_context& ns_cxt, handler_type& handler) :
146  m_wrapper(_tokens, handler),
147  m_parser(content, size, transient_stream, ns_cxt, m_wrapper)
148 {
149 }
150 
151 template<typename _Handler>
152 sax_token_parser<_Handler>::~sax_token_parser()
153 {
154 }
155 
156 template<typename _Handler>
157 void sax_token_parser<_Handler>::parse()
158 {
159  m_parser.parse();
160 }
161 
162 }
163 
164 #endif
165 /* 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:27
orcus::xml_declaration_t
Definition: types.hpp:353
orcus::xml_token_element_t
Definition: types.hpp:71
orcus::tokens
Definition: tokens.hpp:21