aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod/src/xmlreader.cpp
blob: 7f537108ba30adfc1685734ab10f5c5f638e668b (plain)
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
#include "xmlreader.h"

#include <QDebug>

#include <uibase/utility.h>

using MOBase::Exception;

bool XmlReader::getNextElement(QString const& start)
{
  while (!atEnd()) {
    switch (readNext()) {
    case EndElement:
      if (name() != start) {
        qWarning() << "Got end of " << name() << ", expected " << start << " at "
                   << lineNumber();
        continue;
      }
      return false;

    case StartElement:
      return true;

    case Invalid:
      throw Exception("bad xml");

    default:
      qWarning() << "Unexpected token type " << tokenString() << " at " << lineNumber();
    }
  }
  return false;
}

void XmlReader::unexpected()
{
  qWarning() << "Unexpected element " << name() << " near line " << lineNumber();
  // Eat the contents
  QString s = readElementText(IncludeChildElements);
  // Print them out if in debugging mode
  qDebug() << " contains " << s;
}

void XmlReader::finishedElement()
{
  QString const self = name().toString();
  while (!atEnd()) {
    switch (readNext()) {
    case EndElement:
      if (name() != self) {
        qWarning() << "Got end element for " << name() << ", expected " << self
                   << " at " << lineNumber();
        continue;
      }
      return;

    case Invalid:
      throw Exception("bad xml");
      return;

    case StartElement:
      unexpected();
      break;

    default:
      qWarning() << "Unexpected token type " << tokenString() << " at " << lineNumber();
    }
  }
}

QString XmlReader::getText()
{
  // This reads the text in an element, leaving you at the next element.
  QString result;
  while (QXmlStreamReader::readNext() == Comment || tokenType() == Characters) {
    if (tokenType() == Characters) {
      result += text();
    }
  }
  if (tokenType() != EndElement) {
    qWarning() << "Unexpected token type " << tokenString() << " at " << lineNumber();
  }
  return result;
}