-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLHandler.cpp
717 lines (622 loc) · 17.8 KB
/
XMLHandler.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
XML Viewer
Copyright 2017 David V. Makray
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// XMLHandler.cpp : implementation of the XMLNode, XMLHandler classes
//
#include "stdafx.h"
#include "XMLHandler.h"
XMLNode::XMLNode()
{
m_node_state = ns_empty;
m_node_text_or_tag_name.Empty();
m_nodes.SetSize(0, 2);
}
XMLNode::~XMLNode()
{
m_node_state = ns_empty;
m_node_text_or_tag_name.Empty();
XMLNode* temp;
for (int arrayloop = 0; arrayloop < m_nodes.GetSize(); arrayloop++)
{
temp = (XMLNode*)m_nodes[arrayloop];
if (temp != NULL)
{
delete temp;
temp = NULL;
}
}
}
XMLHandler::XMLHandler()
{
m_root_xml_node = new XMLNode();
//Set a value of a tag that can't be found for now..
m_root_xml_node->m_node_text_or_tag_name = "whole_doc";
m_node_history.SetSize(0, 2);
m_status = hs_none;
m_leftover_text.Empty();
}
XMLHandler::~XMLHandler()
{
delete m_root_xml_node;
m_node_history.RemoveAll();
}
void XMLHandler::add_xml_text(CString& in_text)
{
if (m_status != hs_adding)
{
if (m_status == hs_none)
{
m_node_history.Add((void*)m_root_xml_node);
m_status = hs_adding;
m_looking_for_end_arrow = false;
m_looking_for_end_arrow_to_terminating_tag = false;
}
else
{
//Searching or display may have to finish first.
return;
}
}
//Take the leftover text and put it back into the parser.
in_text = m_leftover_text + in_text;
if (!m_looking_for_end_arrow)
{
in_text+= " ";
}
m_leftover_text.Empty();
CString tag_name_pending_end_arrow;
for (int loop = 0; loop < in_text.GetLength(); loop++)
{
if (in_text[loop] == '<')
{
if (loop > 0)
{
TRACE("The text to take is: %s\n", in_text.Left(loop));
//Take the text.
add_to_text_node(in_text.Left(loop));
in_text = in_text.Right(in_text.GetLength() - loop);
loop = 0;
}
m_looking_for_end_arrow = true;
//Looks like the previous [if any] is an incomplete terminating tag.
m_looking_for_end_arrow_to_terminating_tag = false;
//Check to see if this is an ending tag..
ASSERT(in_text.GetLength() >= loop + 1);
if (in_text[loop + 1] == '/')
{
//Get the name of this tag.
m_potential_tag_name = find_tagname(in_text, loop + 2);
m_looking_for_end_arrow_to_terminating_tag = true;
}
else
{
m_potential_tag_name = find_tagname(in_text, loop + 1);
}
}
if (in_text[loop] == '>' && m_looking_for_end_arrow)
{
//Found a complete tag.. Clear out the tag text.
//Check to see if it terminates itself.. like "/>"
//If so don't bother adding it to history and "continue;".
if (loop > 0)
{
if (in_text[loop - 1] == '/')
{
//looking_for_end_arrow_to_terminating_tag = true;
XMLNode* self_terminate_node = new XMLNode();
self_terminate_node->m_node_text_or_tag_name = m_potential_tag_name;
self_terminate_node->m_node_state = ns_tag;
((XMLNode*)m_node_history[m_node_history.GetUpperBound()])->m_nodes.Add((void*)self_terminate_node);
m_looking_for_end_arrow = false;
m_looking_for_end_arrow_to_terminating_tag = false;
}
}
//Check to see if it finishes a terminating tag from bool."</"
if (m_looking_for_end_arrow_to_terminating_tag)
{
m_looking_for_end_arrow_to_terminating_tag = false;
//Look to see if this latest match is the latest in history.
if (((XMLNode*)m_node_history[m_node_history.GetUpperBound()])->m_node_text_or_tag_name == m_potential_tag_name)
{
//It also must be a tag type to remove. (Hint:not empty). If so remove it.
m_node_history.RemoveAt(m_node_history.GetUpperBound());
}
#ifdef _DEBUG
else
{
TRACE("Ending tag didn't match!\n");
}
#endif
}
else if (m_looking_for_end_arrow)
{
//Add it to the tree node.
XMLNode* temp_node = new XMLNode();
temp_node->m_node_text_or_tag_name = m_potential_tag_name;
temp_node->m_node_state = ns_tag;
//Put into the node on the current furthest branch.
((XMLNode*)m_node_history[m_node_history.GetUpperBound()])->m_nodes.Add((void*)temp_node);
//Plus add it to history.
m_node_history.Add((void*)temp_node);
}
in_text = in_text.Right(in_text.GetLength() - loop - 1);
m_looking_for_end_arrow = false;
loop = -1;
}
}
//Take care of leftover text
m_leftover_text += in_text;
in_text.Empty();
}
void XMLHandler::add_to_text_node(CString& text)
{
//Find out if the last node is a text node..
XMLNode* last_node = (XMLNode*)m_node_history[m_node_history.GetUpperBound()];
XMLNode* ptemp_node = NULL;
if (text == " ")
{
text.Empty();
return;
}
text.TrimLeft();
int white_space = 0;
for (int clip_loop = 0; clip_loop < text.GetLength(); clip_loop++)
{
if (text[clip_loop] == ' ')
{
white_space++;
}
else
{
if(white_space > 1)
{
text = text.Left(clip_loop - white_space + 1) + text.Right(text.GetLength() - clip_loop);
clip_loop -= white_space - 1;
}
white_space = 0;
}
}
text.TrimRight();
if (text.IsEmpty())
{
return;
}
text += " ";
//Rule: Last node is always a tag while adding
if (last_node->m_nodes.GetSize() > 0)
{
XMLNode* text_check = ((XMLNode*)(last_node->m_nodes[last_node->m_nodes.GetUpperBound()]));
if (text_check->m_node_state != ns_text)
{
//Create a new text node..
ptemp_node = new XMLNode();
ASSERT(ptemp_node);
last_node->m_nodes.Add((void*)ptemp_node);
}
else
{
ptemp_node = text_check;
}
}
//Check to see if there's no text node to append too.
if (last_node->m_nodes.GetSize() == 0)
{
ASSERT(last_node->m_node_state != ns_text);
//Create a new text node..
ptemp_node = new XMLNode();
ASSERT(ptemp_node);
last_node->m_nodes.Add((void*)ptemp_node);
}
//Set the text of this node...
//Removal of extra spaces etc too be taken care of here.
if (!ptemp_node->m_node_text_or_tag_name.IsEmpty())
{
ptemp_node->m_node_text_or_tag_name += " ";
}
ptemp_node->m_node_text_or_tag_name += text;
ptemp_node->m_node_state = ns_text;
}
CString XMLHandler::find_tagname(CString& in, int start_pos)
{
int name_check_loop;
for (name_check_loop = start_pos; name_check_loop < in.GetLength(); name_check_loop++)
{
//This could be a while instead.
if (in[name_check_loop] == 32 || in[name_check_loop] == 10 || in[name_check_loop] == 13 || in[name_check_loop] == '<' || in[name_check_loop] == '>' || in[name_check_loop] == '/')
{
//Looks like the name has been found..
break;
}
}
return(in.Mid(start_pos, name_check_loop - start_pos));
}
void XMLHandler::Display(bool start_is_true)
{
if (start_is_true)
{
if (m_status != hs_none)
{
m_node_history.RemoveAll();
}
m_status = hs_displaying;
m_node_history.Add((void*)m_root_xml_node);
}
else
{
m_node_history.RemoveAll();
m_status = hs_none;
}
}
DisplayInfo XMLHandler::DisplayNext()
{
DisplayInfo di;
di.m_change_indent = 0;
di.m_closing_tag = "";
//The m_node_history keeps track of this procedure.
//Look at the latest node in history..
XMLNode* latest_in_history = ((XMLNode*)m_node_history[m_node_history.GetUpperBound()]);
if (latest_in_history->m_node_state != ns_text)
{
//Does this node have sub-notes?
if (latest_in_history->m_nodes.GetSize() > 0)
{
//Display the first node.
XMLNode* node_to_display = ((XMLNode*)(latest_in_history->m_nodes[0]));
di.m_text = node_to_display->m_node_text_or_tag_name;
di.m_node_state = node_to_display->m_node_state;
if (m_node_history.GetSize() != 1)
{
di.m_change_indent++;
}
m_node_history.Add((void*)node_to_display);
return(di);
}
}
//Move on in the tree and find the next node to display.
XMLNode* checking_for_sub_nodes = latest_in_history;
while(checking_for_sub_nodes->m_node_state != ns_empty)
{
//Make sure the last one is saved:
latest_in_history = ((XMLNode*)m_node_history[m_node_history.GetUpperBound()]);
//Remove the last one from history..
int total_in_history = m_node_history.GetUpperBound();
m_node_history.RemoveAt(total_in_history);
if (latest_in_history->m_node_state == ns_tag)
{
di.m_closing_tag = latest_in_history->m_node_text_or_tag_name;
}
checking_for_sub_nodes = ((XMLNode*)m_node_history[total_in_history - 1]);
for (int search_loop = 0; search_loop < checking_for_sub_nodes->m_nodes.GetUpperBound(); search_loop++)
{
if (((XMLNode*)(checking_for_sub_nodes->m_nodes[search_loop])) == latest_in_history)
{
//The next one is returned.
XMLNode* node_to_display = ((XMLNode*)(checking_for_sub_nodes->m_nodes[search_loop + 1]));
di.m_text = node_to_display->m_node_text_or_tag_name;
di.m_node_state = node_to_display->m_node_state;
m_node_history.Add((void*)node_to_display);
return(di);
}
}
di.m_change_indent--;
}
di.m_text = "";
di.m_node_state = ns_empty;
return(di);
}
DisplayInfo XMLHandler::DisplayNextTextExport()
{
static bool travel_deeper = true;
/*Sending the closing tag becomes true when,
1. A tag has no children
2. A tag's contents has begun to be processed, being either a sub tag or a text node.
3. The following function call must send a closing tag.
It reads false when.
1. A sub tag needs to sent out.*/
DisplayInfo di;
di.m_change_indent = 0;
di.m_bclosing = false;
//The m_node_history is going to keep track of this whole proccess.
//Look at the latest node in history..
XMLNode* latest_in_history = ((XMLNode*)m_node_history[m_node_history.GetUpperBound()]);
XMLNode* current_worked_node = RetrieveNextNode(travel_deeper);
TRACE("%s, ", latest_in_history->m_node_text_or_tag_name);
TRACE("%s\n", current_worked_node->m_node_text_or_tag_name);
if (current_worked_node->m_node_state == ns_empty /*&& !travel_deeper*/)
{
//Done with
di.m_text == "";
di.m_node_state = ns_empty;
return(di);
}
//A tag was found...
if(current_worked_node->m_node_state != ns_text)
{
if (travel_deeper)
{//Start tag.
di.m_text = current_worked_node->m_node_text_or_tag_name;
di.m_node_state = current_worked_node->m_node_state;
//What if this were a self terminating tag?
//Found a tag, move in deeper, check otherwise.
di.m_text = "<" + current_worked_node->m_node_text_or_tag_name + ">";
m_node_history.Add((void*)current_worked_node);
return(di);
}
else
{//End tag.
di.m_text = "</" + current_worked_node->m_node_text_or_tag_name;
di.m_node_state = current_worked_node->m_node_state;
TRACE("Figure this one out.\n");
travel_deeper = false; //Rise or stay at same level.
//Now working only with an end tag..!!
}
}
if(current_worked_node->m_node_state == ns_text)
{
//Check if latest is the last text node with end tag needing to be sent next call.
//This text node may have siblings. Look just in case.
travel_deeper = true; //Stay at the same level or rise.
m_node_history.Add((void*)current_worked_node);
//Send the text.
XMLNode* next_call_node = RetrieveNextNode(true);
return(di);
}
di.m_text = "";
return(di);
}
XMLNode* XMLHandler::RetrieveNextNode(bool travel_deeper)
{
/*The retrieval of preceding nodes includes opening and closing tags.*/
//Travel deeper helps determine if the current node had been discovered, and shouldn't be re-traversed.
XMLNode* latest_in_history = ((XMLNode*)m_node_history[m_node_history.GetUpperBound()]);
if (latest_in_history->m_nodes.GetSize() > 0 && travel_deeper) //Does it have children?
{
return(((XMLNode*)(latest_in_history->m_nodes[0])));
}
ASSERT(m_node_history.GetSize() > 1);
XMLNode* parent_of_latest = ((XMLNode*)m_node_history[m_node_history.GetUpperBound() - 1]);
if (parent_of_latest->m_node_state == ns_empty)
{
return(parent_of_latest);
}
for (int search_loop = 0; search_loop < parent_of_latest->m_nodes.GetUpperBound(); search_loop++)
{
if (((XMLNode*)(parent_of_latest->m_nodes[search_loop])) == latest_in_history)
{
if (parent_of_latest->m_nodes.GetUpperBound() > search_loop)
{
return((XMLNode*)parent_of_latest->m_nodes[search_loop + 1]);
}
else
{
return(parent_of_latest);
}
}
}
return(NULL);
}
XMLNode* XMLHandler::RetrieveNextSiblingNode(bool isfirst)
{
XMLNode* current_node = ((XMLNode*)m_node_history[m_node_history.GetUpperBound()]);
XMLNode* parent_node = ((XMLNode*)m_node_history[m_node_history.GetUpperBound() - 1]);
if (isfirst) //Looking for the first sibling.
{
parent_node = current_node;
if (parent_node->m_nodes.GetSize() == 0)
{
return(NULL);
}
return((XMLNode*)parent_node->m_nodes[0]);
}
for (int search_loop = 0; search_loop < parent_node->m_nodes.GetUpperBound(); search_loop++)
{
if (((XMLNode*)(parent_node->m_nodes[search_loop])) == current_node) //If current is found.
{
if (parent_node->m_nodes.GetUpperBound() > search_loop)
{
return((XMLNode*)parent_node->m_nodes[search_loop + 1]);
}
else
{
break;
}
}
}
return(NULL);
}
//Search() counts the amount of hits on a tag name or text of a node.
int XMLHandler::Search(CString& search_text)
{
if (m_status != hs_none && m_status != hs_searching && m_status != hs_adding)
{
return(-1);
}
if (m_status == hs_none)
{
m_node_history.Add((void*)m_root_xml_node);
m_status = hs_searching;
}
int result = 0;
XMLNode* node_to_search = (XMLNode*)m_node_history[m_node_history.GetUpperBound()];
for (int search = 0; search < node_to_search->m_nodes.GetSize(); search++)
{
if (((XMLNode*)node_to_search->m_nodes[search])->m_node_text_or_tag_name == search_text)
{
result++;
}
}
return(result);
}
void XMLHandler::done_adding_text()
{
m_status = hs_none;
m_node_history.RemoveAll();
}
bool XMLHandler::enter_node(CString& node_name, int which)
{
//Done only in search mode.
if (m_status != hs_searching && m_status != hs_adding)
{
return(false);
}
if (Search(node_name) > which)
{
//Enter node here.
XMLNode* node_to_search = (XMLNode*)m_node_history[m_node_history.GetUpperBound()];
int count = 0;
for (int name_search = 0; name_search < node_to_search->m_nodes.GetSize(); name_search++)
{
if (((XMLNode*)node_to_search->m_nodes[name_search])->m_node_text_or_tag_name == node_name)
{
if (count == which)
{
m_node_history.Add((void*)node_to_search->m_nodes[name_search]);
break;
}
count++;
}
}
}
else
{
return(false);
}
return(true);
}
//exit_node() will back out from a node while in searching or adding mode.
void XMLHandler::exit_node()
{
if (m_status != hs_searching && m_status != hs_adding)
{
return;
}
if (m_node_history.GetUpperBound() > 0)
{
m_node_history.RemoveAt(m_node_history.GetUpperBound());
}
}
CString XMLHandler::get_node_info(CString& node_name)
{
if (Search(node_name))
{
if (enter_node(node_name, 0))
{
XMLNode* temp = (XMLNode*)m_node_history[m_node_history.GetUpperBound()];
if (temp->m_nodes.GetUpperBound() != -1)
{
exit_node();
//Will double check later for text node.
return(((XMLNode*)temp->m_nodes[0])->m_node_text_or_tag_name);
}
exit_node();
}
}
return(CString(""));
}
int XMLHandler::get_node_int(CString& node_name)
{
CString info = get_node_info(node_name);
char* to_convert = info.GetBuffer(0);
return(atoi(to_convert));
}
CTime XMLHandler::get_node_date(CString& node_name)
{
CString info = get_node_info(node_name);
TRACE("Node date: '%s' info found\n", info);
int month = atoi(info.GetBuffer(0));
if (month < 1 || month > 12 || info.GetLength() < 6)
{
return(CTime::GetCurrentTime()); //Bail on invalid data
}
int day, skip;
if (month < 10)
{
skip = 2;
}
else
{
skip = 3;
}
day = atoi(info.GetBuffer(0) + skip);
if (day < 1 || (day > 31 && day < 1900))
{
return(CTime::GetCurrentTime()); //Bail on invalid data
}
int year;
if (day > 1900)
{
year = day;
day = 1; //0 or assign it 1.
}
else
{
if (day < 10)
{
skip += 2;
}
else
{
skip += 3;
}
year = atoi(info.GetBuffer(0) + skip);
}
return(CTime(year, month, day, 0, 0, 0));
}
void XMLHandler::reset()
{
m_status = hs_none;
m_node_history.RemoveAll();
}
void XMLHandler::add_mode()
{
if (m_status == hs_none)
{
m_status = hs_adding;
m_node_history.RemoveAll();
m_node_history.Add((void*)m_root_xml_node);
}
}
void XMLHandler::add_tag(CString tag_name, bool enter_tag)
{
//Until a better method is possible.
if (m_status == hs_none)
{
m_node_history.Add((void*)m_root_xml_node);
m_status = hs_adding;
}
ASSERT(m_status == hs_adding);
if (m_status != hs_adding)
{
return;
}
XMLNode* new_tag = new XMLNode();
new_tag->m_node_text_or_tag_name = tag_name;
new_tag->m_node_state = ns_tag;
ASSERT(m_node_history.GetUpperBound() >= 0);
((XMLNode*)m_node_history[m_node_history.GetUpperBound()])->m_nodes.Add((void*)new_tag);
if (enter_tag)
{
m_node_history.Add((void*)new_tag);
}
}