-
Notifications
You must be signed in to change notification settings - Fork 0
/
XML_Parser.cpp
162 lines (133 loc) · 4.99 KB
/
XML_Parser.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
#include "XML_Parser.h"
namespace xmlp
{
// This class required only to extract lines from files with iterators
class line : public std::string {};
// Define operator stream reading from file to line class
// Required because if read to string all spaces will be stop reading line
std::istream &operator>>(std::istream &is, line &l)
{
std::getline(is, l);
return is;
}
bool XmlParser::ValidateFile(const std::string& fileName)
{
std::ifstream fileToValidate(fileName.c_str());
if (!fileToValidate.is_open())
{
LOG("Failed to open file: " + fileName);
return false;
}
std::istream_iterator<line> begin(fileToValidate);
std::istream_iterator<line> end;
return Validate(begin, end, &Data);
}
bool XmlParser::ValidateString(const std::string& str)
{
std::vector<std::string> vec = {str};
return ValidateContainerWithStrings(vec);
}
bool XmlParser::ValidateVectorOfStrings(const std::vector<std::string>& vectorToValidate)
{
return Validate(vectorToValidate.begin(), vectorToValidate.end(), &Data);
}
bool XmlParser::InsertData(const std::list<XmlData>::iterator& posToInsertIt, std::list<std::string> listToInsert)
{
if (posToInsertIt == std::list<xmlp::XmlData>::iterator())
return false;
std::list<XmlData> listWithInsertData;
listToInsert.emplace_front("<VALIDATION_ROOT_TAG>");
listToInsert.emplace_back("</VALIDATION_ROOT_TAG>");
// Check if it appending valid data
if (Validate(listToInsert.begin(), listToInsert.end(), &listWithInsertData))
{
for (auto it = ++listWithInsertData.begin(); it != --listWithInsertData.end(); ++it)
{
std::string properPath = it->Path.substr(it->Path.find('/') + 1);
properPath = properPath.substr(properPath.find('/') + 1);
if (posToInsertIt->DataType == closingTag)
properPath = properPath + posToInsertIt->TagName + "/";
it->Path = posToInsertIt->Path + properPath;
}
Data.insert(posToInsertIt, ++listWithInsertData.begin(), --listWithInsertData.end());
return true;
}
else
return false;
}
bool XmlParser::InsertData(const std::list<XmlData>::iterator& firstPosToInsertIt, const std::list<XmlData>::iterator& secondPosToInsertIt,
std::list<std::string> firstListToInsert, std::list<std::string> secondListToInsert)
{
if (firstPosToInsertIt == std::list<xmlp::XmlData>::iterator() || secondPosToInsertIt == std::list<xmlp::XmlData>::iterator())
return false;
std::list<XmlData> listWithInsertData;
Data.insert(firstPosToInsertIt, firstListToInsert.begin(), firstListToInsert.end());
Data.insert(secondPosToInsertIt, secondListToInsert.begin(), secondListToInsert.end());
// Check if it appending valid data
if (Validate(Data.begin(), Data.end(), &listWithInsertData))
{
Data = listWithInsertData;
return true;
}
else
return false;
}
bool XmlParser::WriteDataToFile(std::string fileName)
{
std::ofstream file(fileName.c_str());
if(!file.is_open())
{
LOG("Failed to open file to write");
file.close();
return false;
}
for (const auto& it : Data)
{
int tabCount = Count(it.Path, '/');
for (int i = 1; i < tabCount; ++i)
file << '\t';
file << it.Data << std::endl;
}
file.close();
return true;
}
bool XmlParser::FindInlineElement(std::string tagName, std::list<XmlData>::iterator& iterator)
{
for (auto it = Data.begin(); it != Data.end(); ++it)
{
if (it->TagName == tagName && it->DataType == xmlp::inlineTag)
{
iterator = it;
return true;
}
}
iterator = Data.end();
return false;
}
bool XmlParser::FindOpeningElement(std::string tagName, std::list<XmlData>::iterator& iterator)
{
for (auto it = Data.begin(); it != Data.end(); ++it)
{
if (it->TagName == tagName && it->DataType == xmlp::openingTag)
{
iterator = it;
return true;
}
}
iterator = Data.end();
return false;
}
bool XmlParser::FindClosingElement(std::string tagName, std::list<XmlData>::iterator& iterator)
{
for (auto it = Data.begin(); it != Data.end(); ++it)
{
if (it->TagName == tagName && it->DataType == xmlp::closingTag)
{
iterator = it;
return true;
}
}
iterator = Data.end();
return false;
}
}