fb2xml.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "fb2xml.hpp"
  2. #include <QtDebug>
  3. //---------------------------------------------------------------------------
  4. // FbXmlHandler::NodeHandler
  5. //---------------------------------------------------------------------------
  6. QString FbXmlHandler::NodeHandler::Value(const QXmlAttributes &attributes, const QString &name)
  7. {
  8. int count = attributes.count();
  9. for (int i = 0; i < count; i++ ) {
  10. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  11. return attributes.value(i);
  12. }
  13. }
  14. return QString();
  15. }
  16. bool FbXmlHandler::NodeHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  17. {
  18. if (m_handler) return m_handler->doStart(name, attributes);
  19. m_handler = NewTag(name, attributes);
  20. if (m_handler) {
  21. if (m_handler->m_closed) {
  22. delete m_handler;
  23. m_handler = NULL;
  24. }
  25. return true;
  26. }
  27. m_handler = new NodeHandler(name);
  28. return true;
  29. }
  30. bool FbXmlHandler::NodeHandler::doText(const QString &text)
  31. {
  32. if (m_handler) m_handler->doText(text); else TxtTag(text);
  33. return true;
  34. }
  35. bool FbXmlHandler::NodeHandler::doEnd(const QString &name, bool & exists)
  36. {
  37. if (m_handler) {
  38. bool found = exists || name == m_name;
  39. m_handler->doEnd(name, found);
  40. if (m_handler->m_closed) { delete m_handler; m_handler = NULL; }
  41. if (found) { exists = true; return true; }
  42. }
  43. bool found = name == m_name;
  44. m_closed = found || exists;
  45. if (m_closed) EndTag(m_name);
  46. exists = found;
  47. return true;
  48. }
  49. //---------------------------------------------------------------------------
  50. // FbXmlHandler
  51. //---------------------------------------------------------------------------
  52. FbXmlHandler::FbXmlHandler()
  53. : QXmlDefaultHandler()
  54. , m_handler(0)
  55. {
  56. }
  57. FbXmlHandler::~FbXmlHandler()
  58. {
  59. if (m_handler) delete m_handler;
  60. }
  61. bool FbXmlHandler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  62. {
  63. Q_UNUSED(namespaceURI);
  64. Q_UNUSED(localName);
  65. const QString name = qName.toLower();
  66. if (m_handler) return m_handler->doStart(name, attributes);
  67. m_handler = CreateRoot(name, attributes);
  68. return m_handler;
  69. }
  70. bool FbXmlHandler::isWhiteSpace(const QString &str)
  71. {
  72. return str.simplified().isEmpty();
  73. }
  74. bool FbXmlHandler::characters(const QString &str)
  75. {
  76. QString s = str.simplified();
  77. if (s.isEmpty()) return true;
  78. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  79. if (isWhiteSpace(str.right(1))) s.append(" ");
  80. return m_handler && m_handler->doText(s);
  81. }
  82. bool FbXmlHandler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  83. {
  84. Q_UNUSED(namespaceURI);
  85. Q_UNUSED(localName);
  86. bool found = false;
  87. return m_handler && m_handler->doEnd(qName.toLower(), found);
  88. }
  89. bool FbXmlHandler::warning(const QXmlParseException& exception)
  90. {
  91. emit log(FbMessage(exception, FbMessage::Warring));
  92. return true;
  93. }
  94. bool FbXmlHandler::error(const QXmlParseException& exception)
  95. {
  96. emit log(FbMessage(exception, FbMessage::Error));
  97. return false;
  98. }
  99. bool FbXmlHandler::fatalError(const QXmlParseException &exception)
  100. {
  101. emit log(FbMessage(exception, FbMessage::Fatal));
  102. return false;
  103. }
  104. QString FbXmlHandler::errorString() const
  105. {
  106. return m_error;
  107. }