fb2read.cpp 21 KB

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