fb2read.cpp 17 KB

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