fb2read.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. #include <QtGui>
  2. #include <QTextEdit>
  3. #include <QtDebug>
  4. #include "fb2read.h"
  5. //---------------------------------------------------------------------------
  6. // Fb2ReadThread
  7. //---------------------------------------------------------------------------
  8. Fb2ReadThread::Fb2ReadThread(QObject *parent, const QString &filename)
  9. : QThread(parent)
  10. , m_filename(filename)
  11. , m_document(NULL)
  12. , m_abort(false)
  13. {
  14. }
  15. Fb2ReadThread::~Fb2ReadThread()
  16. {
  17. stop();
  18. wait();
  19. if (m_document) delete m_document;
  20. }
  21. QTextDocument * Fb2ReadThread::doc()
  22. {
  23. QMutexLocker locker(&mutex);
  24. Q_UNUSED(locker);
  25. return m_document ? m_document->clone() : NULL;
  26. }
  27. const QString & Fb2ReadThread::file()
  28. {
  29. QMutexLocker locker(&mutex);
  30. Q_UNUSED(locker);
  31. return m_filename;
  32. }
  33. void Fb2ReadThread::stop()
  34. {
  35. QMutexLocker locker(&mutex);
  36. Q_UNUSED(locker);
  37. m_abort = true;
  38. }
  39. void Fb2ReadThread::run()
  40. {
  41. QFile file(m_filename);
  42. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  43. qCritical() << tr("Cannot read file %1:\n%2.").arg(m_filename).arg(file.errorString());
  44. return;
  45. }
  46. m_document = new QTextDocument();
  47. Fb2Handler handler(*m_document);
  48. QXmlSimpleReader reader;
  49. reader.setContentHandler(&handler);
  50. reader.setErrorHandler(&handler);
  51. QXmlInputSource source(&file);
  52. bool ok = reader.parse(source);
  53. m_document->moveToThread(QApplication::instance()->thread());
  54. if (ok) emit sendDocument();
  55. }
  56. //---------------------------------------------------------------------------
  57. // Fb2Handler::BaseHandler
  58. //---------------------------------------------------------------------------
  59. #define FB2_BEGIN_KEYHASH(x) \
  60. Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
  61. { \
  62. static const KeywordHash map; \
  63. KeywordHash::const_iterator i = map.find(name); \
  64. return i == map.end() ? None : i.value(); \
  65. } \
  66. Fb2Handler::x::KeywordHash::KeywordHash() {
  67. #define FB2_END_KEYHASH }
  68. static QString Value(const QXmlAttributes &attributes, const QString &name)
  69. {
  70. int count = attributes.count();
  71. for (int i = 0; i < count; i++ ) {
  72. if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
  73. return attributes.value(i);
  74. }
  75. }
  76. return QString();
  77. }
  78. Fb2Handler::BaseHandler::~BaseHandler()
  79. {
  80. if (m_handler) delete m_handler;
  81. }
  82. bool Fb2Handler::BaseHandler::doStart(const QString &name, const QXmlAttributes &attributes)
  83. {
  84. if (m_handler) return m_handler->doStart(name, attributes);
  85. m_handler = NewTag(name, attributes); if (m_handler) return true;
  86. // qCritical() << QObject::tr("Unknown XML child tag: <%1> <%2>").arg(m_name).arg(name);
  87. m_handler = new BaseHandler(name);
  88. return true;
  89. }
  90. bool Fb2Handler::BaseHandler::doText(const QString &text)
  91. {
  92. if (m_handler) m_handler->doText(text); else TxtTag(text);
  93. return true;
  94. }
  95. bool Fb2Handler::BaseHandler::doEnd(const QString &name, bool & exists)
  96. {
  97. if (m_handler) {
  98. bool found = exists || name == m_name;
  99. m_handler->doEnd(name, found);
  100. if (m_handler->m_closed) { delete m_handler; m_handler = NULL; }
  101. if (found) { exists = true; return true; }
  102. }
  103. bool found = name == m_name;
  104. if (!found) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
  105. m_closed = found || exists;
  106. if (m_closed) EndTag(m_name);
  107. exists = found;
  108. return true;
  109. }
  110. //---------------------------------------------------------------------------
  111. // Fb2Handler::RootHandler
  112. //---------------------------------------------------------------------------
  113. FB2_BEGIN_KEYHASH(RootHandler)
  114. insert("stylesheet", Style);
  115. insert("description", Descr);
  116. insert("body", Body);
  117. insert("binary", Binary);
  118. FB2_END_KEYHASH
  119. Fb2Handler::RootHandler::RootHandler(QTextDocument &document, const QString &name)
  120. : BaseHandler(name)
  121. , m_document(document)
  122. , m_cursor(&document, false)
  123. , m_empty(true)
  124. {
  125. m_cursor.beginEditBlock();
  126. }
  127. Fb2Handler::RootHandler::~RootHandler()
  128. {
  129. QTextBlockFormat blockFormat;
  130. blockFormat.setTopMargin(6);
  131. blockFormat.setBottomMargin(6);
  132. m_cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
  133. m_cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  134. m_cursor.mergeBlockFormat(blockFormat);
  135. m_cursor.endEditBlock();
  136. }
  137. Fb2Handler::BaseHandler * Fb2Handler::RootHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  138. {
  139. switch (toKeyword(name)) {
  140. case Body : { BaseHandler * handler = new BodyHandler(m_cursor, name); m_empty = false; return handler; }
  141. case Descr : return new DescrHandler(m_cursor, name);
  142. case Binary : return new BinaryHandler(m_document, name, attributes);
  143. default: return NULL;
  144. }
  145. }
  146. //---------------------------------------------------------------------------
  147. // Fb2Handler::DescrHandler
  148. //---------------------------------------------------------------------------
  149. FB2_BEGIN_KEYHASH(DescrHandler)
  150. insert( "title-info" , Title );
  151. insert( "publish-info" , Publish );
  152. FB2_END_KEYHASH
  153. Fb2Handler::BaseHandler * Fb2Handler::DescrHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  154. {
  155. Q_UNUSED(attributes);
  156. switch (toKeyword(name)) {
  157. case Title : return new HeaderHandler(*this, name);
  158. case Publish : return new BaseHandler(name);
  159. default: return NULL;
  160. }
  161. }
  162. //---------------------------------------------------------------------------
  163. // Fb2Handler::HeaderHandler
  164. //---------------------------------------------------------------------------
  165. FB2_BEGIN_KEYHASH(HeaderHandler)
  166. insert( "book-title" , Title );
  167. insert( "author" , Author );
  168. insert( "sequence" , Sequence );
  169. insert( "genre" , Genre );
  170. insert( "lang" , Lang );
  171. insert( "annotation" , Annot );
  172. insert( "coverpage" , Cover );
  173. FB2_END_KEYHASH
  174. Fb2Handler::BaseHandler * Fb2Handler::HeaderHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  175. {
  176. Q_UNUSED(attributes);
  177. switch (toKeyword(name)) {
  178. case Title : return new BaseHandler(name);
  179. case Annot : return new BaseHandler(name);
  180. default: return NULL;
  181. }
  182. }
  183. //---------------------------------------------------------------------------
  184. // Fb2Handler::TextHandler
  185. //---------------------------------------------------------------------------
  186. Fb2Handler::TextHandler::TextHandler(Fb2TextCursor &cursor, const QString &name)
  187. : BaseHandler(name)
  188. , m_cursor(cursor)
  189. , m_blockFormat(m_cursor.blockFormat())
  190. , m_charFormat(m_cursor.charFormat())
  191. {
  192. }
  193. Fb2Handler::TextHandler::TextHandler(TextHandler &parent, const QString &name)
  194. : BaseHandler(name)
  195. , m_cursor(parent.m_cursor)
  196. , m_blockFormat(m_cursor.blockFormat())
  197. , m_charFormat(m_cursor.charFormat())
  198. {
  199. }
  200. Fb2Handler::BaseHandler * Fb2Handler::TextHandler::NewTag(const QString & name, const QXmlAttributes &attributes)
  201. {
  202. return BaseHandler::NewTag(name, attributes);
  203. }
  204. void Fb2Handler::TextHandler::EndTag(const QString &name)
  205. {
  206. Q_UNUSED(name);
  207. cursor().setCharFormat(m_charFormat);
  208. }
  209. //---------------------------------------------------------------------------
  210. // Fb2Handler::BodyHandler
  211. //---------------------------------------------------------------------------
  212. FB2_BEGIN_KEYHASH(BodyHandler)
  213. insert("image", Image);
  214. insert("title", Title);
  215. insert("epigraph", Epigraph);
  216. insert("section", Section);
  217. insert("p", Paragraph);
  218. insert("poem", Poem);
  219. insert("stanza", Stanza);
  220. insert("v", Verse);
  221. FB2_END_KEYHASH
  222. Fb2Handler::BodyHandler::BodyHandler(Fb2TextCursor &cursor, const QString &name)
  223. : TextHandler(cursor, name)
  224. , m_frame(cursor.currentFrame())
  225. , m_feed(false)
  226. {
  227. QTextFrameFormat frameFormat;
  228. frameFormat.setBorder(1);
  229. frameFormat.setPadding(8);
  230. frameFormat.setTopMargin(4);
  231. frameFormat.setBottomMargin(4);
  232. cursor.insertFrame(frameFormat);
  233. }
  234. Fb2Handler::BaseHandler * Fb2Handler::BodyHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  235. {
  236. switch (toKeyword(name)) {
  237. case Paragraph : return new BlockHandler(*this, name, attributes);
  238. case Image : return new ImageHandler(*this, name, attributes);
  239. case Section : return new SectionHandler(*this, name, attributes);
  240. case Title : return new TitleHandler(*this, name, attributes);
  241. case Poem : return new SectionHandler(*this, name, attributes);
  242. case Stanza : return new SectionHandler(*this, name, attributes);
  243. default: return NULL;
  244. }
  245. }
  246. void Fb2Handler::BodyHandler::EndTag(const QString &name)
  247. {
  248. Q_UNUSED(name);
  249. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  250. }
  251. //---------------------------------------------------------------------------
  252. // Fb2Handler::SectionHandler
  253. //---------------------------------------------------------------------------
  254. FB2_BEGIN_KEYHASH(SectionHandler)
  255. insert("title", Title);
  256. insert("epigraph", Epigraph);
  257. insert("image", Image);
  258. insert("annotation", Annotation);
  259. insert("section", Section);
  260. insert("p", Paragraph);
  261. insert("poem", Poem);
  262. insert("subtitle", Subtitle);
  263. insert("cite", Cite);
  264. insert("empty-line", Emptyline);
  265. insert("table", Table);
  266. FB2_END_KEYHASH
  267. Fb2Handler::SectionHandler::SectionHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  268. : TextHandler(parent, name)
  269. , m_frame(cursor().currentFrame())
  270. , m_feed(false)
  271. {
  272. Q_UNUSED(attributes);
  273. QTextFrameFormat frameFormat;
  274. frameFormat.setBorder(1);
  275. frameFormat.setPadding(8);
  276. frameFormat.setTopMargin(4);
  277. frameFormat.setBottomMargin(4);
  278. if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
  279. cursor().insertFrame(frameFormat);
  280. }
  281. Fb2Handler::BaseHandler * Fb2Handler::SectionHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  282. {
  283. Keyword keyword = toKeyword(name);
  284. switch (keyword) {
  285. case Paragraph:
  286. case Emptyline:
  287. case Image:
  288. case Title:
  289. if (m_feed) cursor().insertBlock();
  290. m_feed = true;
  291. break;
  292. default:
  293. m_feed = false;
  294. }
  295. switch (keyword) {
  296. case Emptyline : return new BaseHandler(name); break;
  297. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  298. case Section : return new SectionHandler(*this, name, attributes); break;
  299. case Title : return new TitleHandler(*this, name, attributes); break;
  300. case Poem : return new PoemHandler(*this, name, attributes); break;
  301. case Image : return new ImageHandler(*this, name, attributes); break;
  302. default : return new UnknowHandler(*this, name); break;
  303. }
  304. ;
  305. }
  306. void Fb2Handler::SectionHandler::EndTag(const QString &name)
  307. {
  308. Q_UNUSED(name);
  309. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  310. }
  311. //---------------------------------------------------------------------------
  312. // Fb2Handler::TitleHandler
  313. //---------------------------------------------------------------------------
  314. FB2_BEGIN_KEYHASH(TitleHandler)
  315. insert("p", Paragraph);
  316. insert("empty-line", Emptyline);
  317. FB2_END_KEYHASH
  318. Fb2Handler::TitleHandler::TitleHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  319. : TextHandler(parent, name)
  320. , m_frame(cursor().currentFrame())
  321. , m_table(NULL)
  322. , m_feed(false)
  323. {
  324. Q_UNUSED(attributes);
  325. QTextTableFormat tableFormat;
  326. tableFormat.setBorder(0);
  327. tableFormat.setCellPadding(4);
  328. tableFormat.setCellSpacing(0);
  329. tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
  330. m_table = cursor().insertTable(1, 1, tableFormat);
  331. QTextTableCellFormat cellFormat;
  332. cellFormat.setBackground(Qt::darkGreen);
  333. cellFormat.setForeground(Qt::white);
  334. m_table->cellAt(cursor()).setFormat(cellFormat);
  335. QTextBlockFormat blockFormat;
  336. blockFormat.setAlignment(Qt::AlignHCenter);
  337. cursor().mergeBlockFormat(blockFormat);
  338. }
  339. Fb2Handler::BaseHandler * Fb2Handler::TitleHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  340. {
  341. if (m_feed) cursor().insertBlock(); else m_feed = true;
  342. switch (toKeyword(name)) {
  343. case Paragraph : return new BlockHandler(*this, name, attributes); break;
  344. case Emptyline : return new BlockHandler(*this, name, attributes); break;
  345. default : return new UnknowHandler(*this, name); break;
  346. }
  347. }
  348. void Fb2Handler::TitleHandler::EndTag(const QString &name)
  349. {
  350. Q_UNUSED(name);
  351. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  352. }
  353. //---------------------------------------------------------------------------
  354. // Fb2Handler::PoemHandler
  355. //---------------------------------------------------------------------------
  356. FB2_BEGIN_KEYHASH(PoemHandler)
  357. insert("title", Title);
  358. insert("epigraph", Epigraph);
  359. insert("stanza", Stanza);
  360. insert("author", Author);
  361. insert("date", Date);
  362. FB2_END_KEYHASH
  363. Fb2Handler::PoemHandler::PoemHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  364. : TextHandler(parent, name)
  365. , m_frame(cursor().currentFrame())
  366. , m_table(NULL)
  367. , m_feed(false)
  368. {
  369. Q_UNUSED(attributes);
  370. QTextTableFormat format;
  371. format.setBorder(1);
  372. format.setCellPadding(4);
  373. format.setCellSpacing(4);
  374. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
  375. m_table = cursor().insertTable(1, 1, format);
  376. }
  377. Fb2Handler::BaseHandler * Fb2Handler::PoemHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  378. {
  379. switch (toKeyword(name)) {
  380. case Title:
  381. case Epigraph:
  382. case Stanza:
  383. case Author:
  384. if (m_feed) m_table->appendRows(1);
  385. m_feed = true;
  386. return new StanzaHandler(*this, name, attributes);
  387. default:
  388. return NULL;
  389. }
  390. }
  391. void Fb2Handler::PoemHandler::EndTag(const QString &name)
  392. {
  393. Q_UNUSED(name);
  394. if (m_frame) cursor().setPosition(m_frame->lastPosition());
  395. }
  396. //---------------------------------------------------------------------------
  397. // Fb2Handler::StanzaHandler
  398. //---------------------------------------------------------------------------
  399. FB2_BEGIN_KEYHASH(StanzaHandler)
  400. insert("title", Title);
  401. insert("subtitle", Subtitle);
  402. insert("v", Verse);
  403. FB2_END_KEYHASH
  404. Fb2Handler::StanzaHandler::StanzaHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  405. : TextHandler(parent, name)
  406. , m_feed(false)
  407. {
  408. Q_UNUSED(attributes);
  409. }
  410. Fb2Handler::BaseHandler * Fb2Handler::StanzaHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  411. {
  412. Keyword keyword = toKeyword(name);
  413. switch (keyword) {
  414. case Title:
  415. case Subtitle:
  416. case Verse:
  417. if (m_feed) cursor().insertBlock();
  418. m_feed = true;
  419. default: ;
  420. }
  421. switch (keyword) {
  422. case Title:
  423. case Subtitle : return new TitleHandler(*this, name, attributes);
  424. case Verse : return new BlockHandler(*this, name, attributes);
  425. default: return NULL;
  426. }
  427. }
  428. //---------------------------------------------------------------------------
  429. // Fb2Handler::BlockHandler
  430. //---------------------------------------------------------------------------
  431. FB2_BEGIN_KEYHASH(BlockHandler)
  432. insert("strong" , Strong);
  433. insert("emphasis" , Emphasis);
  434. insert("style" , Style);
  435. insert("a" , Anchor);
  436. insert("strikethrough" , Strike);
  437. insert("sub" , Sub);
  438. insert("sup" , Sup);
  439. insert("code" , Code);
  440. insert("image" , Image);
  441. FB2_END_KEYHASH
  442. Fb2Handler::BlockHandler::BlockHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes, QTextCharFormat * format)
  443. : TextHandler(parent, name)
  444. {
  445. Q_UNUSED(attributes);
  446. if (format) cursor().mergeCharFormat(*format);
  447. }
  448. Fb2Handler::BaseHandler * Fb2Handler::BlockHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  449. {
  450. Q_UNUSED(attributes);
  451. QTextCharFormat format;
  452. switch (toKeyword(name)) {
  453. case Image : return new ImageHandler(*this, name, attributes);
  454. case Strong : format.setFontWeight(QFont::Bold); break;
  455. case Emphasis : format.setFontItalic(true); break;
  456. case Strike : format.setFontStrikeOut(true); break;
  457. case Sub : format.setVerticalAlignment(QTextCharFormat::AlignSubScript); break;
  458. case Sup : format.setVerticalAlignment(QTextCharFormat::AlignSuperScript); break;
  459. case Anchor : {
  460. QString href = Value(attributes, "href");
  461. if (!href.isEmpty()) {
  462. format.setAnchorHref(href);
  463. format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
  464. }
  465. } break;
  466. default:
  467. return new UnknowHandler(*this, name); break;
  468. }
  469. return new BlockHandler(*this, name, attributes, &format);
  470. }
  471. void Fb2Handler::BlockHandler::TxtTag(const QString &text)
  472. {
  473. cursor().insertText(text);
  474. }
  475. //---------------------------------------------------------------------------
  476. // Fb2Handler::ImageHandler
  477. //---------------------------------------------------------------------------
  478. Fb2Handler::ImageHandler::ImageHandler(TextHandler &parent, const QString &name, const QXmlAttributes &attributes)
  479. : TextHandler(parent, name)
  480. {
  481. QString image = Value(attributes, "href");
  482. while (image.left(1) == "#") image.remove(0, 1);
  483. if (!image.isEmpty()) cursor().insertImage(image);
  484. }
  485. Fb2Handler::BaseHandler * Fb2Handler::ImageHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  486. {
  487. Q_UNUSED(name);
  488. Q_UNUSED(attributes);
  489. return false;
  490. }
  491. //---------------------------------------------------------------------------
  492. // Fb2Handler::UnknowHandler
  493. //---------------------------------------------------------------------------
  494. Fb2Handler::UnknowHandler::UnknowHandler(TextHandler &parent, const QString &name)
  495. : TextHandler(parent, name)
  496. , m_parent(parent)
  497. {
  498. }
  499. Fb2Handler::BaseHandler * Fb2Handler::UnknowHandler::NewTag(const QString &name, const QXmlAttributes &attributes)
  500. {
  501. return m_parent.NewTag(name, attributes);
  502. }
  503. void Fb2Handler::UnknowHandler::TxtTag(const QString &text)
  504. {
  505. cursor().insertText(text);
  506. }
  507. //---------------------------------------------------------------------------
  508. // Fb2Handler::BinaryHandler
  509. //---------------------------------------------------------------------------
  510. Fb2Handler::BinaryHandler::BinaryHandler(QTextDocument &document, const QString &name, const QXmlAttributes &attributes)
  511. : BaseHandler(name)
  512. , m_document(document)
  513. , m_file(Value(attributes, "id"))
  514. {
  515. }
  516. void Fb2Handler::BinaryHandler::TxtTag(const QString &text)
  517. {
  518. m_text += text;
  519. }
  520. void Fb2Handler::BinaryHandler::EndTag(const QString &name)
  521. {
  522. Q_UNUSED(name);
  523. QByteArray in; in.append(m_text);
  524. QImage img = QImage::fromData(QByteArray::fromBase64(in));
  525. if (!m_file.isEmpty()) m_document.addResource(QTextDocument::ImageResource, QUrl(m_file), img);
  526. }
  527. //---------------------------------------------------------------------------
  528. // Fb2Handler
  529. //---------------------------------------------------------------------------
  530. Fb2Handler::Fb2Handler(QTextDocument & document)
  531. : m_document(document)
  532. , m_handler(NULL)
  533. {
  534. document.clear();
  535. }
  536. Fb2Handler::~Fb2Handler()
  537. {
  538. if (m_handler) delete m_handler;
  539. }
  540. bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
  541. {
  542. Q_UNUSED(namespaceURI);
  543. Q_UNUSED(localName);
  544. const QString name = qName.toLower();
  545. if (m_handler) return m_handler->doStart(name, attributes);
  546. qCritical() << name;
  547. if (name == "fictionbook") {
  548. m_handler = new RootHandler(m_document, name);
  549. return true;
  550. } else {
  551. m_error = QObject::tr("The file is not an FB2 file.");
  552. return false;
  553. }
  554. }
  555. static bool isWhiteSpace(const QString &str)
  556. {
  557. return str.simplified().isEmpty();
  558. }
  559. bool Fb2Handler::characters(const QString &str)
  560. {
  561. QString s = str.simplified();
  562. if (s.isEmpty()) return true;
  563. if (isWhiteSpace(str.left(1))) s.prepend(" ");
  564. if (isWhiteSpace(str.right(1))) s.append(" ");
  565. return m_handler && m_handler->doText(s);
  566. }
  567. bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
  568. {
  569. Q_UNUSED(namespaceURI);
  570. Q_UNUSED(localName);
  571. bool found = false;
  572. return m_handler && m_handler->doEnd(qName.toLower(), found);
  573. }
  574. bool Fb2Handler::fatalError(const QXmlParseException &exception)
  575. {
  576. qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
  577. .arg(exception.lineNumber())
  578. .arg(exception.columnNumber())
  579. .arg(exception.message());
  580. return false;
  581. }
  582. QString Fb2Handler::errorString() const
  583. {
  584. return m_error;
  585. }
  586. #undef FB2_BEGIN_KEYHASH
  587. #undef FB2_END_KEYHASH