fb2xml.hpp 2.9 KB

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