-
Notifications
You must be signed in to change notification settings - Fork 127
/
xml_encoder.h
371 lines (332 loc) · 10.4 KB
/
xml_encoder.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
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
/*
* Copyright (C) 2021 Duowan Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __X_PACK_XML_ENCODER_H
#define __X_PACK_XML_ENCODER_H
#include <list>
#include <sstream>
#include "xencoder.h"
namespace xpack {
class XmlWriter {
struct Attr {
const char* key;
std::string val;
Attr(const char*_key, const std::string&_val):key(_key), val(_val){}
};
struct Node {
Node(const char*_key=NULL, const Extend *ext=NULL) {
(void)ext;
if (_key != NULL) {
key = _key;
}
}
~Node() {
std::list<Node*>::iterator it;
for (it = childs.begin(); it!=childs.end(); ++it) {
delete *it;
}
}
std::string key; // key for this node
std::string val; // value for this node, only for leaf node
std::list<Attr> attrs; // attribute
std::list<Node*> childs;// child nodes
std::string vec_key; // key for vector
};
friend class XEncoder<XmlWriter>;
friend class XmlEncoder;
const static bool support_null = false;
public:
XmlWriter(int indentCount = -1, char indentChar = ' ', int decimalPlaces = 324):_indentCount(indentCount),_indentChar(indentChar),_decimalPlaces(decimalPlaces) {
if (_indentCount > 0) {
if (_indentChar!=' ' && _indentChar!='\t') {
throw std::runtime_error("indentChar must be space or tab");
}
}
_cur = &_root;
}
private:
std::string String() {
merge();
return _output;
}
inline static const char *Name() {
return "xml";
}
inline const char *IndexKey(size_t index) {
(void)index;
return _cur->vec_key.c_str();
}
void ArrayBegin(const char *key, const Extend *ext) {
Node *n = new Node(key, ext);
if (NULL!=_cur && !_cur->vec_key.empty()) { // vector<vector<...>>
n->vec_key = n->key;
} else if (NULL != ext && NULL != ext->alias) {
if (!ext->alias->Flag("xml", "vl", &n->vec_key)) { // forward compatible, support vector label
if (ext->alias->Flag("xml", "sbs")) { // no top label, vector item will side by side
n->vec_key.swap(n->key); // n->vec_key=key and n->key="";
} else {
n->vec_key = n->key; // has top level
}
}
} else {
n->vec_key = n->key;
}
_cur->childs.push_back(n);
_stack.push_back(_cur);
_cur = n;
}
void ArrayEnd(const char *key, const Extend *ext) {
(void)key;
(void)ext;
_cur = _stack.back();
_stack.pop_back();
}
void ObjectBegin(const char *key, const Extend *ext) {
Node *n = new Node(key, ext);
_cur->childs.push_back(n);
_stack.push_back(_cur);
_cur = n;
}
void ObjectEnd(const char *key, const Extend *ext) {
(void)key;
(void)ext;
_cur = _stack.back();
_stack.pop_back();
}
bool WriteNull(const char*key, const Extend *ext) {
static std::string empty;
return this->encode_string(key, empty, ext);
}
// string
bool encode_string(const char*key, const std::string &val, const Extend *ext) {
if (Extend::Attribute(ext)) {
_cur->attrs.push_back(Attr(key, string_quote(val)));
} else if (Extend::XmlContent(ext)) {
_cur->val = val;
} else if (!Extend::AliasFlag(ext, "xml", "cdata")) {
Node *n = new Node(key);
n->val = string_quote(val);
_cur->childs.push_back(n);
} else {
Node *n = new Node(key);
n->val = "<![CDATA[" + val + "]]>";
_cur->childs.push_back(n);
}
return true;
}
// bool
bool encode_bool(const char*key, const bool &val, const Extend *ext) {
std::string bval;
if (val) {
bval = "true";
} else {
bval = "false";
}
if (Extend::Attribute(ext)) {
_cur->attrs.push_back(Attr(key, bval));
} else if (Extend::XmlContent(ext)) {
_cur->val = bval;
} else {
Node *n = new Node(key);
n->val = bval;
_cur->childs.push_back(n);
}
return true;
}
// integer
template <class T>
typename x_enable_if<numeric<T>::is_integer, bool>::type encode_number(const char*key, const T& val, const Extend *ext) {
if (val==0 && Extend::OmitEmpty(ext)) {
return false;
} else if (Extend::Attribute(ext)) {
_cur->attrs.push_back(Attr(key, Util::itoa(val)));
} else if (Extend::XmlContent(ext)) {
_cur->val = Util::itoa(val);
} else {
Node *n = new Node(key);
n->val = Util::itoa(val);
_cur->childs.push_back(n);
}
return true;
}
// float
template <class T>
typename x_enable_if<numeric<T>::is_float, bool>::type encode_number(const char*key, const T &val, const Extend *ext) {
if (val==0 && Extend::OmitEmpty(ext)) {
return false;
} else {
std::ostringstream os;
os.precision(_decimalPlaces+1);
os<<val;
std::string fval = os.str();
if (Extend::Attribute(ext)) {
_cur->attrs.push_back(Attr(key, fval));
} else if (Extend::XmlContent(ext)) {
_cur->val = fval;
} else {
Node *n = new Node(key);
n->val = fval;
_cur->childs.push_back(n);
}
}
return true;
}
// escape string
static std::string string_quote(const std::string &val) {
std::string ret;
ret.reserve(val.length()*2);
for (size_t i=0; i<val.length(); ++i) {
int c = (int)(val[i]) & 0xFF;
switch (c) {
case '<':
ret += "<";
break;
case '>':
ret += ">";
break;
case '&':
ret += "&";
break;
case '\'':
ret += "'";
break;
case '\"':
ret += """;
break;
default:
if(c >= ' ') {
ret.push_back(val[i]);
} else {
ret += "\\x";
unsigned char uch = (unsigned char)val[i];
unsigned char h = (uch>>4)&0xf;
unsigned char l = uch&0xf;
if (h < 10) {
ret.push_back(h+'0');
} else {
ret.push_back(h-10+'A');
}
if (l < 10) {
ret.push_back(l+'0');
} else {
ret.push_back(l-10+'A');
}
}
}
}
return ret;
}
void appendNode(const Node *nd, int depth) {
bool indentEnd = true;
if (!nd->key.empty()) { // for array that without label
indent(depth);
_output.push_back('<');
_output += nd->key;
} else if (depth > 0) {
depth--;
}
// output attribute
std::list<Attr>::const_iterator it;
for (it=nd->attrs.begin(); it!=nd->attrs.end(); ++it) {
_output.push_back(' ');
_output += it->key;
_output += "=\"";
_output += it->val;
_output.push_back('"');
}
// no content, end object
if (nd->val.empty() && nd->childs.size()==0) {
if (!nd->key.empty()) {
_output += "/>";
}
return;
}
// key finished
if (!nd->key.empty()) {
_output.push_back('>');
}
if (!nd->val.empty()) {
_output += nd->val;
indentEnd = false;
} else {
std::list<Node*>::const_iterator it;
for (it=nd->childs.begin(); it!=nd->childs.end(); ++it) {
appendNode(*it, depth+1);
}
}
if (!nd->key.empty()) {
if (indentEnd) {
indent(depth);
}
_output += "</";
_output += nd->key;
_output.push_back('>');
}
}
void merge() {
if (_output.length() > 0 || _root.childs.size()==0) {
return;
}
appendNode(_root.childs.front(), 0);
}
void indent(int depth) {
if (_indentCount < 0) {
return;
}
_output.push_back('\n');
if (_indentCount == 0) {
return;
}
for (int i=0; i<depth*_indentCount; ++i) {
_output.push_back(_indentChar);
}
}
Node _root;
Node *_cur;
std::list<Node*> _stack;
std::string _output;
int _indentCount;
char _indentChar;
int _decimalPlaces;
};
class XmlEncoder {
public:
XmlEncoder() {
indentCount = -1;
indentChar = ' ';
maxDecimalPlaces = 324;
}
XmlEncoder(int _indentCount, char _indentChar, int _maxDecimalPlaces = 324) { // compat
indentCount = _indentCount;
indentChar = _indentChar;
maxDecimalPlaces = _maxDecimalPlaces;
}
void SetMaxDecimalPlaces(int _maxDecimalPlaces) {
maxDecimalPlaces = _maxDecimalPlaces;
}
template <class T>
std::string encode(const T&val, const std::string&root) {
XmlWriter wr(indentCount, indentChar, maxDecimalPlaces);
XEncoder<XmlWriter> en(wr);
en.encode(root.c_str(), val, NULL);
return wr.String();
}
private:
int indentCount;
char indentChar;
int maxDecimalPlaces;
};
}
#endif