Add RoundTrip example for XML Streaming API + backport lates libstudxml changes from upstream

This commit is contained in:
Marian Krivos
2015-08-23 14:38:57 +02:00
parent ce003c5522
commit eca1119c82
8 changed files with 615 additions and 30 deletions

View File

@@ -55,8 +55,16 @@ public:
void startElement(const QName& qname);
void startElement(const std::string& name);
void startElement(const std::string& ns, const std::string& name);
void endElement();
// "Checked" end element. That is, it checks that the element
// you think you are ending matches the current one.
//
void endElement (const QName& qname);
void endElement (const std::string& name);
void endElement (const std::string& ns, const std::string& name);
// 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.
@@ -79,7 +87,17 @@ public:
void startAttribute(const QName& qname);
void startAttribute(const std::string& name);
void startAttribute(const std::string& ns, const std::string& name);
void endAttribute();
// "Checked" end attribute. That is, it checks that the attribute
// you think you are ending matches the current one.
//
void endAttribute (const QName& qname);
void endAttribute (const std::string& name);
void endAttribute (const std::string& ns, const std::string& name);
void attribute(const QName& qname, const std::string& value);
template<typename T>
void attribute(const QName& qname, const T& value);
@@ -100,23 +118,61 @@ public:
/// 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="").
/// This function should be called after start_element().
void xmlDecl(const std::string& version = "1.0", const std::string& encoding = "UTF-8", const std::string& standalone = "");
/// XML declaration. If encoding or standalone are not specified,
/// then these attributes are omitted from the output.
// DOCTYPE declaration. If encoding or standalone are not specified,
// then these attributes are omitted from the output.
//
void doctypeDecl (const std::string& root_element,
const std::string& public_id = "",
const std::string& system_id = "",
const std::string& internal_subset = "");
// Utility functions.
//
bool lookupNamespacePrefix(const std::string& ns, std::string& prefix);
bool lookupNamespacePrefix(const std::string& ns, std::string& prefix) const;
/// Return true if there is a mapping. In this case, prefix contains
/// the mapped prefix.
// Return the current element, that is, the latest element for which
// start_element() but not endElement() have been called.
//
QName currentElement () const;
// Return the current attribute, that is, the latest attribute for
// which start_attribute() but not endAttribute() have been called.
//
QName currentAttribute () const;
// Suspend/resume indentation.
//
// Indentation can be suspended only inside an element and, unless
// explicitly resumed, it will remain suspended until the end of
// that element. You should only explicitly resume indentation at
// the element nesting level of suspension. If indentation is already
// suspended at an outer nesting level, then subsequent calls to
// suspend/resume are ignored. The indentation_suspended() function
// can be used to check if indentation is currently suspended. If it
// is not, then this function returns 0. Otherwise, it returns the
// level at which pretty-printing was suspended, with root element
// being level 1.
//
void suspendIndentation ();
void resumeIndentation ();
std::size_t indentationSuspended () const;
private:
XMLStreamSerializer(const XMLStreamSerializer&);
XMLStreamSerializer& operator=(const XMLStreamSerializer&);
void handleError(genxStatus);
void handleError(genxStatus) const;
std::ostream& _outputStream;
std::ostream::iostate _lastStreamState;// Original exception state.
@@ -127,6 +183,18 @@ private:
std::size_t _depth;
};
XMLStreamSerializer& operator<< (XMLStreamSerializer&, void (*func) (XMLStreamSerializer&));
/// Stream-like interface for serializer. If the passed argument
/// is a function with the void f(serializer&) signature or is a
/// function object with the void operator() (serializer&) const
/// operator, then this function (object) is called with the passed
/// serializer. Otherwise, the argument is passed to the serializer's
// characters() function.
template <typename T>
XMLStreamSerializer& operator<< (XMLStreamSerializer&, const T& value);
inline void XMLStreamSerializer::startElement(const QName& qname)
{
startElement(qname.namespace_(), qname.name());
@@ -137,6 +205,19 @@ inline void XMLStreamSerializer::startElement(const std::string& name)
startElement(std::string(), name);
}
inline void XMLStreamSerializer::endElement (const QName& qname)
{
endElement (qname.namespace_ (), qname.name ());
}
inline void XMLStreamSerializer::endElement (const std::string& name)
{
endElement (std::string (), name);
}
inline void XMLStreamSerializer::element(const std::string& v)
{
if (!v.empty())
@@ -193,6 +274,18 @@ inline void XMLStreamSerializer::startAttribute(const std::string& name)
}
inline void XMLStreamSerializer::endAttribute (const QName& qname)
{
endAttribute (qname.namespace_ (), qname.name ());
}
inline void XMLStreamSerializer::endAttribute (const std::string& name)
{
endAttribute (std::string (), name);
}
inline void XMLStreamSerializer::attribute(const QName& qname, const std::string& value)
{
attribute(qname.namespace_(), qname.name(), value);
@@ -233,6 +326,62 @@ inline void XMLStreamSerializer::characters(const T& value)
}
// operator<<
//
inline XMLStreamSerializer& operator<< (XMLStreamSerializer& s, void (*func) (XMLStreamSerializer&))
{
func (s);
return s;
}
namespace details
{
// Detect whether T defines void operator(A) const.
//
template <typename T, typename A>
struct is_functor
{
typedef char no[1];
typedef char yes[2];
template <typename X, X> struct check;
template <typename>
static no& test (...);
template <typename X>
static yes& test (check<void (X::*) (A) const, &X::operator ()>*);
static const bool value = sizeof (test<T> (0)) == sizeof (yes);
};
template <typename T, bool = is_functor<T, XMLStreamSerializer&>::value>
struct inserter;
template <typename T>
struct inserter<T, true>
{
static void insert (XMLStreamSerializer& s, const T& f) {f (s);}
};
template <typename T>
struct inserter<T, false>
{
static void insert (XMLStreamSerializer& s, const T& v) {s.characters (v);}
};
}
template <typename T>
inline XMLStreamSerializer& operator<< (XMLStreamSerializer& s, const T& value)
{
details::inserter<T>::insert (s, value);
return s;
}
}
}

View File

@@ -117,6 +117,24 @@ void * genxGetUserData(genxWriter w);
genxStatus genxSetPrettyPrint(genxWriter w, int indentation);
int genxGetPrettyPrint(genxWriter w);
/*
* Suspend/resume pretty-printing. Pretty-printing can be suspended
* only inside an element and, unless explicitly resumed, it will
* remain suspended until the end of that element. You should only
* explicitly resume pretty-printing at the element nesting level
* of suspension. If pretty-printing is already suspended at an
* outer nesting level, then subsequent calls to suspend/resume
* are ignored. The PrettyPrintSuspended() function can be used
* to check if pretty-printing is currently suspended. If it is
* not, then this function returns 0. Otherwise, it returns the
* level at which pretty-printing was suspended, with root element
* being level 1.
*/
genxStatus genxSuspendPrettyPrint(genxWriter w);
genxStatus genxResumePrettyPrint(genxWriter w);
int genxPrettyPrintSuspended(genxWriter w);
/*
* Set/get canonicalization. If true, then output explicit closing
* tags and sort attributes. Default is false.
@@ -162,7 +180,7 @@ genxNamespace genxDeclareNamespace(genxWriter w,
* If something failed, returns NULL and sets the status code via statusP
*/
genxElement genxDeclareElement(genxWriter w,
genxNamespace ns, constUtf8 type,
genxNamespace ns, constUtf8 name,
genxStatus * statusP);
/*
@@ -192,7 +210,7 @@ typedef struct
genxStatus genxStartDocSender(genxWriter w, genxSender * sender);
/*
* End a document. Calls "flush"
* End a document. Calls "flush".
*/
genxStatus genxEndDocument(genxWriter w);
@@ -204,6 +222,19 @@ genxStatus genxXmlDeclaration(genxWriter w,
constUtf8 version,
constUtf8 encoding,
constUtf8 standalone);
/*
* Write DOCTYPE declaration. If public_id is not NULL, then this is
* a PUBLIC DOCTYPE declaration, otherwise, if system_id is not NULL,
* then this is a SYSTEM DOCTYPE. If both are NULL, then a DOCTYPE
* that only contains the root element and, if not NULL, internal
* subset is written.
*/
genxStatus genxDoctypeDeclaration(genxWriter w,
constUtf8 root_element,
constUtf8 public_id,
constUtf8 system_id,
constUtf8 internal_subset);
/*
* Write a comment
*/
@@ -218,7 +249,7 @@ genxStatus genxPI(genxWriter w, constUtf8 target, constUtf8 text);
* Start an element
*/
genxStatus genxStartElementLiteral(genxWriter w,
constUtf8 xmlns, constUtf8 type);
constUtf8 xmlns, constUtf8 name);
/*
* Start a predeclared element
@@ -226,28 +257,44 @@ genxStatus genxStartElementLiteral(genxWriter w,
*/
genxStatus genxStartElement(genxElement e);
/*
* Get current element. The returned values are valid until this
* element ceases to be current (i.e., EndElement() is called).
* If the element is unqualified, then xmlns is set to NULL.
*/
genxStatus genxGetCurrentElement (genxWriter w,
constUtf8* xmlns, constUtf8* name);
/*
* Write an attribute
*/
genxStatus genxAddAttributeLiteral(genxWriter w, constUtf8 xmlns,
constUtf8 name, constUtf8 value);
/*
* Write a predeclared attribute
*/
genxStatus genxAddAttribute(genxAttribute a, constUtf8 value);
/*
* Start an attribute
*/
genxStatus genxStartAttributeLiteral(genxWriter w,
constUtf8 xmlns, constUtf8 name);
/*
* Write a predeclared attribute
*/
genxStatus genxAddAttribute(genxAttribute a, constUtf8 value);
/*
* Start a predeclared attribute
*/
genxStatus genxStartAttribute(genxAttribute a);
/*
* Get current attribute. The returned values are valid until this
* attribute ceases to be current (i.e., EndAttribute() is called).
* If the attribute is unqualified, then xmlns is set to NULL.
*/
genxStatus genxGetCurrentAttribute (genxWriter w,
constUtf8* xmlns, constUtf8* name);
/*
* End an attribute
*/