/
Document.cpp
executable file
·263 lines (230 loc) · 8.24 KB
/
Document.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
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
#include "Document.h"
#include "ElementNode.h"
#include <regex>
namespace tinyXMLpp{
/**
* Function which returns the root element of the document.
*
*@return The root element of type ElementNode*.
*/
ElementNode* Document::getRootElement() const
{
return this->rootElement;
}
/**
* Destructor.
*/
Document::~Document()
{
int i;
for (i = 0; i < this->childNodes.size(); ++i) {
delete this->childNodes[i];
}
}
/**
* Function which checks whether an input string contains only a combination of spaces, newlines and tabs. It uses regex to
* do this checking.
*
*@param input The input string
*@return A bool value which is true if the input string contains only spaces, newlines and tabs
*/
bool Document::isEmptyText (const std::string& input) const {
regex rx("[\\r\\n\\t\\s]*");
return regex_match(input.begin(), input.end(), rx);
}
/**
* Function to validate that a given Node is not of types CDATANode and Document types. It also makes sure that if the given node
* is of type TextNode, its contents are empty. This is used whenever a child is being added to the document object. Only ElementNode,
* CommentNode or TextNodes with empty strings can be added as a child of a Document node.
*
* @param child The node object to be validated.
*/
void Document::validate(Node* child)
{
if (dynamic_cast<TextNode*>(child) != NULL) {
if (!isEmptyText(static_cast<TextNode*>(child)->getText())) {
throw XMLException ("Cannot add or remove a Text node directly to the XML Document");
}
}
if (dynamic_cast<CDATANode*>(child) != NULL || dynamic_cast<Document*>(child) != NULL) {
throw XMLException ("Cannot add or remove a CDATA Node or an XML Document directly to the XML Document");
}
}
/**
* Function which sets the root element of the Document object.
*
* @param child The node to be set as the root element of the Document object.
*/
void Document::setRootElement (Node* child)
{
ElementNode* temp = dynamic_cast<ElementNode*>(child);
if (this->isRootSet == false || temp == NULL) {
if ( temp != NULL) {
this->rootElement = temp;
this->isRootSet = true;
}
}
else
throw XMLException("Error! Root Element already exists for the document. A document cannot have more than one root.");
}
/**
* Function to add a child node to the Document object.
*
* @param child The node to be added as a child of the Document object.
*/
void Document::addChildNode (Node* child)
{
validate(child);
setRootElement(child);
this->childNodes.push_back(child);
}
/**
* Function to add a child node to the Document object, at a given index.
*
* @param child The node to be added as a child of the Document object.
* @param index The index at which 'child' should be added as a child of the Document object.
*/
void Document::addChildNode (Node* child, int index)
{
validate(child);
setRootElement(child);
if(index >= childNodes.size() || index < 0 )
throw XMLException("\nError! Trying to add a child node at an index that doesn't exist");
childNodes.insert(childNodes.begin() + index, child);
}
/**
* Function to remove a node from the child nodes list of the Document object.
*
* @param child The node to be removed from the list of child nodes of the Document object.
*/
void Document::removeChildNode (Node* child)
{
validate(child);
if (dynamic_cast<ElementNode*>(child) != NULL){
this->isRootSet = false;
this->rootElement = nullptr;
}
auto it = childNodes.begin();
while (it != childNodes.end()){
if (*it == child) {
childNodes.erase(it);
delete child;
return;
}
++it;
}
throw XMLException("\nError! Trying to remove a child node that doesn't exist");
}
/**
* Function to remove a child at the given index from the list of child nodes of the Document object.
*
* @param index The index from which a child node should be removed from the list of children of the Document object.
*/
void Document::removeChildNode (int index)
{
if(index >= this->childNodes.size())
throw XMLException("\nError! Trying to remove a child node from an index that doesn't exist");
Node* child = this->childNodes[index];
if (dynamic_cast<ElementNode*>(child) != NULL){
this->isRootSet = false;
this->rootElement = nullptr;
}
childNodes.erase(childNodes.begin() + index);
delete child;
}
/**
* Function to write the Document node as an XML node into the given output stream.
*
* @param os The output stream to which the Document node should be written as an XML node.
*/
void Document::write(std::ostream& os) const
{
if(!this->isRootSet)
throw XMLException("XML Document does not have a root");
for(int i=0; i < childNodes.size(); ++i){
childNodes[i]->write( os );
}
}
/**
* Function to write the Document node as an XML node into a file at the given path.
*
* @param path The path to the file to which the Document object should be written as an XML node.
*/
void Document::write(const std::string& path) const
{
if(!this->isRootSet)
throw XMLException("XML Document does not have a root");
std::ofstream file(path.c_str());
write(file);
file.close();
}
/**
* Recursive function called by the actual getElementById, that is exposed to the user.
*
* @param node A pointer to the current node in the recursion
* @param id The string being searched for in the attribute values.
* @return A pointer to the Node which has an attribute value that matches id
*/
Node* Document::getElementById (Node* node, const std::string& id) {
if (node != nullptr) {
vector<Attribute*> attributes = static_cast<ElementNode*>(node)->getAttributes();
for (int i = 0; i < attributes.size(); ++i) {
Attribute* attrib = attributes[i];
if (attrib->getValue() == id) {
return node;
}
}
vector<Node*> children = node->getChildren();
for (int i = 0; i < children.size(); ++i) {
if (dynamic_cast<ElementNode*>(children[i]) == NULL)
continue;
Node* temp = getElementById(children[i], id);
if(!temp)
return temp;
}
}
return nullptr;
}
/**
* Function to find the first node in the XML DOM object that has an attribute with the value provided as 'id'. This function
* in turn calls the private getElementById function that takes in a Node* and std::string as arguments.
*
* @param id The string being searched for inside attribute values.
* @return A pointer to an ElementNode which contains an attribute that has the value = id
*/
ElementNode* Document::getElementById (const std::string& id) {
return static_cast<ElementNode*>(getElementById(this->rootElement, id));
}
/**
* Private, recursive function that gets called by the getElementsByTagName function that is exposed to users. It does a DFS
*
* @param node The current node at this level of recursion.
* @param tagName The tag name being searched for.
* @param nodes A vector to hold the list of ElementNode that has a name matching 'tagName'
*/
void Document::getElementsByTagName (Node* node, const std::string& tagName, vector<ElementNode*>& nodes) {
if (node != nullptr) {
if (static_cast<ElementNode*>(node)->getName() == tagName) {
nodes.push_back(static_cast<ElementNode*>(node));
}
vector<Node*> children = node->getChildren();
for (int i = 0; i < children.size(); ++i) {
if (dynamic_cast<ElementNode*>(children[i]) == NULL)
continue;
getElementsByTagName(static_cast<ElementNode*>(children[i]), tagName, nodes);
}
}
return;
}
/**
* Function exposed to the user, used to get a vector of ElementNode whose name is same as the value passed to the function
*
* @param tagName The name being searched for, in all the element nodes.
* @return A vector of ElementNode* which points to all the element nodes that have a name equal to the value passed to the function
*/
vector<ElementNode*> Document::getElementsByTagName (const std::string& tagName) {
vector<ElementNode*> outputNodes;
getElementsByTagName (this->rootElement, tagName, outputNodes);
return outputNodes;
}
}