-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstackXML.cpp
368 lines (305 loc) · 13.7 KB
/
stackXML.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
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
#include "stackXML.h"
tT Node<T>::Node() {}
tT Node<T>::Node(const T val)
{
this->data=val;
}
tT Stack<T>::Stack()
{
top=nullptr;
}
tT bool Stack<T>::IsEmpty()
{
if(!top)
return true;
return false;
}
tT bool Stack<T>::pop(T &val)
{
if(!IsEmpty())
{
val=top->data;
Node<T>* aux = top;
top=top->next;
aux->next=nullptr;
aux=nullptr;
return true;
}
return false;
}
tT bool Stack<T>::push(const T &val)
{
Node<T>* aux=new Node<T>(val);
aux->next=top;
top=aux;
return true;
}
tT void Stack<T>::print()
{
Node<T>* aux=top;
while(aux!=nullptr)
{
cout<<aux->data<<"\t";
if(aux->next==nullptr)
break;
aux=aux->next;
}
}
tT T Stack<T>::Top()
{
return top->data;
}
tT Stack<T>::~Stack()
{
}
XMLData::XMLData()
{
lineNumber=0;
StartOrEnd=0;
}
void XMLData::printXML()
{
cout<<"Tag name = "<<tagText<<", Line Number = "<<lineNumber<<", ";
if(!StartOrEnd)
cout<<"Starting tag\n";
else
cout<<"Ending tag\n";
}
void checkXML(string filename)
{
Stack<XMLData> St;
int lineCounter = 1;
bool foundError = false;
bool attributeError = false;
ifstream fin;
fin.open(filename);
while(!fin.eof())
{
XMLData xml;
string line;
getline(fin, line, '\n');//reads whole line so that line number can be tracked
int CurrentLineLength=line.length();
while( CurrentLineLength>0 )
{
int OpeningAngularBracketIndex = line.find('<');
int ClosingAngularBracketIndex = line.find('>');
if(OpeningAngularBracketIndex == -1 && ClosingAngularBracketIndex == -1)//if true, it means the current line contain no tag
CurrentLineLength = 0;
//NOTE: XML prolog is optional. If it exists, it must come first in the document.
if(line[OpeningAngularBracketIndex+1] == '?')//if true, it's a prolog
{
if(lineCounter==1)
{
line = line.substr(OpeningAngularBracketIndex+2, line.size());//ignore s[0]='<' and s[1]='?'
CurrentLineLength=line.length();
int endOfProlog = line.find("?>");
if(endOfProlog==-1)//if false, it means prolog is complete
{
cout<<"---- ERROR ---- \tThe prolog is not complete.\n";
foundError = true;
break;
}
else//if prolog is complete, then check if there is another tag
{
int secondAngularBracketIndex = line.find('<');
if (secondAngularBracketIndex==-1)//if there is no other tag besides prolog, then clear the current line
{
CurrentLineLength=0;
}
else//else, delete the contents of line before another tag (i.e., tag after prolog in the current line)
{
line = line.substr(secondAngularBracketIndex, line.size());
CurrentLineLength=line.length();
}
}
}
else//if prolog is not in the first line; show error
{
cout<<"---- ERROR ---- \tProlog must be at the start of xml file.\n";
foundError = true;
break;
}
}
//check if there's a proper end of prolog
else if( (OpeningAngularBracketIndex==-1 || OpeningAngularBracketIndex>ClosingAngularBracketIndex) && line[ClosingAngularBracketIndex-1]=='?' )
{
if(lineCounter==1)
{
cout<<"---- ERROR ---- \tThere's no start of prolog but its end exists.\n";
foundError = true;
break;
}
else
{
cout<<"---- ERROR ---- \tThere's no start of prolog but its end exists. It's at line "<<lineCounter<<" (prolog must be at line 1)\n";
foundError = true;
break;
}
}
//check if it's end of a comment
else if( (line[ClosingAngularBracketIndex-1] == '-') && (line[ClosingAngularBracketIndex-2] == '-') && (line.find("-->")==line.find("--")) )
{
xml=St.Top();
if(xml.tagText == "Comment")
{
St.pop(xml);
//cout<<"Popped: "; xml.printXML();
}
else
{
cout<<"---- ERROR ---- \tfound closing tag of Comment at line "<<lineCounter<<", but there's no starting tag for it.";
foundError = true;
break;
}
line = line.substr(ClosingAngularBracketIndex+1, line.size());
CurrentLineLength=line.length();
}
//check if it's start of a simple tag with or without attribute.
else if( (line[OpeningAngularBracketIndex]=='<') && ((line[OpeningAngularBracketIndex+1] >= 65 && line[OpeningAngularBracketIndex+1] <= 90) || (line[OpeningAngularBracketIndex+1] >=97 && line[OpeningAngularBracketIndex+1] <= 122)) /*&& (OpeningAngularBracketIndex < ClosingAngularBracketIndex)*/ )
{
xml.lineNumber=lineCounter;
xml.StartOrEnd=0;
string Tag;
int FirstSpaceAfterTag = line.find(' ');
//check if there's an attribute
if(FirstSpaceAfterTag != -1 && (FirstSpaceAfterTag < ClosingAngularBracketIndex || ClosingAngularBracketIndex==-1))
{
Tag = line.substr(1, FirstSpaceAfterTag-1);
line = line.substr(FirstSpaceAfterTag+1, line.size());//delete tag name from current line
int firstQuoteS = line.find("'");//find first single quote
int firstQuoteD = line.find('"');//find first double quote
//if first "double quote" exists and there's a charachter after that double quote (i.e., name of attribute)
if(firstQuoteD<firstQuoteS && firstQuoteD!=-1 && ((line[firstQuoteD+1] >= 65 && line[firstQuoteD+1] <= 90) || (line[firstQuoteD+1] >=97 && line[firstQuoteD+1] <= 122)))
{
line=line.substr(firstQuoteD+1, line.size());//delete line from start upto first double quote(icluding)
int secondQuoteD = line.find('"');//find second double quote
ClosingAngularBracketIndex = line.find('>');//update index of '>'
//checking if current tag is properly closed; both when there's only one tag or multiple tags in a single line.
if( (ClosingAngularBracketIndex==-1 || (secondQuoteD!=-1 && secondQuoteD<ClosingAngularBracketIndex)) && ( (line.find("\">")==-1) || (line.find("\">")!=-1 && secondQuoteD<line.find("\">")) ) )
{
cout<<"---- ERROR ---- \tNo proper closing of tag: <"<<Tag<<"> at line "<<lineCounter;
foundError = true;
break;
}
//checking if current attribute is quoted; both when there's only one attribute or multiple attributes in a single line.
else if(ClosingAngularBracketIndex!=-1 && (secondQuoteD>ClosingAngularBracketIndex || secondQuoteD==-1))
{
cout<<"---- ERROR ---- \tThe attribute is not properly quoted at line "<<lineCounter;
attributeError = true;
break;
}
}
//if first 'single quote' exists and there's a charachter after that single quote (i.e., name of attribute)
else if(firstQuoteS!=-1 && ((line[firstQuoteS+1] >= 65 && line[firstQuoteS+1] <= 90) || (line[firstQuoteS+1] >=97 && line[firstQuoteS+1] <= 122)))
{
line=line.substr(firstQuoteS+1, line.size());//delete line from start upto first single quote(icluding)
int secondQuoteS = line.find("'");//find second 'single quote'
ClosingAngularBracketIndex = line.find('>');//update index of '>'
//checking if current tag is properly closed; both when there's only one tag or multiple tags in a single line.
if( (ClosingAngularBracketIndex==-1 || (secondQuoteS!=-1 && secondQuoteS<ClosingAngularBracketIndex)) && ( (line.find("'>")==-1) || (line.find("'>")!=-1 && secondQuoteS<line.find("'>")) ) )
{
cout<<"---- ERROR ---- \tNo proper closing of tag: <"<<Tag<<"> at line "<<lineCounter;
foundError = true;
break;
}
//checking if current attribute is quoted; both when there's only one attribute or multiple attributes in a single line.
else if(ClosingAngularBracketIndex!=-1 && (secondQuoteS>ClosingAngularBracketIndex || secondQuoteS==-1))
{
cout<<"---- ERROR ---- \tThe attribute is not properly quoted at line "<<lineCounter;
attributeError = true;
break;
}
}
}
else//if there's no attribute
Tag = line.substr(1, ClosingAngularBracketIndex-1);
xml.tagText=Tag;
St.push(xml);
//cout<<"Pushed: "; xml.printXML();
ClosingAngularBracketIndex=line.find('>');
line = line.substr( ClosingAngularBracketIndex+1, line.size());
CurrentLineLength=line.length();
}
//icheck if it's end of a simple tag with or without attribute.
else if( (line[OpeningAngularBracketIndex]=='<') && (line[OpeningAngularBracketIndex+1]=='/') && ((line[OpeningAngularBracketIndex+2] >= 65 && line[OpeningAngularBracketIndex+2] <= 90) || (line[OpeningAngularBracketIndex+2] >=97 && line[OpeningAngularBracketIndex+2] <= 122)) )
{
line = line.substr(OpeningAngularBracketIndex+2, line.size());
int TagLength = line.find('>');
string Tag = line.substr(0, TagLength);
if(!St.IsEmpty())//check if there's a starting tag against incoming ending tag
xml = St.Top();
else
{
cout<<"---- ERROR ---- \tfound closing tag: </"<<Tag<<"> at line "<<lineCounter<<", but there's no starting tag for it.";
foundError = true;
break;
}
int x=xml.tagText.compare(Tag);
if(x==0)
{
St.pop(xml);
//cout<<"Popped: "; xml.printXML();
}
else
{
cout<<"---- ERROR ---- \tThere MUST be a closing tag: </"<<xml.tagText<<"> before encountered closing tag i.e., </"<<Tag<<"> at line "<<lineCounter;
foundError = true;
break;
}
line = line.substr(TagLength+1, line.size());
CurrentLineLength=line.length();
}
//check if it's start of a comment
else if(line[OpeningAngularBracketIndex+1] == '!' && line[OpeningAngularBracketIndex+2] == '-' && line[OpeningAngularBracketIndex+3] == '-')
{
line = line.substr(OpeningAngularBracketIndex+4, line.size());// <!-- these 4 charachters are deleted from string
CurrentLineLength=line.length();
xml.lineNumber=lineCounter;
xml.StartOrEnd=0;
xml.tagText="Comment";
St.push(xml);
//cout<<"Pushed: "; xml.printXML();
}
//check if there are double dashes in the comment
else if( line.find("--") != line.find("-->") )
{
if(OpeningAngularBracketIndex==-1 && ClosingAngularBracketIndex==-1)
{
CurrentLineLength=0;
}
else
{
cout<<"---- ERROR ---- \tAt line "<<lineCounter<<", Two dashes in the middle of a comment are not allowed.";
foundError = true;
break;
}
}
}
if(foundError || attributeError)
break;
lineCounter++;
}
//show error if stack is not empty
if(!St.IsEmpty() && !attributeError && !foundError)
{
cout<<"---- ERROR ---- \tFound ";
if(St.Top().StartOrEnd==0)
cout<<"Starting";
else
cout<<"Ending";
cout<<" tag \""<<St.Top().tagText<<"\" at line "<<St.Top().lineNumber<<" but it has no ";
if(St.Top().StartOrEnd==0)
cout<<"Ending";
else
cout<<"Starting";
cout<<" tag\n";
}
else if(!foundError && !attributeError)
cout<<"No Error found in this xml file.\n";
fin.close();
}
int main()
{
checkXML("xml.txt");
return 0;
}