root/src/XmlReader.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
////////////////////////////////////////////////////////////////////////
// Copyright (c) Nehmulos 2011-2014
// This file is part of N0 Strain Serialization Library.
//
// N0Strain-Serialization-Library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// N0Strain-Serialization-Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with N0Strain-Serialization-Library.  If not, see https://gnu.org/licenses/lgpl-3.0
////////////////////////////////////////////////////////////////////////




#include "XmlReader.h"

namespace nw {

XmlReader::XmlReader(const String filepath)
{
        MarkupReader::init(filepath);
}

XmlReader::XmlReader(std::istream& input)
{
    MarkupReader::init();
    readWholeDocument(input);
}

XmlReader::~XmlReader() {

}

void XmlReader::describeValueArray(const String arrayName, unsigned int size)
{
        describeArray(arrayName, "value", size);
        tagArrays.top().isValueArray = true;
}

int XmlReader::readWholeDocument(std::istream& input)
{
        int error = parseXml(input);
        parentTag = motherTag;

        if(error != 0)
        {
                errorMessage << "It fucked up!\n";
                switch (error) {
                        case 1:
                                errorMessage << "The file closed unexpectedly!\n";
                                errorMessage << "Seems like it's to short, broken, or just bad Xml\n";
                                break;
                        case 2:
                                errorMessage << "A CloseTag doesn't match!\n";//TODO say which close tag
                                errorMessage << "In a later " N0Slib_Name " version we will tell you which CloseTag\n";
                                errorMessage << "So, for now, please use a XmlValidator and keep looking for newer " N0Slib_Name " versions\n";
                                break;
                        default:
                                errorMessage << "The reason is unknown the Error code is " << error << '\n';
                                errorMessage << "Please Contact the " N0Slib_Name " developer \n";
                                errorMessage << "and report a bug sending an example Xml and your " N0Slib_Name " version\n";
                                break;
                }
        }
        return error;
}


//nearly 500 lines for 1 function ... 
//TODO some validation would be fine
int XmlReader::parseXml(std::istream&   file)
{
        char c;
        PARSER_STATE state = searchNewTags;
        std::stringstream tagname, value;
        Tag* currentTag = NULL;
        Tag* definitions = NULL;
        char quoteType = '"';
        char lastCharOfComment;
        unsigned int currentLine = 1;
        while(!file.eof())
        {
                file.get(c);

                if(c == '\n')
                {
                        currentLine++; //linecounter
                }

                if(c == '\r')
                {
                        if(state != searchNewTags && state != readAttributeValue)
                                continue; // skip this iteration and jump to the next char
            //else
                //errorMessage << "A caged \r appeared!\n";
                }

                switch (state)
                {
                        case searchNewTags:
                        {
                                //<tag>value</tag>
                                if(c == '<')
                                {
                                        state = readTagType;
                                }
                                else
                                {
                                        //TODO add boolean to filter newLines and tabs from Strings
                                        //Don't write a seperator, if the last char was a seperator
                                        //if(!this->trimTabsAndNewlines || (c == ' ' || !nwIsSeparator(c)))
                                                value << c;
                                }
                        }
                        break;
                        case readTagType:
                        {
                                //</closeTag>
                                if(c == '/')
                                {
                                        if(value.str().size() != 0)
                                        {
                                                if(currentTag != NULL)
                                                        currentTag->setValue(value.str());
                                                value.str("");
                                        }
                                        state = closeTag;
                                }
                                //<!-- comment -->
                                else if(c == '!')
                                {
                                        state = readCommentType;
                                }
                                //<?xml version="1.0"?>
                                else if(c == '?')
                                {
                                        state = readXmlVersion;
                                }
                                //<normalTag...
                                else if(!nwIsSeparator(c))
                                {
                                        //<hi>text<bye> other tag opens while the first one wasn't closed
                                        if(value.str().size() != 0)
                                        {
                                                //does the value only contain separator chars?
                                                String v = value.str();
                                                bool containsCharacterData;
                                                for (unsigned int i = 0; i < v.size(); ++i)
                                                {
                                                        if(!nwIsSeparator(v.at(i)))
                                                        {
                                                                containsCharacterData = true;
                                                                //Print Warning
                                                                String parentname;
                                                                String childname;
                                                                if(currentTag != NULL)
                                                                {
                                                                        currentTag->setValue(v);

                                                                        childname = currentTag->getName();
                                                                        if(currentTag->getParent() != NULL)
                                                                                parentname = currentTag->getParent()->getName();
                                                                }
                                                                errorMessage << "Warning: a new Tag opens in line " << currentLine
                                                                                << " directly after \"" << v << "\"\n";
                                                                errorMessage << "Warning! the tag <" << childname << "> in <" <<
                                                                                parentname << "> does not close properly\n";
                                                                errorMessage << "This file is maybe unreadable from here!\n";
                                                                break;
                                                        }
                                                }
                                                if(!containsCharacterData)
                                                {
                                                        value.str("");
                                                }
                                        }
                                        //no errors opening tag
                                        state = readTagName;
                                        tagname << c;
                                }
                        }
                        break;
                        case readTagName:
                        {
                                //<tag ...attributes
                                if(nwIsSeparator(c))
                                {
                                        //is first tag in file
                                        if(currentTag == NULL)
                                        {
                                                currentTag = new Tag(tagname.str());
                                        }
                                        else
                                        {
                                                currentTag->addNewChild(tagname.str());
                                                currentTag = currentTag->getLastChild();
                                        }
                                        tagname.str("");
                                        state = searchAttributes;
                                }
                                //<statement/>
                                else if(c == '/')
                                {
                                        state = closeTag;
                                }
                                //<nor...
                                else if(c != '>')
                                {
                                        tagname << c;
                                }
                                //<normalTag>
                                else
                                {
                                        //is first tag in file
                                        if(currentTag == NULL)
                                        {
                                                currentTag = new Tag(tagname.str());
                                        }
                                        else
                                        {
                                                currentTag->addNewChild(tagname.str());
                                                currentTag = currentTag->getLastChild();
                                        }
                                        tagname.str("");
                                        state = searchNewTags;
                                }
                        }
                        break;
                        case closeTag :
                        {
                                if(c == '>')
                                {
                                        state = searchNewTags;
                                        if(currentTag != NULL && currentTag->getParent() != NULL)
                                        {
                                                currentTag = currentTag->getParent();
                                        }
                                }
                        }
                        break;
                        //Read attributes as Tags, Xml style 2.0 baby!
                        case searchAttributes:
                        {
                                if(c == '/')
                                {
                                        state = closeTag;
                                }
                                else if(c == '>')
                                {
                                        state = searchNewTags;
                                }
                                else if(!nwIsSeparator(c))
                                {
                                        tagname << c;
                                        state = readAttributeName;
                                }
                        }
                        break;
                        case readAttributeName:
                        {
                                if(nwIsSeparator(c) || c == '=')
                                {
                                        //is first tag in file
                                        if(currentTag != NULL)
                                        {
                                                currentTag->addNewChild(tagname.str());
                                                currentTag = currentTag->getLastChild();
                                                //Don't enter this tag, as it can not have derived child
                                        }
                                        tagname.str("");
                                        state = assignAttribute;
                                }
                                else
                                {
                                        tagname << c;
                                }
                        }
                        break;
                        //TODO support CDATA in attributes
                        case assignAttribute:
                        {
                                if(c == '"' || c == '\'')
                                {
                                        state = readAttributeValue;
                                        quoteType = c;
                                }
                        }
                        break;
                        case readAttributeValue:
                        {
                                if(c == quoteType)
                                {
                                        currentTag->setValue(value.str());
                                        value.str("");
                                        //Enter the attribute-holding-tag
                                        currentTag = currentTag->getParent();
                                        state = searchAttributes;
                                }
                                else
                                {
                                        value << c;
                                }
                        }
                        break;
                        case readCommentType:
                        {
                                //<![CDATA[<br> and other content]]>
                                if(c == '[')
                                {
                                        state = cDataTagName;
                                }
                                //TODO read the second -, too.
                                //<!-- ...
                                else if (c == '-')
                                {
                                        state = xmlComment;
                                }
                                //<!DEFINITION...
                                else
                                {
                                        state = definitionTagName;
                                }
                        }
                        break;
                        case cDataTagName:
                        {
                                String cdataTagName = "CDATA[";
                                //We alread have the '['
                                bool failed = false;
                                for (int i = 0; i < 6; ++i)
                                {
                                        if(!file.eof()) {
                                                if(c != cdataTagName.at(i))
                                                {
                                                        state = xmlComment;
                                                        failed = true;
                                                        break;
                                                }
                                                if(i < 5) //prevent from fetching the first char after '['
                                                        file.get(c);
                                        }
                                }
                                if(!failed)
                                {
                                        state = cDataValue;
                                }
                                else
                                {
                                        // consider it as different type definition
                                        state = definitionTagName;
                                }
                        }
                        break;
                        case cDataValue:
                        {
                                //TODO horrible if else, close your eyes or turn to stone
                                if(c == ']')
                                {
                                        if(!file.eof())
                                        {
                                                file.get(c);
                                                if(c == ']')
                                                {
                                                        if(!file.eof())
                                                        {
                                                                file.get(c);
                                                                if(c == '>')
                                                                {
                                                                        state = searchNewTags;
                                                                }
                                                                else
                                                                {
                                                                        value << "]]" << c;
                                                                }
                                                        }
                                                }
                                                else
                                                {
                                                        value << ']' << c;
                                                }
                                        }
                                }
                                else
                                {
                                        value << c; //TODO don't add the last ]
                                }
                                lastCharOfComment = c;
                        }
                        break;
                        case xmlComment:
                        {
                                if(lastCharOfComment == '-' && c == '-')
                                {
                                        if(!file.eof())
                                        {
                                                file >> c;
                                                if(c == '>')
                                                {
                                                        state = searchNewTags;
                                                }
                                        }
                                }
                                lastCharOfComment = c;
                        }
                        break;
                        case readXmlVersion:
                        {
                                //TODO 3.0 may use the version somehow
                                if(quoteType == 'x' && (c == '\"' || c == '\''))
                                {
                                        quoteType = c;
                                }
                                else if(quoteType != 'x' && c == quoteType)
                                {
                                        quoteType = 'x';
                                }
                                //<?xml?>
                                else if(c == '>')
                                {
                                        state = searchNewTags;
                                }
                        }
                        break;
                        case definitionTagName:
                        {
                                if(nwIsSeparator(c))
                                {
                                        if(definitions == NULL)
                                        {
                                                definitions = new Tag(tagname.str());
                                        }
                                        else
                                        {
                                                definitions->addNewChild(tagname.str());
                                                definitions = definitions->getLastChild();
                                        }
                                        state = definitionValue;
                                        tagname.str("");
                                }
                                else
                                {
                                        tagname << c;
                                }
                        }
                        break;
                        case definitionValue:
                        {
                                //TODO call some parse doctype function
                                if(quoteType == 'x' && (c == '\"' || c == '\''))
                                {
                                        quoteType = c;
                                }
                                else if(quoteType != 'x' && c == quoteType)
                                {
                                        quoteType = 'x';
                                }
                                //<!DOCTYPE SOME [ <!THAT OTHER THING> ] VALUE>
                                else if(c == '<')
                                {
                                        definitions->setValue(value.str());
                                        value.str("");
                                        state = definitionTagName;
                                }
                                else if(c == '>')
                                {
                                        definitions->setValue(value.str());
                                        value.str("");
                                        if(definitions->getParent() == NULL)
                                        {
                                                state = searchNewTags;
                                        }
                                        else
                                        {
                                                definitions = definitions->getParent();
                                        }
                                }
                                else
                                {
                                        value << c;
                                }
                        }
                        break;
                        default:
                        break;
                }
        }
        //End of file
        if(state != searchNewTags)
        {
                errorMessage << "Warning! File ended unexpectedly\n";
        }
        if(currentTag != NULL)
        {
                while(currentTag->getParent() != NULL)
                {
                        currentTag = currentTag->getParent();
                }
        }
        if(motherTag != NULL)
        {
                delete motherTag;
        }
        motherTag = currentTag;

        //TODO do something with the doc type definitions
        delete definitions;
        return 0;
}

}  // namespace nw