Line data Source code
1 : // file : xsd/cxx/xml/dom/serialization-source.hxx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : #ifndef XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX
6 : #define XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX
7 :
8 : #include <string>
9 : #include <cstring> // std::memcpy
10 : #include <ostream>
11 :
12 : #include <xercesc/dom/DOMAttr.hpp>
13 : #include <xercesc/dom/DOMElement.hpp>
14 : #include <xercesc/dom/DOMDocument.hpp>
15 : #include <xercesc/dom/DOMErrorHandler.hpp>
16 : #include <xercesc/framework/XMLFormatter.hpp> // XMLFormatTarget, XMLFormatter
17 :
18 : #include <xsd/cxx/xml/error-handler.hxx>
19 : #include <xsd/cxx/xml/dom/auto-ptr.hxx>
20 : #include <xsd/cxx/xml/dom/elements.hxx> // name
21 : #include <xsd/cxx/xml/dom/serialization-header.hxx>
22 :
23 : namespace xsd
24 : {
25 : namespace cxx
26 : {
27 : namespace xml
28 : {
29 : namespace dom
30 : {
31 : //
32 : //
33 : template <typename C>
34 : xercesc::DOMAttr&
35 : create_attribute (const C* name, xercesc::DOMElement&);
36 :
37 : template <typename C>
38 : xercesc::DOMAttr&
39 : create_attribute (const C* name, const C* ns, xercesc::DOMElement&);
40 :
41 : template <typename C>
42 : xercesc::DOMElement&
43 : create_element (const C* name, xercesc::DOMElement&);
44 :
45 : template <typename C>
46 : xercesc::DOMElement&
47 : create_element (const C* name, const C* ns, xercesc::DOMElement&);
48 :
49 : // Add namespace declarations and schema locations.
50 : //
51 : template <typename C>
52 : void
53 : add_namespaces (xercesc::DOMElement&, const namespace_infomap<C>&);
54 :
55 : // Serialization flags.
56 : //
57 : const unsigned long no_xml_declaration = 0x00010000UL;
58 : const unsigned long dont_pretty_print = 0x00020000UL;
59 :
60 : template <typename C>
61 : XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
62 : serialize (const std::basic_string<C>& root_element,
63 : const std::basic_string<C>& root_element_namespace,
64 : const namespace_infomap<C>& map,
65 : unsigned long flags);
66 :
67 : // This one helps Sun C++ to overcome its fears.
68 : //
69 : template <typename C>
70 : inline XSD_DOM_AUTO_PTR<xercesc::DOMDocument>
71 0 : serialize (const C* root_element,
72 : const C* root_element_namespace,
73 : const namespace_infomap<C>& map,
74 : unsigned long flags)
75 : {
76 0 : return serialize (std::basic_string<C> (root_element),
77 0 : std::basic_string<C> (root_element_namespace),
78 : map,
79 0 : flags);
80 : }
81 :
82 : //
83 : //
84 : template <typename C>
85 : bool
86 : serialize (xercesc::XMLFormatTarget& target,
87 : const xercesc::DOMDocument& doc,
88 : const std::basic_string<C>& enconding,
89 : error_handler<C>& eh,
90 : unsigned long flags);
91 :
92 : template <typename C>
93 : bool
94 : serialize (xercesc::XMLFormatTarget& target,
95 : const xercesc::DOMDocument& doc,
96 : const std::basic_string<C>& enconding,
97 : xercesc::DOMErrorHandler& eh,
98 : unsigned long flags);
99 :
100 :
101 : class ostream_format_target: public xercesc::XMLFormatTarget
102 : {
103 : public:
104 0 : ostream_format_target (std::ostream& os)
105 0 : : n_ (0), os_ (os)
106 : {
107 0 : }
108 :
109 : public:
110 : // I know, some of those consts are stupid. But that's what
111 : // Xerces folks put into their interfaces and VC thinks there
112 : // are different signatures if one strips this fluff off.
113 : //
114 : virtual void
115 0 : writeChars (const XMLByte* const buf,
116 : const XMLSize_t size,
117 : xercesc::XMLFormatter* const)
118 : {
119 : // Ignore the write request if there was a stream failure and the
120 : // stream is not using exceptions.
121 : //
122 0 : if (os_.fail ())
123 0 : return;
124 :
125 : // Flush the buffer if the block is too large or if we don't have
126 : // any space left.
127 : //
128 0 : if ((size >= buf_size_ / 8 || n_ + size > buf_size_) && n_ != 0)
129 : {
130 0 : os_.write (buf_, static_cast<std::streamsize> (n_));
131 0 : n_ = 0;
132 :
133 0 : if (os_.fail ())
134 0 : return;
135 : }
136 :
137 0 : if (size < buf_size_ / 8)
138 : {
139 0 : std::memcpy (buf_ + n_, reinterpret_cast<const char*> (buf), size);
140 0 : n_ += size;
141 : }
142 : else
143 0 : os_.write (reinterpret_cast<const char*> (buf),
144 : static_cast<std::streamsize> (size));
145 : }
146 :
147 :
148 : virtual void
149 0 : flush ()
150 : {
151 : // Ignore the flush request if there was a stream failure
152 : // and the stream is not using exceptions.
153 : //
154 0 : if (!os_.fail ())
155 : {
156 0 : if (n_ != 0)
157 : {
158 0 : os_.write (buf_, static_cast<std::streamsize> (n_));
159 0 : n_ = 0;
160 :
161 0 : if (os_.fail ())
162 0 : return;
163 : }
164 :
165 0 : os_.flush ();
166 : }
167 : }
168 :
169 : private:
170 : static const std::size_t buf_size_ = 1024;
171 : char buf_[buf_size_];
172 : std::size_t n_;
173 : std::ostream& os_;
174 : };
175 : }
176 : }
177 : }
178 : }
179 :
180 : #include <xsd/cxx/xml/dom/serialization-source.txx>
181 :
182 : #endif // XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX
|