123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #ifndef FB2READ_H
- #define FB2READ_H
- #include <QXmlDefaultHandler>
- #include <QTextCursor>
- #include <QStringList>
- #include <QTextFrameFormat>
- #include <QTextBlockFormat>
- #include <QTextCharFormat>
- QT_BEGIN_NAMESPACE
- class QTextEdit;
- QT_END_NAMESPACE
- class Fb2Handler : public QXmlDefaultHandler
- {
- public:
- Fb2Handler(QTextDocument & document);
- virtual ~Fb2Handler();
- bool startElement(const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &attributes);
- bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
- bool characters(const QString &str);
- bool fatalError(const QXmlParseException &exception);
- QString errorString() const;
- private:
- class ContentHandler
- {
- public:
- ContentHandler(Fb2Handler &owner, const QString &name);
- ContentHandler(ContentHandler &parent, const QString &name);
- virtual ~ContentHandler();
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doText(const QString &text);
- virtual bool doEnd(const QString &name, bool & exit);
- QTextDocument & document() { return m_owner.document(); }
- QTextCursor & cursor() { return m_owner.cursor(); }
- protected:
- void CloseHandler(QTextCursor &cursor);
- Fb2Handler & m_owner;
- ContentHandler * m_handler;
- const QString m_name;
- };
- class RootHandler : public ContentHandler
- {
- public:
- RootHandler(Fb2Handler & owner, const QString &name);
- virtual bool doStart(const QString & name, const QXmlAttributes &attributes);
- private:
- enum Keyword {
- None = 0,
- Style,
- Descr,
- Body,
- Binary,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString & name);
- };
- class DescrHandler : public ContentHandler
- {
- public:
- DescrHandler(ContentHandler &parent, const QString &name) : ContentHandler(parent, name) {}
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doEnd(const QString &name, bool & exit);
- };
- class BodyHandler : public ContentHandler
- {
- public:
- BodyHandler(ContentHandler &parent, const QString &name);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- private:
- enum Keyword {
- None = 0,
- Image,
- Title,
- Epigraph,
- Section,
- Paragraph,
- Poem,
- Stanza,
- Verse,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- private:
- bool m_feed;
- };
- class TextHandler : public ContentHandler
- {
- public:
- TextHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doText(const QString &text);
- private:
- enum Keyword {
- None = 0,
- Strong,
- Emphasis,
- Style,
- Anchor,
- Strikethrough,
- Sub,
- Sup,
- Code,
- Image,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- };
- class ImageHandler : public ContentHandler
- {
- public:
- ImageHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- };
- class SectionHandler : public ContentHandler
- {
- public:
- SectionHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doEnd(const QString &name, bool & exit);
- private:
- enum Keyword {
- None = 0,
- Title,
- Epigraph,
- Image,
- Annotation,
- Section,
- Paragraph,
- Poem,
- Subtitle,
- Cite,
- Emptyline,
- Table,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- private:
- QTextFrame * m_frame;
- bool m_feed;
- };
- class TitleHandler : public ContentHandler
- {
- public:
- TitleHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doEnd(const QString &name, bool & exit);
- private:
- enum Keyword {
- None = 0,
- Paragraph,
- Emptyline,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- private:
- QTextFrame * m_frame;
- QTextTable * m_table;
- bool m_feed;
- };
- class PoemHandler : public ContentHandler
- {
- public:
- PoemHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doEnd(const QString &name, bool & exit);
- private:
- enum Keyword {
- None = 0,
- Title,
- Epigraph,
- Stanza,
- Author,
- Date,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- private:
- QTextFrame * m_frame;
- QTextTable * m_table;
- bool m_feed;
- };
- class StanzaHandler : public ContentHandler
- {
- public:
- StanzaHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- private:
- enum Keyword {
- None = 0,
- Title,
- Subtitle,
- Verse,
- };
- class KeywordHash : public QHash<QString, Keyword> { public: KeywordHash(); };
- static Keyword toKeyword(const QString &name);
- private:
- bool m_feed;
- };
- class BinaryHandler : public ContentHandler
- {
- public:
- BinaryHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes);
- virtual bool doStart(const QString &name, const QXmlAttributes &attributes);
- virtual bool doText(const QString &text);
- virtual bool doEnd(const QString &name, bool & exit);
- private:
- QString m_file;
- QString m_text;
- };
- public:
- bool setHandler(ContentHandler * handler) { m_handler = handler; return handler; }
- QTextDocument & document() { return m_document; }
- QTextCursor & cursor() { return m_cursor; }
- private:
- QTextDocument & m_document;
- QTextCursor m_cursor;
- ContentHandler * m_handler;
- QString m_error;
- };
- #endif // FB2READ_H
|