mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 08:31:43 +02:00
Initial adding of XMLStream API based on libstudxml library
This commit is contained in:
43
XML/include/Poco/XML/Content.h
Normal file
43
XML/include/Poco/XML/Content.h
Normal file
@@ -0,0 +1,43 @@
|
||||
// file : xml/content -*- C++ -*-
|
||||
// copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
|
||||
// license : MIT; see accompanying LICENSE file
|
||||
|
||||
#ifndef POCO_XML_CONTENT
|
||||
#define POCO_XML_CONTENT
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
|
||||
/// XML content model. C++11 enum class emulated for C++98.
|
||||
struct Content
|
||||
{
|
||||
enum value
|
||||
{
|
||||
// element characters whitespaces notes
|
||||
Empty, // no no ignored
|
||||
Simple, // no yes preserved content accumulated
|
||||
Complex, // yes no ignored
|
||||
Mixed // yes yes preserved
|
||||
};
|
||||
|
||||
Content(value v)
|
||||
: v_(v)
|
||||
{
|
||||
}
|
||||
|
||||
operator value() const
|
||||
{
|
||||
return v_;
|
||||
}
|
||||
|
||||
private:
|
||||
value v_;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // XML_CONTENT
|
103
XML/include/Poco/XML/QName.h
Normal file
103
XML/include/Poco/XML/QName.h
Normal file
@@ -0,0 +1,103 @@
|
||||
// file : cutl/xml/QName.hxx
|
||||
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
|
||||
// license : MIT; see accompanying LICENSE file
|
||||
|
||||
#ifndef POCO_XML_QNAME_HXX
|
||||
#define POCO_XML_QNAME_HXX
|
||||
|
||||
#include "Poco/XML/XML.h"
|
||||
|
||||
#include <string>
|
||||
#include <iosfwd>
|
||||
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
|
||||
// Note that the optional prefix is just a "syntactic sugar". In
|
||||
// particular, it is ignored by the comparison operators and the
|
||||
// std::ostream insertion operator.
|
||||
//
|
||||
class XML_API QName
|
||||
{
|
||||
public:
|
||||
QName()
|
||||
{
|
||||
}
|
||||
QName(const std::string& name) :
|
||||
name_(name)
|
||||
{
|
||||
}
|
||||
QName(const std::string& ns, const std::string& name) :
|
||||
ns_(ns),
|
||||
name_(name)
|
||||
{
|
||||
}
|
||||
QName(const std::string& ns, const std::string& name, const std::string& prefix) :
|
||||
ns_(ns),
|
||||
name_(name),
|
||||
prefix_(prefix)
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& namespace_() const
|
||||
{
|
||||
return ns_;
|
||||
}
|
||||
const std::string& name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
const std::string& prefix() const
|
||||
{
|
||||
return prefix_;
|
||||
}
|
||||
|
||||
std::string& namespace_()
|
||||
{
|
||||
return ns_;
|
||||
}
|
||||
std::string& name()
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
std::string& prefix()
|
||||
{
|
||||
return prefix_;
|
||||
}
|
||||
|
||||
// Printable representation in the [<namespace>#]<name> form.
|
||||
//
|
||||
std::string string() const;
|
||||
|
||||
// Note that comparison operators
|
||||
//
|
||||
public:
|
||||
friend bool operator<(const QName& x, const QName& y)
|
||||
{
|
||||
return x.ns_ < y.ns_ || (x.ns_ == y.ns_ && x.name_ < y.name_);
|
||||
}
|
||||
|
||||
friend bool operator==(const QName& x, const QName& y)
|
||||
{
|
||||
return x.ns_ == y.ns_ && x.name_ == y.name_;
|
||||
}
|
||||
|
||||
friend bool operator!=(const QName& x, const QName& y)
|
||||
{
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string ns_;
|
||||
std::string name_;
|
||||
std::string prefix_;
|
||||
};
|
||||
|
||||
XML_API std::ostream& operator<<(std::ostream&, const QName&);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CUTL_XML_QNAME_HXX
|
90
XML/include/Poco/XML/ValueTraits.h
Normal file
90
XML/include/Poco/XML/ValueTraits.h
Normal file
@@ -0,0 +1,90 @@
|
||||
// file : cutl/xml/value-traits.hxx
|
||||
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
|
||||
// license : MIT; see accompanying LICENSE file
|
||||
|
||||
#ifndef POCO_XML_VALUE_TRAITS_HXX
|
||||
#define POCO_XML_VALUE_TRAITS_HXX
|
||||
|
||||
#include <string>
|
||||
#include <cstddef> // std::size_t
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "XMLStreamParserException.h"
|
||||
#include "XMLStreamSerializerException.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
class XMLStreamParser;
|
||||
class XMLStreamSerializer;
|
||||
|
||||
template<typename T>
|
||||
struct default_value_traits
|
||||
{
|
||||
static T
|
||||
parse(std::string, const XMLStreamParser&);
|
||||
|
||||
static std::string
|
||||
serialize(const T&, const XMLStreamSerializer&);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct XML_API default_value_traits<bool>
|
||||
{
|
||||
static bool
|
||||
parse(std::string, const XMLStreamParser&);
|
||||
|
||||
static std::string serialize(bool v, const XMLStreamSerializer&)
|
||||
{
|
||||
return v ? "true" : "false";
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct XML_API default_value_traits<std::string>
|
||||
{
|
||||
static std::string parse(std::string s, const XMLStreamParser&)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
static std::string serialize(const std::string& v, const XMLStreamSerializer&)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ValueTraits: default_value_traits<T>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
struct ValueTraits<T[N]> : default_value_traits<const T*>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T default_value_traits<T>::parse(std::string s, const XMLStreamParser& p)
|
||||
{
|
||||
T r;
|
||||
std::istringstream is(s);
|
||||
if (!(is >> r && is.eof()))
|
||||
throw XMLStreamParserException(p, "invalid value '" + s + "'");
|
||||
return r;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string default_value_traits<T>::serialize(const T& v, const XMLStreamSerializer& s)
|
||||
{
|
||||
std::ostringstream os;
|
||||
if (!(os << v))
|
||||
throw XMLStreamSerializerException(s, "invalid value");
|
||||
return os.str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CUTL_XML_VALUE_TRAITS_HXX
|
614
XML/include/Poco/XML/XMLStreamParser.h
Normal file
614
XML/include/Poco/XML/XMLStreamParser.h
Normal file
@@ -0,0 +1,614 @@
|
||||
// file : XMLStreamParser.hxx
|
||||
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
|
||||
// license : MIT; see accompanying LICENSE file
|
||||
|
||||
#ifndef POCO_XML_PARSER_HXX
|
||||
#define POCO_XML_PARSER_HXX
|
||||
|
||||
// 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> // std::size_t
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
class XML_API XMLStreamParser
|
||||
{
|
||||
public:
|
||||
typedef unsigned short FeatureType;
|
||||
|
||||
// If both receive_attributes_event and receive_attributes_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;
|
||||
|
||||
// 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(std::istream&, const std::string& input_name, FeatureType = RECEIVE_DEFAULT);
|
||||
|
||||
// Parse memory buffer that contains the whole document. Input name
|
||||
// is used in diagnostics to identify the document being parsed.
|
||||
//
|
||||
XMLStreamParser(const void* data, std::size_t size, const std::string& input_name, FeatureType = RECEIVE_DEFAULT);
|
||||
|
||||
const std::string& input_name() const
|
||||
{
|
||||
return iname_;
|
||||
}
|
||||
|
||||
~XMLStreamParser();
|
||||
|
||||
/// Parsing events.
|
||||
enum EventType
|
||||
{
|
||||
// If adding new events, also update the stream insertion operator.
|
||||
//
|
||||
StartElement,
|
||||
EndElement,
|
||||
StartAttribute,
|
||||
EndAttribute,
|
||||
Characters,
|
||||
StartNamespaceDecl,
|
||||
EndNamespaceDecl,
|
||||
Eof
|
||||
};
|
||||
|
||||
EventType next();
|
||||
|
||||
// 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);
|
||||
|
||||
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();
|
||||
|
||||
// Return the even that was last returned by the call to next() or
|
||||
// peek().
|
||||
//
|
||||
EventType event()
|
||||
{
|
||||
return event_;
|
||||
}
|
||||
|
||||
// Event data.
|
||||
//
|
||||
const QName& qname() const
|
||||
{
|
||||
return *pqname_;
|
||||
}
|
||||
|
||||
const std::string& namespace_() const
|
||||
{
|
||||
return pqname_->namespace_();
|
||||
}
|
||||
|
||||
const std::string& name() const
|
||||
{
|
||||
return pqname_->name();
|
||||
}
|
||||
|
||||
const std::string& prefix() const
|
||||
{
|
||||
return pqname_->prefix();
|
||||
}
|
||||
|
||||
std::string& value()
|
||||
{
|
||||
return *pvalue_;
|
||||
}
|
||||
|
||||
const std::string& value() const
|
||||
{
|
||||
return *pvalue_;
|
||||
}
|
||||
|
||||
template<typename T> T value() const;
|
||||
|
||||
Poco::UInt64 line() const
|
||||
{
|
||||
return line_;
|
||||
}
|
||||
|
||||
Poco::UInt64 column() const
|
||||
{
|
||||
return column_;
|
||||
}
|
||||
|
||||
// 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 startElement. As a special case,
|
||||
// the map is still valid after peek() that returned end_element until
|
||||
// this end_element event is retrieved with next().
|
||||
//
|
||||
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& default_value) const;
|
||||
|
||||
template<typename T>
|
||||
T attribute(const std::string& name, const T& default_value) 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& default_value) const;
|
||||
|
||||
template<typename T>
|
||||
T attribute(const QName& qname, const T& default_value) const;
|
||||
|
||||
bool attributePresent(const std::string& name) const;
|
||||
|
||||
bool attributePresent(const QName& qname) const;
|
||||
|
||||
// Low-level attribute map access. Note that this API assumes
|
||||
// all attributes are handled.
|
||||
//
|
||||
struct AttributeValueType
|
||||
{
|
||||
std::string value;
|
||||
mutable bool handled;
|
||||
};
|
||||
|
||||
typedef std::map<QName, AttributeValueType> AttributeMapType;
|
||||
|
||||
const AttributeMapType& attributeMap() const;
|
||||
|
||||
// Optional content processing.
|
||||
//
|
||||
|
||||
// Note that you cannot get/set content while peeking.
|
||||
//
|
||||
void content(Content);
|
||||
|
||||
Content content() const;
|
||||
|
||||
// Versions that also set the content. Event type must be startElement.
|
||||
//
|
||||
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 startElement 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).
|
||||
//
|
||||
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& 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);
|
||||
|
||||
// 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.
|
||||
//
|
||||
struct Iterator
|
||||
{
|
||||
typedef EventType value_type;
|
||||
|
||||
Iterator(XMLStreamParser* p = 0, EventType e = Eof) :
|
||||
p_(p),
|
||||
e_(e)
|
||||
{
|
||||
}
|
||||
value_type operator*() const
|
||||
{
|
||||
return e_;
|
||||
}
|
||||
Iterator& operator++()
|
||||
{
|
||||
e_ = p_->next();
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Comparison only makes sense when comparing to end (eof).
|
||||
//
|
||||
bool operator==(Iterator y) const
|
||||
{
|
||||
return e_ == Eof && y.e_ == Eof;
|
||||
}
|
||||
bool operator!=(Iterator y) const
|
||||
{
|
||||
return !(*this == y);
|
||||
}
|
||||
|
||||
private:
|
||||
XMLStreamParser* p_;
|
||||
EventType e_;
|
||||
};
|
||||
|
||||
Iterator begin()
|
||||
{
|
||||
return Iterator(this, next());
|
||||
}
|
||||
Iterator end()
|
||||
{
|
||||
return Iterator(this, Eof);
|
||||
}
|
||||
|
||||
private:
|
||||
XMLStreamParser(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*);
|
||||
|
||||
void init();
|
||||
EventType next_(bool peek);
|
||||
EventType next_body();
|
||||
void handle_error();
|
||||
|
||||
// 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 iname_;
|
||||
FeatureType feature_;
|
||||
|
||||
XML_Parser p_;
|
||||
std::size_t depth_;
|
||||
bool accumulate_; // Whether we are accumulating character content.
|
||||
enum
|
||||
{
|
||||
state_next, state_peek
|
||||
}state_;
|
||||
EventType event_;
|
||||
EventType queue_;
|
||||
|
||||
QName qname_;
|
||||
std::string value_;
|
||||
|
||||
// These are used to avoid copying when we are handling attributes
|
||||
// and namespace decls.
|
||||
//
|
||||
const QName* pqname_;
|
||||
std::string* pvalue_;
|
||||
|
||||
Poco::UInt64 line_;
|
||||
Poco::UInt64 column_;
|
||||
|
||||
// Attributes as events.
|
||||
//
|
||||
struct attribute_type
|
||||
{
|
||||
QName qname;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
typedef std::vector<attribute_type> attributes;
|
||||
|
||||
attributes attr_;
|
||||
attributes::size_type attr_i_; // Index of the current attribute.
|
||||
|
||||
// Namespace declarations.
|
||||
//
|
||||
typedef std::vector<QName> namespace_decls;
|
||||
|
||||
namespace_decls start_ns_;
|
||||
namespace_decls::size_type start_ns_i_;// Index of the current decl.
|
||||
|
||||
namespace_decls end_ns_;
|
||||
namespace_decls::size_type end_ns_i_;// Index of the current decl.
|
||||
|
||||
// Element state consisting of the content model and attribute map.
|
||||
//
|
||||
struct ElementEntry
|
||||
{
|
||||
ElementEntry(std::size_t d, Content c = Content::Mixed) :
|
||||
depth(d),
|
||||
content(c),
|
||||
attr_unhandled_(0)
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t depth;
|
||||
Content content;
|
||||
AttributeMapType attr_map_;
|
||||
mutable AttributeMapType::size_type attr_unhandled_;
|
||||
};
|
||||
|
||||
typedef std::vector<ElementEntry> ElementState;
|
||||
std::vector<ElementEntry> element_state_;
|
||||
|
||||
// Empty attribute map to return when an element has no attributes.
|
||||
//
|
||||
const AttributeMapType empty_attr_map_;
|
||||
|
||||
// Return the element entry corresponding to the current depth, if
|
||||
// exists, and NULL otherwise.
|
||||
//
|
||||
const ElementEntry* getElement() const;
|
||||
|
||||
const ElementEntry* get_element_() const;
|
||||
|
||||
void pop_element();
|
||||
};
|
||||
|
||||
XML_API std::ostream& operator<<(std::ostream&, XMLStreamParser::EventType);
|
||||
|
||||
inline XMLStreamParser::XMLStreamParser(std::istream& is, const std::string& iname, FeatureType f)
|
||||
: size_(0), iname_(iname), feature_(f)
|
||||
{
|
||||
data_.is = &is;
|
||||
init();
|
||||
}
|
||||
|
||||
inline XMLStreamParser::XMLStreamParser(const void* data, std::size_t size, const std::string& iname, FeatureType f)
|
||||
: size_(size), iname_(iname), feature_(f)
|
||||
{
|
||||
assert(data != 0 && size != 0);
|
||||
|
||||
data_.buf = data;
|
||||
init();
|
||||
}
|
||||
|
||||
inline XMLStreamParser::EventType XMLStreamParser::peek()
|
||||
{
|
||||
if (state_ == state_peek)
|
||||
return event_;
|
||||
else
|
||||
{
|
||||
EventType e(next_(true));
|
||||
state_ = state_peek; // Set it after the call to next_().
|
||||
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->attr_unhandled_ = 0; // Assume all handled.
|
||||
return e->attr_map_;
|
||||
}
|
||||
|
||||
return empty_attr_map_;
|
||||
}
|
||||
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const QName& qn)
|
||||
{
|
||||
nextExpect(e, qn.namespace_(), qn.name());
|
||||
}
|
||||
|
||||
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);
|
||||
assert(e == StartElement);
|
||||
content(c);
|
||||
}
|
||||
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const std::string& n, Content c)
|
||||
{
|
||||
nextExpect(e, std::string(), n);
|
||||
assert(e == StartElement);
|
||||
content(c);
|
||||
}
|
||||
|
||||
inline void XMLStreamParser::nextExpect(EventType e, const std::string& ns, const std::string& n, Content c)
|
||||
{
|
||||
nextExpect(e, ns, n);
|
||||
assert(e == StartElement);
|
||||
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(StartElement, n);
|
||||
return element();
|
||||
}
|
||||
|
||||
inline std::string XMLStreamParser::element(const QName& qn)
|
||||
{
|
||||
nextExpect(StartElement, 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)
|
||||
{
|
||||
assert(state_ == state_next);
|
||||
|
||||
if (!element_state_.empty() && element_state_.back().depth == depth_)
|
||||
element_state_.back().content = c;
|
||||
else
|
||||
element_state_.push_back(ElementEntry(depth_, c));
|
||||
}
|
||||
|
||||
inline Content XMLStreamParser::content() const
|
||||
{
|
||||
assert(state_ == state_next);
|
||||
|
||||
return !element_state_.empty() && element_state_.back().depth == depth_ ? element_state_.back().content : Content(Content::Mixed);
|
||||
}
|
||||
|
||||
inline const XMLStreamParser::ElementEntry* XMLStreamParser::getElement() const
|
||||
{
|
||||
return element_state_.empty() ? 0 : get_element_();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T XMLStreamParser::attribute(const QName& qn, const T& dv) const
|
||||
{
|
||||
if (const ElementEntry* e = getElement())
|
||||
{
|
||||
AttributeMapType::const_iterator i(e->attr_map_.find(qn));
|
||||
|
||||
if (i != e->attr_map_.end())
|
||||
{
|
||||
if (!i->second.handled)
|
||||
{
|
||||
i->second.handled = true;
|
||||
e->attr_unhandled_--;
|
||||
}
|
||||
return ValueTraits < T > ::parse(i->second.value, *this);
|
||||
}
|
||||
}
|
||||
|
||||
return dv;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T XMLStreamParser::element(const QName& qn, const T& dv)
|
||||
{
|
||||
if (peek() == StartElement && qname() == qn)
|
||||
{
|
||||
next();
|
||||
return element<T>();
|
||||
}
|
||||
|
||||
return dv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CUTL_XML_PARSER_HXX
|
69
XML/include/Poco/XML/XMLStreamParserException.h
Normal file
69
XML/include/Poco/XML/XMLStreamParserException.h
Normal file
@@ -0,0 +1,69 @@
|
||||
///
|
||||
/// \package metamodel
|
||||
/// \file XMLStreamException.h
|
||||
///
|
||||
/// \author Marian Krivos <marian.krivos@rsys.sk>
|
||||
/// \date Aug 21, 2015 - 6:52:24 PM
|
||||
/// \brief definicia typu
|
||||
///
|
||||
/// (C) Copyright 2015 R-SYS,s.r.o
|
||||
/// All rights reserved.
|
||||
///
|
||||
|
||||
#ifndef POCO_XML_XMLSTREAMPARSEREXCEPTION_H_
|
||||
#define POCO_XML_XMLSTREAMPARSEREXCEPTION_H_
|
||||
|
||||
#include <Poco/DOM/DOMException.h>
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
class XMLStreamParser;
|
||||
|
||||
class XML_API XMLStreamParserException :
|
||||
public Poco::XML::XMLException
|
||||
{
|
||||
public:
|
||||
XMLStreamParserException(const std::string& name, Poco::UInt64 line, Poco::UInt64 column, const std::string& description);
|
||||
|
||||
XMLStreamParserException(const XMLStreamParser&, const std::string& description);
|
||||
|
||||
virtual ~XMLStreamParserException() throw ();
|
||||
|
||||
const char* name() const throw()
|
||||
{
|
||||
return name_.c_str();
|
||||
}
|
||||
|
||||
Poco::UInt64 line() const
|
||||
{
|
||||
return line_;
|
||||
}
|
||||
|
||||
Poco::UInt64 column() const
|
||||
{
|
||||
return column_;
|
||||
}
|
||||
|
||||
const std::string& description() const
|
||||
{
|
||||
return description_;
|
||||
}
|
||||
|
||||
virtual const char* what() const throw ();
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
std::string name_;
|
||||
Poco::UInt64 line_;
|
||||
Poco::UInt64 column_;
|
||||
std::string description_;
|
||||
std::string what_;
|
||||
};
|
||||
|
||||
}
|
||||
/* namespace XML */
|
||||
} /* namespace Poco */
|
||||
#endif /* POCO_XML_XMLSTREAMPARSEREXCEPTION_H_ */
|
239
XML/include/Poco/XML/XMLStreamSerializer.h
Normal file
239
XML/include/Poco/XML/XMLStreamSerializer.h
Normal file
@@ -0,0 +1,239 @@
|
||||
// file : xml/XMLStreamSerializer -*- C++ -*-
|
||||
// copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
|
||||
// license : MIT; see accompanying LICENSE file
|
||||
|
||||
#ifndef POCO_XML_XMLSERIALIZER
|
||||
#define POCO_XML_XMLSERIALIZER
|
||||
|
||||
#include "QName.h"
|
||||
#include "ValueTraits.h"
|
||||
#include "genx.h"
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
#include <cstddef> // std::size_t
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
class XMLStreamSerializer;
|
||||
|
||||
class XML_API XMLStreamSerializer
|
||||
{
|
||||
public:
|
||||
// Serialize to std::ostream. Output name is used in diagnostics to
|
||||
// identify the document being serialized. The indentation argument
|
||||
// specifies the number of indentation spaces that should be used for
|
||||
// pretty-printing. If 0 is passed, no pretty-printing is performed.
|
||||
//
|
||||
// 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 XMLStreamSerializerException exception.
|
||||
//
|
||||
XMLStreamSerializer(std::ostream&, const std::string& output_name, unsigned short indentation = 2);
|
||||
|
||||
const std::string& outputName() const
|
||||
{
|
||||
return oname_;
|
||||
}
|
||||
|
||||
~XMLStreamSerializer();
|
||||
|
||||
void startElement(const QName& qname);
|
||||
|
||||
void startElement(const std::string& name);
|
||||
|
||||
void startElement(const std::string& ns, const std::string& name);
|
||||
|
||||
void endElement();
|
||||
|
||||
// Helpers for serializing elements with simple content. The first two
|
||||
// functions assume that startElement() has already been called. The
|
||||
// other two serialize the complete element, from start to end.
|
||||
//
|
||||
void element(const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void element(const T& value);
|
||||
|
||||
void element(const std::string& name, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void element(const std::string& name, const T& value);
|
||||
|
||||
void element(const QName& qname, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void element(const QName& qname, const T& value);
|
||||
|
||||
void element(const std::string& namespace_, const std::string& name, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void element(const std::string& namespace_, const std::string& name, const T& value);
|
||||
|
||||
// Attributes.
|
||||
//
|
||||
void startAttribute(const QName& qname);
|
||||
|
||||
void startAttribute(const std::string& name);
|
||||
|
||||
void startAttribute(const std::string& ns, const std::string& name);
|
||||
|
||||
void endAttribute();
|
||||
|
||||
void attribute(const QName& qname, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void attribute(const QName& qname, const T& value);
|
||||
|
||||
void attribute(const std::string& name, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void attribute(const std::string& name, const T& value);
|
||||
|
||||
void attribute(const std::string& ns, const std::string& name, const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void attribute(const std::string& ns, const std::string& name, const T& value);
|
||||
|
||||
// Characters.
|
||||
//
|
||||
void characters(const std::string& value);
|
||||
|
||||
template<typename T>
|
||||
void characters(const T& value);
|
||||
|
||||
// Namespaces declaration. If prefix is empty, then the default
|
||||
// namespace is declared. If both prefix and namespace are empty,
|
||||
// then the default namespace declaration is cleared (xmlns="").
|
||||
//
|
||||
void namespaceDecl(const std::string& ns, const std::string& prefix);
|
||||
|
||||
// XML declaration. If encoding or standalone are not specified,
|
||||
// then these attributes are omitted from the output.
|
||||
//
|
||||
void xmlDecl(const std::string& version = "1.0", const std::string& encoding = "UTF-8", const std::string& standalone = "");
|
||||
|
||||
// Utility functions.
|
||||
//
|
||||
// Return true if there is a mapping. In this case, prefix contains
|
||||
// the mapped prefix.
|
||||
//
|
||||
bool lookupNamespacePrefix(const std::string& ns, std::string& prefix);
|
||||
|
||||
private:
|
||||
XMLStreamSerializer(const XMLStreamSerializer&);
|
||||
XMLStreamSerializer& operator=(const XMLStreamSerializer&);
|
||||
|
||||
void handleError(genxStatus);
|
||||
|
||||
std::ostream& os_;
|
||||
std::ostream::iostate os_state_;// Original exception state.
|
||||
const std::string oname_;
|
||||
|
||||
genxWriter s_;
|
||||
genxSender sender_;
|
||||
std::size_t depth_;
|
||||
};
|
||||
|
||||
inline void XMLStreamSerializer::startElement(const QName& qname)
|
||||
{
|
||||
startElement(qname.namespace_(), qname.name());
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::startElement(const std::string& name)
|
||||
{
|
||||
startElement(std::string(), name);
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::element(const std::string& v)
|
||||
{
|
||||
if (!v.empty())
|
||||
characters(v);
|
||||
|
||||
endElement();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::element(const T& v)
|
||||
{
|
||||
element(ValueTraits < T > ::serialize(v, *this));
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::element(const std::string& n, const std::string& v)
|
||||
{
|
||||
element(std::string(), n, v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::element(const std::string& n, const T& v)
|
||||
{
|
||||
element(n, ValueTraits < T > ::serialize(v, *this));
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::element(const QName& qn, const std::string& v)
|
||||
{
|
||||
element(qn.namespace_(), qn.name(), v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::element(const QName& qn, const T& v)
|
||||
{
|
||||
element(qn, ValueTraits < T > ::serialize(v, *this));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::element(const std::string& ns, const std::string& n, const T& v)
|
||||
{
|
||||
element(ns, n, ValueTraits < T > ::serialize(v, *this));
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::startAttribute(const QName& qname)
|
||||
{
|
||||
startAttribute(qname.namespace_(), qname.name());
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::startAttribute(const std::string& name)
|
||||
{
|
||||
startAttribute(std::string(), name);
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::attribute(const QName& qname, const std::string& value)
|
||||
{
|
||||
attribute(qname.namespace_(), qname.name(), value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::attribute(const QName& qname, const T& value)
|
||||
{
|
||||
attribute(qname, ValueTraits < T > ::serialize(value, *this));
|
||||
}
|
||||
|
||||
inline void XMLStreamSerializer::attribute(const std::string& name, const std::string& value)
|
||||
{
|
||||
attribute(std::string(), name, value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::attribute(const std::string& name, const T& value)
|
||||
{
|
||||
attribute(name, ValueTraits < T > ::serialize(value, *this));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::attribute(const std::string& ns, const std::string& name, const T& value)
|
||||
{
|
||||
attribute(ns, name, ValueTraits < T > ::serialize(value, *this));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void XMLStreamSerializer::characters(const T& value)
|
||||
{
|
||||
characters(ValueTraits < T > ::serialize(value, *this));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif // XML_SERIALIZER
|
57
XML/include/Poco/XML/XMLStreamSerializerException.h
Normal file
57
XML/include/Poco/XML/XMLStreamSerializerException.h
Normal file
@@ -0,0 +1,57 @@
|
||||
///
|
||||
/// \package metamodel
|
||||
/// \file XMLStreamException.h
|
||||
///
|
||||
/// \author Marian Krivos <marian.krivos@rsys.sk>
|
||||
/// \date Aug 21, 2015 - 6:52:24 PM
|
||||
/// \brief definicia typu
|
||||
///
|
||||
/// (C) Copyright 2015 R-SYS,s.r.o
|
||||
/// All rights reserved.
|
||||
///
|
||||
|
||||
#ifndef POCO_XML_XMLSTREAMSERIALIZEREXCEPTION_H_
|
||||
#define POCO_XML_XMLSTREAMSERIALIZEREXCEPTION_H_
|
||||
|
||||
#include <Poco/DOM/DOMException.h>
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace XML
|
||||
{
|
||||
class XMLStreamSerializer;
|
||||
|
||||
struct XML_API XMLStreamSerializerException:
|
||||
public Poco::XML::XMLException
|
||||
{
|
||||
virtual ~XMLStreamSerializerException() throw ();
|
||||
|
||||
XMLStreamSerializerException(const std::string& name, const std::string& description);
|
||||
|
||||
XMLStreamSerializerException(const XMLStreamSerializer&, const std::string& description);
|
||||
|
||||
const char* name() const throw ()
|
||||
{
|
||||
return name_.c_str();
|
||||
}
|
||||
|
||||
const std::string& description() const
|
||||
{
|
||||
return description_;
|
||||
}
|
||||
|
||||
virtual const char* what() const throw ();
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string description_;
|
||||
std::string what_;
|
||||
};
|
||||
|
||||
}
|
||||
/* namespace XML */
|
||||
} /* namespace Poco */
|
||||
#endif /* POCO_XML_XMLSTREAMPARSEREXCEPTION_H_ */
|
Reference in New Issue
Block a user