| 1 |
// file : cutl/xml/parser.hxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#ifndef CUTL_XML_PARSER_HXX |
| 6 |
#define CUTL_XML_PARSER_HXX |
| 7 |
|
| 8 |
#include <map> |
| 9 |
#include <vector> |
| 10 |
#include <string> |
| 11 |
#include <iosfwd> |
| 12 |
#include <cstddef> // std::size_t |
| 13 |
#include <cassert> |
| 14 |
|
| 15 |
#include <cutl/details/config.hxx> // LIBCUTL_EXTERNAL_EXPAT |
| 16 |
|
| 17 |
#ifndef LIBCUTL_EXTERNAL_EXPAT |
| 18 |
# include <cutl/details/expat/expat.h> |
| 19 |
#else |
| 20 |
# include <expat.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
// We only support UTF-8 expat. |
| 24 |
// |
| 25 |
#ifdef XML_UNICODE |
| 26 |
# error UTF-16 expat (XML_UNICODE defined) is not supported |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <cutl/xml/qname.hxx> |
| 30 |
#include <cutl/xml/exception.hxx> |
| 31 |
|
| 32 |
#include <cutl/details/export.hxx> |
| 33 |
|
| 34 |
namespace cutl |
| 35 |
{ |
| 36 |
namespace xml |
| 37 |
{ |
| 38 |
class parser; |
| 39 |
|
| 40 |
struct LIBCUTL_EXPORT parsing: exception |
| 41 |
{ |
| 42 |
virtual |
| 43 |
~parsing () throw (); |
| 44 |
|
| 45 |
parsing (const std::string& name, |
| 46 |
unsigned long long line, |
| 47 |
unsigned long long column, |
| 48 |
const std::string& description); |
| 49 |
|
| 50 |
parsing (const parser&, const std::string& description); |
| 51 |
|
| 52 |
const std::string& |
| 53 |
name () const {return name_;} |
| 54 |
|
| 55 |
unsigned long long |
| 56 |
line () const {return line_;} |
| 57 |
|
| 58 |
unsigned long long |
| 59 |
column () const {return column_;} |
| 60 |
|
| 61 |
const std::string& |
| 62 |
description () const {return description_;} |
| 63 |
|
| 64 |
virtual const char* |
| 65 |
what () const throw (); |
| 66 |
|
| 67 |
private: |
| 68 |
void |
| 69 |
init (); |
| 70 |
|
| 71 |
private: |
| 72 |
std::string name_; |
| 73 |
unsigned long long line_; |
| 74 |
unsigned long long column_; |
| 75 |
std::string description_; |
| 76 |
std::string what_; |
| 77 |
}; |
| 78 |
|
| 79 |
class LIBCUTL_EXPORT parser |
| 80 |
{ |
| 81 |
public: |
| 82 |
~parser (); |
| 83 |
|
| 84 |
typedef xml::qname qname_type; |
| 85 |
typedef unsigned short feature_type; |
| 86 |
|
| 87 |
// If both receive_attributes_event and receive_attributes_map are |
| 88 |
// specified, then receive_attributes_event is assumed. |
| 89 |
// |
| 90 |
static const feature_type receive_elements = 0x0001; |
| 91 |
static const feature_type receive_characters = 0x0002; |
| 92 |
static const feature_type receive_attributes_map = 0x0004; |
| 93 |
static const feature_type receive_attributes_event = 0x0008; |
| 94 |
static const feature_type receive_namespace_decls = 0x0010; |
| 95 |
|
| 96 |
static const feature_type receive_default = receive_elements | |
| 97 |
receive_characters | |
| 98 |
receive_attributes_map; |
| 99 |
|
| 100 |
// Parse std::istream. Input name is used in diagnostics to identify |
| 101 |
// the document being parsed. std::ios_base::failure exception is |
| 102 |
// used to report io errors (badbit and failbit). |
| 103 |
// |
| 104 |
parser (std::istream&, |
| 105 |
const std::string& input_name, |
| 106 |
feature_type = receive_default); |
| 107 |
|
| 108 |
const std::string& |
| 109 |
input_name () const {return iname_;} |
| 110 |
|
| 111 |
// Parsing events. |
| 112 |
// |
| 113 |
public: |
| 114 |
enum event_type |
| 115 |
{ |
| 116 |
// If adding new events, also update the stream insertion operator. |
| 117 |
// |
| 118 |
start_element, |
| 119 |
end_element, |
| 120 |
start_attribute, |
| 121 |
end_attribute, |
| 122 |
characters, |
| 123 |
start_namespace_decl, |
| 124 |
end_namespace_decl, |
| 125 |
eof |
| 126 |
}; |
| 127 |
|
| 128 |
event_type |
| 129 |
next () |
| 130 |
{ |
| 131 |
if (state_ == state_next) |
| 132 |
return next_ (false); |
| 133 |
else |
| 134 |
{ |
| 135 |
// If we previously peeked at start/end_element, then adjust |
| 136 |
// state accordingly. |
| 137 |
// |
| 138 |
switch (event_) |
| 139 |
{ |
| 140 |
case end_element: |
| 141 |
{ |
| 142 |
if (!element_state_.empty () && |
| 143 |
element_state_.back ().depth == depth_) |
| 144 |
pop_element (); |
| 145 |
|
| 146 |
depth_--; |
| 147 |
break; |
| 148 |
} |
| 149 |
case start_element: |
| 150 |
{ |
| 151 |
depth_++; |
| 152 |
break; |
| 153 |
} |
| 154 |
default: |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
state_ = state_next; |
| 159 |
return event_; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// Get the next event and make sure that it's what's expected. If it |
| 164 |
// is not, then throw an appropriate parsing exception. |
| 165 |
// |
| 166 |
void |
| 167 |
next_expect (event_type); |
| 168 |
|
| 169 |
void |
| 170 |
next_expect (event_type, const qname_type& qname); |
| 171 |
|
| 172 |
void |
| 173 |
next_expect (event_type, const std::string& name); |
| 174 |
|
| 175 |
void |
| 176 |
next_expect (event_type, const std::string& ns, const std::string& name); |
| 177 |
|
| 178 |
event_type |
| 179 |
peek () |
| 180 |
{ |
| 181 |
if (state_ == state_peek) |
| 182 |
return event_; |
| 183 |
else |
| 184 |
{ |
| 185 |
event_type e (next_ (true)); |
| 186 |
state_ = state_peek; // Set it after the call to next_(). |
| 187 |
return e; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Return the even that was last returned by the call to next() or |
| 192 |
// peek(). |
| 193 |
// |
| 194 |
event_type |
| 195 |
event () {return event_;} |
| 196 |
|
| 197 |
// Event data. |
| 198 |
// |
| 199 |
public: |
| 200 |
const qname_type& qname () const {return *pqname_;} |
| 201 |
|
| 202 |
const std::string& namespace_ () const {return pqname_->namespace_ ();} |
| 203 |
const std::string& name () const {return pqname_->name ();} |
| 204 |
const std::string& prefix () const {return pqname_->prefix ();} |
| 205 |
|
| 206 |
const std::string& value () const {return *pvalue_;} |
| 207 |
|
| 208 |
unsigned long long line () const {return line_;} |
| 209 |
unsigned long long column () const {return column_;} |
| 210 |
|
| 211 |
// Attribute map lookup. If attribute is not found, then the version |
| 212 |
// without the default value throws an appropriate parsing exception |
| 213 |
// while the version with the default value returns that value. |
| 214 |
// |
| 215 |
// Note also that there is no attribute(ns,name) version since it |
| 216 |
// would conflict with attribute(name,dv) (qualified attributes |
| 217 |
// are not very common). |
| 218 |
// |
| 219 |
// Attribute map is valid throughout at the "element level" until |
| 220 |
// end_element and not just during start_element. As a special case, |
| 221 |
// the map is still valid after peek() that returned end_element until |
| 222 |
// this end_element event is retrieved with next(). |
| 223 |
// |
| 224 |
const std::string& |
| 225 |
attribute (const std::string& name) const; |
| 226 |
|
| 227 |
template <typename T> |
| 228 |
T |
| 229 |
attribute (const std::string& name) const; |
| 230 |
|
| 231 |
std::string |
| 232 |
attribute (const std::string& name, const std::string& dv) const; |
| 233 |
|
| 234 |
template <typename T> |
| 235 |
T |
| 236 |
attribute (const std::string& name, const T& dv) const; |
| 237 |
|
| 238 |
const std::string& |
| 239 |
attribute (const qname_type& qname) const; |
| 240 |
|
| 241 |
template <typename T> |
| 242 |
T |
| 243 |
attribute (const qname_type& qname) const; |
| 244 |
|
| 245 |
std::string |
| 246 |
attribute (const qname_type& qname, const std::string& dv) const; |
| 247 |
|
| 248 |
template <typename T> |
| 249 |
T |
| 250 |
attribute (const qname_type& qname, const T& dv) const; |
| 251 |
|
| 252 |
bool |
| 253 |
attribute_present (const std::string& name) const; |
| 254 |
|
| 255 |
bool |
| 256 |
attribute_present (const qname_type& qname) const; |
| 257 |
|
| 258 |
// Low-level attribute map access. Note that this API assumes |
| 259 |
// all attributes are handled. |
| 260 |
// |
| 261 |
struct attribute_value_type |
| 262 |
{ |
| 263 |
std::string value; |
| 264 |
mutable bool handled; |
| 265 |
}; |
| 266 |
|
| 267 |
typedef std::map<qname_type, attribute_value_type> attribute_map_type; |
| 268 |
|
| 269 |
const attribute_map_type& |
| 270 |
attribute_map () const; |
| 271 |
|
| 272 |
// Optional content processing. |
| 273 |
// |
| 274 |
public: |
| 275 |
enum content_type |
| 276 |
{ |
| 277 |
// element characters whitespaces |
| 278 |
empty, // no no ignored |
| 279 |
simple, // no yes preserved |
| 280 |
complex, // yes no ignored |
| 281 |
mixed // yes yes preserved |
| 282 |
}; |
| 283 |
|
| 284 |
// Note that you cannot get/set content while peeking. |
| 285 |
// |
| 286 |
void |
| 287 |
content (content_type c) |
| 288 |
{ |
| 289 |
assert (state_ == state_next); |
| 290 |
|
| 291 |
if (!element_state_.empty () && element_state_.back ().depth == depth_) |
| 292 |
element_state_.back ().content = c; |
| 293 |
else |
| 294 |
element_state_.push_back (element_entry (depth_, c)); |
| 295 |
} |
| 296 |
|
| 297 |
content_type |
| 298 |
content () const |
| 299 |
{ |
| 300 |
assert (state_ == state_next); |
| 301 |
|
| 302 |
return |
| 303 |
!element_state_.empty () && element_state_.back ().depth == depth_ |
| 304 |
? element_state_.back ().content |
| 305 |
: mixed; |
| 306 |
} |
| 307 |
|
| 308 |
private: |
| 309 |
static void XMLCALL |
| 310 |
start_element_ (void*, const XML_Char*, const XML_Char**); |
| 311 |
|
| 312 |
static void XMLCALL |
| 313 |
end_element_ (void*, const XML_Char*); |
| 314 |
|
| 315 |
static void XMLCALL |
| 316 |
characters_ (void*, const XML_Char*, int); |
| 317 |
|
| 318 |
static void XMLCALL |
| 319 |
start_namespace_decl_ (void*, const XML_Char*, const XML_Char*); |
| 320 |
|
| 321 |
static void XMLCALL |
| 322 |
end_namespace_decl_ (void*, const XML_Char*); |
| 323 |
|
| 324 |
private: |
| 325 |
event_type |
| 326 |
next_ (bool peek); |
| 327 |
|
| 328 |
event_type |
| 329 |
next_body (); |
| 330 |
|
| 331 |
void |
| 332 |
handle_error (); |
| 333 |
|
| 334 |
private: |
| 335 |
std::istream& is_; |
| 336 |
const std::string iname_; |
| 337 |
feature_type feature_; |
| 338 |
|
| 339 |
XML_Parser p_; |
| 340 |
std::size_t depth_; |
| 341 |
enum {state_next, state_peek} state_; |
| 342 |
event_type event_; |
| 343 |
event_type queue_; |
| 344 |
|
| 345 |
qname_type qname_; |
| 346 |
std::string value_; |
| 347 |
|
| 348 |
// These are used to avoid copying when we are handling attributes |
| 349 |
// and namespace decls. |
| 350 |
// |
| 351 |
const qname_type* pqname_; |
| 352 |
const std::string* pvalue_; |
| 353 |
|
| 354 |
unsigned long long line_; |
| 355 |
unsigned long long column_; |
| 356 |
|
| 357 |
// Attributes as events. |
| 358 |
// |
| 359 |
struct attribute_type |
| 360 |
{ |
| 361 |
qname_type qname; |
| 362 |
std::string value; |
| 363 |
}; |
| 364 |
|
| 365 |
typedef std::vector<attribute_type> attributes; |
| 366 |
|
| 367 |
attributes attr_; |
| 368 |
attributes::size_type attr_i_; // Index of the current attribute. |
| 369 |
|
| 370 |
// Namespace declarations. |
| 371 |
// |
| 372 |
typedef std::vector<qname_type> namespace_decls; |
| 373 |
|
| 374 |
namespace_decls start_ns_; |
| 375 |
namespace_decls::size_type start_ns_i_; // Index of the current decl. |
| 376 |
|
| 377 |
namespace_decls end_ns_; |
| 378 |
namespace_decls::size_type end_ns_i_; // Index of the current decl. |
| 379 |
|
| 380 |
// Element state consisting of the content model and attribute map. |
| 381 |
// |
| 382 |
struct element_entry |
| 383 |
{ |
| 384 |
element_entry (std::size_t d, content_type c = mixed) |
| 385 |
: depth (d), content (c), attr_unhandled_ (0) {} |
| 386 |
|
| 387 |
std::size_t depth; |
| 388 |
content_type content; |
| 389 |
attribute_map_type attr_map_; |
| 390 |
mutable attribute_map_type::size_type attr_unhandled_; |
| 391 |
}; |
| 392 |
|
| 393 |
typedef std::vector<element_entry> element_state; |
| 394 |
std::vector<element_entry> element_state_; |
| 395 |
|
| 396 |
// Empty attribute map to return when an element has no attributes. |
| 397 |
// |
| 398 |
const attribute_map_type empty_attr_map_; |
| 399 |
|
| 400 |
// Return the element entry corresponding to the current depth, if |
| 401 |
// exists, and NULL otherwise. |
| 402 |
// |
| 403 |
const element_entry* |
| 404 |
get_element () const; |
| 405 |
|
| 406 |
void |
| 407 |
pop_element (); |
| 408 |
}; |
| 409 |
|
| 410 |
LIBCUTL_EXPORT |
| 411 |
std::ostream& |
| 412 |
operator<< (std::ostream&, parser::event_type); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
#include <cutl/xml/parser.ixx> |
| 417 |
#include <cutl/xml/parser.txx> |
| 418 |
|
| 419 |
#endif // CUTL_XML_PARSER_HXX |