fb2read.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef FB2READ_H
  2. #define FB2READ_H
  3. #include <QXmlDefaultHandler>
  4. #include <QTextCursor>
  5. #include <QStringList>
  6. #include <QTextFrameFormat>
  7. #include <QTextBlockFormat>
  8. #include <QTextCharFormat>
  9. QT_BEGIN_NAMESPACE
  10. class QTextEdit;
  11. QT_END_NAMESPACE
  12. class Fb2Handler : public QXmlDefaultHandler
  13. {
  14. public:
  15. Fb2Handler(QTextDocument & document);
  16. virtual ~Fb2Handler();
  17. bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes);
  18. bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
  19. bool characters(const QString &str);
  20. bool fatalError(const QXmlParseException &exception);
  21. QString errorString() const;
  22. private:
  23. class ContentHandler
  24. {
  25. public:
  26. ContentHandler(Fb2Handler &owner, const QString &name);
  27. ContentHandler(ContentHandler &parent, const QString &name);
  28. virtual ~ContentHandler();
  29. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  30. virtual bool doText(const QString &text);
  31. virtual bool doEnd(const QString &name, bool & exit);
  32. QTextDocument & document() { return m_owner.document(); }
  33. QTextCursor & cursor() { return m_owner.cursor(); }
  34. protected:
  35. void CloseHandler(QTextCursor &cursor);
  36. Fb2Handler & m_owner;
  37. ContentHandler * m_handler;
  38. const QString m_name;
  39. };
  40. class RootHandler : public ContentHandler
  41. {
  42. public:
  43. RootHandler(Fb2Handler & owner, const QString &name);
  44. virtual bool doStart(const QString & name, const QXmlAttributes &attributes);
  45. private:
  46. enum Keyword {
  47. None = 0,
  48. Style,
  49. Descr,
  50. Body,
  51. Binary,
  52. };
  53. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  54. static Keyword toKeyword(const QString & name);
  55. };
  56. class DescrHandler : public ContentHandler
  57. {
  58. public:
  59. DescrHandler(ContentHandler &parent, const QString &name) : ContentHandler(parent, name) {}
  60. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  61. virtual bool doEnd(const QString &name, bool & exit);
  62. };
  63. class BodyHandler : public ContentHandler
  64. {
  65. public:
  66. BodyHandler(ContentHandler &parent, const QString &name);
  67. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  68. private:
  69. enum Keyword {
  70. None = 0,
  71. Image,
  72. Title,
  73. Epigraph,
  74. Section,
  75. Paragraph,
  76. Poem,
  77. Stanza,
  78. Verse,
  79. };
  80. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  81. static Keyword toKeyword(const QString &name);
  82. private:
  83. bool m_feed;
  84. };
  85. class TextHandler : public ContentHandler
  86. {
  87. public:
  88. TextHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  89. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  90. virtual bool doText(const QString &text);
  91. private:
  92. enum Keyword {
  93. None = 0,
  94. Strong,
  95. Emphasis,
  96. Style,
  97. Anchor,
  98. Strikethrough,
  99. Sub,
  100. Sup,
  101. Code,
  102. Image,
  103. };
  104. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  105. static Keyword toKeyword(const QString &name);
  106. };
  107. class ImageHandler : public ContentHandler
  108. {
  109. public:
  110. ImageHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  111. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  112. };
  113. class SectionHandler : public ContentHandler
  114. {
  115. public:
  116. SectionHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  117. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  118. virtual bool doEnd(const QString &name, bool & exit);
  119. private:
  120. enum Keyword {
  121. None = 0,
  122. Title,
  123. Epigraph,
  124. Image,
  125. Annotation,
  126. Section,
  127. Paragraph,
  128. Poem,
  129. Subtitle,
  130. Cite,
  131. Emptyline,
  132. Table,
  133. };
  134. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  135. static Keyword toKeyword(const QString &name);
  136. private:
  137. QTextFrame * m_frame;
  138. bool m_feed;
  139. };
  140. class TitleHandler : public ContentHandler
  141. {
  142. public:
  143. TitleHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  144. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  145. virtual bool doEnd(const QString &name, bool & exit);
  146. private:
  147. enum Keyword {
  148. None = 0,
  149. Paragraph,
  150. Emptyline,
  151. };
  152. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  153. static Keyword toKeyword(const QString &name);
  154. private:
  155. QTextFrame * m_frame;
  156. QTextTable * m_table;
  157. bool m_feed;
  158. };
  159. class PoemHandler : public ContentHandler
  160. {
  161. public:
  162. PoemHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  163. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  164. virtual bool doEnd(const QString &name, bool & exit);
  165. private:
  166. enum Keyword {
  167. None = 0,
  168. Title,
  169. Epigraph,
  170. Stanza,
  171. Author,
  172. Date,
  173. };
  174. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  175. static Keyword toKeyword(const QString &name);
  176. private:
  177. QTextFrame * m_frame;
  178. QTextTable * m_table;
  179. bool m_feed;
  180. };
  181. class StanzaHandler : public ContentHandler
  182. {
  183. public:
  184. StanzaHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  185. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  186. private:
  187. enum Keyword {
  188. None = 0,
  189. Title,
  190. Subtitle,
  191. Verse,
  192. };
  193. class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
  194. static Keyword toKeyword(const QString &name);
  195. private:
  196. bool m_feed;
  197. };
  198. class BinaryHandler : public ContentHandler
  199. {
  200. public:
  201. BinaryHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
  202. virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
  203. virtual bool doText(const QString &text);
  204. virtual bool doEnd(const QString &name, bool & exit);
  205. private:
  206. QString m_file;
  207. QString m_text;
  208. };
  209. public:
  210. bool setHandler(ContentHandler * handler) { m_handler = handler; return handler; }
  211. QTextDocument & document() { return m_document; }
  212. QTextCursor & cursor() { return m_cursor; }
  213. private:
  214. QTextDocument & m_document;
  215. QTextCursor m_cursor;
  216. ContentHandler * m_handler;
  217. QString m_error;
  218. };
  219. #endif // FB2READ_H