summaryrefslogtreecommitdiff
path: root/slicer/xml/serializer.cpp
blob: 581eae21d6ec76650d1252b4f679038fc5ec9822 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "serializer.h"
#include <boost/lexical_cast.hpp>
#include <compileTimeFormatter.h>
#include <functional>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#ifndef __clang__
#	pragma GCC diagnostic ignored "-Wuseless-cast"
#endif
#include <glibmm/ustring.h>
#pragma GCC diagnostic pop
#include <libxml++/document.h>
#include <libxml++/parsers/domparser.h>
#include <slicer/metadata.h>
#include <stdexcept>
#include <xmlExceptions.h>

NAMEDFACTORY(".xml", Slicer::XmlFileSerializer, Slicer::FileSerializerFactory);
NAMEDFACTORY(".xml", Slicer::XmlFileDeserializer, Slicer::FileDeserializerFactory);
NAMEDFACTORY("application/xml", Slicer::XmlStreamSerializer, Slicer::StreamSerializerFactory);
NAMEDFACTORY("application/xml", Slicer::XmlStreamDeserializer, Slicer::StreamDeserializerFactory);

namespace Slicer {
	constexpr std::string_view md_attribute {"xml:attribute"};
	constexpr std::string_view md_text {"xml:text"};
	constexpr std::string_view md_bare {"xml:bare"};
	constexpr std::string_view md_attributes {"xml:attributes"};
	constexpr std::string_view md_elements {"xml:elements"};
	constexpr std::string_view keyName {"key"};
	constexpr std::string_view valueName {"value"};

	constexpr auto defaultElementCreator = [](auto && element, auto && name) {
		return element->add_child_element(name);
	};

	static const Glib::ustring TrueText("true");
	static const Glib::ustring FalseText("false");

	class XmlValueSource : public ValueSource {
	public:
		explicit XmlValueSource(Glib::ustring s) : value(std::move(s)) { }

		void
		set(bool & v) const override
		{
			if (value == TrueText) {
				v = true;
				return;
			}
			if (value == FalseText) {
				v = false;
				return;
			}
			throw BadBooleanValue(value);
		}

		void
		set(Ice::Byte & v) const override
		{
			v = boost::numeric_cast<Ice::Byte>(boost::lexical_cast<int>(value));
		}

		void
		set(Ice::Short & v) const override
		{
			v = boost::lexical_cast<Ice::Short>(value);
		}

		void
		set(Ice::Int & v) const override
		{
			v = boost::lexical_cast<Ice::Int>(value);
		}

		void
		set(Ice::Long & v) const override
		{
			v = boost::lexical_cast<Ice::Long>(value);
		}

		void
		set(Ice::Float & v) const override
		{
			v = boost::lexical_cast<Ice::Float>(value);
		}

		void
		set(Ice::Double & v) const override
		{
			v = boost::lexical_cast<Ice::Double>(value);
		}

		void
		set(std::string & v) const override
		{
			v = value.raw();
		}

	private:
		const Glib::ustring value;
	};

	class XmlContentValueSource : public XmlValueSource {
	public:
		explicit XmlContentValueSource() : XmlValueSource(Glib::ustring()) { }
		explicit XmlContentValueSource(const xmlpp::ContentNode * c) : XmlValueSource(c->get_content()) { }
	};

	class XmlAttributeValueSource : public XmlValueSource {
	public:
		explicit XmlAttributeValueSource(const xmlpp::Attribute * a) : XmlValueSource(a->get_value()) { }
	};

	class XmlValueTarget : public ValueTarget {
	public:
		explicit XmlValueTarget(std::function<void(const Glib::ustring &)> a) : apply(std::move(a)) { }

		void
		get(const bool & value) const override
		{
			if (value) {
				apply(TrueText);
			}
			else {
				apply(FalseText);
			}
		}

		void
		get(const Ice::Byte & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring, int>(value));
		}

		void
		get(const Ice::Short & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring>(value));
		}

		void
		get(const Ice::Int & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring>(value));
		}

		void
		get(const Ice::Long & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring>(value));
		}

		void
		get(const Ice::Float & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring>(value));
		}

		void
		get(const Ice::Double & value) const override
		{
			apply(boost::lexical_cast<Glib::ustring>(value));
		}

		void
		get(const std::string & value) const override
		{
			apply(value);
		}

	private:
		const std::function<void(const Glib::ustring &)> apply;
	};

	class XmlAttributeValueTarget : public XmlValueTarget {
	public:
		explicit XmlAttributeValueTarget(xmlpp::Element * p, const std::string & n) :
			XmlValueTarget([p, n](auto && PH1) {
				p->set_attribute(n, PH1);
			})
		{
		}
	};

	class XmlContentValueTarget : public XmlValueTarget {
	public:
		explicit XmlContentValueTarget(xmlpp::Element * p) :
			XmlValueTarget([p](auto && PH1) {
				p->set_first_child_text(PH1);
			})
		{
		}

		explicit XmlContentValueTarget(const CurrentElementCreator & cec) :
			XmlValueTarget([&](auto && PH1) {
				cec->set_first_child_text(PH1);
			})
		{
		}
	};

	void
	XmlDeserializer::DocumentTreeIterateDictAttrs(
			const xmlpp::Element::const_AttributeList & attrs, const ModelPartPtr & dict)
	{
		for (const auto & attr : attrs) {
			auto emp = dict->GetAnonChild();
			emp->Create();
			auto key = emp->GetChild(keyName);
			auto value = emp->GetChild(valueName);
			key->SetValue(XmlValueSource(attr->get_name()));
			key->Complete();
			value->SetValue(XmlValueSource(attr->get_value()));
			value->Complete();
			emp->Complete();
		}
	}

	void
	XmlDeserializer::DocumentTreeIterateDictElements(const xmlpp::Element * element, const ModelPartPtr & dict)
	{
		auto node = element->get_first_child();
		while (node) {
			if (auto element = dynamic_cast<const xmlpp::Element *>(node)) {
				auto emp = dict->GetAnonChild();
				emp->Create();
				auto key = emp->GetChild(keyName);
				auto value = emp->GetChildRef(valueName);
				key->SetValue(XmlValueSource(element->get_name()));
				key->Complete();
				DocumentTreeIterateElement(element, value.Child(), value);
				emp->Complete();
			}
			node = node->get_next_sibling();
		}
	}

	void
	XmlDeserializer::DocumentTreeIterateElement(const xmlpp::Element * element, ModelPartPtr smp, const ChildRef & smpr)
	{
		if (auto typeIdPropName = smp->GetTypeIdProperty()) {
			if (auto typeAttr = element->get_attribute(*typeIdPropName)) {
				smp = smp->GetSubclassModelPart(typeAttr->get_value());
			}
		}
		smp->Create();
		if (smpr.ChildMetaData().flagSet(md_attributes)) {
			auto attrs(element->get_attributes());
			if (!attrs.empty()) {
				DocumentTreeIterateDictAttrs(attrs, smp);
			}
		}
		else if (smpr.ChildMetaData().flagSet(md_elements)) {
			DocumentTreeIterateDictElements(element, smp);
		}
		else {
			auto attrs(element->get_attributes());
			if (!attrs.empty()) {
				DocumentTreeIterate(attrs.front(), smp);
			}
			auto firstChild = element->get_first_child();
			if (firstChild) {
				DocumentTreeIterate(firstChild, smp);
			}
			else {
				smp->SetValue(XmlContentValueSource());
			}
		}
		smp->Complete();
	}

	void
	XmlDeserializer::DocumentTreeIterate(const xmlpp::Node * node, const ModelPartPtr & mp)
	{
		while (node) {
			if (auto element = dynamic_cast<const xmlpp::Element *>(node)) {
				auto smpr = mp->GetChildRef(element->get_name().raw(), [](const auto & h) {
					return h->GetMetadata().flagNotSet(md_attribute);
				});
				if (smpr) {
					auto smp = smpr.Child();
					if (smpr.ChildMetaData().flagSet(md_bare)) {
						smp = smp->GetAnonChild();
					}
					if (smp) {
						DocumentTreeIterateElement(element, smp, smpr);
					}
				}
			}
			else if (auto attribute = dynamic_cast<const xmlpp::Attribute *>(node)) {
				auto smp = mp->GetChild(attribute->get_name().raw(), [](const auto & h) {
					return h->GetMetadata().flagSet(md_attribute);
				});
				if (smp) {
					smp->Create();
					smp->SetValue(XmlAttributeValueSource(attribute));
					smp->Complete();
				}
			}
			else if (auto content = dynamic_cast<const xmlpp::ContentNode *>(node)) {
				ModelPartPtr smp;
				if (!content->is_white_space()) {
					smp = mp->GetAnonChild([](const auto & h) {
						return h->GetMetadata().flagSet(md_text);
					});
				}
				if (smp) {
					smp->SetValue(XmlContentValueSource(content));
				}
				else {
					mp->SetValue(XmlContentValueSource(content));
				}
			}
			node = node->get_next_sibling();
		}
	}

	void
	XmlDeserializer::DocumentTreeIterate(const xmlpp::Document * doc, const ModelPartPtr & mp)
	{
		DocumentTreeIterate(doc->get_root_node(), mp);
	}

	void
	XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, const ModelPartPtr & mp,
			const HookCommon * hp, const ElementCreator & ec)
	{
		if (name.empty()) {
			return;
		}
		if (hp && hp->GetMetadata().flagSet(md_attribute)) {
			mp->GetValue(XmlAttributeValueTarget(n, name));
		}
		else if (hp && hp->GetMetadata().flagSet(md_text)) {
			mp->GetValue(XmlContentValueTarget(n));
		}
		else if (hp && hp->GetMetadata().flagSet(md_attributes)) {
			ModelTreeIterateDictAttrs(n->add_child_element(name), mp);
		}
		else if (hp && hp->GetMetadata().flagSet(md_elements)) {
			ModelTreeIterateDictElements(n->add_child_element(name), mp);
		}
		else {
			if (hp && hp->GetMetadata().flagSet(md_bare)) {
				ModelTreeProcessElement(n, mp, [name](auto && PH1, auto &&) {
					return PH1->add_child_element(name);
				});
			}
			else {
				CurrentElementCreator cec([ec, n, name] {
					return ec(n, name);
				});
				ModelTreeProcessElement(cec, mp, defaultElementCreator);
			}
		}
	}

	void
	XmlSerializer::ModelTreeIterateDictAttrs(xmlpp::Element * element, const ModelPartPtr & dict)
	{
		dict->OnEachChild([element](const auto &, const auto & mp, const auto &) {
			if (mp->HasValue()) {
				mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp, element](const auto & name) {
					mp->GetChild(valueName)->GetValue(XmlAttributeValueTarget(element, name));
				}));
			}
		});
	}

	void
	XmlSerializer::ModelTreeIterateDictElements(xmlpp::Element * element, const ModelPartPtr & dict)
	{
		dict->OnEachChild([element](const auto &, const auto & mp, const auto &) {
			if (mp->HasValue()) {
				mp->GetChild(keyName)->GetValue(XmlValueTarget([&mp, element](const auto & name) {
					CurrentElementCreator cec([&element, &name]() {
						return element->add_child_element(name);
					});
					ModelTreeProcessElement(cec, mp->GetChild(valueName), defaultElementCreator);
				}));
			}
		});
	}

	void
	XmlSerializer::ModelTreeProcessElement(
			const CurrentElementCreator & cec, ModelPartPtr mp, const ElementCreator & ec)
	{
		if (mp->GetType() == ModelPartType::Simple) {
			mp->GetValue(XmlContentValueTarget(cec));
		}
		else if (mp->HasValue()) {
			auto typeIdPropName = mp->GetTypeIdProperty();
			auto typeId = mp->GetTypeId();
			auto element = cec.get();
			if (typeId && typeIdPropName) {
				element->set_attribute(*typeIdPropName, *typeId);
				mp = mp->GetSubclassModelPart(*typeId);
			}
			mp->OnEachChild([element, ec](auto && PH1, auto && PH2, auto && PH3) {
				return XmlSerializer::ModelTreeIterate(element, PH1, PH2, PH3, ec);
			});
		}
	}

	void
	XmlSerializer::ModelTreeIterateRoot(xmlpp::Document * doc, const std::string & name, const ModelPartPtr & mp)
	{
		ModelTreeProcessElement(doc->create_root_node(name), mp, defaultElementCreator);
	}

	XmlStreamSerializer::XmlStreamSerializer(std::ostream & s) : strm(s) { }

	XmlStreamDeserializer::XmlStreamDeserializer(std::istream & s) : strm(s) { }

	void
	XmlStreamDeserializer::Deserialize(ModelPartForRootPtr modelRoot)
	{
		xmlpp::DomParser dom;
		dom.parse_stream(strm);
		auto doc = dom.get_document();
		DocumentTreeIterate(doc, modelRoot);
	}

	void
	XmlStreamSerializer::Serialize(ModelPartForRootPtr modelRoot)
	{
		xmlpp::Document doc;
		modelRoot->OnEachChild([&doc](auto && PH1, auto && PH2, auto &&) {
			return XmlSerializer::ModelTreeIterateRoot(&doc, PH1, PH2);
		});
		doc.write_to_stream(strm);
	}

	XmlFileSerializer::XmlFileSerializer(std::filesystem::path p) : path(std::move(p)) { }

	XmlFileDeserializer::XmlFileDeserializer(std::filesystem::path p) : path(std::move(p)) { }

	void
	XmlFileDeserializer::Deserialize(ModelPartForRootPtr modelRoot)
	{
		xmlpp::DomParser dom(path);
		auto doc = dom.get_document();
		DocumentTreeIterate(doc, modelRoot);
	}

	void
	XmlFileSerializer::Serialize(ModelPartForRootPtr modelRoot)
	{
		xmlpp::Document doc;
		modelRoot->OnEachChild([&doc](auto && PH1, auto && PH2, auto &&) {
			return XmlSerializer::ModelTreeIterateRoot(&doc, PH1, PH2);
		});
		doc.write_to_file_formatted(path);
	}

	XmlDocumentSerializer::XmlDocumentSerializer(xmlpp::Document *& d) : doc(d) { }

	XmlDocumentDeserializer::XmlDocumentDeserializer(const xmlpp::Document * d) : doc(d) { }

	void
	XmlDocumentDeserializer::Deserialize(ModelPartForRootPtr modelRoot)
	{
		DocumentTreeIterate(doc, modelRoot);
	}

	void
	XmlDocumentSerializer::Serialize(ModelPartForRootPtr modelRoot)
	{
		doc = new xmlpp::Document();
		modelRoot->OnEachChild([this](auto && PH1, auto && PH2, auto &&) {
			return XmlSerializer::ModelTreeIterateRoot(doc, PH1, PH2);
		});
	}

	AdHocFormatter(BadBooleanValueMsg, "Bad boolean value [%?]");
	void
	BadBooleanValue::ice_print(std::ostream & s) const
	{
		BadBooleanValueMsg::write(s, text);
	}
}