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
18namespace orcus {
19
20class tokens;
21
22namespace sax {
23
24#if ORCUS_DEBUG_SAX_PARSER
25template<typename _Attr, typename _Tokens>
26class attr_printer : public ::std::unary_function<_Attr, void>
27{
28public:
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 }
40private:
41 const _Tokens& m_tokens;
42 ::std::string m_indent;
43};
44#endif
45
46}
47
48class ORCUS_PSR_DLLPUBLIC sax_token_handler_wrapper_base
49{
50protected:
51 xml_declaration_t m_declaration;
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
58public:
60
61 void attribute(const pstring& name, const pstring& val);
62 void attribute(const sax_ns_parser_attribute& attr);
63};
64
68template<typename _Handler>
70{
71public:
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
86private:
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
129private:
130 handler_wrapper m_wrapper;
132};
133
134template<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
142template<typename _Handler>
143sax_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
151template<typename _Handler>
152sax_token_parser<_Handler>::~sax_token_parser()
153{
154}
155
156template<typename _Handler>
157void sax_token_parser<_Handler>::parse()
158{
159 m_parser.parse();
160}
161
162}
163
164#endif
165/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: pstring.hpp:28
Definition: sax_ns_parser.hpp:95
Definition: sax_token_parser.hpp:49
Definition: sax_token_parser.hpp:70
Definition: tokens.hpp:22
Definition: xml_namespace.hpp:83
Definition: sax_parser_base.hpp:46
Definition: sax_ns_parser.hpp:32
Definition: sax_ns_parser.hpp:23
Definition: types.hpp:354
Definition: types.hpp:72