configxmlparser.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <QDir>
  2. #include <QDomDocument>
  3. #include <QDomNodeList>
  4. #include <QApplication>
  5. #include "configxmlparser.h"
  6. #include "defines.h"
  7. #include "generatordata.h"
  8. ConfigXmlParser::ConfigXmlParser(GeneratorOutputView &outputView, GeneratorData &generatorData) :
  9. m_outputView(outputView), m_generatorData(generatorData)
  10. {
  11. }
  12. void ConfigXmlParser::handleNode(const QDomElement &element, const QString &keyPrefix)
  13. {
  14. if(element.tagName().isEmpty())
  15. return;
  16. QMap<QString, QString> &configXmlData = m_generatorData.configXmlData();
  17. QString key = keyPrefix.isEmpty() ? element.tagName() : keyPrefix+"."+element.tagName();
  18. for(int i = 0; i < element.attributes().count(); i++) {
  19. QDomNode attrNode = element.attributes().item(i);
  20. configXmlData.insert(key + ":" + attrNode.toAttr().name(), attrNode.toAttr().value());
  21. }
  22. if(element.tagName() != W3C_ROOT_TAG && !element.text().isEmpty())
  23. configXmlData.insert(key + ":", element.text().trimmed());
  24. }
  25. void ConfigXmlParser::handleChildNodes(const QDomElement &element, const QString &keyPrefix)
  26. {
  27. QDomNodeList childNodes = element.childNodes();
  28. for(int i = 0; i < childNodes.count(); i++) {
  29. handleNode(childNodes.at(i).toElement(), keyPrefix);
  30. if(childNodes.at(i).hasChildNodes()) {
  31. handleChildNodes(childNodes.at(i).toElement(), keyPrefix+"."+childNodes.at(i).toElement().tagName());
  32. }
  33. }
  34. }
  35. bool ConfigXmlParser::parse()
  36. {
  37. QString widgetDirectory = m_generatorData.widgetDirectory();
  38. QDir dir(widgetDirectory);
  39. // read in config.xml
  40. QFile configXmlFile(dir.filePath(W3C_CONFIG_FILE));
  41. if(!configXmlFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
  42. QString errMsg = QString(ERR_FILE_OPEN_READ_FAILED_CANNOT_CONTINUE).arg(W3C_CONFIG_FILE);
  43. m_outputView.printOutput(errMsg);
  44. return false;
  45. }
  46. QDomDocument configXmlDocument;
  47. if(!configXmlDocument.setContent(&configXmlFile)) {
  48. QString errMsg = QString(ERR_FILE_PARSING_FAILED_CANNOT_CONTINUE).arg(W3C_CONFIG_FILE);
  49. m_outputView.printOutput(errMsg);
  50. configXmlFile.close();
  51. return false;
  52. }
  53. QDomElement element = configXmlDocument.firstChildElement(W3C_ROOT_TAG);
  54. if(element.isNull()) {
  55. QString errMsg = QString(ERR_WIDGET_TAG_NOT_FOUND_CANNOT_CONTINUE);
  56. m_outputView.printOutput(errMsg);
  57. return false;
  58. }
  59. handleNode(element, "");
  60. handleChildNodes(element, W3C_ROOT_TAG);
  61. /*
  62. QMap<QString, QString> &configXmlData = m_generatorData.configXmlData();
  63. foreach(const QString &key, configXmlData.keys())
  64. m_outputView.printOutput(key + ": " + configXmlData.find(key).value());
  65. */
  66. return true;
  67. }