Added XMLStream API tests

This commit is contained in:
Marian Krivos
2015-08-22 20:58:45 +02:00
parent 0bd0559508
commit 5c0ab24c41
16 changed files with 872 additions and 152 deletions

View File

@@ -12,16 +12,42 @@ namespace Poco
namespace XML
{
QName::QName()
{
}
QName::QName(const std::string& name) :
_name(name)
{
}
QName::QName(const std::string& ns, const std::string& name) :
_ns(ns),
_name(name)
{
}
QName::QName(const std::string& ns, const std::string& name, const std::string& prefix) :
_ns(ns),
_name(name),
_prefix(prefix)
{
}
string QName::string() const
{
std::string r;
if (!ns_.empty())
if (!_ns.empty())
{
r += ns_;
r += _ns;
r += '#';
}
r += name_;
r += _name;
return r;
}

View File

@@ -17,7 +17,6 @@ namespace Poco
namespace XML
{
struct stream_exception_controller
{
~stream_exception_controller()
@@ -160,6 +159,7 @@ void XMLStreamParser::handle_error()
throw XMLStreamParserException(iname_, XML_GetCurrentLineNumber(p_), XML_GetCurrentColumnNumber(p_), XML_ErrorString(e));
}
XMLStreamParser::EventType XMLStreamParser::next()
{
if (state_ == state_next)
@@ -174,7 +174,7 @@ XMLStreamParser::EventType XMLStreamParser::next()
case EndElement:
{
if (!element_state_.empty() && element_state_.back().depth == depth_)
pop_element();
popElement();
depth_--;
break;
@@ -329,7 +329,7 @@ const XMLStreamParser::ElementEntry* XMLStreamParser::get_element_() const
}
void XMLStreamParser::pop_element()
void XMLStreamParser::popElement()
{
// Make sure there are no unhandled attributes left.
//
@@ -372,7 +372,7 @@ XMLStreamParser::EventType XMLStreamParser::next_(bool peek)
if (!peek)
{
if (!element_state_.empty() && element_state_.back().depth == depth_)
pop_element();
popElement();
depth_--;
}

View File

@@ -27,14 +27,14 @@ XMLStreamParserException::~XMLStreamParserException() throw ()
XMLStreamParserException::XMLStreamParserException(const string& n, Poco::UInt64 l, Poco::UInt64 c, const string& d)
: name_(n), line_(l), column_(c), description_(d)
: _name(n), _line(l), _column(c), _description(d)
{
init();
}
XMLStreamParserException::XMLStreamParserException(const XMLStreamParser& p, const std::string& d)
: name_(p.input_name()), line_(p.line()), column_(p.column()), description_(d)
: _name(p.inputName()), _line(p.line()), _column(p.column()), _description(d)
{
init();
}
@@ -43,16 +43,36 @@ XMLStreamParserException::XMLStreamParserException(const XMLStreamParser& p, con
void XMLStreamParserException::init()
{
std::ostringstream os;
if (!name_.empty())
os << name_ << ':';
os << line_ << ':' << column_ << ": error: " << description_;
what_ = os.str();
if (!_name.empty())
os << _name << ':';
os << _line << ':' << _column << ": error: " << _description;
_what = os.str();
}
const char* XMLStreamParserException::name() const throw()
{
return _name.c_str();
}
Poco::UInt64 XMLStreamParserException::line() const
{
return _line;
}
Poco::UInt64 XMLStreamParserException::column() const
{
return _column;
}
const std::string& XMLStreamParserException::description() const
{
return _description;
}
char const* XMLStreamParserException::what() const throw ()
{
return what_.c_str();
return _what.c_str();
}

View File

@@ -47,39 +47,39 @@ extern "C" genxStatus genx_flush(void* p)
XMLStreamSerializer::~XMLStreamSerializer()
{
if (s_ != 0)
genxDispose (s_);
if (_writer != 0)
genxDispose (_writer);
}
XMLStreamSerializer::XMLStreamSerializer(ostream& os, const string& oname, unsigned short ind)
: os_(os), os_state_(os.exceptions()), oname_(oname), depth_(0)
: _outputStream(os), _osState_(os.exceptions()), _oname(oname), _depth(0)
{
// Temporarily disable exceptions on the stream.
//
os_.exceptions(ostream::goodbit);
_outputStream.exceptions(ostream::goodbit);
// Allocate the XMLStreamSerializer. Make sure nothing else can throw after
// this call since otherwise we will leak it.
//
s_ = genxNew(0, 0, 0);
_writer = genxNew(0, 0, 0);
if (s_ == 0)
if (_writer == 0)
throw bad_alloc();
genxSetUserData(s_, &os_);
genxSetUserData(_writer, &_outputStream);
if (ind != 0)
genxSetPrettyPrint(s_, ind);
genxSetPrettyPrint(_writer, ind);
sender_.send = &genx_write;
sender_.sendBounded = &genx_write_bound;
sender_.flush = &genx_flush;
_sender.send = &genx_write;
_sender.sendBounded = &genx_write_bound;
_sender.flush = &genx_flush;
if (genxStatus e = genxStartDocSender(s_, &sender_))
if (genxStatus e = genxStartDocSender(_writer, &_sender))
{
string m(genxGetErrorMessage(s_, e));
genxDispose (s_);
string m(genxGetErrorMessage(_writer, e));
genxDispose (_writer);
throw XMLStreamSerializerException(oname, m);
}
}
@@ -97,38 +97,38 @@ void XMLStreamSerializer::handleError(genxStatus e)
// configure the stream to throw), then fall back to the
// serialiation exception.
//
os_.exceptions(os_state_);
_outputStream.exceptions(_osState_);
// Fall through.
default:
throw XMLStreamSerializerException(oname_, genxGetErrorMessage(s_, e));
throw XMLStreamSerializerException(_oname, genxGetErrorMessage(_writer, e));
}
}
void XMLStreamSerializer::startElement(const string& ns, const string& name)
{
if (genxStatus e = genxStartElementLiteral(s_, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str())))
if (genxStatus e = genxStartElementLiteral(_writer, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str())))
handleError(e);
depth_++;
_depth++;
}
void XMLStreamSerializer::endElement()
{
if (genxStatus e = genxEndElement(s_))
if (genxStatus e = genxEndElement(_writer))
handleError(e);
// Call EndDocument() if we are past the root element.
//
if (--depth_ == 0)
if (--_depth == 0)
{
if (genxStatus e = genxEndDocument(s_))
if (genxStatus e = genxEndDocument(_writer))
handleError(e);
// Also restore the original exception state on the stream.
//
os_.exceptions(os_state_);
_outputStream.exceptions(_osState_);
}
}
@@ -142,21 +142,21 @@ void XMLStreamSerializer::element(const string& ns, const string& n, const strin
void XMLStreamSerializer::startAttribute(const string& ns, const string& name)
{
if (genxStatus e = genxStartAttributeLiteral(s_, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str())))
if (genxStatus e = genxStartAttributeLiteral(_writer, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str())))
handleError(e);
}
void XMLStreamSerializer::endAttribute()
{
if (genxStatus e = genxEndAttribute(s_))
if (genxStatus e = genxEndAttribute(_writer))
handleError(e);
}
void XMLStreamSerializer::attribute(const string& ns, const string& name, const string& value)
{
if (genxStatus e = genxAddAttributeLiteral(s_, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str()),
if (genxStatus e = genxAddAttributeLiteral(_writer, reinterpret_cast<constUtf8>(ns.empty() ? 0 : ns.c_str()), reinterpret_cast<constUtf8>(name.c_str()),
reinterpret_cast<constUtf8>(value.c_str())))
handleError(e);
}
@@ -164,7 +164,7 @@ void XMLStreamSerializer::attribute(const string& ns, const string& name, const
void XMLStreamSerializer::characters(const string& value)
{
if (genxStatus e = genxAddCountedText(s_, reinterpret_cast<constUtf8>(value.c_str()), value.size()))
if (genxStatus e = genxAddCountedText(_writer, reinterpret_cast<constUtf8>(value.c_str()), value.size()))
handleError(e);
}
@@ -173,15 +173,15 @@ void XMLStreamSerializer::namespaceDecl(const string& ns, const string& p)
{
if (genxStatus e =
ns.empty() && p.empty() ?
genxUnsetDefaultNamespace(s_) :
genxAddNamespaceLiteral(s_, reinterpret_cast<constUtf8>(ns.c_str()), reinterpret_cast<constUtf8>(p.c_str())))
genxUnsetDefaultNamespace(_writer) :
genxAddNamespaceLiteral(_writer, reinterpret_cast<constUtf8>(ns.c_str()), reinterpret_cast<constUtf8>(p.c_str())))
handleError(e);
}
void XMLStreamSerializer::xmlDecl(const string& ver, const string& enc, const string& stl)
{
if (genxStatus e = genxXmlDeclaration(s_, reinterpret_cast<constUtf8>(ver.c_str()), (enc.empty() ? 0 : reinterpret_cast<constUtf8>(enc.c_str())),
if (genxStatus e = genxXmlDeclaration(_writer, reinterpret_cast<constUtf8>(ver.c_str()), (enc.empty() ? 0 : reinterpret_cast<constUtf8>(enc.c_str())),
(stl.empty() ? 0 : reinterpret_cast<constUtf8>(stl.c_str()))))
handleError(e);
}
@@ -193,7 +193,7 @@ bool XMLStreamSerializer::lookupNamespacePrefix(const string& ns, string& p)
// already exist.
//
genxStatus e;
genxNamespace gns(genxDeclareNamespace(s_, reinterpret_cast<constUtf8>(ns.c_str()), 0, &e));
genxNamespace gns(genxDeclareNamespace(_writer, reinterpret_cast<constUtf8>(ns.c_str()), 0, &e));
if (e != GENX_SUCCESS)
handleError(e);

View File

@@ -25,45 +25,45 @@ XMLStreamSerializerException::~XMLStreamSerializerException() throw ()
}
XMLStreamSerializerException::XMLStreamSerializerException(const string& n, const string& d)
: name_(n), description_(d)
: _name(n), _description(d)
{
init();
}
XMLStreamSerializerException::XMLStreamSerializerException(const XMLStreamSerializer& s, const std::string& d)
: name_(s.outputName()), description_(d)
: _name(s.outputName()), _description(d)
{
init();
}
void XMLStreamSerializerException::init()
{
if (!name_.empty())
if (!_name.empty())
{
what_ += name_;
what_ += ": ";
_what += _name;
_what += ": ";
}
what_ += "error: ";
what_ += description_;
_what += "error: ";
_what += _description;
}
const char* XMLStreamSerializerException::name() const throw ()
{
return name_.c_str();
return _name.c_str();
}
const std::string& XMLStreamSerializerException::description() const
{
return description_;
return _description;
}
char const* XMLStreamSerializerException::what() const throw ()
{
return what_.c_str();
return _what.c_str();
}
} /* namespace XML */