// file      : xsd/cxx/xml/dom/serialization.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_XML_DOM_SERIALIZATION_HXX
#define XSD_CXX_XML_DOM_SERIALIZATION_HXX

#include <map>
#include <string>
#include <ostream>
#include <sstream>

#include <xsd/cxx/xml/dom/elements.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace xml
    {
      namespace dom
      {
        //
        //
        template <typename C>
        struct namespace_info
        {
          typedef std::basic_string<C> string;

          namespace_info ()
          {
          }

          namespace_info (string const& name_, string const& schema_)
              : name (name_),
                schema (schema_)
          {
          }

          std::basic_string<C> name;
          std::basic_string<C> schema;
        };


        // Map of namespace prefix to namespace_info.
        //
        template <typename C>
        struct namespace_infomap:
          public std::map<std::basic_string<C>, namespace_info<C> >
        {
        };


        // No mapping provided for a namespace.
        //
        template <typename C>
        struct mapping
        {
          mapping (std::basic_string<C> const& name)
              : name_ (name)
          {
          }

          std::basic_string<C> const&
          name () const
          {
            return name_;
          }

        private:
          std::basic_string<C> name_;
        };


        // 'xsi' prefix is already in use and no user-defined mapping has
        //  been provided.
        //
        struct xsi_already_in_use {};


        template <typename C>
        xml::dom::auto_ptr<xercesc::DOMDocument>
        dom (std::basic_string<C> const& root_element,
             std::basic_string<C> const& root_element_namespace,
             namespace_infomap<C> const& map);


        //
        //
        template <typename C>
        bool
        serialize (xercesc::XMLFormatTarget& target,
                   xercesc::DOMDocument const& doc,
                   std::basic_string<C> const& enconding,
                   xercesc::DOMErrorHandler* eh);

        //
        //
        class ostream_format_target: public xercesc::XMLFormatTarget
        {
        public:
          ostream_format_target (std::ostream& os)
              : os_ (os)
          {
          }


        public:
          // I know, some of those consts are stupid. But that's what
          // Xerces folks put into their interfaces and VC-7.1 thinks
          // there are different signatures if one strips this fluff off.
          //
          virtual void
          writeChars (XMLByte const* const buf,
                      unsigned int const size,
                      xercesc::XMLFormatter* const)
          {
            os_.write (reinterpret_cast<char const*> (buf),
                       static_cast<std::streamsize> (size));
          }


          virtual void
          flush ()
          {
            os_.flush ();
          }

        private:
          std::ostream& os_;
        };


        // Serialization of basic types.
        //


        // 8-bit
        //
        template <typename C>
        void
        operator<< (element<C>& e, signed char c)
        {
          std::basic_ostringstream<C> os;
          os << static_cast<short> (c);
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, signed char c)
        {
          std::basic_ostringstream<C> os;
          os << static_cast<short> (c);
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& e, unsigned char c)
        {
          std::basic_ostringstream<C> os;
          os << static_cast<unsigned short> (c);
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, unsigned char c)
        {
          std::basic_ostringstream<C> os;
          os << static_cast<unsigned short> (c);
          a.value (os.str ());
        }


        // 16-bit
        //
        template <typename C>
        void
        operator<< (element<C>& e, short s)
        {
          std::basic_ostringstream<C> os;
          os << s;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, short s)
        {
          std::basic_ostringstream<C> os;
          os << s;
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& e, unsigned short s)
        {
          std::basic_ostringstream<C> os;
          os << s;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, unsigned short s)
        {
          std::basic_ostringstream<C> os;
          os << s;
          a.value (os.str ());
        }


        // 32-bit
        //
        template <typename C>
        void
        operator<< (element<C>& e, int i)
        {
          std::basic_ostringstream<C> os;
          os << i;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, int i)
        {
          std::basic_ostringstream<C> os;
          os << i;
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& e, unsigned int i)
        {
          std::basic_ostringstream<C> os;
          os << i;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, unsigned int i)
        {
          std::basic_ostringstream<C> os;
          os << i;
          a.value (os.str ());
        }


        // 64-bit
        //
        template <typename C>
        void
        operator<< (element<C>& e, long long l)
        {
          std::basic_ostringstream<C> os;
          os << l;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, long long l)
        {
          std::basic_ostringstream<C> os;
          os << l;
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& n, unsigned long long l)
        {
          std::basic_ostringstream<C> os;
          os << l;
          n.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, unsigned long long l)
        {
          std::basic_ostringstream<C> os;
          os << l;
          a.value (os.str ());
        }


        // Boolean.
        //
        template <typename C>
        void
        operator<< (element<C>& e, bool b)
        {
          std::basic_ostringstream<C> os;
          os << std::boolalpha << b;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, bool b)
        {
          std::basic_ostringstream<C> os;
          os << std::boolalpha << b;
          a.value (os.str ());
        }


        // Floating-point types.
        //
        template <typename C>
        void
        operator<< (element<C>& e, float f)
        {
          std::basic_ostringstream<C> os;
          os << f;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, float f)
        {
          std::basic_ostringstream<C> os;
          os << f;
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& e, double d)
        {
          std::basic_ostringstream<C> os;
          os << d;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, double d)
        {
          std::basic_ostringstream<C> os;
          os << d;
          a.value (os.str ());
        }

        template <typename C>
        void
        operator<< (element<C>& e, long double ld)
        {
          // xsd:decimal (which we currently map to long double)
          // can not be in scientific notation.
          //
          std::basic_ostringstream<C> os;
          os << std::fixed << ld;
          e.value (os.str ());
        }

        template <typename C>
        void
        operator<< (attribute<C>& a, long double ld)
        {
          // xsd:decimal (which we currently map to long double)
          // can not be in scientific notation.
          //
          std::basic_ostringstream<C> os;
          os << std::fixed << ld;
          a.value (os.str ());
        }
      }
    }
  }
}

#include <xsd/cxx/xml/dom/serialization.txx>

#endif  // XSD_CXX_XML_DOM_SERIALIZATION_HXX
