mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-16 11:48:54 +01:00
more style fixes
This commit is contained in:
parent
733c466657
commit
9feb6ea987
@ -63,11 +63,9 @@ public:
|
||||
std::string& prefix();
|
||||
/// Returns the namespace prefix of the name.
|
||||
|
||||
std::string string() const;
|
||||
std::string toString() const;
|
||||
/// Returns a printable representation in the [<namespace>#]<name> form.
|
||||
|
||||
// Note that comparison operators
|
||||
//
|
||||
public:
|
||||
friend bool operator < (const QName& x, const QName& y)
|
||||
{
|
||||
@ -130,7 +128,7 @@ inline std::string& QName::prefix()
|
||||
}
|
||||
|
||||
|
||||
XML_API std::ostream& operator<<(std::ostream&, const QName&);
|
||||
XML_API std::ostream& operator << (std::ostream&, const QName&);
|
||||
|
||||
|
||||
} } // namespace Poco::XML
|
||||
|
@ -38,7 +38,6 @@
|
||||
#include <string>
|
||||
#include <iosfwd>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -64,7 +63,7 @@ class XML_API XMLStreamParser
|
||||
/// are not very common).
|
||||
///
|
||||
/// Attribute map is valid throughout at the "element level" until
|
||||
/// end_element and not just during startElement. As a special case,
|
||||
/// 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().
|
||||
///
|
||||
@ -75,17 +74,17 @@ class XML_API XMLStreamParser
|
||||
/// {
|
||||
/// switch (e)
|
||||
/// {
|
||||
/// case XMLStreamParser::startElement:
|
||||
/// case XMLStreamParser::EV_START_ELEMENT:
|
||||
/// cerr << p.line () << ':' << p.column () << ": start " << p.name () << endl;
|
||||
/// break;
|
||||
/// case XMLStreamParser::endElement:
|
||||
/// case XMLStreamParser::EV_END_ELEMENT:
|
||||
/// cerr << p.line () << ':' << p.column () << ": end " << p.name () << endl;
|
||||
/// break;
|
||||
/// case XMLStreamParser::startAttribute:
|
||||
/// case XMLStreamParser::EV_START_ATTRIBUTE:
|
||||
/// ...
|
||||
/// case XMLStreamParser::endAttribute:
|
||||
/// case XMLStreamParser::EV_END_ATTRIBUTE:
|
||||
/// ...
|
||||
/// case XMLStreamParser::characters:
|
||||
/// case XMLStreamParser::EV_CHARACTERS:
|
||||
/// ...
|
||||
/// }
|
||||
/// }
|
||||
@ -94,14 +93,14 @@ public:
|
||||
enum EventType
|
||||
/// Parsing events.
|
||||
{
|
||||
StartElement,
|
||||
EndElement,
|
||||
StartAttribute,
|
||||
EndAttribute,
|
||||
Characters,
|
||||
StartNamespaceDecl,
|
||||
EndNamespaceDecl,
|
||||
Eof
|
||||
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;
|
||||
@ -130,16 +129,18 @@ public:
|
||||
{
|
||||
typedef EventType value_type;
|
||||
|
||||
Iterator(XMLStreamParser* p = 0, EventType e = Eof) :
|
||||
Iterator(XMLStreamParser* p = 0, EventType e = EV_EOF):
|
||||
_parser(p),
|
||||
_e(e)
|
||||
{
|
||||
}
|
||||
value_type operator*() const
|
||||
|
||||
value_type operator * () const
|
||||
{
|
||||
return _e;
|
||||
}
|
||||
Iterator& operator++()
|
||||
|
||||
Iterator& operator ++ ()
|
||||
{
|
||||
_e = _parser->next();
|
||||
return *this;
|
||||
@ -148,10 +149,10 @@ public:
|
||||
bool operator == (Iterator y) const
|
||||
/// Comparison only makes sense when comparing to end (eof).
|
||||
{
|
||||
return _e == Eof && y._e == Eof;
|
||||
return _e == EV_EOF && y._e == EV_EOF;
|
||||
}
|
||||
|
||||
bool operator!=(Iterator y) const
|
||||
bool operator != (Iterator y) const
|
||||
/// Comparison only makes sense when comparing to end (eof).
|
||||
{
|
||||
return !(*this == y);
|
||||
@ -169,10 +170,10 @@ public:
|
||||
|
||||
Iterator end()
|
||||
{
|
||||
return Iterator(this, Eof);
|
||||
return Iterator(this, EV_EOF);
|
||||
}
|
||||
|
||||
XMLStreamParser(std::istream&, const std::string& input_name, FeatureType = RECEIVE_DEFAULT);
|
||||
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.
|
||||
@ -184,7 +185,7 @@ public:
|
||||
/// 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& input_name, FeatureType = RECEIVE_DEFAULT);
|
||||
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.
|
||||
|
||||
@ -204,7 +205,7 @@ public:
|
||||
|
||||
EventType peek();
|
||||
EventType event();
|
||||
/// Return the even that was last returned by the call to next() or peek().
|
||||
/// Return the event that was last returned by the call to next() or peek().
|
||||
|
||||
const std::string& inputName() const;
|
||||
const QName& getQName() const;
|
||||
@ -213,21 +214,21 @@ public:
|
||||
const std::string& prefix() const;
|
||||
std::string& value();
|
||||
const std::string& value() const;
|
||||
template<typename T> T 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>
|
||||
template <typename T>
|
||||
T attribute(const std::string& name) const;
|
||||
std::string attribute(const std::string& name, const std::string& default_value) const;
|
||||
template<typename T>
|
||||
T attribute(const std::string& name, const T& default_value) 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>
|
||||
template <typename T>
|
||||
T attribute(const QName& qname) const;
|
||||
std::string attribute(const QName& qname, const std::string& default_value) const;
|
||||
template<typename T>
|
||||
T attribute(const QName& qname, const T& default_value) 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;
|
||||
@ -240,46 +241,44 @@ public:
|
||||
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 startElement has already been parsed. The
|
||||
// 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,default_value).
|
||||
//
|
||||
// overload since it would conflicts with (namespace,deflt).
|
||||
std::string element();
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T element();
|
||||
std::string element(const std::string& name);
|
||||
std::string element(const QName& qname);
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T element(const std::string& name);
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T element(const QName& qname);
|
||||
std::string element(const std::string& name, const std::string& default_value);
|
||||
std::string element(const QName& qname, const std::string& default_value);
|
||||
template<typename T>
|
||||
T element(const std::string& name, const T& default_value);
|
||||
template<typename T>
|
||||
T element(const QName& qname, const T& default_value);
|
||||
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&);
|
||||
XMLStreamParser& operator = (const XMLStreamParser&);
|
||||
|
||||
static void XMLCALL start_element_(void*, const XML_Char*, const XML_Char**);
|
||||
static void XMLCALL end_element_(void*, const XML_Char*);
|
||||
static void XMLCALL characters_(void*, const XML_Char*, int);
|
||||
static void XMLCALL start_namespace_decl_(void*, const XML_Char*, const XML_Char*);
|
||||
static void XMLCALL end_namespace_decl_(void*, const XML_Char*);
|
||||
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 next_(bool peek);
|
||||
EventType next_body();
|
||||
void handle_error();
|
||||
EventType nextImpl(bool peek);
|
||||
EventType nextBody();
|
||||
void handleError();
|
||||
|
||||
// If size_ is 0, then data is std::istream. Otherwise, it is a buffer.
|
||||
//
|
||||
// If _size is 0, then data is std::istream. Otherwise, it is a buffer.
|
||||
union
|
||||
{
|
||||
std::istream* is;
|
||||
@ -303,13 +302,13 @@ private:
|
||||
Poco::UInt64 _line;
|
||||
Poco::UInt64 _column;
|
||||
|
||||
struct attribute_type
|
||||
struct AttributeType
|
||||
{
|
||||
QName qname;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
typedef std::vector<attribute_type> attributes;
|
||||
typedef std::vector<AttributeType> attributes;
|
||||
attributes _attributes;
|
||||
attributes::size_type _currentAttributeIndex; // Index of the current attribute.
|
||||
|
||||
@ -321,7 +320,7 @@ private:
|
||||
|
||||
struct ElementEntry
|
||||
{
|
||||
ElementEntry(std::size_t d, Content c = Content::Mixed) :
|
||||
ElementEntry(std::size_t d, Content c = Content::Mixed):
|
||||
depth(d),
|
||||
content(c),
|
||||
attributesUnhandled(0)
|
||||
@ -418,14 +417,14 @@ inline XMLStreamParser::EventType XMLStreamParser::peek()
|
||||
return _currentEvent;
|
||||
else
|
||||
{
|
||||
EventType e(next_(true));
|
||||
_parserState = state_peek; // Set it after the call to next_().
|
||||
EventType e(nextImpl(true));
|
||||
_parserState = state_peek; // Set it after the call to nextImpl().
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::value() const
|
||||
{
|
||||
return ValueTraits < T > ::parse(value(), *this);
|
||||
@ -438,7 +437,7 @@ inline const std::string& XMLStreamParser::attribute(const std::string& n) const
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::attribute(const std::string& n) const
|
||||
{
|
||||
return attribute < T > (QName(n));
|
||||
@ -451,14 +450,14 @@ inline std::string XMLStreamParser::attribute(const std::string& n, const std::s
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::attribute(const std::string& n, const T& dv) const
|
||||
{
|
||||
return attribute < T > (QName(n), dv);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::attribute(const QName& qn) const
|
||||
{
|
||||
return ValueTraits < T > ::parse(attribute(qn), *this);
|
||||
@ -498,7 +497,7 @@ inline void XMLStreamParser::nextExpect(EventType e, const std::string& n)
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const QName& qn, Content c)
|
||||
{
|
||||
nextExpect(e, qn);
|
||||
assert(e == StartElement);
|
||||
poco_assert(e == EV_START_ELEMENT);
|
||||
content(c);
|
||||
}
|
||||
|
||||
@ -506,7 +505,7 @@ inline void XMLStreamParser::nextExpect(EventType e, const QName& qn, Content c)
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const std::string& n, Content c)
|
||||
{
|
||||
nextExpect(e, std::string(), n);
|
||||
assert(e == StartElement);
|
||||
poco_assert(e == EV_START_ELEMENT);
|
||||
content(c);
|
||||
}
|
||||
|
||||
@ -514,12 +513,12 @@ inline void XMLStreamParser::nextExpect(EventType e, const std::string& n, Conte
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const std::string& ns, const std::string& n, Content c)
|
||||
{
|
||||
nextExpect(e, ns, n);
|
||||
assert(e == StartElement);
|
||||
poco_assert(e == EV_START_ELEMENT);
|
||||
content(c);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::element()
|
||||
{
|
||||
return ValueTraits < T > ::parse(element(), *this);
|
||||
@ -528,26 +527,26 @@ inline T XMLStreamParser::element()
|
||||
|
||||
inline std::string XMLStreamParser::element(const std::string& n)
|
||||
{
|
||||
nextExpect(StartElement, n);
|
||||
nextExpect(EV_START_ELEMENT, n);
|
||||
return element();
|
||||
}
|
||||
|
||||
|
||||
inline std::string XMLStreamParser::element(const QName& qn)
|
||||
{
|
||||
nextExpect(StartElement, qn);
|
||||
nextExpect(EV_START_ELEMENT, qn);
|
||||
return element();
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::element(const std::string& n)
|
||||
{
|
||||
return ValueTraits < T > ::parse(element(n), *this);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::element(const QName& qn)
|
||||
{
|
||||
return ValueTraits < T > ::parse(element(qn), *this);
|
||||
@ -560,7 +559,7 @@ inline std::string XMLStreamParser::element(const std::string& n, const std::str
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
inline T XMLStreamParser::element(const std::string& n, const T& dv)
|
||||
{
|
||||
return element < T > (QName(n), dv);
|
||||
@ -569,7 +568,7 @@ inline T XMLStreamParser::element(const std::string& n, const T& dv)
|
||||
|
||||
inline void XMLStreamParser::content(Content c)
|
||||
{
|
||||
assert(_parserState == state_next);
|
||||
poco_assert(_parserState == state_next);
|
||||
|
||||
if (!_elementState.empty() && _elementState.back().depth == _depth)
|
||||
_elementState.back().content = c;
|
||||
@ -580,7 +579,7 @@ inline void XMLStreamParser::content(Content c)
|
||||
|
||||
inline Content XMLStreamParser::content() const
|
||||
{
|
||||
assert(_parserState == state_next);
|
||||
poco_assert(_parserState == state_next);
|
||||
|
||||
return !_elementState.empty() && _elementState.back().depth == _depth ? _elementState.back().content : Content(Content::Mixed);
|
||||
}
|
||||
@ -592,7 +591,7 @@ inline const XMLStreamParser::ElementEntry* XMLStreamParser::getElement() const
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T XMLStreamParser::attribute(const QName& qn, const T& dv) const
|
||||
{
|
||||
if (const ElementEntry* e = getElement())
|
||||
@ -614,10 +613,10 @@ T XMLStreamParser::attribute(const QName& qn, const T& dv) const
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
T XMLStreamParser::element(const QName& qn, const T& dv)
|
||||
{
|
||||
if (peek() == StartElement && getQName() == qn)
|
||||
if (peek() == EV_START_ELEMENT && getQName() == qn)
|
||||
{
|
||||
next();
|
||||
return element<T>();
|
||||
|
@ -7,8 +7,6 @@
|
||||
// Package: XML
|
||||
// Module: QName
|
||||
//
|
||||
// Definition of the QName class.
|
||||
//
|
||||
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
@ -53,7 +51,7 @@ QName::QName(const std::string& ns, const std::string& name, const std::string&
|
||||
}
|
||||
|
||||
|
||||
std::string QName::string() const
|
||||
std::string QName::toString() const
|
||||
{
|
||||
std::string r;
|
||||
if (!_ns.empty())
|
||||
@ -67,9 +65,9 @@ std::string QName::string() const
|
||||
}
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const QName& qn)
|
||||
std::ostream& operator << (std::ostream& os, const QName& qn)
|
||||
{
|
||||
return os << qn.string();
|
||||
return os << qn.toString();
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,8 +7,6 @@
|
||||
// Package: XML
|
||||
// Module: XMLStreamParser
|
||||
//
|
||||
// Definition of the XMLStreamParser class.
|
||||
//
|
||||
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
@ -21,7 +19,6 @@
|
||||
|
||||
#include "Poco/XML/XMLStreamParser.h"
|
||||
#include <new>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
@ -105,7 +102,7 @@ XMLStreamParser::XMLStreamParser(const void* data, std::size_t size, const std::
|
||||
_inputName(iname),
|
||||
_feature(f)
|
||||
{
|
||||
assert(data != 0 && size != 0);
|
||||
poco_assert(data != 0 && size != 0);
|
||||
|
||||
_data.buf = data;
|
||||
init();
|
||||
@ -122,8 +119,8 @@ void XMLStreamParser::init()
|
||||
{
|
||||
_depth = 0;
|
||||
_parserState = state_next;
|
||||
_currentEvent = Eof;
|
||||
_queue = Eof;
|
||||
_currentEvent = EV_EOF;
|
||||
_queue = EV_EOF;
|
||||
|
||||
_qualifiedName = &_qname;
|
||||
_pvalue = &_value;
|
||||
@ -156,26 +153,26 @@ void XMLStreamParser::init()
|
||||
|
||||
if ((_feature & RECEIVE_ELEMENTS) != 0)
|
||||
{
|
||||
XML_SetStartElementHandler(_parser, &start_element_);
|
||||
XML_SetEndElementHandler(_parser, &end_element_);
|
||||
XML_SetStartElementHandler(_parser, &handleStartElement);
|
||||
XML_SetEndElementHandler(_parser, &handleEndElement);
|
||||
}
|
||||
|
||||
if ((_feature & RECEIVE_CHARACTERS) != 0)
|
||||
XML_SetCharacterDataHandler(_parser, &characters_);
|
||||
XML_SetCharacterDataHandler(_parser, &handleCharacters);
|
||||
|
||||
if ((_feature & RECEIVE_NAMESPACE_DECLS) != 0)
|
||||
XML_SetNamespaceDeclHandler(_parser, &start_namespace_decl_, &end_namespace_decl_);
|
||||
XML_SetNamespaceDeclHandler(_parser, &handleStartNamespaceDecl, &handleEndNamespaceDecl);
|
||||
}
|
||||
|
||||
|
||||
void XMLStreamParser::handle_error()
|
||||
void XMLStreamParser::handleError()
|
||||
{
|
||||
XML_Error e(XML_GetErrorCode(_parser));
|
||||
|
||||
if (e == XML_ERROR_ABORTED)
|
||||
{
|
||||
// For now we only abort the XMLStreamParser in the characters_() and
|
||||
// start_element_() handlers.
|
||||
// For now we only abort the XMLStreamParser in the handleCharacters() and
|
||||
// handleStartElement() handlers.
|
||||
//
|
||||
switch (content())
|
||||
{
|
||||
@ -186,7 +183,7 @@ void XMLStreamParser::handle_error()
|
||||
case Content::Complex:
|
||||
throw XMLStreamParserException(*this, "characters in complex content");
|
||||
default:
|
||||
assert(false);
|
||||
poco_assert(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -197,7 +194,7 @@ void XMLStreamParser::handle_error()
|
||||
XMLStreamParser::EventType XMLStreamParser::next()
|
||||
{
|
||||
if (_parserState == state_next)
|
||||
return next_(false);
|
||||
return nextImpl(false);
|
||||
else
|
||||
{
|
||||
// If we previously peeked at start/end_element, then adjust
|
||||
@ -205,7 +202,7 @@ XMLStreamParser::EventType XMLStreamParser::next()
|
||||
//
|
||||
switch (_currentEvent)
|
||||
{
|
||||
case EndElement:
|
||||
case EV_END_ELEMENT:
|
||||
{
|
||||
if (!_elementState.empty() && _elementState.back().depth == _depth)
|
||||
popElement();
|
||||
@ -213,7 +210,7 @@ XMLStreamParser::EventType XMLStreamParser::next()
|
||||
_depth--;
|
||||
break;
|
||||
}
|
||||
case StartElement:
|
||||
case EV_START_ELEMENT:
|
||||
{
|
||||
_depth++;
|
||||
break;
|
||||
@ -245,7 +242,7 @@ const std::string& XMLStreamParser::attribute(const QName& qn) const
|
||||
}
|
||||
}
|
||||
|
||||
throw XMLStreamParserException(*this, "attribute '" + qn.string() + "' expected");
|
||||
throw XMLStreamParserException(*this, "attribute '" + qn.toString() + "' expected");
|
||||
}
|
||||
|
||||
|
||||
@ -301,7 +298,7 @@ void XMLStreamParser::nextExpect(EventType e)
|
||||
void XMLStreamParser::nextExpect(EventType e, const std::string& ns, const std::string& n)
|
||||
{
|
||||
if (next() != e || namespaceURI() != ns || localName() != n)
|
||||
throw XMLStreamParserException(*this, std::string(parserEventStrings[e]) + " '" + QName(ns, n).string() + "' expected");
|
||||
throw XMLStreamParserException(*this, std::string(parserEventStrings[e]) + " '" + QName(ns, n).toString() + "' expected");
|
||||
}
|
||||
|
||||
|
||||
@ -314,7 +311,7 @@ std::string XMLStreamParser::element()
|
||||
// will be no characters event.
|
||||
//
|
||||
EventType e(next());
|
||||
if (e == Characters)
|
||||
if (e == EV_CHARACTERS)
|
||||
{
|
||||
r.swap(value());
|
||||
e = next();
|
||||
@ -323,7 +320,7 @@ std::string XMLStreamParser::element()
|
||||
// We cannot really get anything other than end_element since
|
||||
// the simple content validation won't allow it.
|
||||
//
|
||||
assert(e == EndElement);
|
||||
poco_assert(e == EV_END_ELEMENT);
|
||||
|
||||
return r;
|
||||
}
|
||||
@ -331,7 +328,7 @@ std::string XMLStreamParser::element()
|
||||
|
||||
std::string XMLStreamParser::element(const QName& qn, const std::string& dv)
|
||||
{
|
||||
if (peek() == StartElement && getQName() == qn)
|
||||
if (peek() == EV_START_ELEMENT && getQName() == qn)
|
||||
{
|
||||
next();
|
||||
return element();
|
||||
@ -343,7 +340,7 @@ std::string XMLStreamParser::element(const QName& qn, const std::string& dv)
|
||||
|
||||
const XMLStreamParser::ElementEntry* XMLStreamParser::getElementImpl() const
|
||||
{
|
||||
// The start_element_() Expat handler may have already provisioned
|
||||
// The handleStartElement() Expat handler may have already provisioned
|
||||
// an entry in the element stack. In this case, we need to get the
|
||||
// one before it, if any.
|
||||
//
|
||||
@ -375,21 +372,21 @@ void XMLStreamParser::popElement()
|
||||
for (AttributeMapType::const_iterator i(e.attributeMap.begin()); i != e.attributeMap.end(); ++i)
|
||||
{
|
||||
if (!i->second.handled)
|
||||
throw XMLStreamParserException(*this, "unexpected attribute '" + i->first.string() + "'");
|
||||
throw XMLStreamParserException(*this, "unexpected attribute '" + i->first.toString() + "'");
|
||||
}
|
||||
assert(false);
|
||||
poco_assert(false);
|
||||
}
|
||||
|
||||
_elementState.pop_back();
|
||||
}
|
||||
|
||||
|
||||
XMLStreamParser::EventType XMLStreamParser::next_(bool peek)
|
||||
XMLStreamParser::EventType XMLStreamParser::nextImpl(bool peek)
|
||||
{
|
||||
EventType e(next_body());
|
||||
EventType e(nextBody());
|
||||
|
||||
// Content-specific processing. Note that we handle characters in the
|
||||
// characters_() Expat handler for two reasons. Firstly, it is faster
|
||||
// handleCharacters() Expat handler for two reasons. Firstly, it is faster
|
||||
// to ignore the whitespaces at the source. Secondly, this allows us
|
||||
// to distinguish between element and attribute characters. We can
|
||||
// move this processing to the handler because the characters event
|
||||
@ -397,7 +394,7 @@ XMLStreamParser::EventType XMLStreamParser::next_(bool peek)
|
||||
//
|
||||
switch (e)
|
||||
{
|
||||
case EndElement:
|
||||
case EV_END_ELEMENT:
|
||||
{
|
||||
// If this is a peek, then avoid popping the stack just yet.
|
||||
// This way, the attribute map will still be valid until we
|
||||
@ -412,7 +409,7 @@ XMLStreamParser::EventType XMLStreamParser::next_(bool peek)
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StartElement:
|
||||
case EV_START_ELEMENT:
|
||||
{
|
||||
if (const ElementEntry* e = getElement())
|
||||
{
|
||||
@ -442,7 +439,7 @@ XMLStreamParser::EventType XMLStreamParser::next_(bool peek)
|
||||
}
|
||||
|
||||
|
||||
XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
XMLStreamParser::EventType XMLStreamParser::nextBody()
|
||||
{
|
||||
// See if we have any start namespace declarations we need to return.
|
||||
//
|
||||
@ -452,7 +449,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
//
|
||||
switch (_currentEvent)
|
||||
{
|
||||
case StartNamespaceDecl:
|
||||
case EV_START_NAMESPACE_DECL:
|
||||
{
|
||||
if (++_startNamespaceIndex == _startNamespace.size())
|
||||
{
|
||||
@ -463,16 +460,16 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
}
|
||||
// Fall through.
|
||||
}
|
||||
case StartElement:
|
||||
case EV_START_ELEMENT:
|
||||
{
|
||||
_currentEvent = StartNamespaceDecl;
|
||||
_currentEvent = EV_START_NAMESPACE_DECL;
|
||||
_qualifiedName = &_startNamespace[_startNamespaceIndex];
|
||||
return _currentEvent;
|
||||
}
|
||||
default:
|
||||
{
|
||||
assert(false);
|
||||
return _currentEvent = Eof;
|
||||
poco_assert(false);
|
||||
return _currentEvent = EV_EOF;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -485,18 +482,18 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
//
|
||||
switch (_currentEvent)
|
||||
{
|
||||
case StartAttribute:
|
||||
case EV_START_ATTRIBUTE:
|
||||
{
|
||||
_currentEvent = Characters;
|
||||
_currentEvent = EV_CHARACTERS;
|
||||
_pvalue = &_attributes[_currentAttributeIndex].value;
|
||||
return _currentEvent;
|
||||
}
|
||||
case Characters:
|
||||
case EV_CHARACTERS:
|
||||
{
|
||||
_currentEvent = EndAttribute; // Name is already set.
|
||||
_currentEvent = EV_END_ATTRIBUTE; // Name is already set.
|
||||
return _currentEvent;
|
||||
}
|
||||
case EndAttribute:
|
||||
case EV_END_ATTRIBUTE:
|
||||
{
|
||||
if (++_currentAttributeIndex == _attributes.size())
|
||||
{
|
||||
@ -508,17 +505,17 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
}
|
||||
// Fall through.
|
||||
}
|
||||
case StartElement:
|
||||
case StartNamespaceDecl:
|
||||
case EV_START_ELEMENT:
|
||||
case EV_START_NAMESPACE_DECL:
|
||||
{
|
||||
_currentEvent = StartAttribute;
|
||||
_currentEvent = EV_START_ATTRIBUTE;
|
||||
_qualifiedName = &_attributes[_currentAttributeIndex].qname;
|
||||
return _currentEvent;
|
||||
}
|
||||
default:
|
||||
{
|
||||
assert(false);
|
||||
return _currentEvent = Eof;
|
||||
poco_assert(false);
|
||||
return _currentEvent = EV_EOF;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -531,7 +528,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
//
|
||||
switch (_currentEvent)
|
||||
{
|
||||
case EndNamespaceDecl:
|
||||
case EV_END_NAMESPACE_DECL:
|
||||
{
|
||||
if (++_endNamespaceIndex == _endNamespace.size())
|
||||
{
|
||||
@ -547,7 +544,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
//
|
||||
default:
|
||||
{
|
||||
_currentEvent = EndNamespaceDecl;
|
||||
_currentEvent = EV_END_NAMESPACE_DECL;
|
||||
_qualifiedName = &_endNamespace[_endNamespaceIndex];
|
||||
return _currentEvent;
|
||||
}
|
||||
@ -556,10 +553,10 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
|
||||
// Check the queue.
|
||||
//
|
||||
if (_queue != Eof)
|
||||
if (_queue != EV_EOF)
|
||||
{
|
||||
_currentEvent = _queue;
|
||||
_queue = Eof;
|
||||
_queue = EV_EOF;
|
||||
|
||||
_line = XML_GetCurrentLineNumber(_parser);
|
||||
_column = XML_GetCurrentColumnNumber(_parser);
|
||||
@ -583,12 +580,12 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
}
|
||||
case XML_PARSING:
|
||||
{
|
||||
assert(false);
|
||||
return _currentEvent = Eof;
|
||||
poco_assert(false);
|
||||
return _currentEvent = EV_EOF;
|
||||
}
|
||||
case XML_FINISHED:
|
||||
{
|
||||
return _currentEvent = Eof;
|
||||
return _currentEvent = EV_EOF;
|
||||
}
|
||||
case XML_SUSPENDED:
|
||||
{
|
||||
@ -607,12 +604,12 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
// unless this was the last chunk, in which case this is eof.
|
||||
//
|
||||
if (ps.finalBuffer)
|
||||
return _currentEvent = Eof;
|
||||
return _currentEvent = EV_EOF;
|
||||
|
||||
break;
|
||||
}
|
||||
case XML_STATUS_ERROR:
|
||||
handle_error();
|
||||
handleError();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -622,7 +619,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
// or reach eof.
|
||||
//
|
||||
if (!_accumulateContent)
|
||||
_currentEvent = Eof;
|
||||
_currentEvent = EV_EOF;
|
||||
|
||||
XML_Status s;
|
||||
do
|
||||
@ -632,7 +629,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
s = XML_Parse(_parser, static_cast<const char*>(_data.buf), static_cast<int>(_size), true);
|
||||
|
||||
if (s == XML_STATUS_ERROR)
|
||||
handle_error();
|
||||
handleError();
|
||||
|
||||
break;
|
||||
}
|
||||
@ -664,7 +661,7 @@ XMLStreamParser::EventType XMLStreamParser::next_body()
|
||||
s = XML_ParseBuffer(_parser, static_cast<int>(is.gcount()), eof);
|
||||
|
||||
if (s == XML_STATUS_ERROR)
|
||||
handle_error();
|
||||
handleError();
|
||||
|
||||
if (eof)
|
||||
break;
|
||||
@ -710,7 +707,7 @@ static void splitName(const XML_Char* s, QName& qn)
|
||||
}
|
||||
|
||||
|
||||
void XMLCALL XMLStreamParser::start_element_(void* v, const XML_Char* name, const XML_Char** atts)
|
||||
void XMLCALL XMLStreamParser::handleStartElement(void* v, const XML_Char* name, const XML_Char** atts)
|
||||
{
|
||||
XMLStreamParser& p(*static_cast<XMLStreamParser*>(v));
|
||||
|
||||
@ -725,7 +722,7 @@ void XMLCALL XMLStreamParser::start_element_(void* v, const XML_Char* name, cons
|
||||
|
||||
// Cannot be a followup event.
|
||||
//
|
||||
assert(ps.parsing == XML_PARSING);
|
||||
poco_assert(ps.parsing == XML_PARSING);
|
||||
|
||||
// When accumulating characters in simple content, we expect to
|
||||
// see more characters or end element. Seeing start element is
|
||||
@ -742,7 +739,7 @@ void XMLCALL XMLStreamParser::start_element_(void* v, const XML_Char* name, cons
|
||||
return;
|
||||
}
|
||||
|
||||
p._currentEvent = StartElement;
|
||||
p._currentEvent = EV_START_ELEMENT;
|
||||
splitName(name, p._qname);
|
||||
|
||||
p._line = XML_GetCurrentLineNumber(p._parser);
|
||||
@ -779,7 +776,7 @@ void XMLCALL XMLStreamParser::start_element_(void* v, const XML_Char* name, cons
|
||||
}
|
||||
else
|
||||
{
|
||||
p._attributes.push_back(attribute_type());
|
||||
p._attributes.push_back(AttributeType());
|
||||
splitName(*atts, p._attributes.back().qname);
|
||||
p._attributes.back().value = *(atts + 1);
|
||||
}
|
||||
@ -794,7 +791,7 @@ void XMLCALL XMLStreamParser::start_element_(void* v, const XML_Char* name, cons
|
||||
}
|
||||
|
||||
|
||||
void XMLCALL XMLStreamParser::end_element_(void* v, const XML_Char* name)
|
||||
void XMLCALL XMLStreamParser::handleEndElement(void* v, const XML_Char* name)
|
||||
{
|
||||
XMLStreamParser& p(*static_cast<XMLStreamParser*>(v));
|
||||
|
||||
@ -811,7 +808,7 @@ void XMLCALL XMLStreamParser::end_element_(void* v, const XML_Char* name)
|
||||
// case the element name is already set.
|
||||
//
|
||||
if (ps.parsing != XML_PARSING)
|
||||
p._queue = EndElement;
|
||||
p._queue = EV_END_ELEMENT;
|
||||
else
|
||||
{
|
||||
splitName(name, p._qname);
|
||||
@ -819,10 +816,10 @@ void XMLCALL XMLStreamParser::end_element_(void* v, const XML_Char* name)
|
||||
// If we are accumulating characters, then queue this event.
|
||||
//
|
||||
if (p._accumulateContent)
|
||||
p._queue = EndElement;
|
||||
p._queue = EV_END_ELEMENT;
|
||||
else
|
||||
{
|
||||
p._currentEvent = EndElement;
|
||||
p._currentEvent = EV_END_ELEMENT;
|
||||
|
||||
p._line = XML_GetCurrentLineNumber(p._parser);
|
||||
p._column = XML_GetCurrentColumnNumber(p._parser);
|
||||
@ -833,7 +830,7 @@ void XMLCALL XMLStreamParser::end_element_(void* v, const XML_Char* name)
|
||||
}
|
||||
|
||||
|
||||
void XMLCALL XMLStreamParser::characters_(void* v, const XML_Char* s, int n)
|
||||
void XMLCALL XMLStreamParser::handleCharacters(void* v, const XML_Char* s, int n)
|
||||
{
|
||||
XMLStreamParser& p(*static_cast<XMLStreamParser*>(v));
|
||||
|
||||
@ -881,12 +878,12 @@ void XMLCALL XMLStreamParser::characters_(void* v, const XML_Char* s, int n)
|
||||
//
|
||||
if (p._accumulateContent || ps.parsing != XML_PARSING)
|
||||
{
|
||||
assert(p._currentEvent == Characters);
|
||||
poco_assert(p._currentEvent == EV_CHARACTERS);
|
||||
p._value.append(s, n);
|
||||
}
|
||||
else
|
||||
{
|
||||
p._currentEvent = Characters;
|
||||
p._currentEvent = EV_CHARACTERS;
|
||||
p._value.assign(s, n);
|
||||
|
||||
p._line = XML_GetCurrentLineNumber(p._parser);
|
||||
@ -904,7 +901,7 @@ void XMLCALL XMLStreamParser::characters_(void* v, const XML_Char* s, int n)
|
||||
}
|
||||
|
||||
|
||||
void XMLCALL XMLStreamParser::start_namespace_decl_(void* v, const XML_Char* prefix, const XML_Char* ns)
|
||||
void XMLCALL XMLStreamParser::handleStartNamespaceDecl(void* v, const XML_Char* prefix, const XML_Char* ns)
|
||||
{
|
||||
XMLStreamParser& p(*static_cast<XMLStreamParser*>(v));
|
||||
|
||||
@ -923,7 +920,7 @@ void XMLCALL XMLStreamParser::start_namespace_decl_(void* v, const XML_Char* pre
|
||||
}
|
||||
|
||||
|
||||
void XMLCALL XMLStreamParser::end_namespace_decl_(void* v, const XML_Char* prefix)
|
||||
void XMLCALL XMLStreamParser::handleEndNamespaceDecl(void* v, const XML_Char* prefix)
|
||||
{
|
||||
XMLStreamParser& p(*static_cast<XMLStreamParser*>(v));
|
||||
|
||||
|
@ -7,8 +7,6 @@
|
||||
// Package: XML
|
||||
// Module: XMLStreamParserException
|
||||
//
|
||||
// Definition of the XMLStreamParserException class.
|
||||
//
|
||||
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
|
@ -13,7 +13,7 @@ objects = AttributesImplTest ChildNodesTest DOMTestSuite DocumentTest \
|
||||
NamespaceSupportTest NodeIteratorTest NodeTest ParserWriterTest \
|
||||
SAXParserTest SAXTestSuite TextTest TreeWalkerTest \
|
||||
XMLTestSuite XMLWriterTest NodeAppenderTest \
|
||||
XMLStreamParserTestSuite
|
||||
XMLStreamParserTest
|
||||
|
||||
target = testrunner
|
||||
target_version = 1
|
||||
|
505
XML/testsuite/src/XMLStreamParserTest.cpp
Normal file
505
XML/testsuite/src/XMLStreamParserTest.cpp
Normal file
@ -0,0 +1,505 @@
|
||||
//
|
||||
// XMLStreamParserTest.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "XMLStreamParserTest.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/XML/XMLStreamParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
using namespace Poco::XML;
|
||||
|
||||
|
||||
XMLStreamParserTest::XMLStreamParserTest(const std::string& name):
|
||||
CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
XMLStreamParserTest::~XMLStreamParserTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void XMLStreamParserTest::testParser()
|
||||
{
|
||||
// Test error handling.
|
||||
//
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root><nested>X</nasted></root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == "X");
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root/>");
|
||||
is.exceptions(std::ios_base::badbit | std::ios_base::failbit);
|
||||
XMLStreamParser p(is, "test");
|
||||
|
||||
is.setstate(std::ios_base::badbit);
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const std::ios_base::failure&)
|
||||
{
|
||||
}
|
||||
|
||||
// Test the nextExpect() functionality.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root1");
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test nextExpect() with content setting.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root> </root>");
|
||||
XMLStreamParser p(is, "empty");
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root", Content::Empty);
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
p.nextExpect(XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
// Test namespace declarations.
|
||||
//
|
||||
{
|
||||
// Followup end element event that should be precedeeded by end
|
||||
// namespace declaration.
|
||||
//
|
||||
std::istringstream is("<root xmlns:a='a'/>");
|
||||
XMLStreamParser p(is, "test", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
p.nextExpect(XMLStreamParser::EV_START_NAMESPACE_DECL);
|
||||
p.nextExpect(XMLStreamParser::EV_END_NAMESPACE_DECL);
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
// Test value extraction.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root>123</root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
p.nextExpect(XMLStreamParser::EV_CHARACTERS);
|
||||
assert(p.value<int>() == 123);
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
// Test attribute maps.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root a='a' b='b' d='123' t='true'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
|
||||
assert(p.attribute("a") == "a");
|
||||
assert(p.attribute("b", "B") == "b");
|
||||
assert(p.attribute("c", "C") == "C");
|
||||
assert(p.attribute<int>("d") == 123);
|
||||
assert(p.attribute<bool>("t") == true);
|
||||
assert(p.attribute("f", false) == false);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
{
|
||||
std::istringstream is("<root a='a'><nested a='A'><inner/></nested></root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
assert(p.attribute("a") == "a");
|
||||
assert(p.peek() == XMLStreamParser::EV_START_ELEMENT && p.localName() == "nested");
|
||||
assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "nested");
|
||||
assert(p.attribute("a") == "A");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "inner");
|
||||
assert(p.attribute("a", "") == "");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.attribute("a") == "A");
|
||||
assert(p.peek() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.attribute("a") == "A"); // Still valid.
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.attribute("a", "") == "");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root a='a' b='b'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root a='abc'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
p.attribute<int>("a");
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test peeking and getting the current event.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root x='x'>x<nested/></root>");
|
||||
XMLStreamParser p(is, "peek", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
assert(p.event() == XMLStreamParser::EV_EOF);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.event() == XMLStreamParser::EV_START_ELEMENT);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_START_ATTRIBUTE);
|
||||
assert(p.event() == XMLStreamParser::EV_START_ATTRIBUTE);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ATTRIBUTE);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
assert(p.event() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_END_ATTRIBUTE);
|
||||
assert(p.event() == XMLStreamParser::EV_END_ATTRIBUTE);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ATTRIBUTE);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
assert(p.event() == XMLStreamParser::EV_CHARACTERS && p.value() == "x");
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.event() == XMLStreamParser::EV_START_ELEMENT);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.event() == XMLStreamParser::EV_END_ELEMENT);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.event() == XMLStreamParser::EV_END_ELEMENT);
|
||||
|
||||
assert(p.peek() == XMLStreamParser::EV_EOF);
|
||||
assert(p.next() == XMLStreamParser::EV_EOF);
|
||||
assert(p.event() == XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
// Test content processing.
|
||||
//
|
||||
|
||||
// empty
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root x=' x '> \n\t </root>");
|
||||
XMLStreamParser p(is, "empty", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.content(Content::Empty);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ATTRIBUTE);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == " x ");
|
||||
assert(p.next() == XMLStreamParser::EV_END_ATTRIBUTE);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root> \n & X \t </root>");
|
||||
XMLStreamParser p(is, "empty");
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.content(Content::Empty);
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// simple
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root> X </root>");
|
||||
XMLStreamParser p(is, "simple");
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.content(Content::Simple);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == " X ");
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root> ? <nested/></root>");
|
||||
XMLStreamParser p(is, "simple");
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.content(Content::Simple);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == " ? ");
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
{
|
||||
// Test content accumulation in simple content.
|
||||
//
|
||||
std::istringstream is("<root xmlns:a='a'>123</root>");
|
||||
XMLStreamParser p(is, "simple", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.nextExpect(XMLStreamParser::EV_START_NAMESPACE_DECL);
|
||||
p.content(Content::Simple);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == "123");
|
||||
p.nextExpect(XMLStreamParser::EV_END_NAMESPACE_DECL);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Test error handling in accumulation in simple content.
|
||||
//
|
||||
std::istringstream is("<root xmlns:a='a'>12<nested/>3</root>");
|
||||
XMLStreamParser p(is, "simple", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.nextExpect(XMLStreamParser::EV_START_NAMESPACE_DECL);
|
||||
p.content(Content::Simple);
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// complex
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root x=' x '>\n"
|
||||
" <nested>\n"
|
||||
" <inner/>\n"
|
||||
" <inner> X </inner>\n"
|
||||
" </nested>\n"
|
||||
"</root>\n");
|
||||
XMLStreamParser p(is, "complex", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT); // root
|
||||
p.content(Content::Complex);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ATTRIBUTE);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == " x ");
|
||||
assert(p.next() == XMLStreamParser::EV_END_ATTRIBUTE);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT); // nested
|
||||
p.content(Content::Complex);
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT); // inner
|
||||
p.content(Content::Empty);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT); // inner
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT); // inner
|
||||
p.content(Content::Simple);
|
||||
assert(p.next() == XMLStreamParser::EV_CHARACTERS && p.value() == " X ");
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT); // inner
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT); // nested
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT); // root
|
||||
assert(p.next() == XMLStreamParser::EV_EOF);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
std::istringstream is("<root> \n<n/> X <n> X </n> </root>");
|
||||
XMLStreamParser p(is, "complex");
|
||||
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
p.content(Content::Complex);
|
||||
assert(p.next() == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(p.next() == XMLStreamParser::EV_END_ELEMENT);
|
||||
p.next();
|
||||
assert(false);
|
||||
}
|
||||
catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test element with simple content helpers.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root>"
|
||||
" <nested>X</nested>"
|
||||
" <nested/>"
|
||||
" <nested>123</nested>"
|
||||
" <nested>Y</nested>"
|
||||
" <t:nested xmlns:t='test'>Z</t:nested>"
|
||||
" <nested>234</nested>"
|
||||
" <t:nested xmlns:t='test'>345</t:nested>"
|
||||
" <nested>A</nested>"
|
||||
" <t:nested xmlns:t='test'>B</t:nested>"
|
||||
" <nested1>A</nested1>"
|
||||
" <t:nested1 xmlns:t='test'>B</t:nested1>"
|
||||
" <nested>1</nested>"
|
||||
" <t:nested xmlns:t='test'>2</t:nested>"
|
||||
" <nested1>1</nested1>"
|
||||
" <t:nested1 xmlns:t='test'>2</t:nested1>"
|
||||
"</root>");
|
||||
XMLStreamParser p(is, "element");
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root", Content::Complex);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "nested");
|
||||
assert(p.element() == "X");
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "nested");
|
||||
assert(p.element() == "");
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "nested");
|
||||
assert(p.element<unsigned int>() == 123);
|
||||
|
||||
assert(p.element("nested") == "Y");
|
||||
assert(p.element(QName("test", "nested")) == "Z");
|
||||
|
||||
assert(p.element<unsigned int>("nested") == 234);
|
||||
assert(p.element<unsigned int>(QName("test", "nested")) == 345);
|
||||
|
||||
assert(p.element("nested", "a") == "A");
|
||||
assert(p.element(QName("test", "nested"), "b") == "B");
|
||||
|
||||
assert(p.element("nested", "a") == "a" && p.element("nested1") == "A");
|
||||
assert(p.element(QName("test", "nested"), "b") == "b" && p.element(QName("test", "nested1")) == "B");
|
||||
|
||||
assert(p.element<unsigned int>("nested", 10) == 1);
|
||||
assert(p.element<unsigned int>(QName("test", "nested"), 20) == 2);
|
||||
|
||||
assert(p.element<unsigned int>("nested", 10) == 10 && p.element<unsigned int>("nested1") == 1);
|
||||
assert(p.element<unsigned int>(QName("test", "nested"), 20) == 20 && p.element<unsigned int>(QName("test", "nested1")) == 2);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
// Test the iterator interface.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root><nested>X</nested></root>");
|
||||
XMLStreamParser p(is, "iterator");
|
||||
|
||||
std::vector<XMLStreamParser::EventType> v;
|
||||
|
||||
for (XMLStreamParser::Iterator i(p.begin()); i != p.end(); ++i)
|
||||
v.push_back(*i);
|
||||
|
||||
//for (XMLStreamParser::EventType e: p)
|
||||
// v.push_back (e);
|
||||
|
||||
assert(v.size() == 5);
|
||||
assert(v[0] == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(v[1] == XMLStreamParser::EV_START_ELEMENT);
|
||||
assert(v[2] == XMLStreamParser::EV_CHARACTERS);
|
||||
assert(v[3] == XMLStreamParser::EV_END_ELEMENT);
|
||||
assert(v[4] == XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
|
||||
// Test space extraction into the std::string value.
|
||||
//
|
||||
{
|
||||
std::istringstream is("<root a=' a '> b </root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EV_START_ELEMENT, "root");
|
||||
assert(p.attribute<std::string>("a") == " a ");
|
||||
p.nextExpect(XMLStreamParser::EV_CHARACTERS);
|
||||
assert(p.value<std::string>() == " b ");
|
||||
p.nextExpect(XMLStreamParser::EV_END_ELEMENT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void XMLStreamParserTest::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void XMLStreamParserTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* XMLStreamParserTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLStreamParserTest");
|
||||
|
||||
CppUnit_addTest(pSuite, XMLStreamParserTest, testParser);
|
||||
|
||||
return pSuite;
|
||||
}
|
40
XML/testsuite/src/XMLStreamParserTest.h
Normal file
40
XML/testsuite/src/XMLStreamParserTest.h
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// XMLStreamParserTest.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Definition of the XMLStreamParserTest class.
|
||||
//
|
||||
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef XMLStreamParserTest_INCLUDED
|
||||
#define XMLStreamParserTest_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/XML/XML.h"
|
||||
#include "CppUnit/TestCase.h"
|
||||
|
||||
|
||||
class XMLStreamParserTest: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
XMLStreamParserTest(const std::string& name);
|
||||
~XMLStreamParserTest();
|
||||
|
||||
void testParser();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // XMLStreamParserTest_INCLUDED
|
@ -1,489 +0,0 @@
|
||||
//
|
||||
// XMLStreamParserTestSuite.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/XML/testsuite/src/XMLStreamParserTestSuite.cpp#4 $
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
#include "XMLStreamParserTestSuite.h"
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/XML/XMLStreamParser.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace Poco::XML;
|
||||
using namespace std;
|
||||
|
||||
XMLStreamParserTestSuite::XMLStreamParserTestSuite(const std::string& name)
|
||||
: CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
XMLStreamParserTestSuite::~XMLStreamParserTestSuite()
|
||||
{
|
||||
}
|
||||
|
||||
void XMLStreamParserTestSuite::testParser()
|
||||
{
|
||||
// Test error handling.
|
||||
//
|
||||
try
|
||||
{
|
||||
istringstream is("<root><nested>X</nasted></root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == "X");
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root/>");
|
||||
is.exceptions(ios_base::badbit | ios_base::failbit);
|
||||
XMLStreamParser p(is, "test");
|
||||
|
||||
is.setstate(ios_base::badbit);
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const ios_base::failure&)
|
||||
{
|
||||
}
|
||||
|
||||
// Test the nextExpect() functionality.
|
||||
//
|
||||
{
|
||||
istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root1");
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test nextExpect() with content setting.
|
||||
//
|
||||
{
|
||||
istringstream is("<root> </root>");
|
||||
XMLStreamParser p(is, "empty");
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root", Content::Empty);
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
p.nextExpect(XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
// Test namespace declarations.
|
||||
//
|
||||
{
|
||||
// Followup end element event that should be precedeeded by end
|
||||
// namespace declaration.
|
||||
//
|
||||
istringstream is("<root xmlns:a='a'/>");
|
||||
XMLStreamParser p(is, "test", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
p.nextExpect(XMLStreamParser::StartNamespaceDecl);
|
||||
p.nextExpect(XMLStreamParser::EndNamespaceDecl);
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
// Test value extraction.
|
||||
//
|
||||
{
|
||||
istringstream is("<root>123</root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
p.nextExpect(XMLStreamParser::Characters);
|
||||
poco_assert(p.value<int>() == 123);
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
// Test attribute maps.
|
||||
//
|
||||
{
|
||||
istringstream is("<root a='a' b='b' d='123' t='true'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
|
||||
poco_assert(p.attribute("a") == "a");
|
||||
poco_assert(p.attribute("b", "B") == "b");
|
||||
poco_assert(p.attribute("c", "C") == "C");
|
||||
poco_assert(p.attribute<int>("d") == 123);
|
||||
poco_assert(p.attribute<bool>("t") == true);
|
||||
poco_assert(p.attribute("f", false) == false);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
{
|
||||
istringstream is("<root a='a'><nested a='A'><inner/></nested></root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
poco_assert(p.attribute("a") == "a");
|
||||
poco_assert(p.peek() == XMLStreamParser::StartElement && p.localName() == "nested");
|
||||
poco_assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "nested");
|
||||
poco_assert(p.attribute("a") == "A");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "inner");
|
||||
poco_assert(p.attribute("a", "") == "");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
poco_assert(p.attribute("a") == "A");
|
||||
poco_assert(p.peek() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.attribute("a") == "A"); // Still valid.
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
poco_assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
poco_assert(p.attribute("a", "") == "");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root a='a' b='b'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
poco_assert(p.attribute("a") == "a");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root a='abc'/>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
p.attribute<int>("a");
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test peeking and getting the current event.
|
||||
//
|
||||
{
|
||||
istringstream is("<root x='x'>x<nested/></root>");
|
||||
XMLStreamParser p(is, "peek", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
poco_assert(p.event() == XMLStreamParser::Eof);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.event() == XMLStreamParser::StartElement);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::StartAttribute);
|
||||
poco_assert(p.event() == XMLStreamParser::StartAttribute);
|
||||
poco_assert(p.next() == XMLStreamParser::StartAttribute);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::Characters && p.value() == "x");
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == "x");
|
||||
poco_assert(p.event() == XMLStreamParser::Characters && p.value() == "x");
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::EndAttribute);
|
||||
poco_assert(p.event() == XMLStreamParser::EndAttribute);
|
||||
poco_assert(p.next() == XMLStreamParser::EndAttribute);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::Characters && p.value() == "x");
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == "x");
|
||||
poco_assert(p.event() == XMLStreamParser::Characters && p.value() == "x");
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.event() == XMLStreamParser::StartElement);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.event() == XMLStreamParser::EndElement);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.event() == XMLStreamParser::EndElement);
|
||||
|
||||
poco_assert(p.peek() == XMLStreamParser::Eof);
|
||||
poco_assert(p.next() == XMLStreamParser::Eof);
|
||||
poco_assert(p.event() == XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
// Test content processing.
|
||||
//
|
||||
|
||||
// empty
|
||||
//
|
||||
{
|
||||
istringstream is("<root x=' x '> \n\t </root>");
|
||||
XMLStreamParser p(is, "empty", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.content(Content::Empty);
|
||||
poco_assert(p.next() == XMLStreamParser::StartAttribute);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == " x ");
|
||||
poco_assert(p.next() == XMLStreamParser::EndAttribute);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.next() == XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root> \n & X \t </root>");
|
||||
XMLStreamParser p(is, "empty");
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.content(Content::Empty);
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// simple
|
||||
//
|
||||
{
|
||||
istringstream is("<root> X </root>");
|
||||
XMLStreamParser p(is, "simple");
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.content(Content::Simple);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == " X ");
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.next() == XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root> ? <nested/></root>");
|
||||
XMLStreamParser p(is, "simple");
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.content(Content::Simple);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == " ? ");
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
{
|
||||
// Test content accumulation in simple content.
|
||||
//
|
||||
istringstream is("<root xmlns:a='a'>123</root>");
|
||||
XMLStreamParser p(is, "simple", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.nextExpect(XMLStreamParser::StartNamespaceDecl);
|
||||
p.content(Content::Simple);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == "123");
|
||||
p.nextExpect(XMLStreamParser::EndNamespaceDecl);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
poco_assert(p.next() == XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Test error handling in accumulation in simple content.
|
||||
//
|
||||
istringstream is("<root xmlns:a='a'>12<nested/>3</root>");
|
||||
XMLStreamParser p(is, "simple", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_NAMESPACE_DECLS);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.nextExpect(XMLStreamParser::StartNamespaceDecl);
|
||||
p.content(Content::Simple);
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// complex
|
||||
//
|
||||
{
|
||||
istringstream is("<root x=' x '>\n"
|
||||
" <nested>\n"
|
||||
" <inner/>\n"
|
||||
" <inner> X </inner>\n"
|
||||
" </nested>\n"
|
||||
"</root>\n");
|
||||
XMLStreamParser p(is, "complex", XMLStreamParser::RECEIVE_DEFAULT | XMLStreamParser::RECEIVE_ATTRIBUTES_EVENT);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement); // root
|
||||
p.content(Content::Complex);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartAttribute);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == " x ");
|
||||
poco_assert(p.next() == XMLStreamParser::EndAttribute);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement); // nested
|
||||
p.content(Content::Complex);
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement); // inner
|
||||
p.content(Content::Empty);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement); // inner
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement); // inner
|
||||
p.content(Content::Simple);
|
||||
poco_assert(p.next() == XMLStreamParser::Characters && p.value() == " X ");
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement); // inner
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement); // nested
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement); // root
|
||||
poco_assert(p.next() == XMLStreamParser::Eof);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
istringstream is("<root> \n<n/> X <n> X </n> </root>");
|
||||
XMLStreamParser p(is, "complex");
|
||||
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
p.content(Content::Complex);
|
||||
poco_assert(p.next() == XMLStreamParser::StartElement);
|
||||
poco_assert(p.next() == XMLStreamParser::EndElement);
|
||||
p.next();
|
||||
poco_assert(false);
|
||||
} catch (const Poco::Exception&)
|
||||
{
|
||||
// cerr << e.what () << endl;
|
||||
}
|
||||
|
||||
// Test element with simple content helpers.
|
||||
//
|
||||
{
|
||||
istringstream is("<root>"
|
||||
" <nested>X</nested>"
|
||||
" <nested/>"
|
||||
" <nested>123</nested>"
|
||||
" <nested>Y</nested>"
|
||||
" <t:nested xmlns:t='test'>Z</t:nested>"
|
||||
" <nested>234</nested>"
|
||||
" <t:nested xmlns:t='test'>345</t:nested>"
|
||||
" <nested>A</nested>"
|
||||
" <t:nested xmlns:t='test'>B</t:nested>"
|
||||
" <nested1>A</nested1>"
|
||||
" <t:nested1 xmlns:t='test'>B</t:nested1>"
|
||||
" <nested>1</nested>"
|
||||
" <t:nested xmlns:t='test'>2</t:nested>"
|
||||
" <nested1>1</nested1>"
|
||||
" <t:nested1 xmlns:t='test'>2</t:nested1>"
|
||||
"</root>");
|
||||
XMLStreamParser p(is, "element");
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root", Content::Complex);
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "nested");
|
||||
poco_assert(p.element() == "X");
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "nested");
|
||||
poco_assert(p.element() == "");
|
||||
|
||||
p.nextExpect(XMLStreamParser::StartElement, "nested");
|
||||
poco_assert(p.element<unsigned int>() == 123);
|
||||
|
||||
poco_assert(p.element("nested") == "Y");
|
||||
poco_assert(p.element(QName("test", "nested")) == "Z");
|
||||
|
||||
poco_assert(p.element<unsigned int>("nested") == 234);
|
||||
poco_assert(p.element<unsigned int>(QName("test", "nested")) == 345);
|
||||
|
||||
poco_assert(p.element("nested", "a") == "A");
|
||||
poco_assert(p.element(QName("test", "nested"), "b") == "B");
|
||||
|
||||
poco_assert(p.element("nested", "a") == "a" && p.element("nested1") == "A");
|
||||
poco_assert(p.element(QName("test", "nested"), "b") == "b" && p.element(QName("test", "nested1")) == "B");
|
||||
|
||||
poco_assert(p.element<unsigned int>("nested", 10) == 1);
|
||||
poco_assert(p.element<unsigned int>(QName("test", "nested"), 20) == 2);
|
||||
|
||||
poco_assert(p.element<unsigned int>("nested", 10) == 10 && p.element<unsigned int>("nested1") == 1);
|
||||
poco_assert(p.element<unsigned int>(QName("test", "nested"), 20) == 20 && p.element<unsigned int>(QName("test", "nested1")) == 2);
|
||||
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
// Test the iterator interface.
|
||||
//
|
||||
{
|
||||
istringstream is("<root><nested>X</nested></root>");
|
||||
XMLStreamParser p(is, "iterator");
|
||||
|
||||
vector<XMLStreamParser::EventType> v;
|
||||
|
||||
for (XMLStreamParser::Iterator i(p.begin()); i != p.end(); ++i)
|
||||
v.push_back(*i);
|
||||
|
||||
//for (XMLStreamParser::EventType e: p)
|
||||
// v.push_back (e);
|
||||
|
||||
poco_assert(v.size() == 5);
|
||||
poco_assert(v[0] == XMLStreamParser::StartElement);
|
||||
poco_assert(v[1] == XMLStreamParser::StartElement);
|
||||
poco_assert(v[2] == XMLStreamParser::Characters);
|
||||
poco_assert(v[3] == XMLStreamParser::EndElement);
|
||||
poco_assert(v[4] == XMLStreamParser::EndElement);
|
||||
}
|
||||
|
||||
// Test space extraction into the std::string value.
|
||||
//
|
||||
{
|
||||
istringstream is("<root a=' a '> b </root>");
|
||||
XMLStreamParser p(is, "test");
|
||||
p.nextExpect(XMLStreamParser::StartElement, "root");
|
||||
poco_assert(p.attribute<std::string>("a") == " a ");
|
||||
p.nextExpect(XMLStreamParser::Characters);
|
||||
poco_assert(p.value<std::string>() == " b ");
|
||||
p.nextExpect(XMLStreamParser::EndElement);
|
||||
}
|
||||
}
|
||||
|
||||
void XMLStreamParserTestSuite::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
void XMLStreamParserTestSuite::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
CppUnit::Test* XMLStreamParserTestSuite::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLStreamParserTestSuite");
|
||||
|
||||
CppUnit_addTest(pSuite, XMLStreamParserTestSuite, testParser);
|
||||
|
||||
return pSuite;
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
//
|
||||
// XMLStreamParserTestSuite.h
|
||||
//
|
||||
// $Id: //poco/1.4/XML/testsuite/src/XMLStreamParserTestSuite.h#2 $
|
||||
//
|
||||
// Definition of the XMLStreamParserTestSuite class.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
//
|
||||
|
||||
|
||||
#ifndef XMLStreamParserTestSuite_INCLUDED
|
||||
#define XMLStreamParserTestSuite_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/XML/XML.h"
|
||||
#include "CppUnit/TestCase.h"
|
||||
|
||||
|
||||
class XMLStreamParserTestSuite: public CppUnit::TestCase
|
||||
{
|
||||
public:
|
||||
XMLStreamParserTestSuite(const std::string& name);
|
||||
~XMLStreamParserTestSuite();
|
||||
|
||||
void testParser();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
|
||||
#endif // XMLStreamParserTestSuite_INCLUDED
|
@ -16,7 +16,7 @@
|
||||
#include "XMLWriterTest.h"
|
||||
#include "SAXTestSuite.h"
|
||||
#include "DOMTestSuite.h"
|
||||
#include "XMLStreamParserTestSuite.h"
|
||||
#include "XMLStreamParserTest.h"
|
||||
|
||||
CppUnit::Test* XMLTestSuite::suite()
|
||||
{
|
||||
@ -27,7 +27,7 @@ CppUnit::Test* XMLTestSuite::suite()
|
||||
pSuite->addTest(XMLWriterTest::suite());
|
||||
pSuite->addTest(SAXTestSuite::suite());
|
||||
pSuite->addTest(DOMTestSuite::suite());
|
||||
pSuite->addTest(XMLStreamParserTestSuite::suite());
|
||||
pSuite->addTest(XMLStreamParserTest::suite());
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user