forked from stanthomas/tinyxml2-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtixml2cx.h
189 lines (160 loc) · 5.98 KB
/
tixml2cx.h
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
/*
tinyxml2ex - a set of add-on classes and helper functions bringing C++11/14 features, such as iterators, strings and exceptions, to tinyxml2
tixml2cx.h implements the copy operations of tinyxml2ex
these copy a branch of an xml document to another document or another location within the same document,
optionally substituting element parameter values to allow customisation of templated xml snippets
it is separate from the base tinyxml2 extensions because it uses an additional collection class (unordered_map)
Copyright (c) 2017 Stan Thomas
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
tinyxml2 is the work of Lee Thomason (www.grinninglizard.com) and others.
It can be found here: https://github.com/leethomason/tinyxml2 and has it's own licensing terms.
*/
#pragma once
#include <unordered_map>
#ifndef __TINYXML_EX__
#include <tixml2ex.h>
#endif // !__TINYXML_EX__
namespace tinyxml2
{
inline namespace tixml2ex
{
class XMLCopy : public XMLVisitor
{
public:
XMLCopy (XMLElement * target) : _target(target) { _newDoc = target->GetDocument(); }
virtual bool VisitEnter (const XMLElement & element, const XMLAttribute * attribute) override
{
auto e = _newDoc->NewElement (element.Name());
_target->InsertEndChild (e);
while (attribute)
{
e->SetAttribute (attribute->Name(), attribute->Value());
attribute = attribute->Next();
}
_target = e;
return true;
}
virtual bool VisitExit (const XMLElement & element) override
{
_target = const_cast <XMLElement *> (_target->Parent()->ToElement());
return true;
}
virtual bool Visit (const XMLDeclaration & declaration) override
{
auto d = declaration.ShallowClone (_newDoc);
_target->InsertEndChild (d);
return true;
}
virtual bool Visit (const XMLText & txt) override
{
auto t = txt.ShallowClone (_newDoc);
_target->InsertEndChild (t);
return true;
}
virtual bool Visit (const XMLComment & comment) override
{
auto c = comment.ShallowClone (_newDoc);
_target->InsertEndChild (c);
return true;
}
protected:
XMLElement * _target;
XMLDocument * _newDoc;
}; // XMLCopy
class XMLCopyAndReplace : public XMLCopy
{
public:
XMLCopyAndReplace (XMLElement * target, const std::unordered_map<std::string, std::string> & params, char openDelim, char closeDelim)
: XMLCopy(target), _params(params), _openDelim(openDelim), _closeDelim(closeDelim) {}
bool VisitEnter (const XMLElement & element, const XMLAttribute * attribute) override
{
if (XMLCopy::VisitEnter (element, attribute))
{
auto a = const_cast <XMLAttribute *> (_target->FirstAttribute());
while (a)
{
auto subst = substitute (a->Value());
if (subst.first)
a->SetAttribute (subst.second.c_str());
a = const_cast <XMLAttribute *> (a->Next());
}
return true;
}
else
return false;
}
virtual bool Visit (const XMLText & txt) override
{
if (XMLCopy::Visit (txt))
{
auto t = _target->LastChild()->ToText(); // it's the element text 'cos that the last one we added
if (!t->CData()) // we don't substitute CDATA
{
auto subst = substitute (t->Value());
if (subst.first)
t->SetValue (subst.second.c_str());
}
return true;
}
else
return false;
}
private:
std::pair <bool, std::string> substitute (const std::string & val)
{
bool substituted{ false };
std::string newValue;
std::string::size_type cursor = 0;
auto ps = val.find (_openDelim);
while (ps != std::string::npos)
{
newValue += val.substr (cursor, ps - cursor);
auto pe = val.find (_closeDelim, ps);
if (pe != std::string::npos)
{
cursor = pe + 1;
auto px = _params.find (val.substr (ps + 1, pe - ps - 1));
if (px != _params.end())
{
newValue += px->second;
}
else
throw XmlException ("no value for parameter " + (val.substr (ps + 1, pe - ps - 1)));
substituted = true;
ps = val.find (_openDelim, pe);
}
else
{
ps = std::string::npos;
break;
}
}
if (substituted)
return std::make_pair (true, newValue + val.substr (cursor, ps - cursor));
else
return std::make_pair (false, val);
}
private:
const std::unordered_map<std::string, std::string> & _params;
const char _openDelim;
const char _closeDelim;
}; // XMLCopyAndReplace
inline void xcopy (const XMLElement * source, XMLElement * destinationParent)
{
XMLCopy copier (destinationParent);
source->Accept (&copier);
}
inline void xcopy (const XMLElement * source, XMLElement * destinationParent, const std::unordered_map<std::string, std::string> & params, char openDelim = '{', char closeDelim = '}')
{
XMLCopyAndReplace copier (destinationParent, params, openDelim, closeDelim);
source->Accept (&copier);
}
}
}