Orcus
sax_parser_base.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_PARSER_BASE_HPP
9 #define INCLUDED_ORCUS_SAX_PARSER_BASE_HPP
10 
11 #include "env.hpp"
12 #include "cell_buffer.hpp"
13 #include "parser_global.hpp"
14 #include "parser_base.hpp"
15 
16 #include <cassert>
17 #include <cstdlib>
18 #include <exception>
19 #include <sstream>
20 #include <memory>
21 
22 #define ORCUS_DEBUG_SAX_PARSER 0
23 
24 #if ORCUS_DEBUG_SAX_PARSER
25 #include <iostream>
26 using std::cout;
27 using std::endl;
28 #endif
29 
30 namespace orcus { namespace sax {
31 
37 {
38  enum class keyword_type { dtd_public, dtd_private };
39 
40  keyword_type keyword;
41  std::string_view root_element;
42  std::string_view fpi;
43  std::string_view uri;
44 };
45 
57 ORCUS_PSR_DLLPUBLIC char decode_xml_encoded_char(const char* p, size_t n);
58 
70 ORCUS_PSR_DLLPUBLIC std::string decode_xml_unicode_char(const char* p, size_t n);
71 
77 {
79  std::string_view ns;
81  std::string_view name;
83  std::ptrdiff_t begin_pos;
85  std::ptrdiff_t end_pos;
86 };
87 
96 {
98  std::string_view ns;
100  std::string_view name;
102  std::string_view value;
104  bool transient;
105 };
106 
107 class ORCUS_PSR_DLLPUBLIC parser_base : public ::orcus::parser_base
108 {
109  struct impl;
110  std::unique_ptr<impl> mp_impl;
111 
112  parser_base() = delete;
113  parser_base(const parser_base&) = delete;
114  parser_base& operator=(const parser_base&) = delete;
115 protected:
116  size_t m_nest_level;
117  size_t m_buffer_pos;
118  bool m_root_elem_open:1;
119 
120 protected:
121  parser_base(const char* content, size_t size);
122  ~parser_base();
123 
124  void next_check()
125  {
126  next();
127  if (!has_char())
128  throw malformed_xml_error("xml stream ended prematurely.", offset());
129  }
130 
131  void nest_up() { ++m_nest_level; }
132  void nest_down()
133  {
134  if (m_nest_level == 0)
135  throw malformed_xml_error("incorrect nesting in xml stream", offset());
136 
137  --m_nest_level;
138  }
139 
140  void inc_buffer_pos();
141  void reset_buffer_pos() { m_buffer_pos = 0; }
142 
143  void has_char_throw(const char* msg) const
144  {
145  if (!has_char())
146  throw malformed_xml_error(msg, offset());
147  }
148 
149  char cur_char_checked() const
150  {
151  if (!has_char())
152  throw malformed_xml_error("xml stream ended prematurely.", offset());
153 
154  return *mp_char;
155  }
156 
157  char next_and_char()
158  {
159  next();
160 #if ORCUS_DEBUG_SAX_PARSER
161  if (mp_char >= mp_end)
162  throw malformed_xml_error("xml stream ended prematurely.", offset());
163 #endif
164  return *mp_char;
165  }
166 
167  char next_char_checked()
168  {
169  next();
170  if (!has_char())
171  throw malformed_xml_error("xml stream ended prematurely.", offset());
172 
173  return *mp_char;
174  }
175 
176  cell_buffer& get_cell_buffer();
177 
178  void comment();
179 
180  void expects_next(const char* p, size_t n);
181 
182  void parse_encoded_char(cell_buffer& buf);
183  void value_with_encoded_char(cell_buffer& buf, std::string_view& str, char quote_char);
184 
196  bool value(std::string_view& str, bool decode);
197 
198  void name(std::string_view& str);
199  void element_name(parser_element& elem, std::ptrdiff_t begin_pos);
200  void attribute_name(std::string_view& attr_ns, std::string_view& attr_name);
201  void characters_with_encoded_char(cell_buffer& buf);
202 };
203 
204 }}
205 
206 #endif
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: cell_buffer.hpp:22
Definition: exception.hpp:121
Definition: parser_base.hpp:23
Definition: sax_parser_base.hpp:108
bool value(std::string_view &str, bool decode)
Definition: sax_parser_base.hpp:37
Definition: sax_parser_base.hpp:96
std::string_view name
Definition: sax_parser_base.hpp:100
std::string_view ns
Definition: sax_parser_base.hpp:98
std::string_view value
Definition: sax_parser_base.hpp:102
Definition: sax_parser_base.hpp:77
std::string_view name
Definition: sax_parser_base.hpp:81
std::ptrdiff_t begin_pos
Definition: sax_parser_base.hpp:83
std::ptrdiff_t end_pos
Definition: sax_parser_base.hpp:85
std::string_view ns
Definition: sax_parser_base.hpp:79