| 1 |
// file : cutl/xml/serializer.cxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#include <new> // std::bad_alloc |
| 6 |
#include <cstring> // std::strlen |
| 7 |
|
| 8 |
#include <cutl/xml/serializer.hxx> |
| 9 |
|
| 10 |
using namespace std; |
| 11 |
|
| 12 |
namespace cutl |
| 13 |
{ |
| 14 |
namespace xml |
| 15 |
{ |
| 16 |
// serialization |
| 17 |
// |
| 18 |
serialization:: |
| 19 |
~serialization () throw () {} |
| 20 |
|
| 21 |
serialization:: |
| 22 |
serialization (const string& n, const string& d) |
| 23 |
: name_ (n), description_ (d) |
| 24 |
{ |
| 25 |
init (); |
| 26 |
} |
| 27 |
|
| 28 |
serialization:: |
| 29 |
serialization (const serializer& s, const std::string& d) |
| 30 |
: name_ (s.output_name ()), description_ (d) |
| 31 |
{ |
| 32 |
init (); |
| 33 |
} |
| 34 |
|
| 35 |
void serialization:: |
| 36 |
init () |
| 37 |
{ |
| 38 |
if (!name_.empty ()) |
| 39 |
{ |
| 40 |
what_ += name_; |
| 41 |
what_ += ": "; |
| 42 |
} |
| 43 |
|
| 44 |
what_ += "error: "; |
| 45 |
what_ += description_; |
| 46 |
} |
| 47 |
|
| 48 |
char const* serialization:: |
| 49 |
what () const throw () |
| 50 |
{ |
| 51 |
return what_.c_str (); |
| 52 |
} |
| 53 |
|
| 54 |
// serializer |
| 55 |
// |
| 56 |
extern "C" genxStatus |
| 57 |
genx_write (void* p, constUtf8 us) |
| 58 |
{ |
| 59 |
// It would have been easier to throw the exception directly, |
| 60 |
// however, the Genx code is most likely not exception safe. |
| 61 |
// |
| 62 |
ostream* os (static_cast<ostream*> (p)); |
| 63 |
const char* s (reinterpret_cast<const char*> (us)); |
| 64 |
os->write (s, static_cast<streamsize> (strlen (s))); |
| 65 |
return os->good () ? GENX_SUCCESS : GENX_IO_ERROR; |
| 66 |
} |
| 67 |
|
| 68 |
extern "C" genxStatus |
| 69 |
genx_write_bound (void* p, constUtf8 start, constUtf8 end) |
| 70 |
{ |
| 71 |
ostream* os (static_cast<ostream*> (p)); |
| 72 |
const char* s (reinterpret_cast<const char*> (start)); |
| 73 |
streamsize n (static_cast<streamsize> (end - start)); |
| 74 |
os->write (s, n); |
| 75 |
return os->good () ? GENX_SUCCESS : GENX_IO_ERROR; |
| 76 |
} |
| 77 |
|
| 78 |
extern "C" genxStatus |
| 79 |
genx_flush (void* p) |
| 80 |
{ |
| 81 |
ostream* os (static_cast<ostream*> (p)); |
| 82 |
os->flush (); |
| 83 |
return os->good () ? GENX_SUCCESS : GENX_IO_ERROR; |
| 84 |
} |
| 85 |
|
| 86 |
serializer:: |
| 87 |
~serializer () |
| 88 |
{ |
| 89 |
if (s_ != 0) |
| 90 |
genxDispose (s_); |
| 91 |
} |
| 92 |
|
| 93 |
serializer:: |
| 94 |
serializer (ostream& os, const string& oname, unsigned short ind) |
| 95 |
: os_ (os), os_state_ (os.exceptions ()), oname_ (oname), depth_ (0) |
| 96 |
{ |
| 97 |
// Temporarily disable exceptions on the stream. |
| 98 |
// |
| 99 |
os_.exceptions (ostream::goodbit); |
| 100 |
|
| 101 |
// Allocate the serializer. Make sure nothing else can throw after |
| 102 |
// this call since otherwise we will leak it. |
| 103 |
// |
| 104 |
s_ = genxNew (0, 0, 0); |
| 105 |
|
| 106 |
if (s_ == 0) |
| 107 |
throw bad_alloc (); |
| 108 |
|
| 109 |
genxSetUserData (s_, &os_); |
| 110 |
|
| 111 |
if (ind != 0) |
| 112 |
genxSetPrettyPrint (s_, ind); |
| 113 |
|
| 114 |
sender_.send = &genx_write; |
| 115 |
sender_.sendBounded = &genx_write_bound; |
| 116 |
sender_.flush = &genx_flush; |
| 117 |
|
| 118 |
if (genxStatus e = genxStartDocSender (s_, &sender_)) |
| 119 |
{ |
| 120 |
string m (genxGetErrorMessage (s_, e)); |
| 121 |
genxDispose (s_); |
| 122 |
throw serialization (oname, m); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void serializer:: |
| 127 |
handle_error (genxStatus e) |
| 128 |
{ |
| 129 |
switch (e) |
| 130 |
{ |
| 131 |
case GENX_ALLOC_FAILED: |
| 132 |
throw bad_alloc (); |
| 133 |
case GENX_IO_ERROR: |
| 134 |
// Restoring the original exception state should trigger the |
| 135 |
// exception. If it doesn't (e.g., because the user didn't |
| 136 |
// configure the stream to throw), then fall back to the |
| 137 |
// serialiation exception. |
| 138 |
// |
| 139 |
os_.exceptions (os_state_); |
| 140 |
// Fall through. |
| 141 |
default: |
| 142 |
throw serialization (oname_, genxGetErrorMessage (s_, e)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void serializer:: |
| 147 |
start_element (const string& ns, const string& name) |
| 148 |
{ |
| 149 |
if (genxStatus e = genxStartElementLiteral ( |
| 150 |
s_, |
| 151 |
reinterpret_cast<constUtf8> (ns.empty () ? 0 : ns.c_str ()), |
| 152 |
reinterpret_cast<constUtf8> (name.c_str ()))) |
| 153 |
handle_error (e); |
| 154 |
|
| 155 |
depth_++; |
| 156 |
} |
| 157 |
|
| 158 |
void serializer:: |
| 159 |
end_element () |
| 160 |
{ |
| 161 |
if (genxStatus e = genxEndElement (s_)) |
| 162 |
handle_error (e); |
| 163 |
|
| 164 |
// Call EndDocument() if we are past the root element. |
| 165 |
// |
| 166 |
if (--depth_ == 0) |
| 167 |
{ |
| 168 |
if (genxStatus e = genxEndDocument (s_)) |
| 169 |
handle_error (e); |
| 170 |
|
| 171 |
// Also restore the original exception state on the stream. |
| 172 |
// |
| 173 |
os_.exceptions (os_state_); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void serializer:: |
| 178 |
start_attribute (const string& ns, const string& name) |
| 179 |
{ |
| 180 |
if (genxStatus e = genxStartAttributeLiteral ( |
| 181 |
s_, |
| 182 |
reinterpret_cast<constUtf8> (ns.empty () ? 0 : ns.c_str ()), |
| 183 |
reinterpret_cast<constUtf8> (name.c_str ()))) |
| 184 |
handle_error (e); |
| 185 |
} |
| 186 |
|
| 187 |
void serializer:: |
| 188 |
end_attribute () |
| 189 |
{ |
| 190 |
if (genxStatus e = genxEndAttribute (s_)) |
| 191 |
handle_error (e); |
| 192 |
} |
| 193 |
|
| 194 |
void serializer:: |
| 195 |
attribute (const string& ns, |
| 196 |
const string& name, |
| 197 |
const string& value) |
| 198 |
{ |
| 199 |
if (genxStatus e = genxAddAttributeLiteral ( |
| 200 |
s_, |
| 201 |
reinterpret_cast<constUtf8> (ns.empty () ? 0 : ns.c_str ()), |
| 202 |
reinterpret_cast<constUtf8> (name.c_str ()), |
| 203 |
reinterpret_cast<constUtf8> (value.c_str ()))) |
| 204 |
handle_error (e); |
| 205 |
} |
| 206 |
|
| 207 |
void serializer:: |
| 208 |
characters (const string& value) |
| 209 |
{ |
| 210 |
if (genxStatus e = genxAddCountedText ( |
| 211 |
s_, |
| 212 |
reinterpret_cast<constUtf8> (value.c_str ()), |
| 213 |
static_cast<int> (value.size ()))) |
| 214 |
handle_error (e); |
| 215 |
} |
| 216 |
|
| 217 |
void serializer:: |
| 218 |
namespace_decl (const string& ns, const string& p) |
| 219 |
{ |
| 220 |
if (genxStatus e = ns.empty () && p.empty () |
| 221 |
? genxUnsetDefaultNamespace (s_) |
| 222 |
: genxAddNamespaceLiteral ( |
| 223 |
s_, |
| 224 |
reinterpret_cast<constUtf8> (ns.c_str ()), |
| 225 |
reinterpret_cast<constUtf8> (p.c_str ()))) |
| 226 |
handle_error (e); |
| 227 |
} |
| 228 |
|
| 229 |
void serializer:: |
| 230 |
xml_decl (const string& ver, const string& enc, const string& stl) |
| 231 |
{ |
| 232 |
if (genxStatus e = genxXmlDeclaration ( |
| 233 |
s_, |
| 234 |
reinterpret_cast<constUtf8> (ver.c_str ()), |
| 235 |
(enc.empty () ? 0 : reinterpret_cast<constUtf8> (enc.c_str ())), |
| 236 |
(stl.empty () ? 0 : reinterpret_cast<constUtf8> (stl.c_str ())))) |
| 237 |
handle_error (e); |
| 238 |
} |
| 239 |
|
| 240 |
bool serializer:: |
| 241 |
lookup_namespace_prefix (const string& ns, string& p) |
| 242 |
{ |
| 243 |
// Currently Genx will create a namespace mapping if one doesn't |
| 244 |
// already exist. |
| 245 |
// |
| 246 |
genxStatus e; |
| 247 |
genxNamespace gns ( |
| 248 |
genxDeclareNamespace ( |
| 249 |
s_, reinterpret_cast<constUtf8> (ns.c_str ()), 0, &e)); |
| 250 |
|
| 251 |
if (e != GENX_SUCCESS) |
| 252 |
handle_error (e); |
| 253 |
|
| 254 |
p = reinterpret_cast<const char*> (genxGetNamespacePrefix (gns)); |
| 255 |
return true; |
| 256 |
} |
| 257 |
} |
| 258 |
} |