From 2615896faa646e5830abf2c269150e1165c66515 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- libxsd/libxsd/cxx/tree/serialization.txx | 801 +++++++++++++++++++++++++++++++ 1 file changed, 801 insertions(+) create mode 100644 libxsd/libxsd/cxx/tree/serialization.txx (limited to 'libxsd/libxsd/cxx/tree/serialization.txx') diff --git a/libxsd/libxsd/cxx/tree/serialization.txx b/libxsd/libxsd/cxx/tree/serialization.txx new file mode 100644 index 0000000..8d611c8 --- /dev/null +++ b/libxsd/libxsd/cxx/tree/serialization.txx @@ -0,0 +1,801 @@ +// file : libxsd/cxx/tree/serialization.txx +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include // xml::{string, transcode} +#include // dom::{prefix, clear} + +#include +#include +#include + +// The only way to make the following serialization operators +// for fundamental types work is to defined them in the xercesc +// namespace so that they can be found by ADL. Placing them into +// the global namespace does not work. +// + +namespace XERCES_CPP_NAMESPACE +{ + // Serialization of std::basic_string and C string. Used in other + // serializers. Also used to serialize enumerators. + // + template + void + operator<< (xercesc::DOMElement& e, const C* s) + { + xsd::cxx::xml::dom::clear (e); + + if (*s != C (0)) + e.setTextContent (xsd::cxx::xml::string (s).c_str ()); + } + + template + void + operator<< (xercesc::DOMAttr& a, const C* s) + { + a.setValue (xsd::cxx::xml::string (s).c_str ()); + } + + // We duplicate the code above instead of delegating in order to + // allow the xml::string type to take advantage of cached string + // sizes. + // + template + void + operator<< (xercesc::DOMElement& e, const std::basic_string& s) + { + xsd::cxx::xml::dom::clear (e); + + if (!s.empty ()) + e.setTextContent (xsd::cxx::xml::string (s).c_str ()); + } + + template + void + operator<< (xercesc::DOMAttr& a, const std::basic_string& s) + { + a.setValue (xsd::cxx::xml::string (s).c_str ()); + } +} + +namespace xsd +{ + namespace cxx + { + namespace tree + { + // List serialization operators for std::basic_string and C string. + // + + template + void + operator<< (list_stream& ls, const C* s) + { + ls.os_ << s; + } + + template + void + operator<< (list_stream& ls, const std::basic_string& s) + { + ls.os_ << s; + } + + // Insertion operators for type. + // + inline void + operator<< (xercesc::DOMElement& e, const type& x) + { + xml::dom::clear (e); + + if (!x.null_content () && x.dom_content ().present ()) + { + // Cannot use 'using namespace' because of MSXML conflict. + // + using xercesc::DOMAttr; + using xercesc::DOMNode; + using xercesc::DOMElement; + using xercesc::DOMDocument; + using xercesc::DOMNamedNodeMap; + + // Clone the contents of the element. + // + DOMDocument& doc (*e.getOwnerDocument ()); + const DOMElement& se (x.dom_content ().get ()); + DOMNamedNodeMap& sa (*se.getAttributes ()); + + for (XMLSize_t i (0), n (sa.getLength ()); i != n; ++i) + e.setAttributeNode ( + static_cast (doc.importNode (sa.item (i), true))); + + for (DOMNode* sn (se.getFirstChild ()); + sn != 0; + sn = sn->getNextSibling ()) + e.appendChild (doc.importNode (sn, true)); + } + } + + inline void + operator<< (xercesc::DOMAttr&, const type&) + { + } + + template + inline void + operator<< (list_stream&, const type&) + { + } + + // Insertion operators for simple_type. + // + template + inline void + operator<< (xercesc::DOMElement& e, const simple_type& x) + { + if (x.null_content ()) + xml::dom::clear (e); + else + e << x.text_content (); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const simple_type& x) + { + if (!x.null_content ()) + a << x.text_content (); + } + + template + inline void + operator<< (list_stream& ls, const simple_type& x) + { + if (!x.null_content ()) + ls << x.text_content (); + } + + // Insertion operators for list. + // + template + void + operator<< (xercesc::DOMElement& e, const list& v) + { + std::basic_ostringstream os; + list_stream ls (os, e); + + ls << v; + + e << os.str (); + } + + template + void + operator<< (xercesc::DOMAttr& a, const list& v) + { + std::basic_ostringstream os; + list_stream ls (os, *a.getOwnerElement ()); + + ls << v; + + a << os.str (); + } + + template + void + operator<< (list_stream& ls, const list& v) + { + for (typename list::const_iterator + b (v.begin ()), e (v.end ()), i (b); i != e; ++i) + { + if (i != b) + ls.os_ << C (' '); + + ls << *i; + } + } + + // Specializations for double and decimal. + // + template + void + operator<< (list_stream& ls, + const list& v) + { + for (typename list::const_iterator + b (v.begin ()), e (v.end ()), i (b); i != e; ++i) + { + if (i != b) + ls.os_ << C (' '); + + ls << as_double (*i); + } + } + + template + void + operator<< (list_stream& ls, + const list& v) + { + for (typename list::const_iterator + b (v.begin ()), e (v.end ()), i (b); i != e; ++i) + { + if (i != b) + ls.os_ << C (' '); + + ls << as_decimal (*i); + } + } + + + // Insertion operators for fundamental_base. + // + template + void + operator<< (xercesc::DOMElement& e, + const fundamental_base& x) + { + const T& r (x); + e << r; + } + + template + void + operator<< (xercesc::DOMAttr& a, const fundamental_base& x) + { + const T& r (x); + a << r; + } + + template + void + operator<< (list_stream& ls, const fundamental_base& x) + { + const T& r (x); + ls << r; + } + + // Specializations for double. + // + template + void + operator<< ( + xercesc::DOMElement& e, + const fundamental_base& x) + { + e << as_double (x); + } + + template + void + operator<< ( + xercesc::DOMAttr& a, + const fundamental_base& x) + { + a << as_double (x); + } + + template + void + operator<< ( + list_stream& ls, + const fundamental_base& x) + { + ls << as_double (x); + } + + // Specializations for decimal. + // + template + void + operator<< ( + xercesc::DOMElement& e, + const fundamental_base& x) + { + e << as_decimal (x, x._facet_table ()); + } + + template + void + operator<< ( + xercesc::DOMAttr& a, + const fundamental_base& x) + { + a << as_decimal (x, x._facet_table ()); + } + + template + void + operator<< ( + list_stream& ls, + const fundamental_base& x) + { + ls << as_decimal (x, x._facet_table ()); + } + + // Insertion operators for built-in types. + // + + namespace bits + { + template + void + insert (xercesc::DOMElement& e, const T& x) + { + std::basic_ostringstream os; + os << x; + e << os.str (); + } + + template + void + insert (xercesc::DOMAttr& a, const T& x) + { + std::basic_ostringstream os; + os << x; + a << os.str (); + } + } + + + // string + // + template + inline void + operator<< (xercesc::DOMElement& e, const string& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const string& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const string& x) + { + ls.os_ << x; + } + + + // normalized_string + // + template + inline void + operator<< (xercesc::DOMElement& e, const normalized_string& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const normalized_string& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const normalized_string& x) + { + ls.os_ << x; + } + + + // token + // + template + inline void + operator<< (xercesc::DOMElement& e, const token& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const token& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const token& x) + { + ls.os_ << x; + } + + + // nmtoken + // + template + inline void + operator<< (xercesc::DOMElement& e, const nmtoken& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const nmtoken& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const nmtoken& x) + { + ls.os_ << x; + } + + + // nmtokens + // + template + inline void + operator<< (xercesc::DOMElement& e, const nmtokens& v) + { + const list& r (v); + e << r; + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const nmtokens& v) + { + const list& r (v); + a << r; + } + + template + inline void + operator<< (list_stream& ls, const nmtokens& v) + { + const list& r (v); + ls << r; + } + + + // name + // + template + inline void + operator<< (xercesc::DOMElement& e, const name& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const name& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const name& x) + { + ls.os_ << x; + } + + + // ncname + // + template + inline void + operator<< (xercesc::DOMElement& e, const ncname& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const ncname& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const ncname& x) + { + ls.os_ << x; + } + + + // language + // + template + inline void + operator<< (xercesc::DOMElement& e, const language& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const language& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const language& x) + { + ls.os_ << x; + } + + + // id + // + template + inline void + operator<< (xercesc::DOMElement& e, const id& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const id& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const id& x) + { + ls.os_ << x; + } + + + // idref + // + template + inline void + operator<< (xercesc::DOMElement& e, const idref& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const idref& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const idref& x) + { + ls.os_ << x; + } + + + // idrefs + // + template + inline void + operator<< (xercesc::DOMElement& e, const idrefs& v) + { + const list& r (v); + e << r; + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const idrefs& v) + { + const list& r (v); + a << r; + } + + template + inline void + operator<< (list_stream& ls, const idrefs& v) + { + const list& r (v); + ls << r; + } + + + // uri + // + template + inline void + operator<< (xercesc::DOMElement& e, const uri& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const uri& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const uri& x) + { + ls.os_ << x; + } + + + // qname + // + template + void + operator<< (xercesc::DOMElement& e, const qname& x) + { + std::basic_ostringstream os; + + if (x.qualified ()) + { + // Note: prefix in case uri doesn't derive from basic_string. + // + std::basic_string p (xml::dom::prefix (x.namespace_ (), e)); + + if (!p.empty ()) + os << p << C (':'); + } + + os << x.name (); + e << os.str (); + } + + template + void + operator<< (xercesc::DOMAttr& a, const qname& x) + { + std::basic_ostringstream os; + + if (x.qualified ()) + { + // Note: prefix in case uri doesn't derive from basic_string. + // + std::basic_string p ( + xml::dom::prefix (x.namespace_ (), *a.getOwnerElement ())); + + if (!p.empty ()) + os << p << C (':'); + } + + os << x.name (); + a << os.str (); + } + + template + void + operator<< (list_stream& ls, const qname& x) + { + if (x.qualified ()) + { + // Note: prefix in case uri doesn't derive from basic_string. + // + std::basic_string p ( + xml::dom::prefix (x.namespace_ (), ls.parent_)); + + if (!p.empty ()) + ls.os_ << p << C (':'); + } + + ls.os_ << x.name (); + } + + + // base64_binary + // + template + inline void + operator<< (xercesc::DOMElement& e, const base64_binary& x) + { + e << x.encode (); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const base64_binary& x) + { + a << x.encode (); + } + + template + inline void + operator<< (list_stream& ls, const base64_binary& x) + { + ls.os_ << x.encode (); + } + + + // hex_binary + // + template + inline void + operator<< (xercesc::DOMElement& e, const hex_binary& x) + { + e << x.encode (); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const hex_binary& x) + { + a << x.encode (); + } + + template + inline void + operator<< (list_stream& ls, const hex_binary& x) + { + ls.os_ << x.encode (); + } + + + // entity + // + template + inline void + operator<< (xercesc::DOMElement& e, const entity& x) + { + bits::insert (e, x); + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const entity& x) + { + bits::insert (a, x); + } + + template + inline void + operator<< (list_stream& ls, const entity& x) + { + ls.os_ << x; + } + + + // entities + // + template + inline void + operator<< (xercesc::DOMElement& e, const entities& v) + { + const list& r (v); + e << r; + } + + template + inline void + operator<< (xercesc::DOMAttr& a, const entities& v) + { + const list& r (v); + a << r; + } + + template + inline void + operator<< (list_stream& ls, const entities& v) + { + const list& r (v); + ls << r; + } + } + } +} -- cgit v1.1