fb2read.cpp 17 KB

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