fb2xml.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef FB2XML_H
  2. #define FB2XML_H
  3. #include <QHash>
  4. #include <QXmlDefaultHandler>
  5. #include <QXmlStreamReader>
  6. #include <QXmlStreamWriter>
  7. #include "fb2logs.hpp"
  8. #define FB2_BEGIN_KEYLIST private: enum Keyword {
  9. #define FB2_END_KEYLIST None }; \
  10. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); }; \
  11. static Keyword toKeyword(const QString &name); private:
  12. #define FB2_BEGIN_KEYHASH(x) \
  13. x::Keyword x::toKeyword(const QString &name) \
  14. { \
  15. static const KeywordHash map; \
  16. KeywordHash::const_iterator i = map.find(name); \
  17. return i == map.end() ? None : i.value(); \
  18. } \
  19. x::KeywordHash::KeywordHash() {
  20. #define FB2_END_KEYHASH }
  21. #define FB2_KEY(key,str) insert(str,key);
  22. class FbXmlHandler : public QObject, public QXmlDefaultHandler
  23. {
  24. Q_OBJECT
  25. public:
  26. explicit FbXmlHandler();
  27. virtual ~FbXmlHandler();
  28. bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes);
  29. bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
  30. bool characters(const QString &str);
  31. bool error(const QXmlParseException& exception);
  32. bool warning(const QXmlParseException& exception);
  33. bool fatalError(const QXmlParseException &exception);
  34. QString errorString() const;
  35. signals:
  36. void log(const FbMessage &msg);
  37. protected:
  38. class NodeHandler
  39. {
  40. public:
  41. static QString Value(const QXmlAttributes &attributes, const QString &name);
  42. explicit NodeHandler(const QString &name)
  43. : m_name(name), m_handler(0), m_closed(false) {}
  44. virtual ~NodeHandler()
  45. { if (m_handler) delete m_handler; }
  46. bool doStart(const QString &name, const QXmlAttributes &attributes);
  47. bool doText(const QString &text);
  48. bool doEnd(const QString &name, bool & found);
  49. protected:
  50. virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &attributes)
  51. { Q_UNUSED(name); Q_UNUSED(attributes); return NULL; }
  52. virtual void TxtTag(const QString &text)
  53. { Q_UNUSED(text); }
  54. virtual void EndTag(const QString &name)
  55. { Q_UNUSED(name); }
  56. const QString & Name() const
  57. { return m_name; }
  58. private:
  59. const QString m_name;
  60. NodeHandler * m_handler;
  61. bool m_closed;
  62. };
  63. protected:
  64. virtual NodeHandler * CreateRoot(const QString &name, const QXmlAttributes &attributes) = 0;
  65. static bool isWhiteSpace(const QString &str);
  66. protected:
  67. NodeHandler * m_handler;
  68. QString m_error;
  69. };
  70. #endif // FB2XML_H