| 1 |
// file : cutl/xml/serializer.hxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#ifndef CUTL_XML_SERIALIZER_HXX |
| 6 |
#define CUTL_XML_SERIALIZER_HXX |
| 7 |
|
| 8 |
#include <string> |
| 9 |
#include <ostream> |
| 10 |
#include <cstddef> // std::size_t |
| 11 |
|
| 12 |
#include <cutl/details/genx/genx.h> |
| 13 |
|
| 14 |
#include <cutl/xml/qname.hxx> |
| 15 |
#include <cutl/xml/exception.hxx> |
| 16 |
|
| 17 |
#include <cutl/details/export.hxx> |
| 18 |
|
| 19 |
namespace cutl |
| 20 |
{ |
| 21 |
namespace xml |
| 22 |
{ |
| 23 |
class serializer; |
| 24 |
|
| 25 |
struct LIBCUTL_EXPORT serialization: exception |
| 26 |
{ |
| 27 |
virtual |
| 28 |
~serialization () throw (); |
| 29 |
|
| 30 |
serialization (const std::string& name, |
| 31 |
const std::string& description); |
| 32 |
|
| 33 |
serialization (const serializer&, const std::string& description); |
| 34 |
|
| 35 |
const std::string& |
| 36 |
name () const {return name_;} |
| 37 |
|
| 38 |
const std::string& |
| 39 |
description () const {return description_;} |
| 40 |
|
| 41 |
virtual const char* |
| 42 |
what () const throw (); |
| 43 |
|
| 44 |
private: |
| 45 |
void |
| 46 |
init (); |
| 47 |
|
| 48 |
private: |
| 49 |
std::string name_; |
| 50 |
std::string description_; |
| 51 |
std::string what_; |
| 52 |
}; |
| 53 |
|
| 54 |
class LIBCUTL_EXPORT serializer |
| 55 |
{ |
| 56 |
public: |
| 57 |
~serializer (); |
| 58 |
|
| 59 |
typedef xml::qname qname_type; |
| 60 |
|
| 61 |
// Serialize to std::ostream. Output name is used in diagnostics to |
| 62 |
// identify the document being serialized. std::ios_base::failure |
| 63 |
// exception is used to report io errors (badbit and failbit). The |
| 64 |
// indentation argument specifies the number of indentation spaces |
| 65 |
// that should be used for pretty-printing. If 0 is passed, no |
| 66 |
// pretty-printing is performed. |
| 67 |
// |
| 68 |
serializer (std::ostream&, |
| 69 |
const std::string& output_name, |
| 70 |
unsigned short indentation = 2); |
| 71 |
|
| 72 |
const std::string& |
| 73 |
output_name () const {return oname_;} |
| 74 |
|
| 75 |
// Serialization functions. |
| 76 |
// |
| 77 |
public: |
| 78 |
|
| 79 |
// Elements. |
| 80 |
// |
| 81 |
void |
| 82 |
start_element (const qname_type& qname); |
| 83 |
|
| 84 |
void |
| 85 |
start_element (const std::string& name); |
| 86 |
|
| 87 |
void |
| 88 |
start_element (const std::string& ns, const std::string& name); |
| 89 |
|
| 90 |
void |
| 91 |
end_element (); |
| 92 |
|
| 93 |
// Attributes. |
| 94 |
// |
| 95 |
void |
| 96 |
start_attribute (const qname_type& qname); |
| 97 |
|
| 98 |
void |
| 99 |
start_attribute (const std::string& name); |
| 100 |
|
| 101 |
void |
| 102 |
start_attribute (const std::string& ns, const std::string& name); |
| 103 |
|
| 104 |
void |
| 105 |
end_attribute (); |
| 106 |
|
| 107 |
void |
| 108 |
attribute (const qname_type& qname, const std::string& value); |
| 109 |
|
| 110 |
template <typename T> |
| 111 |
void |
| 112 |
attribute (const qname_type& qname, const T& value); |
| 113 |
|
| 114 |
void |
| 115 |
attribute (const std::string& name, const std::string& value); |
| 116 |
|
| 117 |
template <typename T> |
| 118 |
void |
| 119 |
attribute (const std::string& name, const T& value); |
| 120 |
|
| 121 |
void |
| 122 |
attribute (const std::string& ns, |
| 123 |
const std::string& name, |
| 124 |
const std::string& value); |
| 125 |
|
| 126 |
template <typename T> |
| 127 |
void |
| 128 |
attribute (const std::string& ns, |
| 129 |
const std::string& name, |
| 130 |
const T& value); |
| 131 |
|
| 132 |
// Characters. |
| 133 |
// |
| 134 |
void |
| 135 |
characters (const std::string& value); |
| 136 |
|
| 137 |
template <typename T> |
| 138 |
void |
| 139 |
characters (const T& value); |
| 140 |
|
| 141 |
// Namespaces declaration. If prefix is empty, then the default |
| 142 |
// namespace is declared. If both prefix and namespace are empty, |
| 143 |
// then the default namespace declaration is cleared (xmlns=""). |
| 144 |
// |
| 145 |
void |
| 146 |
namespace_decl (const std::string& ns, const std::string& prefix); |
| 147 |
|
| 148 |
// XML Declaration. If encoding or standalone are not specified, |
| 149 |
// then these attributes are omitted from the output. |
| 150 |
// |
| 151 |
void |
| 152 |
xml_decl (const std::string& version = "1.0", |
| 153 |
const std::string& encoding = "UTF-8", |
| 154 |
const std::string& standalone = ""); |
| 155 |
|
| 156 |
// Other functions. |
| 157 |
// |
| 158 |
public: |
| 159 |
// Return true if there is a mapping. In this case, prefix contains |
| 160 |
// the mapped prefix. |
| 161 |
// |
| 162 |
bool |
| 163 |
lookup_namespace_prefix (const std::string& ns, std::string& prefix); |
| 164 |
|
| 165 |
private: |
| 166 |
void |
| 167 |
handle_error (genxStatus); |
| 168 |
|
| 169 |
private: |
| 170 |
std::ostream& os_; |
| 171 |
std::ostream::iostate os_state_; // Original exception state. |
| 172 |
const std::string oname_; |
| 173 |
|
| 174 |
genxWriter s_; |
| 175 |
genxSender sender_; |
| 176 |
std::size_t depth_; |
| 177 |
}; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
#include <cutl/xml/serializer.ixx> |
| 182 |
|
| 183 |
#endif // CUTL_XML_SERIALIZER_HXX |