/usr/include/Poco/XML/XMLStreamParser.h is in libpoco-dev 1.8.0.1-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | //
// XMLStreamParser.h
//
// Library: XML
// Package: XML
// Module: XMLStreamParser
//
// Definition of the XMLStreamParser class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Based on libstudxml (http://www.codesynthesis.com/projects/libstudxml/).
// Copyright (c) 2009-2013 Code Synthesis Tools CC.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef XML_XMLStreamParser_INCLUDED
#define XML_XMLStreamParser_INCLUDED
// We only support UTF-8 expat.
#ifdef XML_UNICODE
#error UTF-16 expat (XML_UNICODE defined) is not supported
#endif
#include "Poco/XML/QName.h"
#include "Poco/XML/ValueTraits.h"
#include "Poco/XML/Content.h"
#include <Poco/XML/expat.h>
#include <map>
#include <vector>
#include <string>
#include <iosfwd>
#include <cstddef>
namespace Poco {
namespace XML {
class XML_API XMLStreamParser
/// The streaming XML pull parser and streaming XML serializer. The parser
/// is a conforming, non-validating XML 1.0 implementation (see Implementation Notes
/// for details). The application character encoding (that is, the encoding used
/// in the application's memory) for both parser and serializer is UTF-8.
/// The output encoding of the serializer is UTF-8 as well. The parser supports
/// UTF-8, UTF-16, ISO-8859-1, and US-ASCII input encodings.
///
/// Attribute map:
///
/// Attribute map lookup. If attribute is not found, then the version
/// without the default value throws an appropriate parsing exception
/// while the version with the default value returns that value.
///
/// Note also that there is no attribute(ns, name) version since it
/// would conflict with attribute(name, dv) (qualified attributes
/// are not very common).
///
/// Attribute map is valid throughout at the "element level" until
/// end_element and not just during EV_START_ELEMENT. As a special case,
/// the map is still valid after peek() that returned end_element until
/// this end_element event is retrieved with next().
///
/// Using parser:
///
/// XMLStreamParser p(ifs, argv[1]);
/// for (XMLStreamParser::EventType e: p)
/// {
/// switch (e)
/// {
/// case XMLStreamParser::EV_START_ELEMENT:
/// cerr << p.line () << ':' << p.column () << ": start " << p.name () << endl;
/// break;
/// case XMLStreamParser::EV_END_ELEMENT:
/// cerr << p.line () << ':' << p.column () << ": end " << p.name () << endl;
/// break;
/// case XMLStreamParser::EV_START_ATTRIBUTE:
/// ...
/// case XMLStreamParser::EV_END_ATTRIBUTE:
/// ...
/// case XMLStreamParser::EV_CHARACTERS:
/// ...
/// }
/// }
{
public:
enum EventType
/// Parsing events.
{
EV_START_ELEMENT,
EV_END_ELEMENT,
EV_START_ATTRIBUTE,
EV_END_ATTRIBUTE,
EV_CHARACTERS,
EV_START_NAMESPACE_DECL,
EV_END_NAMESPACE_DECL,
EV_EOF
};
typedef unsigned short FeatureType;
/// If both receive_attributes_event and RECEIVE_ATTRIBUTE_MAP are
/// specified, then RECEIVE_ATTRIBUTES_EVENT is assumed.
static const FeatureType RECEIVE_ELEMENTS = 0x0001;
static const FeatureType RECEIVE_CHARACTERS = 0x0002;
static const FeatureType RECEIVE_ATTRIBUTE_MAP = 0x0004;
static const FeatureType RECEIVE_ATTRIBUTES_EVENT = 0x0008;
static const FeatureType RECEIVE_NAMESPACE_DECLS = 0x0010;
static const FeatureType RECEIVE_DEFAULT = RECEIVE_ELEMENTS | RECEIVE_CHARACTERS | RECEIVE_ATTRIBUTE_MAP;
struct XML_API AttributeValueType
{
std::string value;
mutable bool handled;
};
typedef std::map<QName, AttributeValueType> AttributeMapType;
struct XML_API Iterator
// C++11 range-based for support. Generally, the iterator interface
// doesn't make much sense for the XMLStreamParser so for now we have an
// implementation that is just enough to the range-based for.
{
typedef EventType value_type;
Iterator(XMLStreamParser* p = 0, EventType e = EV_EOF):
_parser(p),
_e(e)
{
}
value_type operator * () const
{
return _e;
}
Iterator& operator ++ ()
{
_e = _parser->next();
return *this;
}
bool operator == (Iterator y) const
/// Comparison only makes sense when comparing to end (eof).
{
return _e == EV_EOF && y._e == EV_EOF;
}
bool operator != (Iterator y) const
/// Comparison only makes sense when comparing to end (eof).
{
return !(*this == y);
}
private:
XMLStreamParser* _parser;
EventType _e;
};
Iterator begin()
{
return Iterator(this, next());
}
Iterator end()
{
return Iterator(this, EV_EOF);
}
XMLStreamParser(std::istream&, const std::string& inputName, FeatureType = RECEIVE_DEFAULT);
/// The parser constructor takes three arguments: the stream to parse,
/// input name that is used in diagnostics to identify the document being
/// parsed, and the list of events we want the parser to report.
///
/// Parse std::istream. Input name is used in diagnostics to identify
/// the document being parsed.
///
/// If stream exceptions are enabled then std::ios_base::failure
/// exception is used to report io errors (badbit and failbit).
/// Otherwise, those are reported as the parsing exception.
XMLStreamParser(const void* data, std::size_t size, const std::string& inputName, FeatureType = RECEIVE_DEFAULT);
/// Parse memory buffer that contains the whole document. Input name
/// is used in diagnostics to identify the document being parsed.
~XMLStreamParser();
/// Destroys the XMLStreamParser.
EventType next();
/// Call the next() function when we are ready to handle the next piece of XML.
void nextExpect(EventType);
/// Get the next event and make sure that it's what's expected. If it
/// is not, then throw an appropriate parsing exception.
void nextExpect(EventType, const std::string& name);
void nextExpect(EventType, const QName& qname);
void nextExpect(EventType, const std::string& ns, const std::string& name);
EventType peek();
EventType event();
/// Return the event that was last returned by the call to next() or peek().
const std::string& inputName() const;
const QName& getQName() const;
const std::string& namespaceURI() const;
const std::string& localName() const;
const std::string& prefix() const;
std::string& value();
const std::string& value() const;
template <typename T> T value() const;
Poco::UInt64 line() const;
Poco::UInt64 column() const;
const std::string& attribute(const std::string& name) const;
template <typename T>
T attribute(const std::string& name) const;
std::string attribute(const std::string& name, const std::string& deflt) const;
template <typename T>
T attribute(const std::string& name, const T& deflt) const;
const std::string& attribute(const QName& qname) const;
template <typename T>
T attribute(const QName& qname) const;
std::string attribute(const QName& qname, const std::string& deflt) const;
template <typename T>
T attribute(const QName& qname, const T& deflt) const;
bool attributePresent(const std::string& name) const;
bool attributePresent(const QName& qname) const;
const AttributeMapType& attributeMap() const;
void content(Content);
Content content() const;
void nextExpect(EventType, const std::string& name, Content);
void nextExpect(EventType, const QName& qname, Content);
void nextExpect(EventType, const std::string& ns, const std::string& name, Content);
// Helpers for parsing elements with simple content. The first two
// functions assume that EV_START_ELEMENT has already been parsed. The
// rest parse the complete element, from start to end.
//
// Note also that as with attribute(), there is no (namespace,name)
// overload since it would conflicts with (namespace,deflt).
std::string element();
template <typename T>
T element();
std::string element(const std::string& name);
std::string element(const QName& qname);
template <typename T>
T element(const std::string& name);
template <typename T>
T element(const QName& qname);
std::string element(const std::string& name, const std::string& deflt);
std::string element(const QName& qname, const std::string& deflt);
template <typename T>
T element(const std::string& name, const T& deflt);
template <typename T>
T element(const QName& qname, const T& deflt);
private:
XMLStreamParser(const XMLStreamParser&);
XMLStreamParser& operator = (const XMLStreamParser&);
static void XMLCALL handleStartElement(void*, const XML_Char*, const XML_Char**);
static void XMLCALL handleEndElement(void*, const XML_Char*);
static void XMLCALL handleCharacters(void*, const XML_Char*, int);
static void XMLCALL handleStartNamespaceDecl(void*, const XML_Char*, const XML_Char*);
static void XMLCALL handleEndNamespaceDecl(void*, const XML_Char*);
void init();
EventType nextImpl(bool peek);
EventType nextBody();
void handleError();
// If _size is 0, then data is std::istream. Otherwise, it is a buffer.
union
{
std::istream* is;
const void* buf;
}
_data;
std::size_t _size;
const std::string _inputName;
FeatureType _feature;
XML_Parser _parser;
std::size_t _depth;
bool _accumulateContent; // Whether we are accumulating character content.
enum { state_next, state_peek } _parserState;
EventType _currentEvent;
EventType _queue;
QName _qname;
std::string _value;
const QName* _qualifiedName;
std::string* _pvalue;
Poco::UInt64 _line;
Poco::UInt64 _column;
struct AttributeType
{
QName qname;
std::string value;
};
typedef std::vector<AttributeType> attributes;
attributes _attributes;
attributes::size_type _currentAttributeIndex; // Index of the current attribute.
typedef std::vector<QName> NamespaceDecls;
NamespaceDecls _startNamespace;
NamespaceDecls::size_type _startNamespaceIndex;// Index of the current decl.
NamespaceDecls _endNamespace;
NamespaceDecls::size_type _endNamespaceIndex;// Index of the current decl.
struct ElementEntry
{
ElementEntry(std::size_t d, Content c = Content::Mixed):
depth(d),
content(c),
attributesUnhandled(0)
{
}
std::size_t depth;
Content content;
AttributeMapType attributeMap;
mutable AttributeMapType::size_type attributesUnhandled;
};
typedef std::vector<ElementEntry> ElementState;
std::vector<ElementEntry> _elementState;
const AttributeMapType _emptyAttrMap;
const ElementEntry* getElement() const;
const ElementEntry* getElementImpl() const;
void popElement();
};
XML_API std::ostream& operator << (std::ostream&, XMLStreamParser::EventType);
//
// inlines
//
inline XMLStreamParser::EventType XMLStreamParser::event()
// Return the even that was last returned by the call to next() or peek().
{
return _currentEvent;
}
inline const std::string& XMLStreamParser::inputName() const
{
return _inputName;
}
inline const QName& XMLStreamParser::getQName() const
{
return *_qualifiedName;
}
inline const std::string& XMLStreamParser::namespaceURI() const
{
return _qualifiedName->namespaceURI();
}
inline const std::string& XMLStreamParser::localName() const
{
return _qualifiedName->localName();
}
inline const std::string& XMLStreamParser::prefix() const
{
return _qualifiedName->prefix();
}
inline std::string& XMLStreamParser::value()
{
return *_pvalue;
}
inline const std::string& XMLStreamParser::value() const
{
return *_pvalue;
}
inline Poco::UInt64 XMLStreamParser::line() const
{
return _line;
}
inline Poco::UInt64 XMLStreamParser::column() const
{
return _column;
}
inline XMLStreamParser::EventType XMLStreamParser::peek()
{
if (_parserState == state_peek)
return _currentEvent;
else
{
EventType e(nextImpl(true));
_parserState = state_peek; // Set it after the call to nextImpl().
return e;
}
}
template <typename T>
inline T XMLStreamParser::value() const
{
return ValueTraits < T > ::parse(value(), *this);
}
inline const std::string& XMLStreamParser::attribute(const std::string& n) const
{
return attribute(QName(n));
}
template <typename T>
inline T XMLStreamParser::attribute(const std::string& n) const
{
return attribute < T > (QName(n));
}
inline std::string XMLStreamParser::attribute(const std::string& n, const std::string& dv) const
{
return attribute(QName(n), dv);
}
template <typename T>
inline T XMLStreamParser::attribute(const std::string& n, const T& dv) const
{
return attribute < T > (QName(n), dv);
}
template <typename T>
inline T XMLStreamParser::attribute(const QName& qn) const
{
return ValueTraits < T > ::parse(attribute(qn), *this);
}
inline bool XMLStreamParser::attributePresent(const std::string& n) const
{
return attributePresent(QName(n));
}
inline const XMLStreamParser::AttributeMapType& XMLStreamParser::attributeMap() const
{
if (const ElementEntry* e = getElement())
{
e->attributesUnhandled = 0; // Assume all handled.
return e->attributeMap;
}
return _emptyAttrMap;
}
inline void XMLStreamParser::nextExpect(EventType e, const QName& qn)
{
nextExpect(e, qn.namespaceURI(), qn.localName());
}
inline void XMLStreamParser::nextExpect(EventType e, const std::string& n)
{
nextExpect(e, std::string(), n);
}
inline void XMLStreamParser::nextExpect(EventType e, const QName& qn, Content c)
{
nextExpect(e, qn);
poco_assert(e == EV_START_ELEMENT);
content(c);
}
inline void XMLStreamParser::nextExpect(EventType e, const std::string& n, Content c)
{
nextExpect(e, std::string(), n);
poco_assert(e == EV_START_ELEMENT);
content(c);
}
inline void XMLStreamParser::nextExpect(EventType e, const std::string& ns, const std::string& n, Content c)
{
nextExpect(e, ns, n);
poco_assert(e == EV_START_ELEMENT);
content(c);
}
template <typename T>
inline T XMLStreamParser::element()
{
return ValueTraits < T > ::parse(element(), *this);
}
inline std::string XMLStreamParser::element(const std::string& n)
{
nextExpect(EV_START_ELEMENT, n);
return element();
}
inline std::string XMLStreamParser::element(const QName& qn)
{
nextExpect(EV_START_ELEMENT, qn);
return element();
}
template <typename T>
inline T XMLStreamParser::element(const std::string& n)
{
return ValueTraits < T > ::parse(element(n), *this);
}
template <typename T>
inline T XMLStreamParser::element(const QName& qn)
{
return ValueTraits < T > ::parse(element(qn), *this);
}
inline std::string XMLStreamParser::element(const std::string& n, const std::string& dv)
{
return element(QName(n), dv);
}
template <typename T>
inline T XMLStreamParser::element(const std::string& n, const T& dv)
{
return element < T > (QName(n), dv);
}
inline void XMLStreamParser::content(Content c)
{
poco_assert(_parserState == state_next);
if (!_elementState.empty() && _elementState.back().depth == _depth)
_elementState.back().content = c;
else
_elementState.push_back(ElementEntry(_depth, c));
}
inline Content XMLStreamParser::content() const
{
poco_assert(_parserState == state_next);
return !_elementState.empty() && _elementState.back().depth == _depth ? _elementState.back().content : Content(Content::Mixed);
}
inline const XMLStreamParser::ElementEntry* XMLStreamParser::getElement() const
{
return _elementState.empty() ? 0 : getElementImpl();
}
template <typename T>
T XMLStreamParser::attribute(const QName& qn, const T& dv) const
{
if (const ElementEntry* e = getElement())
{
AttributeMapType::const_iterator i(e->attributeMap.find(qn));
if (i != e->attributeMap.end())
{
if (!i->second.handled)
{
i->second.handled = true;
e->attributesUnhandled--;
}
return ValueTraits < T > ::parse(i->second.value, *this);
}
}
return dv;
}
template <typename T>
T XMLStreamParser::element(const QName& qn, const T& dv)
{
if (peek() == EV_START_ELEMENT && getQName() == qn)
{
next();
return element<T>();
}
return dv;
}
} } // namespace Poco::XML
#endif // XML_XMLStreamParser_INCLUDED
|