1
0

fb2xml.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 warning(int row, int col, const QString &msg);
  37. void error(int row, int col, const QString &msg);
  38. void fatal(int row, int col, const QString &msg);
  39. protected:
  40. class NodeHandler
  41. {
  42. public:
  43. static QString Value(const QXmlAttributes &attributes, const QString &name);
  44. explicit NodeHandler(const QString &name)
  45. : m_name(name), m_handler(0), m_closed(false) {}
  46. virtual ~NodeHandler()
  47. { if (m_handler) delete m_handler; }
  48. bool doStart(const QString &name, const QXmlAttributes &attributes);
  49. bool doText(const QString &text);
  50. bool doEnd(const QString &name, bool & found);
  51. protected:
  52. virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &attributes)
  53. { Q_UNUSED(name); Q_UNUSED(attributes); return NULL; }
  54. virtual void TxtTag(const QString &text)
  55. { Q_UNUSED(text); }
  56. virtual void EndTag(const QString &name)
  57. { Q_UNUSED(name); }
  58. const QString & Name() const
  59. { return m_name; }
  60. private:
  61. const QString m_name;
  62. NodeHandler * m_handler;
  63. bool m_closed;
  64. };
  65. protected:
  66. virtual NodeHandler * CreateRoot(const QString &name, const QXmlAttributes &attributes) = 0;
  67. static bool isWhiteSpace(const QString &str);
  68. protected:
  69. NodeHandler * m_handler;
  70. QString m_error;
  71. };
  72. #endif // FB2XML_H