fb2read.cpp 17 KB

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