fb2read.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include <QtDebug>
  4. #include "fb2read.h"
  5. #define FB2_BEGIN_KEYHASH(x) \
  6. Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
  7. { \
  8. static const KeywordHash map; \
  9. KeywordHash::const_iterator i = map.find(name); \
  10. return i == map.end() ? None : i.value(); \
  11. } \
  12. Fb2Handler::x::KeywordHash::KeywordHash() {
  13. #define FB2_END_KEYHASH }
  14. static QString Value(const QXmlAttributes &attributes, const QString &name)
  15. {
  16. int count = attributes.count();
  17. for (int i = 0; i < count; i++ ) {
  18. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  19. return attributes.value(i);
  20. }
  21. }
  22. return QString();
  23. }
  24. //---------------------------------------------------------------------------
  25. // Fb2Handler::BaseHandler
  26. //---------------------------------------------------------------------------
  27. Fb2Handler::BaseHandler::~BaseHandler()
  28. {
  29. if (m_handler) delete m_handler;
  30. }
  31. bool Fb2Handler::BaseHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  32. {
  33. if (m_handler) return m_handler->doStart(name, attributes);
  34. m_handler = new BaseHandler(name);
  35. return true;
  36. }
  37. bool Fb2Handler::BaseHandler::doText(const QString &text)
  38. {
  39. if (m_handler) return m_handler->doText(text);
  40. return true;
  41. }
  42. bool Fb2Handler::BaseHandler::doEnd(const QString &name, bool & exit)
  43. {
  44. if (m_handler) {
  45. bool ok = m_handler->doEnd(name, exit);
  46. if (exit) {
  47. delete m_handler;
  48. m_handler = NULL;
  49. exit = false;
  50. }
  51. return ok;
  52. } else {
  53. if (name != m_name) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
  54. exit = true;
  55. return true;
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. // Fb2Handler::RootHandler
  60. //---------------------------------------------------------------------------
  61. FB2_BEGIN_KEYHASH(RootHandler)
  62. insert("stylesheet", Style);
  63. insert("description", Descr);
  64. insert("body", Body);
  65. insert("binary", Binary);
  66. FB2_END_KEYHASH
  67. Fb2Handler::RootHandler::RootHandler(Fb2MainDocument &document, const QString &name)
  68. : BaseHandler(name)
  69. , m_document(document)
  70. , m_cursor1(&document)
  71. , m_cursor2(&document.child())
  72. , m_empty(true)
  73. {
  74. m_cursor1.beginEditBlock();
  75. m_cursor2.beginEditBlock();
  76. }
  77. Fb2Handler::RootHandler::~RootHandler()
  78. {
  79. m_cursor1.endEditBlock();
  80. m_cursor2.endEditBlock();
  81. }
  82. bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  83. {
  84. if (m_handler) return m_handler->doStart(name, attributes);
  85. switch (toKeyword(name)) {
  86. case Descr : m_handler = new DescrHandler(name); break;
  87. case Body : m_handler = new BodyHandler(m_empty ? m_cursor1 : m_cursor2, name); m_empty = false; break;
  88. case Binary : m_handler = new BinaryHandler(m_document, name, attributes); break;
  89. default:
  90. qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
  91. m_handler = new BaseHandler(name);
  92. }
  93. return true;
  94. }
  95. //---------------------------------------------------------------------------
  96. // Fb2Handler::DescrHandler
  97. //---------------------------------------------------------------------------
  98. bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  99. {
  100. Q_UNUSED(name);
  101. Q_UNUSED(attributes);
  102. return true;
  103. }
  104. bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
  105. {
  106. Q_UNUSED(name);
  107. if (name == "description") exit = true;
  108. return true;
  109. }
  110. //---------------------------------------------------------------------------
  111. // Fb2Handler::TextHandler
  112. //---------------------------------------------------------------------------
  113. Fb2Handler::TextHandler::TextHandler(QTextCursor &cursor, const QString &name)
  114. : BaseHandler(name)
  115. , m_cursor(cursor)
  116. , m_blockFormat(m_cursor.blockFormat())
  117. , m_charFormat(m_cursor.charFormat())
  118. {
  119. }
  120. Fb2Handler::TextHandler::TextHandler(TextHandler &parent, const QString &name)
  121. : BaseHandler(name)
  122. , m_cursor(parent.m_cursor)
  123. , m_blockFormat(m_cursor.blockFormat())
  124. , m_charFormat(m_cursor.charFormat())
  125. {
  126. }
  127. bool Fb2Handler::TextHandler::doEnd(const QString &name, bool & exit)
  128. {
  129. bool ok = BaseHandler::doEnd(name, exit);
  130. if (!m_handler) cursor().setCharFormat(m_charFormat);
  131. return ok;
  132. }
  133. //---------------------------------------------------------------------------
  134. // Fb2Handler::BodyHandler
  135. //---------------------------------------------------------------------------
  136. FB2_BEGIN_KEYHASH(BodyHandler)
  137. insert("image", Image);
  138. insert("title", Title);
  139. insert("epigraph", Epigraph);
  140. insert("section", Section);
  141. insert("p", Paragraph);
  142. insert("poem", Poem);
  143. insert("stanza", Stanza);
  144. insert("v", Verse);
  145. FB2_END_KEYHASH
  146. Fb2Handler::BodyHandler::BodyHandler(QTextCursor &cursor, const QString &name)
  147. : TextHandler(cursor, name)
  148. , m_feed(false)
  149. {
  150. QTextBlockFormat blockFormat;
  151. blockFormat.setTopMargin(4);
  152. blockFormat.setBottomMargin(4);
  153. cursor.setBlockFormat(blockFormat);
  154. }
  155. bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  156. {
  157. if (m_handler) return m_handler->doStart(name, attributes);
  158. switch (toKeyword(name)) {
  159. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  160. case Image : m_handler = new ImageHandler(*this, name, attributes); break;
  161. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  162. case Title : m_handler = new TitleHandler(*this, name, attributes); break;
  163. case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
  164. case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
  165. default : m_handler = new BaseHandler(name); break;
  166. }
  167. return true;
  168. }
  169. //---------------------------------------------------------------------------
  170. // Fb2Handler::SectionHandler
  171. //---------------------------------------------------------------------------
  172. FB2_BEGIN_KEYHASH(SectionHandler)
  173. insert("title", Title);
  174. insert("epigraph", Epigraph);
  175. insert("image", Image);
  176. insert("annotation", Annotation);
  177. insert("section", Section);
  178. insert("p", Paragraph);
  179. insert("poem", Poem);
  180. insert("subtitle", Subtitle);
  181. insert("cite", Cite);
  182. insert("empty-line", Emptyline);
  183. insert("table", Table);
  184. FB2_END_KEYHASH
  185. Fb2Handler::SectionHandler::SectionHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  186. : TextHandler(parent, name)
  187. , m_frame(cursor().currentFrame())
  188. , m_feed(false)
  189. {
  190. Q_UNUSED(attributes);
  191. QTextFrameFormat frameFormat;
  192. frameFormat.setBorder(1);
  193. frameFormat.setPadding(8);
  194. frameFormat.setTopMargin(4);
  195. frameFormat.setBottomMargin(4);
  196. if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
  197. cursor().insertFrame(frameFormat);
  198. QTextBlockFormat blockFormat;
  199. blockFormat.setTopMargin(4);
  200. blockFormat.setBottomMargin(4);
  201. cursor().setBlockFormat(blockFormat);
  202. }
  203. bool Fb2Handler::SectionHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  204. {
  205. if (m_handler) return m_handler->doStart(name, attributes);
  206. Keyword keyword = toKeyword(name);
  207. switch (keyword) {
  208. case Paragraph:
  209. case Emptyline:
  210. case Image:
  211. if (m_feed) cursor().insertBlock();
  212. m_feed = true;
  213. break;
  214. default:
  215. m_feed = false;
  216. }
  217. switch (keyword) {
  218. case Emptyline : m_handler = new BaseHandler(name); break;
  219. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  220. case Section : m_handler = new SectionHandler(*this, name, attributes); break;
  221. case Title : m_handler = new TitleHandler(*this, name, attributes); break;
  222. case Poem : m_handler = new PoemHandler(*this, name, attributes); break;
  223. case Image : m_handler = new ImageHandler(*this, name, attributes); break;
  224. default : m_handler = new BaseHandler(name); break;
  225. }
  226. return true;
  227. }
  228. bool Fb2Handler::SectionHandler::doEnd(const QString &name, bool & exit)
  229. {
  230. bool ok = BaseHandler::doEnd(name, exit);
  231. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  232. return ok;
  233. }
  234. //---------------------------------------------------------------------------
  235. // Fb2Handler::TitleHandler
  236. //---------------------------------------------------------------------------
  237. FB2_BEGIN_KEYHASH(TitleHandler)
  238. insert("p", Paragraph);
  239. insert("empty-line", Emptyline);
  240. FB2_END_KEYHASH
  241. Fb2Handler::TitleHandler::TitleHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  242. : TextHandler(parent, name)
  243. , m_frame(cursor().currentFrame())
  244. , m_table(NULL)
  245. , m_feed(false)
  246. {
  247. Q_UNUSED(attributes);
  248. QTextTableFormat format;
  249. format.setBorder(0);
  250. format.setCellPadding(4);
  251. format.setCellSpacing(4);
  252. m_table = cursor().insertTable(1, 1, format);
  253. }
  254. bool Fb2Handler::TitleHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  255. {
  256. if (m_handler) return m_handler->doStart(name, attributes);
  257. if (m_feed) cursor().insertBlock(); else m_feed = true;
  258. switch (toKeyword(name)) {
  259. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  260. case Emptyline : m_handler = new BlockHandler(*this, name, attributes); break;
  261. default: m_handler = new BaseHandler(name); break;
  262. }
  263. return true;
  264. }
  265. bool Fb2Handler::TitleHandler::doEnd(const QString &name, bool & exit)
  266. {
  267. bool ok = BaseHandler::doEnd(name, exit);
  268. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  269. return ok;
  270. }
  271. //---------------------------------------------------------------------------
  272. // Fb2Handler::PoemHandler
  273. //---------------------------------------------------------------------------
  274. FB2_BEGIN_KEYHASH(PoemHandler)
  275. insert("title", Title);
  276. insert("epigraph", Epigraph);
  277. insert("stanza", Stanza);
  278. insert("author", Author);
  279. insert("date", Date);
  280. FB2_END_KEYHASH
  281. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  282. : TextHandler(parent, name)
  283. , m_frame(cursor().currentFrame())
  284. , m_table(NULL)
  285. , m_feed(false)
  286. {
  287. Q_UNUSED(attributes);
  288. QTextTableFormat format;
  289. format.setBorder(1);
  290. format.setCellPadding(4);
  291. format.setCellSpacing(4);
  292. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  293. m_table = cursor().insertTable(1, 1, format);
  294. }
  295. bool Fb2Handler::PoemHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  296. {
  297. if (m_handler) return m_handler->doStart(name, attributes);
  298. Keyword keyword = toKeyword(name);
  299. switch (keyword) {
  300. case Title:
  301. case Epigraph:
  302. case Stanza:
  303. case Author:
  304. if (m_feed) m_table->appendRows(1);
  305. m_feed = true;
  306. m_handler = new StanzaHandler(*this, name, attributes);
  307. break;
  308. default:
  309. m_handler = new BaseHandler(name);
  310. }
  311. return true;
  312. }
  313. bool Fb2Handler::PoemHandler::doEnd(const QString &name, bool & exit)
  314. {
  315. bool ok = BaseHandler::doEnd(name, exit);
  316. if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
  317. return ok;
  318. }
  319. //---------------------------------------------------------------------------
  320. // Fb2Handler::StanzaHandler
  321. //---------------------------------------------------------------------------
  322. FB2_BEGIN_KEYHASH(StanzaHandler)
  323. insert("title", Title);
  324. insert("subtitle", Subtitle);
  325. insert("v", Verse);
  326. FB2_END_KEYHASH
  327. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  328. : TextHandler(parent, name)
  329. , m_feed(false)
  330. {
  331. Q_UNUSED(attributes);
  332. }
  333. bool Fb2Handler::StanzaHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  334. {
  335. if (m_handler) return m_handler->doStart(name, attributes);
  336. Keyword keyword = toKeyword(name);
  337. switch (keyword) {
  338. case Title:
  339. case Subtitle:
  340. case Verse:
  341. if (m_feed) cursor().insertBlock();
  342. m_feed = true;
  343. default: ;
  344. }
  345. switch (keyword) {
  346. case Title:
  347. case Subtitle:
  348. m_handler = new TitleHandler(*this, name, attributes); break;
  349. case Verse:
  350. m_handler = new BlockHandler(*this, name, attributes); break;
  351. default:
  352. m_handler = new BaseHandler(name); break;
  353. }
  354. return true;
  355. }
  356. //---------------------------------------------------------------------------
  357. // Fb2Handler::BlockHandler
  358. //---------------------------------------------------------------------------
  359. FB2_BEGIN_KEYHASH(BlockHandler)
  360. insert("strong" , Strong);
  361. insert("emphasis" , Emphasis);
  362. insert("style" , Style);
  363. insert("a" , Anchor);
  364. insert("strikethrough" , Strikethrough);
  365. insert("sub" , Sub);
  366. insert("sup" , Sup);
  367. insert("code" , Code);
  368. insert("image" , Image);
  369. FB2_END_KEYHASH
  370. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  371. : TextHandler(parent, name)
  372. {
  373. Q_UNUSED(attributes);
  374. QTextBlockFormat blockFormat;
  375. blockFormat.setTopMargin(4);
  376. blockFormat.setBottomMargin(4);
  377. cursor().setBlockFormat(blockFormat);
  378. }
  379. bool Fb2Handler::BlockHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  380. {
  381. Q_UNUSED(attributes);
  382. if (m_handler) return m_handler->doStart(name, attributes);
  383. switch (toKeyword(name)) {
  384. default: m_handler = new BaseHandler(name); break;
  385. }
  386. return true;
  387. }
  388. bool Fb2Handler::BlockHandler::doText(const QString &text)
  389. {
  390. if (m_handler) return m_handler->doText(text);
  391. cursor().insertText(text);
  392. return true;
  393. }
  394. //---------------------------------------------------------------------------
  395. // Fb2Handler::ImageHandler
  396. //---------------------------------------------------------------------------
  397. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  398. : TextHandler(parent, name)
  399. {
  400. QString image = Value(attributes, "href");
  401. while (image.left(1) == "#") image.remove(0, 1);
  402. if (!image.isEmpty()) cursor().insertImage(image);
  403. }
  404. bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  405. {
  406. Q_UNUSED(name);
  407. Q_UNUSED(attributes);
  408. return false;
  409. }
  410. //---------------------------------------------------------------------------
  411. // Fb2Handler::BinaryHandler
  412. //---------------------------------------------------------------------------
  413. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  414. : BaseHandler(name)
  415. , m_document(document)
  416. , m_file(Value(attributes, "id"))
  417. {
  418. }
  419. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  420. {
  421. Q_UNUSED(name);
  422. Q_UNUSED(attributes);
  423. return false;
  424. }
  425. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  426. {
  427. m_text += text;
  428. return true;
  429. }
  430. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  431. {
  432. Q_UNUSED(name);
  433. QByteArray in; in.append(m_text);
  434. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  435. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  436. exit = true;
  437. return true;
  438. }
  439. //---------------------------------------------------------------------------
  440. // Fb2Handler
  441. //---------------------------------------------------------------------------
  442. Fb2Handler::Fb2Handler(Fb2MainDocument & document)
  443. : m_document(document)
  444. , m_handler(NULL)
  445. {
  446. document.clear();
  447. m_document.child().clear();
  448. }
  449. Fb2Handler::~Fb2Handler()
  450. {
  451. m_document.clearUndoRedoStacks();
  452. m_document.child().clearUndoRedoStacks();
  453. if (m_handler) delete m_handler;
  454. }
  455. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  456. {
  457. Q_UNUSED(namespaceURI);
  458. Q_UNUSED(localName);
  459. const QString name = qName.toLower();
  460. if (m_handler) return m_handler->doStart(name, attributes);
  461. if (name == "fictionbook") {
  462. m_handler = new RootHandler(m_document, name);
  463. return true;
  464. } else {
  465. m_error = QObject::tr("The file is not an FB2 file.");
  466. return false;
  467. }
  468. }
  469. static bool isWhiteSpace(const QString &str)
  470. {
  471. return str.simplified().isEmpty();
  472. }
  473. bool Fb2Handler::characters(const QString &str)
  474. {
  475. QString s = str.simplified();
  476. if (s.isEmpty()) return true;
  477. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  478. if (isWhiteSpace(str.right(1))) s.append(" ");
  479. return m_handler && m_handler->doText(s);
  480. }
  481. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  482. {
  483. Q_UNUSED(namespaceURI);
  484. Q_UNUSED(localName);
  485. bool exit = false;
  486. return m_handler && m_handler->doEnd(qName.toLower(), exit);
  487. }
  488. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  489. {
  490. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  491. .arg(exception.lineNumber())
  492. .arg(exception.columnNumber())
  493. .arg(exception.message());
  494. return false;
  495. }
  496. QString Fb2Handler::errorString() const
  497. {
  498. return m_error;
  499. }
  500. #undef FB2_BEGIN_KEYHASH
  501. #undef FB2_END_KEYHASH