[111] | 1 | // file : xsd/cxx/xml/dom/serialization-source.hxx |
---|
| 2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
| 3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
| 4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
| 5 | |
---|
| 6 | #ifndef XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX |
---|
| 7 | #define XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX |
---|
| 8 | |
---|
| 9 | #include <string> |
---|
| 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 | |
---|
| 50 | // No mapping provided for a namespace. |
---|
| 51 | // |
---|
| 52 | template <typename C> |
---|
| 53 | struct mapping |
---|
| 54 | { |
---|
| 55 | mapping (const std::basic_string<C>& name) |
---|
| 56 | : name_ (name) |
---|
| 57 | { |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | const std::basic_string<C>& |
---|
| 61 | name () const |
---|
| 62 | { |
---|
| 63 | return name_; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | private: |
---|
| 67 | std::basic_string<C> name_; |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | // Serialization flags. |
---|
| 71 | // |
---|
| 72 | const unsigned long no_xml_declaration = 0x00010000UL; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | // 'xsi' prefix is already in use and no user-defined mapping has |
---|
| 76 | // been provided. |
---|
| 77 | // |
---|
| 78 | struct xsi_already_in_use {}; |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | template <typename C> |
---|
| 82 | xml::dom::auto_ptr<xercesc::DOMDocument> |
---|
| 83 | serialize (const std::basic_string<C>& root_element, |
---|
| 84 | const std::basic_string<C>& root_element_namespace, |
---|
| 85 | const namespace_infomap<C>& map, |
---|
| 86 | unsigned long flags); |
---|
| 87 | |
---|
| 88 | // This one helps Sun C++ to overcome its fears. |
---|
| 89 | // |
---|
| 90 | template <typename C> |
---|
| 91 | inline xml::dom::auto_ptr<xercesc::DOMDocument> |
---|
| 92 | serialize (const C* root_element, |
---|
| 93 | const C* root_element_namespace, |
---|
| 94 | const namespace_infomap<C>& map, |
---|
| 95 | unsigned long flags) |
---|
| 96 | { |
---|
| 97 | return serialize (std::basic_string<C> (root_element), |
---|
| 98 | std::basic_string<C> (root_element_namespace), |
---|
| 99 | map, |
---|
| 100 | flags); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | // |
---|
| 104 | // |
---|
| 105 | template <typename C> |
---|
| 106 | bool |
---|
| 107 | serialize (xercesc::XMLFormatTarget& target, |
---|
| 108 | const xercesc::DOMDocument& doc, |
---|
| 109 | const std::basic_string<C>& enconding, |
---|
| 110 | error_handler<C>& eh, |
---|
| 111 | unsigned long flags); |
---|
| 112 | |
---|
| 113 | template <typename C> |
---|
| 114 | bool |
---|
| 115 | serialize (xercesc::XMLFormatTarget& target, |
---|
| 116 | const xercesc::DOMDocument& doc, |
---|
| 117 | const std::basic_string<C>& enconding, |
---|
| 118 | xercesc::DOMErrorHandler& eh, |
---|
| 119 | unsigned long flags); |
---|
| 120 | |
---|
| 121 | // |
---|
| 122 | // |
---|
| 123 | class ostream_format_target: public xercesc::XMLFormatTarget |
---|
| 124 | { |
---|
| 125 | public: |
---|
| 126 | ostream_format_target (std::ostream& os) |
---|
| 127 | : os_ (os) |
---|
| 128 | { |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | public: |
---|
| 133 | // I know, some of those consts are stupid. But that's what |
---|
| 134 | // Xerces folks put into their interfaces and VC-7.1 thinks |
---|
| 135 | // there are different signatures if one strips this fluff off. |
---|
| 136 | // |
---|
| 137 | virtual void |
---|
| 138 | writeChars (const XMLByte* const buf, |
---|
| 139 | #if _XERCES_VERSION >= 30000 |
---|
| 140 | const XMLSize_t size, |
---|
| 141 | #else |
---|
| 142 | const unsigned int size, |
---|
| 143 | #endif |
---|
| 144 | xercesc::XMLFormatter* const) |
---|
| 145 | { |
---|
| 146 | // Ignore the data if there was a stream failure and |
---|
| 147 | // the stream is not using exceptions. |
---|
| 148 | // |
---|
| 149 | if (!(os_.bad () || os_.fail ())) |
---|
| 150 | { |
---|
| 151 | os_.write (reinterpret_cast<const char*> (buf), |
---|
| 152 | static_cast<std::streamsize> (size)); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | virtual void |
---|
| 158 | flush () |
---|
| 159 | { |
---|
| 160 | // Ignore the flush request if there was a stream failure |
---|
| 161 | // and the stream is not using exceptions. |
---|
| 162 | // |
---|
| 163 | if (!(os_.bad () || os_.fail ())) |
---|
| 164 | { |
---|
| 165 | os_.flush (); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | private: |
---|
| 170 | std::ostream& os_; |
---|
| 171 | }; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | #include <xsd/cxx/xml/dom/serialization-source.txx> |
---|
| 178 | |
---|
| 179 | #endif // XSD_CXX_XML_DOM_SERIALIZATION_SOURCE_HXX |
---|