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
|
#include "serializer.h"
#include <libxml++/document.h>
#include <libxml++/parsers/domparser.h>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <stdexcept>
#include <glibmm/ustring.h>
namespace Slicer {
class BadBooleanValue : public std::invalid_argument {
public:
BadBooleanValue(const Glib::ustring &) :
std::invalid_argument("Bad boolean value")
{
}
};
static const Glib::ustring TrueText("true");
static const Glib::ustring FalseText("false");
class XmlValueSource : public ValueSource {
public:
XmlValueSource(const Glib::ustring & s) :
value(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();
}
protected:
const Glib::ustring value;
};
class XmlContentValueSource : public XmlValueSource {
public:
XmlContentValueSource(const xmlpp::ContentNode * c) :
XmlValueSource(c->get_content())
{
}
};
class XmlAttributeValueSource : public XmlValueSource {
public:
XmlAttributeValueSource(const xmlpp::Attribute * a) :
XmlValueSource(a->get_value())
{
}
};
class XmlValueTarget : public ValueTarget {
public:
XmlValueTarget(boost::function<void(const Glib::ustring &)> a) :
apply(a)
{
}
virtual void get(const bool & value) const
{
if (value) {
apply(TrueText);
}
else {
apply(FalseText);
}
}
virtual void get(const Ice::Byte & value) const
{
apply(boost::lexical_cast<Glib::ustring>((int)value));
}
virtual void set(const Ice::Short & value) const
{
apply(boost::lexical_cast<Glib::ustring>(value));
}
virtual void get(const Ice::Int & value) const
{
apply(boost::lexical_cast<Glib::ustring>(value));
}
virtual void get(const Ice::Long & value) const
{
apply(boost::lexical_cast<Glib::ustring>(value));
}
virtual void get(const Ice::Float & value) const
{
apply(boost::lexical_cast<Glib::ustring>(value));
}
virtual void get(const Ice::Double & value) const
{
apply(boost::lexical_cast<Glib::ustring>(value));
}
virtual void get(const std::string & value) const
{
apply(value);
}
private:
const boost::function<void(const Glib::ustring &)> apply;
};
class XmlAttributeValueTarget : public XmlValueTarget {
public:
XmlAttributeValueTarget(xmlpp::Element * p, const std::string & n) :
XmlValueTarget(boost::bind(&xmlpp::Element::set_attribute, p, Glib::ustring(n), _1, Glib::ustring()))
{
}
};
class XmlContentValueTarget : public XmlValueTarget {
public:
XmlContentValueTarget(xmlpp::Element * p) :
XmlValueTarget(boost::bind(&xmlpp::Element::set_child_text, p, _1))
{
}
};
void
Xml::DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp)
{
while (node) {
if (auto element = dynamic_cast<const xmlpp::Element *>(node)) {
auto smp = mp->GetChild(element->get_name());
if (smp) {
auto typeAttr = element->get_attribute("slicer-typeid");
if (typeAttr) {
smp = smp->GetSubclassModelPart(typeAttr->get_value());
}
smp->Create();
auto attrs(element->get_attributes());
if (!attrs.empty()) {
DocumentTreeIterate(attrs.front(), smp);
}
DocumentTreeIterate(element->get_first_child(), smp);
smp->Complete();
}
}
else if (auto attribute = dynamic_cast<const xmlpp::Attribute *>(node)) {
auto smp = mp->GetChild("@" + attribute->get_name());
if (smp) {
smp->Create();
smp->SetValue(new XmlAttributeValueSource(attribute));
smp->Complete();
}
}
else if (auto content = dynamic_cast<const xmlpp::ContentNode *>(node)) {
mp->SetValue(new XmlContentValueSource(content));
}
node = node->get_next_sibling();
}
}
void
Xml::DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp)
{
DocumentTreeIterate(doc->get_root_node(), mp);
}
void
Xml::ModelTreeIterate(xmlpp::Element * n, const std::string & name, ModelPartPtr mp)
{
if (name.empty()) {
return;
}
if (name[0] == '@') {
mp->GetValue(new XmlAttributeValueTarget(n, name.substr(1)));
}
else if (mp) {
auto element = n->add_child(name);
auto typeId = mp->GetTypeId();
if (typeId) {
element->set_attribute("slicer-typeid", *typeId);
mp = mp->GetSubclassModelPart(*typeId);
}
mp->GetValue(new XmlContentValueTarget(element));
mp->OnEachChild(boost::bind(&Xml::ModelTreeIterate, element, _1, _2));
}
}
void
Xml::ModelTreeIterateRoot(xmlpp::Document * doc, const std::string & name, ModelPartPtr mp)
{
auto root = doc->create_root_node(name);
mp->OnEachChild(boost::bind(&Xml::ModelTreeIterate, root, _1, _2));
}
void
Xml::Deserialize(const boost::filesystem::path & path, ModelPartPtr modelRoot)
{
xmlpp::DomParser dom(path.string());
auto doc = dom.get_document();
DocumentTreeIterate(doc, modelRoot);
}
void
Xml::Serialize(const boost::filesystem::path & path, ModelPartPtr modelRoot)
{
xmlpp::Document doc;
modelRoot->OnEachChild(boost::bind(&Xml::ModelTreeIterateRoot, &doc, _1, _2));
doc.write_to_file_formatted(path.string());
}
}
|