1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#ifndef FB2READ_H
#define FB2READ_H
#include "fb2xml.h"
#include <QByteArray>
#include <QMutex>
#include <QThread>
#include <QXmlDefaultHandler>
class Fb2ReadThread : public QThread
{
Q_OBJECT
public:
Fb2ReadThread(QObject *parent, const QString &filename, const QString &xml = QString());
~Fb2ReadThread();
QString * data() { return &m_html; }
signals:
void html(QString name, QString html);
public slots:
void stop();
protected:
void run();
private:
bool parse();
private:
const QString m_filename;
const QString m_xml;
QString m_html;
bool m_abort;
QMutex mutex;
};
class Fb2ReadHandler : public Fb2XmlHandler
{
public:
explicit Fb2ReadHandler(Fb2ReadThread &thread, QXmlStreamWriter &writer);
Fb2ReadThread & thread() { return m_thread; }
QXmlStreamWriter & writer() { return m_writer; }
private:
class BaseHandler : public NodeHandler
{
public:
explicit BaseHandler(Fb2ReadHandler &owner, const QString &name)
: NodeHandler(name), m_owner(owner) {}
protected:
QXmlStreamWriter & writer() { return m_owner.writer(); }
protected:
Fb2ReadHandler &m_owner;
};
class RootHandler : public BaseHandler
{
FB2_BEGIN_KEYLIST
Style,
Descr,
Body,
Binary,
FB2_END_KEYLIST
public:
explicit RootHandler(Fb2ReadHandler &owner, const QString &name);
protected:
virtual NodeHandler * NewTag(const QString & name, const QXmlAttributes &atts);
virtual void EndTag(const QString &name);
};
class HeadHandler : public BaseHandler
{
FB2_BEGIN_KEYLIST
Image,
FB2_END_KEYLIST
public:
explicit HeadHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts);
protected:
virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &atts);
virtual void TxtTag(const QString &text);
virtual void EndTag(const QString &name);
private:
bool m_empty;
};
class DescrHandler : public HeadHandler
{
FB2_BEGIN_KEYLIST
Title,
Document,
Publish,
Custom,
FB2_END_KEYLIST
public:
explicit DescrHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts)
: HeadHandler(owner, name, atts) {}
protected:
virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &atts);
};
class TitleHandler : public HeadHandler
{
public:
explicit TitleHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts)
: HeadHandler(owner, name, atts) {}
protected:
virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &atts);
};
class TextHandler : public BaseHandler
{
FB2_BEGIN_KEYLIST
Section,
Anchor,
Table,
Image,
Parag,
Style,
Strong,
Emphas,
Strike,
Sub,
Sup,
Code,
FB2_END_KEYLIST
public:
explicit TextHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts, const QString &tag, const QString &style = QString());
explicit TextHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts, const QString &tag, const QString &style = QString());
protected:
virtual NodeHandler * NewTag(const QString &name, const QXmlAttributes &atts);
virtual void TxtTag(const QString &text);
virtual void EndTag(const QString &name);
protected:
void Init(const QXmlAttributes &atts);
bool isNotes() const;
protected:
TextHandler *m_parent;
QString m_tag;
QString m_style;
};
class AnchorHandler : public TextHandler
{
public:
explicit AnchorHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts);
};
class ImageHandler : public TextHandler
{
public:
explicit ImageHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts);
};
class BinaryHandler : public BaseHandler
{
public:
explicit BinaryHandler(Fb2ReadHandler &owner, const QString &name, const QXmlAttributes &atts);
protected:
virtual void TxtTag(const QString &text);
virtual void EndTag(const QString &name);
private:
QString m_file;
QString m_text;
};
protected:
virtual NodeHandler * CreateRoot(const QString &name, const QXmlAttributes &atts);
private:
void addFile(const QString &name, const QByteArray &data);
QString getFile(const QString &name);
private:
typedef QHash<QString, QString> StringHash;
Fb2ReadThread &m_thread;
QXmlStreamWriter &m_writer;
StringHash m_hash;
};
#endif // FB2READ_H
|