From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/tree/serialization.txx | 762 ++++++++++++++++++++++++++++++++++ 1 file changed, 762 insertions(+) create mode 100644 libxsd/xsd/cxx/tree/serialization.txx (limited to 'libxsd/xsd/cxx/tree/serialization.txx') diff --git a/libxsd/xsd/cxx/tree/serialization.txx b/libxsd/xsd/cxx/tree/serialization.txx new file mode 100644 index 0000000..b8c8631 --- /dev/null +++ b/libxsd/xsd/cxx/tree/serialization.txx @@ -0,0 +1,762 @@ +// file : xsd/cxx/tree/serialization.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// 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); + 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); + 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&) + { + xml::dom::clear (e); + } + + 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&) + { + xml::dom::clear (e); + } + + template + inline void + operator<< (xercesc::DOMAttr&, const simple_type&) + { + } + + template + inline void + operator<< (list_stream&, const simple_type&) + { + } + + // Insertion operators for list. + // + template + void + operator<< (xercesc::DOMElement& e, const list& v) + { + xml::dom::clear (e); + + 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 ()) + { + 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 ()) + { + 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 ()) + { + 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