plistparser.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Own includes
  2. #include "plistparser.h"
  3. // Qt includes
  4. #include <QDomNode>
  5. #include <QDomDocument>
  6. #include <QDateTime>
  7. #include <QDebug>
  8. QVariant PListParser::parsePList(QIODevice *device) {
  9. QVariantMap result;
  10. QDomDocument doc;
  11. QString errorMessage;
  12. int errorLine;
  13. int errorColumn;
  14. bool success = doc.setContent(device, false, &errorMessage, &errorLine, &errorColumn);
  15. if (!success) {
  16. qDebug() << "PListParser Warning: Could not parse PList file!";
  17. qDebug() << "Error message: " << errorMessage;
  18. qDebug() << "Error line: " << errorLine;
  19. qDebug() << "Error column: " << errorColumn;
  20. return result;
  21. }
  22. QDomElement root = doc.documentElement();
  23. if (root.attribute(QStringLiteral("version"), QStringLiteral("1.0")) != QLatin1String("1.0")) {
  24. qDebug() << "PListParser Warning: plist is using an unknown format version, parsing might fail unexpectedly";
  25. }
  26. return parseElement(root.firstChild().toElement());
  27. }
  28. QVariant PListParser::parseElement(const QDomElement &e) {
  29. QString tagName = e.tagName();
  30. QVariant result;
  31. if (tagName == QLatin1String("dict")) {
  32. result = parseDictElement(e);
  33. }
  34. else if (tagName == QLatin1String("array")) {
  35. result = parseArrayElement(e);
  36. }
  37. else if (tagName == QLatin1String("string")) {
  38. result = e.text();
  39. }
  40. else if (tagName == QLatin1String("data")) {
  41. result = QByteArray::fromBase64(e.text().toUtf8());
  42. }
  43. else if (tagName == QLatin1String("integer")) {
  44. result = e.text().toInt();
  45. }
  46. else if (tagName == QLatin1String("real")) {
  47. result = e.text().toFloat();
  48. }
  49. else if (tagName == QLatin1String("true")) {
  50. result = true;
  51. }
  52. else if (tagName == QLatin1String("false")) {
  53. result = false;
  54. }
  55. else if (tagName == QLatin1String("date")) {
  56. result = QDateTime::fromString(e.text(), Qt::ISODate);
  57. }
  58. else {
  59. qDebug() << "PListParser Warning: Invalid tag found: " << e.tagName() << e.text();
  60. }
  61. return result;
  62. }
  63. QVariantList PListParser::parseArrayElement(const QDomElement& element) {
  64. QVariantList result;
  65. QDomNodeList children = element.childNodes();
  66. for (int i = 0; i < children.count(); i++) {
  67. QDomNode child = children.at(i);
  68. QDomElement e = child.toElement();
  69. if (!e.isNull()) {
  70. result.append(parseElement(e));
  71. }
  72. }
  73. return result;
  74. }
  75. QVariantMap PListParser::parseDictElement(const QDomElement& element) {
  76. QVariantMap result;
  77. QDomNodeList children = element.childNodes();
  78. QString currentKey;
  79. for (int i = 0; i < children.count(); i++) {
  80. QDomNode child = children.at(i);
  81. QDomElement e = child.toElement();
  82. if (!e.isNull()) {
  83. QString tagName = e.tagName();
  84. if (tagName == QLatin1String("key")) {
  85. currentKey = e.text();
  86. }
  87. else if (!currentKey.isEmpty()) {
  88. result[currentKey] = parseElement(e);
  89. }
  90. }
  91. }
  92. return result;
  93. }