forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastXMLVisitor.cpp
More file actions
279 lines (236 loc) · 9.6 KB
/
FastXMLVisitor.cpp
File metadata and controls
279 lines (236 loc) · 9.6 KB
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
// Copyright (c) 2016, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
// See the file license.txt for licensing information.
#include "FastXMLVisitor.h"
#include "xml_util.h"
#include <cstring>
#include <cstdlib>
#include <iostream>
const char *FastXMLVisitor::get_optional_attr(const XMLElement &element,
const char *attr_name,
const char *default_value) const {
return mfast::xml_parser::get_optional_attr(element, attr_name, default_value);
}
bool FastXMLVisitor::is_mandatory_constant(const XMLElement &element) {
if (strcmp("mandatory",
get_optional_attr(element, "presence", "mandatory")) == 0) {
if (element.FirstChildElement("constant")) {
return true;
}
}
return false;
}
FastXMLVisitor::FastXMLVisitor() { num_fields_.push_back(0); }
bool FastXMLVisitor::VisitEnterTemplates(const XMLElement & /* element */) {
return true;
}
bool FastXMLVisitor::VisitExitTemplates(const XMLElement & /* element */,
std::size_t /* numFields */) {
return true;
}
bool FastXMLVisitor::VisitEnterTemplate(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitExitTemplate(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* numFields */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitTemplateRef(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitEnterGroup(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitExitGroup(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* numFields */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitEnterSequence(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitExitSequence(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* numFields */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitInteger(const XMLElement & /* element */,
int /* bits */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitDecimal(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitString(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitByteVector(const XMLElement & /* element */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitIntVector(const XMLElement & /* element */,
int /* bits */,
const std::string & /* name_attr */,
std::size_t /* index */) {
return true;
}
bool FastXMLVisitor::VisitEnterDefine(const XMLElement & /* element */,
const std::string & /* name_attr */) {
return true;
}
bool FastXMLVisitor::VisitExitDefine(const XMLElement & /* element */,
const std::string & /* name_attr */) {
return true;
}
void FastXMLVisitor::save_context(const XMLElement &element) {
context_stack_.push_back(context_stack_.back());
instruction_context &context = context_stack_.back();
context.ns_ = get_optional_attr(element, "ns", context.ns_.c_str());
context.dictionary_ =
get_optional_attr(element, "dictionary", context.dictionary_.c_str());
}
bool parse_bits(const char *str, int &bits, bool &is_vector) {
bits = 0;
if (str[0] == '6' && str[1] == '4') {
bits = 64;
} else if (str[0] == '3' && str[1] == '2') {
bits = 32;
}
if (bits != 0) {
is_vector = std::strcmp(str + 2, "Vector") == 0;
}
return bits != 0;
}
bool FastXMLVisitor::VisitEnter(const XMLElement &element,
const XMLAttribute * /* attr */) {
const char *element_name = element.Name();
if (strcmp(element_name, "templates") == 0) {
instruction_context context;
context.ns_ = get_optional_attr(element, "templateNs", "");
context.dictionary_ = get_optional_attr(element, "dictionary", "");
context_stack_.push_back(context);
return VisitEnterTemplates(element);
}
std::string name_attr = get_optional_attr(element, "name", "");
if (strcmp(element_name, "templateRef") == 0) {
return VisitTemplateRef(element, name_attr, num_fields_.back());
}
bool result = true;
if (strcmp(element_name, "template") == 0) {
save_context(element);
result = VisitEnterTemplate(element, name_attr, num_fields_.back());
} else if (strcmp(element_name, "group") == 0) {
save_context(element);
result = VisitEnterGroup(element, name_attr, num_fields_.back());
} else if (strcmp(element_name, "sequence") == 0) {
save_context(element);
result = VisitEnterSequence(element, name_attr, num_fields_.back());
} else if (strcmp(element_name, "define") == 0) {
return VisitEnterDefine(element, name_attr);
} else {
int bits;
bool is_vector;
if ((strncmp(element_name, "int", 3) == 0 &&
parse_bits(element_name + 3, bits, is_vector)) ||
(strncmp(element_name, "uInt", 4) == 0 &&
parse_bits(element_name + 4, bits, is_vector))) {
// int8, int16 and uint8, uint16 are not standards, convert them to int32
// and uint32 respectively
if (is_vector)
return VisitIntVector(element, bits, name_attr, num_fields_.back());
return VisitInteger(element, bits, name_attr, num_fields_.back());
} else if (strcmp(element_name, "decimal") == 0) {
return VisitDecimal(element, name_attr, num_fields_.back());
} else if (strcmp(element_name, "string") == 0) {
return VisitString(element, name_attr, num_fields_.back());
} else if (strcmp(element_name, "byteVector") == 0) {
return VisitByteVector(element, name_attr, num_fields_.back());
}
return true;
}
num_fields_.push_back(0);
return result;
}
bool FastXMLVisitor::VisitExit(const XMLElement &element) {
const char *element_name = element.Name();
bool result = true;
if (strcmp(element_name, "templates") == 0) {
return VisitExitTemplates(element, num_fields_.back());
}
std::string name_attr = get_optional_attr(element, "name", "");
if (strcmp(element_name, "templateRef") == 0) {
num_fields_.back() += 1;
}
if (name_attr.empty())
return true;
typedef bool (FastXMLVisitor::*VisitExitPtr)(
const XMLElement &, const std::string &, std::size_t, std::size_t index);
VisitExitPtr member_ptr;
if (strcmp(element_name, "template") == 0) {
member_ptr = &FastXMLVisitor::VisitExitTemplate;
} else if (strcmp(element_name, "group") == 0) {
member_ptr = &FastXMLVisitor::VisitExitGroup;
} else if (strcmp(element_name, "sequence") == 0) {
member_ptr = &FastXMLVisitor::VisitExitSequence;
} else if (strcmp(element_name, "define") == 0) {
return VisitExitDefine(element, name_attr);
} else if (strncmp(element_name, "int", 3) == 0 ||
strncmp(element_name, "uInt", 4) == 0 ||
strcmp(element_name, "decimal") == 0 ||
strcmp(element_name, "string") == 0 ||
strcmp(element_name, "byteVector") == 0) {
num_fields_.back() += 1;
return true;
} else {
return true;
}
std::size_t numFields = num_fields_.back();
num_fields_.pop_back();
result =
(this->*member_ptr)(element, name_attr, numFields, num_fields_.back());
num_fields_.back() += 1;
context_stack_.pop_back();
return result;
}
FastXMLVisitor::instruction_context &FastXMLVisitor::current_context() {
return context_stack_.back();
}
const XMLElement *FastXMLVisitor::only_child(const XMLElement &element) {
const XMLElement *first_elem = element.FirstChildElement();
if (strcmp(first_elem->Name(), "length") == 0) {
first_elem = first_elem->NextSiblingElement();
}
if (first_elem->NextSibling() == nullptr)
return first_elem;
return nullptr;
}
// if element has only child and the child element is templateRef, it returns
// the address of the child element; otherwise it returns 0.
const XMLElement *
FastXMLVisitor::only_child_templateRef(const XMLElement &element) {
const XMLElement *first_elem = only_child(element);
if (first_elem && strcmp(first_elem->Name(), "templateRef") == 0)
return first_elem;
return nullptr;
}