fb2read.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 (m_handler->m_closed) {
  47. delete m_handler;
  48. m_handler = NULL;
  49. }
  50. return ok;
  51. } else {
  52. if (name != m_name) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
  53. m_closed = true;
  54. return true;
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. // Fb2Handler::RootHandler
  59. //---------------------------------------------------------------------------
  60. FB2_BEGIN_KEYHASH(RootHandler)
  61. insert("stylesheet", Style);
  62. insert("description", Descr);
  63. insert("body", Body);
  64. insert("binary", Binary);
  65. FB2_END_KEYHASH
  66. Fb2Handler::RootHandler::RootHandler(Fb2MainDocument &document, const QString &name)
  67. : BaseHandler(name)
  68. , m_document(document)
  69. , m_cursor1(&document, false)
  70. , m_cursor2(&document.child(), true)
  71. , m_empty(true)
  72. {
  73. m_cursor1.beginEditBlock();
  74. m_cursor2.beginEditBlock();
  75. }
  76. Fb2Handler::RootHandler::~RootHandler()
  77. {
  78. m_cursor1.endEditBlock();
  79. m_cursor2.endEditBlock();
  80. }
  81. bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  82. {
  83. if (m_handler) return m_handler->doStart(name, attributes);
  84. switch (toKeyword(name)) {
  85. case Descr : m_handler = new DescrHandler(name); break;
  86. case Body : m_handler = new BodyHandler(m_empty ? m_cursor1 : m_cursor2, name); m_empty = false; break;
  87. case Binary : m_handler = new BinaryHandler(m_document, name, attributes); break;
  88. default:
  89. qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
  90. m_handler = new BaseHandler(name);
  91. }
  92. return true;
  93. }
  94. //---------------------------------------------------------------------------
  95. // Fb2Handler::DescrHandler
  96. //---------------------------------------------------------------------------
  97. bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  98. {
  99. Q_UNUSED(name);
  100. Q_UNUSED(attributes);
  101. return true;
  102. }
  103. bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
  104. {
  105. Q_UNUSED(name);
  106. Q_UNUSED(exit);
  107. if (name == "description") m_closed = true;
  108. return true;
  109. }
  110. //---------------------------------------------------------------------------
  111. // Fb2Handler::TextHandler
  112. //---------------------------------------------------------------------------
  113. Fb2Handler::TextHandler::TextHandler(Fb2TextCursor &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(Fb2TextCursor &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 (m_closed && 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 format1;
  249. format1.setBorder(0);
  250. format1.setCellPadding(4);
  251. format1.setCellSpacing(4);
  252. format1.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  253. m_table = cursor().insertTable(1, 1, format1);
  254. QTextTableCellFormat format2;
  255. format2.setBackground(Qt::darkGreen);
  256. format2.setForeground(Qt::white);
  257. m_table->cellAt(cursor()).setFormat(format2);
  258. }
  259. bool Fb2Handler::TitleHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  260. {
  261. if (m_handler) return m_handler->doStart(name, attributes);
  262. if (m_feed) cursor().insertBlock(); else m_feed = true;
  263. switch (toKeyword(name)) {
  264. case Paragraph : m_handler = new BlockHandler(*this, name, attributes); break;
  265. case Emptyline : m_handler = new BlockHandler(*this, name, attributes); break;
  266. default: m_handler = new BaseHandler(name); break;
  267. }
  268. return true;
  269. }
  270. bool Fb2Handler::TitleHandler::doEnd(const QString &name, bool & exit)
  271. {
  272. bool ok = BaseHandler::doEnd(name, exit);
  273. if (m_closed && m_frame) cursor().setPosition(m_frame->lastPosition());
  274. return ok;
  275. }
  276. //---------------------------------------------------------------------------
  277. // Fb2Handler::PoemHandler
  278. //---------------------------------------------------------------------------
  279. FB2_BEGIN_KEYHASH(PoemHandler)
  280. insert("title", Title);
  281. insert("epigraph", Epigraph);
  282. insert("stanza", Stanza);
  283. insert("author", Author);
  284. insert("date", Date);
  285. FB2_END_KEYHASH
  286. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  287. : TextHandler(parent, name)
  288. , m_frame(cursor().currentFrame())
  289. , m_table(NULL)
  290. , m_feed(false)
  291. {
  292. Q_UNUSED(attributes);
  293. QTextTableFormat format;
  294. format.setBorder(1);
  295. format.setCellPadding(4);
  296. format.setCellSpacing(4);
  297. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  298. m_table = cursor().insertTable(1, 1, format);
  299. }
  300. bool Fb2Handler::PoemHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  301. {
  302. if (m_handler) return m_handler->doStart(name, attributes);
  303. Keyword keyword = toKeyword(name);
  304. switch (keyword) {
  305. case Title:
  306. case Epigraph:
  307. case Stanza:
  308. case Author:
  309. if (m_feed) m_table->appendRows(1);
  310. m_feed = true;
  311. m_handler = new StanzaHandler(*this, name, attributes);
  312. break;
  313. default:
  314. m_handler = new BaseHandler(name);
  315. }
  316. return true;
  317. }
  318. bool Fb2Handler::PoemHandler::doEnd(const QString &name, bool & exit)
  319. {
  320. bool ok = BaseHandler::doEnd(name, exit);
  321. if (m_closed && m_frame) cursor().setPosition(m_frame->lastPosition());
  322. return ok;
  323. }
  324. //---------------------------------------------------------------------------
  325. // Fb2Handler::StanzaHandler
  326. //---------------------------------------------------------------------------
  327. FB2_BEGIN_KEYHASH(StanzaHandler)
  328. insert("title", Title);
  329. insert("subtitle", Subtitle);
  330. insert("v", Verse);
  331. FB2_END_KEYHASH
  332. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  333. : TextHandler(parent, name)
  334. , m_feed(false)
  335. {
  336. Q_UNUSED(attributes);
  337. }
  338. bool Fb2Handler::StanzaHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  339. {
  340. if (m_handler) return m_handler->doStart(name, attributes);
  341. Keyword keyword = toKeyword(name);
  342. switch (keyword) {
  343. case Title:
  344. case Subtitle:
  345. case Verse:
  346. if (m_feed) cursor().insertBlock();
  347. m_feed = true;
  348. default: ;
  349. }
  350. switch (keyword) {
  351. case Title:
  352. case Subtitle:
  353. m_handler = new TitleHandler(*this, name, attributes); break;
  354. case Verse:
  355. m_handler = new BlockHandler(*this, name, attributes); break;
  356. default:
  357. m_handler = new BaseHandler(name); break;
  358. }
  359. return true;
  360. }
  361. //---------------------------------------------------------------------------
  362. // Fb2Handler::BlockHandler
  363. //---------------------------------------------------------------------------
  364. FB2_BEGIN_KEYHASH(BlockHandler)
  365. insert("strong" , Strong);
  366. insert("emphasis" , Emphasis);
  367. insert("style" , Style);
  368. insert("a" , Anchor);
  369. insert("strikethrough" , Strikethrough);
  370. insert("sub" , Sub);
  371. insert("sup" , Sup);
  372. insert("code" , Code);
  373. insert("image" , Image);
  374. FB2_END_KEYHASH
  375. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  376. : TextHandler(parent, name)
  377. {
  378. Q_UNUSED(attributes);
  379. QTextBlockFormat blockFormat;
  380. blockFormat.setTopMargin(4);
  381. blockFormat.setBottomMargin(4);
  382. cursor().setBlockFormat(blockFormat);
  383. }
  384. bool Fb2Handler::BlockHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  385. {
  386. Q_UNUSED(attributes);
  387. if (m_handler) return m_handler->doStart(name, attributes);
  388. switch (toKeyword(name)) {
  389. default: m_handler = new BaseHandler(name); break;
  390. }
  391. return true;
  392. }
  393. bool Fb2Handler::BlockHandler::doText(const QString &text)
  394. {
  395. if (m_handler) return m_handler->doText(text);
  396. cursor().insertText(text);
  397. return true;
  398. }
  399. //---------------------------------------------------------------------------
  400. // Fb2Handler::ImageHandler
  401. //---------------------------------------------------------------------------
  402. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  403. : TextHandler(parent, name)
  404. {
  405. QString image = Value(attributes, "href");
  406. while (image.left(1) == "#") image.remove(0, 1);
  407. if (!image.isEmpty()) cursor().insertImage(image);
  408. }
  409. bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  410. {
  411. Q_UNUSED(name);
  412. Q_UNUSED(attributes);
  413. return false;
  414. }
  415. //---------------------------------------------------------------------------
  416. // Fb2Handler::BinaryHandler
  417. //---------------------------------------------------------------------------
  418. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  419. : BaseHandler(name)
  420. , m_document(document)
  421. , m_file(Value(attributes, "id"))
  422. {
  423. }
  424. bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  425. {
  426. Q_UNUSED(name);
  427. Q_UNUSED(attributes);
  428. return false;
  429. }
  430. bool Fb2Handler::BinaryHandler::doText(const QString &text)
  431. {
  432. m_text += text;
  433. return true;
  434. }
  435. bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
  436. {
  437. Q_UNUSED(name);
  438. QByteArray in; in.append(m_text);
  439. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  440. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  441. m_closed = true;
  442. return true;
  443. }
  444. //---------------------------------------------------------------------------
  445. // Fb2Handler
  446. //---------------------------------------------------------------------------
  447. Fb2Handler::Fb2Handler(Fb2MainDocument & document)
  448. : m_document(document)
  449. , m_handler(NULL)
  450. {
  451. document.clear();
  452. m_document.child().clear();
  453. }
  454. Fb2Handler::~Fb2Handler()
  455. {
  456. m_document.clearUndoRedoStacks();
  457. m_document.child().clearUndoRedoStacks();
  458. m_document.setModified(false);
  459. m_document.child().setModified(false);
  460. if (m_handler) delete m_handler;
  461. }
  462. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  463. {
  464. Q_UNUSED(namespaceURI);
  465. Q_UNUSED(localName);
  466. const QString name = qName.toLower();
  467. if (m_handler) return m_handler->doStart(name, attributes);
  468. if (name == "fictionbook") {
  469. m_handler = new RootHandler(m_document, name);
  470. return true;
  471. } else {
  472. m_error = QObject::tr("The file is not an FB2 file.");
  473. return false;
  474. }
  475. }
  476. static bool isWhiteSpace(const QString &str)
  477. {
  478. return str.simplified().isEmpty();
  479. }
  480. bool Fb2Handler::characters(const QString &str)
  481. {
  482. QString s = str.simplified();
  483. if (s.isEmpty()) return true;
  484. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  485. if (isWhiteSpace(str.right(1))) s.append(" ");
  486. return m_handler && m_handler->doText(s);
  487. }
  488. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  489. {
  490. Q_UNUSED(namespaceURI);
  491. Q_UNUSED(localName);
  492. bool exit = false;
  493. return m_handler && m_handler->doEnd(qName.toLower(), exit);
  494. }
  495. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  496. {
  497. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  498. .arg(exception.lineNumber())
  499. .arg(exception.columnNumber())
  500. .arg(exception.message());
  501. return false;
  502. }
  503. QString Fb2Handler::errorString() const
  504. {
  505. return m_error;
  506. }
  507. #undef FB2_BEGIN_KEYHASH
  508. #undef FB2_END_KEYHASH