fb2xml.h 2.4 KB

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