fb2read.cpp 17 KB

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