1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_ORCUS_THREADED_SAX_TOKEN_PARSER_HPP
#define INCLUDED_ORCUS_THREADED_SAX_TOKEN_PARSER_HPP
#include "tokens.hpp"
#include "xml_namespace.hpp"
#include "sax_token_parser_thread.hpp"
#include "sax_parser_base.hpp"
#include "exception.hpp"
#include "detail/thread.hpp"
#include <thread>
namespace orcus {
class xmlns_context;
class string_pool;
template<typename _Handler>
class threaded_sax_token_parser
{
public:
typedef _Handler handler_type;
/**
* Constructor.
*
* @param p pointer to a string stream containing XML content.
* @param n size of the stream.
* @param tks XML token map instance.
* @param ns_cxt namespace context instance.
* @param hdl handler class instance.
* @param min_token_size minimum size of the internal token buffer.
*/
threaded_sax_token_parser(
const char* p, size_t n, const tokens& tks, xmlns_context& ns_cxt,
handler_type& hdl, size_t min_token_size);
/**
* Constructor.
*
* @param p pointer to a string stream containing XML content.
* @param n size of the stream.
* @param tks XML token map instance.
* @param ns_cxt namespace context instance.
* @param hdl handler class instance.
* @param min_token_size minimum size of the internal token buffer.
* @param max_token_size maximum size of the internal token buffer.
*/
threaded_sax_token_parser(
const char* p, size_t n, const tokens& tks, xmlns_context& ns_cxt,
handler_type& hdl, size_t min_token_size, size_t max_token_size);
/**
* Call this method to start parsing.
*/
void parse();
void swap_string_pool(string_pool& pool);
private:
void thread_parse();
void process_tokens(const sax::parse_tokens_t& tokens);
private:
sax::parser_thread m_parser_thread;
handler_type& m_handler;
};
template<typename _Handler>
threaded_sax_token_parser<_Handler>::threaded_sax_token_parser(
const char* p, size_t n, const tokens& tks, xmlns_context& ns_cxt,
handler_type& hdl, size_t min_token_size) :
m_parser_thread(p, n, tks, ns_cxt, min_token_size), m_handler(hdl) {}
template<typename _Handler>
threaded_sax_token_parser<_Handler>::threaded_sax_token_parser(
const char* p, size_t n, const tokens& tks, xmlns_context& ns_cxt, handler_type& hdl,
size_t min_token_size, size_t max_token_size) :
m_parser_thread(p, n, tks, ns_cxt, min_token_size, max_token_size), m_handler(hdl) {}
template<typename _Handler>
void threaded_sax_token_parser<_Handler>::parse()
{
std::thread t(&threaded_sax_token_parser::thread_parse, this);
detail::thread::scoped_guard guard(std::move(t));
sax::parse_tokens_t tokens;
try
{
while (m_parser_thread.next_tokens(tokens))
process_tokens(tokens);
process_tokens(tokens);
}
catch (const std::exception&)
{
m_parser_thread.abort();
throw;
}
}
template<typename _Handler>
void threaded_sax_token_parser<_Handler>::swap_string_pool(string_pool& pool)
{
m_parser_thread.swap_string_pool(pool);
}
template<typename _Handler>
void threaded_sax_token_parser<_Handler>::thread_parse()
{
// Start parsing.
m_parser_thread.start();
}
template<typename _Handler>
void threaded_sax_token_parser<_Handler>::process_tokens(const sax::parse_tokens_t& tks)
{
for (const sax::parse_token& t : tks)
{
switch (t.type)
{
case sax::parse_token_t::start_element:
{
const auto* elem = std::get<const xml_token_element_t*>(t.value);
m_handler.start_element(*elem);
break;
}
case sax::parse_token_t::end_element:
{
const auto* elem = std::get<const xml_token_element_t*>(t.value);
m_handler.end_element(*elem);
break;
}
case sax::parse_token_t::characters:
{
auto s = std::get<std::string_view>(t.value);
m_handler.characters(s, false);
break;
}
case sax::parse_token_t::parse_error:
{
auto v = std::get<parse_error_value_t>(t.value);
throw malformed_xml_error(std::string{v.str}, v.offset);
}
default:
throw general_error("unknown token type encountered.");
}
}
}
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|