forked from jackburton79/ocs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXML.cpp
228 lines (180 loc) · 4.91 KB
/
XML.cpp
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
/*
* XML.cpp
*
* Created on: 01/ago/2014
* Author: Stefano Ceccherini
*/
#include "Support.h"
#include "XML.h"
#include "ZLibCompressor.h"
#include <iostream>
#include <streambuf>
#include <string>
#include <string.h>
#include <tinyxml2/tinyxml2.h>
class ElementFinderByName : public tinyxml2::XMLVisitor {
public:
ElementFinderByName(const std::string& elementName, int matchMode);
virtual bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* attr);
const tinyxml2::XMLElement* Element() const;
private:
std::string fElementName;
const tinyxml2::XMLElement* fElement;
bool fFull;
};
class ElementFinderByAttribute : public tinyxml2::XMLVisitor {
public:
ElementFinderByAttribute(const std::string& attributeName,
const std::string& attributeValue,
int matchMode);
virtual bool VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* attr);
const tinyxml2::XMLElement* Element() const;
private:
std::string fAttributeName;
std::string fAttributeValue;
const tinyxml2::XMLElement* fElement;
bool fFull;
};
// XML
std::string
XML::ToString(const tinyxml2::XMLDocument& document)
{
tinyxml2::XMLPrinter memoryPrinter;
document.Print(&memoryPrinter);
std::string str = memoryPrinter.CStr();
return str;
}
bool
XML::Compress(const tinyxml2::XMLDocument& document, char*& destination, size_t& destLength)
{
tinyxml2::XMLPrinter memoryPrinter;
document.Print(&memoryPrinter);
return ZLibCompressor::Compress(memoryPrinter.CStr(), memoryPrinter.CStrSize() - 1,
destination, destLength);
}
bool
XML::Uncompress(const char* source, size_t sourceLen, tinyxml2::XMLDocument& document)
{
size_t destLength;
char* destination = NULL;
if (!ZLibCompressor::Uncompress(source, sourceLen, destination, destLength))
return false;
tinyxml2::XMLError result = document.Parse(destination, destLength - 1);
delete[] destination;
return result == tinyxml2::XML_SUCCESS;
}
std::string
XML::GetElementText(const tinyxml2::XMLNode& node, const std::string& elementName)
{
std::string result;
const tinyxml2::XMLElement* element = GetElementByName(node, elementName);
if (element != NULL)
result = element->GetText();
return result;
}
std::string
XML::GetFirstChildElementText(const tinyxml2::XMLElement* element, const std::string& elementName)
{
std::string result;
const tinyxml2::XMLElement* foundElement = element->FirstChildElement(elementName.c_str());
if (foundElement != NULL)
result = foundElement->GetText();
return result;
}
const tinyxml2::XMLElement*
XML::GetElementByName(const tinyxml2::XMLNode& node,
std::string elementName,
int matchMode)
{
ElementFinderByName textFinder(elementName, matchMode);
node.Accept(&textFinder);
return textFinder.Element();
}
const tinyxml2::XMLElement*
XML::GetElementByAttribute(const tinyxml2::XMLNode& node,
std::string attributeName,
std::string attributeValue,
int matchMode)
{
ElementFinderByAttribute attributeFinder(attributeName, attributeValue, matchMode);
node.Accept(&attributeFinder);
return attributeFinder.Element();
}
// ElementFinderByName
ElementFinderByName::ElementFinderByName(const std::string& elementName, int matchMode)
:
XMLVisitor(),
fElementName(elementName),
fElement(NULL),
fFull(matchMode == XML::match_full)
{
}
/* virtual */
bool
ElementFinderByName::VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute*)
{
if (fElement != NULL)
return false;
if (fFull) {
if (fElementName.compare(element.Name()) == 0) {
fElement = &element;
return false;
}
} else {
if (fElementName.compare(0, fElementName.length(), element.Name(), fElementName.length()) == 0) {
fElement = &element;
return false;
}
}
return true;
}
const tinyxml2::XMLElement*
ElementFinderByName::Element() const
{
return fElement;
}
// ElementFinderByAttribute
ElementFinderByAttribute::ElementFinderByAttribute(const std::string& attributeName,
const std::string& attributeValue,
int matchMode)
:
XMLVisitor(),
fAttributeName(attributeName),
fAttributeValue(attributeValue),
fElement(NULL),
fFull(matchMode == XML::match_full)
{
}
/* virtual */
bool
ElementFinderByAttribute::VisitEnter(const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* attr)
{
if (fElement != NULL)
return false;
if (attr == NULL)
return true;
const tinyxml2::XMLAttribute* next = attr;
while (next != NULL) {
if (fFull) {
if (fAttributeName.compare(next->Name()) == 0
&& fAttributeValue.compare(next->Value()) == 0) {
fElement = &element;
return false;
}
} else {
if (fAttributeName.compare(next->Name()) == 0
&& fAttributeValue.compare(0, fAttributeValue.length(),
next->Value(), fAttributeValue.length()) == 0) {
fElement = &element;
return false;
}
}
next = next->Next();
}
return true;
}
const tinyxml2::XMLElement*
ElementFinderByAttribute::Element() const
{
return fElement;
}