fb2read.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "fb2read.hpp"
  2. #include <QtDebug>
  3. #include "fb2imgs.hpp"
  4. #include "fb2xml2.h"
  5. //---------------------------------------------------------------------------
  6. // FbReadThread
  7. //---------------------------------------------------------------------------
  8. void FbReadThread::execute(QObject *parent, QXmlInputSource *source, QIODevice *device)
  9. {
  10. FbReadThread *thread = new FbReadThread(parent, source, device);
  11. connect(thread, SIGNAL(html(QString, FbStore*)), parent, SLOT(html(QString, FbStore*)));
  12. thread->start();
  13. }
  14. FbReadThread::FbReadThread(QObject *parent, QXmlInputSource *source, QIODevice *device)
  15. : QThread(parent)
  16. , m_device(device)
  17. , m_source(source)
  18. {
  19. m_store = new FbStore(this);
  20. }
  21. FbReadThread::~FbReadThread()
  22. {
  23. if (m_source) delete m_source;
  24. if (m_device) delete m_device;
  25. }
  26. void FbReadThread::run()
  27. {
  28. if (parse()) {
  29. emit html(m_html, m_store);
  30. } else {
  31. delete m_store;
  32. }
  33. deleteLater();
  34. }
  35. bool FbReadThread::parse()
  36. {
  37. QXmlStreamWriter writer(&m_html);
  38. FbReadHandler handler(writer);
  39. connect(&handler, SIGNAL(binary(QString,QByteArray)), m_store, SLOT(binary(QString,QByteArray)));
  40. connect(&handler, SIGNAL(warning(int,int,QString)), parent(), SIGNAL(warning(int,int,QString)));
  41. connect(&handler, SIGNAL(error(int,int,QString)), parent(), SIGNAL(error(int,int,QString)));
  42. connect(&handler, SIGNAL(fatal(int,int,QString)), parent(), SIGNAL(fatal(int,int,QString)));
  43. #ifdef FB2_USE_LIBXML2
  44. XML2::XmlReader reader;
  45. #else
  46. QXmlSimpleReader reader;
  47. #endif
  48. reader.setContentHandler(&handler);
  49. reader.setLexicalHandler(&handler);
  50. reader.setErrorHandler(&handler);
  51. #ifdef FB2_USE_LIBXML2
  52. if (m_device) {
  53. return reader.parse(m_device);
  54. } else {
  55. return reader.parse(m_source);
  56. }
  57. #else
  58. if (m_device) {
  59. m_source = new QXmlInputSource();
  60. m_source->setData(m_device->readAll());
  61. }
  62. return reader.parse(m_source);
  63. #endif
  64. }
  65. /*
  66. FbReadThread::FbReadThread(QObject *parent, const QString &filename, const QString &xml)
  67. : QThread(parent)
  68. , m_temp(0)
  69. , m_filename(filename)
  70. , m_xml(xml)
  71. , m_abort(false)
  72. {
  73. connect(this, SIGNAL(html(QString)), parent, SLOT(html(QString)));
  74. }
  75. void FbReadThread::stop()
  76. {
  77. QMutexLocker locker(&mutex);
  78. Q_UNUSED(locker);
  79. m_abort = true;
  80. }
  81. void FbReadThread::run()
  82. {
  83. if (parse()) emit html(m_html);
  84. }
  85. #ifdef FB2_USE_LIBXML2
  86. bool FbReadThread::parse()
  87. {
  88. QXmlStreamWriter writer(&m_html);
  89. FbReadHandler handler(*this, writer);
  90. XML2::XmlReader reader;
  91. reader.setContentHandler(&handler);
  92. reader.setLexicalHandler(&handler);
  93. reader.setErrorHandler(&handler);
  94. if (m_xml.isEmpty()) {
  95. QFile file(m_filename);
  96. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  97. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
  98. return false;
  99. }
  100. return reader.parse(file);
  101. } else {
  102. QXmlInputSource source;
  103. source.setData(m_xml);
  104. return reader.parse(source);
  105. }
  106. }
  107. #else
  108. bool FbReadThread::parse()
  109. {
  110. QXmlStreamWriter writer(&m_html);
  111. FbReadHandler handler(*this, writer);
  112. connect(&handler, SIGNAL(binary(QString,QByteArray)), this, SLOT(data(QString,QByteArray)));
  113. QXmlSimpleReader reader;
  114. reader.setContentHandler(&handler);
  115. reader.setLexicalHandler(&handler);
  116. reader.setErrorHandler(&handler);
  117. QXmlInputSource source;
  118. if (m_xml.isEmpty()) {
  119. QFile file(m_filename);
  120. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  121. qCritical() << QObject::tr("Cannot read file %1: %2.").arg(m_filename).arg(file.errorString());
  122. return false;
  123. }
  124. source.setData(file.readAll());
  125. } else {
  126. source.setData(m_xml);
  127. }
  128. return reader.parse(source);
  129. }
  130. #endif
  131. */
  132. //---------------------------------------------------------------------------
  133. // FbReadHandler::RootHandler
  134. //---------------------------------------------------------------------------
  135. FB2_BEGIN_KEYHASH(FbReadHandler::RootHandler)
  136. FB2_KEY( Style , "stylesheet" );
  137. FB2_KEY( Descr , "description" );
  138. FB2_KEY( Body , "body" );
  139. FB2_KEY( Binary , "binary" );
  140. FB2_END_KEYHASH
  141. FbReadHandler::RootHandler::RootHandler(FbReadHandler &owner, const QString &name)
  142. : BaseHandler(owner, name)
  143. , m_head(true)
  144. {
  145. }
  146. FbXmlHandler::NodeHandler * FbReadHandler::RootHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  147. {
  148. switch (toKeyword(name)) {
  149. case Binary: return new BinaryHandler(m_owner, name, atts);
  150. case Style: return new StyleHandler(m_owner, name, m_style);
  151. default: ;
  152. }
  153. if (m_head) {
  154. writeHeader();
  155. m_head = false;
  156. }
  157. return new TextHandler(m_owner, name, atts, "fb:" + name);
  158. }
  159. void FbReadHandler::RootHandler::EndTag(const QString &name)
  160. {
  161. Q_UNUSED(name);
  162. if (!m_head) writer().writeEndElement();
  163. }
  164. void FbReadHandler::RootHandler::writeScript(const QString &src)
  165. {
  166. writer().writeStartElement("script");
  167. writer().writeAttribute("type", "text/javascript");
  168. writer().writeAttribute("src", src);
  169. writer().writeCharacters("");
  170. writer().writeEndElement();
  171. }
  172. void FbReadHandler::RootHandler::writeHeader()
  173. {
  174. writer().writeStartElement("head");
  175. writeScript("qrc:/js/jquery.js");
  176. writeScript("qrc:/js/location.js");
  177. if (!m_style.isEmpty()) {
  178. writer().writeStartElement("style");
  179. writer().writeAttribute("type", "text/css");
  180. writer().writeAttribute("id", "origin");
  181. writer().writeCharacters(m_style);
  182. writer().writeEndElement();
  183. }
  184. writer().writeStartElement("style");
  185. writer().writeAttribute("type", "text/css");
  186. writer().writeAttribute("id", "inline");
  187. writer().writeCharacters("");
  188. writer().writeEndElement();
  189. writer().writeEndElement();
  190. writer().writeStartElement("body");
  191. }
  192. //---------------------------------------------------------------------------
  193. // FbReadHandler::StyleHandler
  194. //---------------------------------------------------------------------------
  195. FbReadHandler::StyleHandler::StyleHandler(FbReadHandler &owner, const QString &name, QString &text)
  196. : BaseHandler(owner, name)
  197. , m_text(text)
  198. {
  199. }
  200. void FbReadHandler::StyleHandler::TxtTag(const QString &text)
  201. {
  202. m_text += text;
  203. }
  204. //---------------------------------------------------------------------------
  205. // FbReadHandler::TextHandler
  206. //---------------------------------------------------------------------------
  207. FB2_BEGIN_KEYHASH(FbReadHandler::TextHandler)
  208. FB2_KEY( Anchor , "a" );
  209. FB2_KEY( Image , "image" );
  210. FB2_KEY( Origin , "table" );
  211. FB2_KEY( Origin , "td" );
  212. FB2_KEY( Origin , "th" );
  213. FB2_KEY( Origin , "tr" );
  214. FB2_KEY( Parag , "empty-line" );
  215. FB2_KEY( Parag , "text-author" );
  216. FB2_KEY( Parag , "subtitle" );
  217. FB2_KEY( Parag , "p" );
  218. FB2_KEY( Parag , "v" );
  219. FB2_KEY( Style , "style" );
  220. FB2_KEY( Strong , "strong" );
  221. FB2_KEY( Emphas , "emphasis" );
  222. FB2_KEY( Strike , "strikethrough" );
  223. FB2_KEY( Sub , "sub" );
  224. FB2_KEY( Sup , "sup" );
  225. FB2_KEY( Code , "code" );
  226. FB2_END_KEYHASH
  227. FbReadHandler::TextHandler::TextHandler(FbReadHandler &owner, const QString &name, const QXmlAttributes &atts, const QString &tag)
  228. : BaseHandler(owner, name)
  229. , m_parent(NULL)
  230. , m_tag(tag)
  231. , m_empty(true)
  232. {
  233. Init(name, atts);
  234. }
  235. FbReadHandler::TextHandler::TextHandler(TextHandler *parent, const QString &name, const QXmlAttributes &atts, const QString &tag)
  236. : BaseHandler(parent->m_owner, name)
  237. , m_parent(parent)
  238. , m_tag(tag)
  239. , m_empty(true)
  240. {
  241. Init(name, atts);
  242. }
  243. void FbReadHandler::TextHandler::Init(const QString &name, const QXmlAttributes &atts)
  244. {
  245. Keyword key = toKeyword(name);
  246. writer().writeStartElement(m_tag);
  247. int count = atts.count();
  248. for (int i = 0; i < count; i++) {
  249. QString name = atts.qName(i);
  250. switch (key) {
  251. case Anchor: { if (atts.localName(i) == "href") name = "href"; break; }
  252. case Image: { if (atts.localName(i) == "href") name = "src"; break; }
  253. default: ;
  254. }
  255. writer().writeAttribute(name, atts.value(i));
  256. }
  257. if (m_tag == "p" && (name == "text-author" || name == "subtitle")) {
  258. writer().writeAttribute("fb:class", name);
  259. }
  260. }
  261. FbXmlHandler::NodeHandler * FbReadHandler::TextHandler::NewTag(const QString &name, const QXmlAttributes &atts)
  262. {
  263. m_empty = false;
  264. QString tag;
  265. switch (toKeyword(name)) {
  266. case Origin : tag = name; break;
  267. case Anchor : tag = "a"; break;
  268. case Image : tag = "img"; break;
  269. case Parag : tag = "p"; break;
  270. case Strong : tag = "b"; break;
  271. case Emphas : tag = "i"; break;
  272. case Strike : tag = "s"; break;
  273. case Code : tag = "tt"; break;
  274. case Sub : tag = "sub"; break;
  275. case Sup : tag = "sup"; break;
  276. case Style : tag = "span"; break;
  277. default : tag = "fb:" + name;
  278. }
  279. return new TextHandler(this, name, atts, tag);
  280. }
  281. void FbReadHandler::TextHandler::TxtTag(const QString &text)
  282. {
  283. writer().writeCharacters(text);
  284. m_empty = false;
  285. }
  286. void FbReadHandler::TextHandler::EndTag(const QString &name)
  287. {
  288. if (m_empty) {
  289. if (name == "p") {
  290. writer().writeEmptyElement("br");
  291. } else {
  292. writer().writeCharacters("");
  293. }
  294. }
  295. writer().writeEndElement();
  296. }
  297. bool FbReadHandler::TextHandler::isNotes() const
  298. {
  299. if (m_style == "notes") return true;
  300. return m_parent ? m_parent->isNotes() : false;
  301. }
  302. //---------------------------------------------------------------------------
  303. // FbReadHandler::BinaryHandler
  304. //---------------------------------------------------------------------------
  305. FbReadHandler::BinaryHandler::BinaryHandler(FbReadHandler &owner, const QString &name, const QXmlAttributes &atts)
  306. : BaseHandler(owner, name)
  307. , m_file(Value(atts, "id"))
  308. {
  309. }
  310. void FbReadHandler::BinaryHandler::TxtTag(const QString &text)
  311. {
  312. m_text += text;
  313. }
  314. void FbReadHandler::BinaryHandler::EndTag(const QString &name)
  315. {
  316. Q_UNUSED(name);
  317. QByteArray in; in.append(m_text);
  318. if (!m_file.isEmpty()) m_owner.addFile(m_file, QByteArray::fromBase64(in));
  319. }
  320. //---------------------------------------------------------------------------
  321. // FbReadHandler
  322. //---------------------------------------------------------------------------
  323. bool FbReadHandler::load(QObject *page, QXmlInputSource &source, QString &html)
  324. {
  325. QXmlStreamWriter writer(&html);
  326. FbReadHandler handler(writer);
  327. connect(&handler, SIGNAL(binary(QString,QByteArray)), page, SLOT(binary(QString,QByteArray)));
  328. #ifdef FB2_USE_LIBXML2
  329. XML2::XmlReader reader;
  330. #else
  331. QXmlSimpleReader reader;
  332. #endif
  333. reader.setContentHandler(&handler);
  334. reader.setLexicalHandler(&handler);
  335. reader.setErrorHandler(&handler);
  336. return reader.parse(source);
  337. }
  338. FbReadHandler::FbReadHandler(QXmlStreamWriter &writer)
  339. : FbXmlHandler()
  340. , m_writer(writer)
  341. {
  342. m_writer.setAutoFormatting(true);
  343. m_writer.setAutoFormattingIndent(2);
  344. m_writer.writeStartElement("html");
  345. }
  346. FbReadHandler::~FbReadHandler()
  347. {
  348. m_writer.writeEndElement();
  349. }
  350. FbXmlHandler::NodeHandler * FbReadHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
  351. {
  352. Q_UNUSED(atts);
  353. if (name == "fictionbook") return new RootHandler(*this, name);
  354. m_error = QObject::tr("The file is not an FB2 file.");
  355. return 0;
  356. }
  357. bool FbReadHandler::comment(const QString& ch)
  358. {
  359. m_writer.writeComment(ch);
  360. return true;
  361. }
  362. void FbReadHandler::addFile(const QString &name, const QByteArray &data)
  363. {
  364. emit binary(name, data);
  365. }