summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/xml/dom/serialization-source.hxx
blob: a1c9c3a529f460ded58ae510f94f0d6f9886bd4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// file      : xsd/cxx/xml/dom/serialization-source.hxx
// copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX
#define XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX

#include <string>
#include <cstring> // std::memcpy
#include <ostream>

#include <xercesc/dom/DOMAttr.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/framework/XMLFormatter.hpp> // XMLFormatTarget, XMLFormatter

#include <xsd/cxx/xml/error-handler.hxx>
#include <xsd/cxx/xml/dom/auto-ptr.hxx>
#include <xsd/cxx/xml/dom/elements.hxx> // name
#include <xsd/cxx/xml/dom/serialization-header.hxx>

namespace xsd
{
  namespace cxx
  {
    namespace xml
    {
      namespace dom
      {
        //
        //
        template <typename C>
        xercesc::DOMAttr&
        create_attribute (const C* name, xercesc::DOMElement&);

        template <typename C>
        xercesc::DOMAttr&
        create_attribute (const C* name, const C* ns, xercesc::DOMElement&);

        template <typename C>
        xercesc::DOMElement&
        create_element (const C* name, xercesc::DOMElement&);

        template <typename C>
        xercesc::DOMElement&
        create_element (const C* name, const C* ns, xercesc::DOMElement&);

        // Serialization flags.
        //
        const unsigned long no_xml_declaration = 0x00010000UL;
        const unsigned long dont_pretty_print  = 0x00020000UL;

        template <typename C>
        XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
        serialize (const std::basic_string<C>& root_element,
                   const std::basic_string<C>& root_element_namespace,
                   const namespace_infomap<C>& map,
                   unsigned long flags);

        // This one helps Sun C++ to overcome its fears.
        //
        template <typename C>
        inline XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
        serialize (const C* root_element,
                   const C* root_element_namespace,
                   const namespace_infomap<C>& map,
                   unsigned long flags)
        {
          return serialize (std::basic_string<C> (root_element),
                            std::basic_string<C> (root_element_namespace),
                            map,
                            flags);
        }

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

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


        class ostream_format_target: public xercesc::XMLFormatTarget
        {
        public:
          ostream_format_target (std::ostream& os)
              : n_ (0), os_ (os)
          {
          }

        public:
          // I know, some of those consts are stupid. But that's what
          // Xerces folks put into their interfaces and VC thinks there
          // are different signatures if one strips this fluff off.
          //
          virtual void
          writeChars (const XMLByte* const buf,
                      const XMLSize_t size,
                      xercesc::XMLFormatter* const)
          {
            // Ignore the write request if there was a stream failure and the
            // stream is not using exceptions.
            //
            if (os_.fail ())
              return;

            // Flush the buffer if the block is too large or if we don't have
            // any space left.
            //
            if ((size >= buf_size_ / 8 || n_ + size > buf_size_) && n_ != 0)
            {
              os_.write (buf_, static_cast<std::streamsize> (n_));
              n_ = 0;

              if (os_.fail ())
                return;
            }

            if (size < buf_size_ / 8)
            {
              std::memcpy (buf_ + n_, reinterpret_cast<const char*> (buf), size);
              n_ += size;
            }
            else
              os_.write (reinterpret_cast<const char*> (buf),
                         static_cast<std::streamsize> (size));
          }


          virtual void
          flush ()
          {
            // Ignore the flush request if there was a stream failure
            // and the stream is not using exceptions.
            //
            if (!os_.fail ())
            {
              if (n_ != 0)
              {
                os_.write (buf_, static_cast<std::streamsize> (n_));
                n_ = 0;

                if (os_.fail ())
                  return;
              }

              os_.flush ();
            }
          }

        private:
          static const std::size_t buf_size_ = 1024;
          char buf_[buf_size_];
          std::size_t n_;
          std::ostream& os_;
        };
      }
    }
  }
}

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

#endif  // XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX