123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- #include <QtGui>
- #include <QTextEdit>
- #include <QtDebug>
- #include "fb2read.h"
- #define FB2KEYHASH(x) \
- Fb2Handler::x::Keyword Fb2Handler::x::toKeyword(const QString &name) \
- { \
- static KeywordHash map; \
- KeywordHash::const_iterator i = map.find(name); \
- return i == map.end() ? None : i.value(); \
- } \
- static QString Value(const QXmlAttributes &attributes, const QString &name)
- {
- int count = attributes.count();
- for (int i = 0; i < count; i++ ) {
- if (attributes.localName(i).compare(name, Qt::CaseInsensitive) == 0) {
- return attributes.value(i);
- }
- }
- return QString();
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::ContentHandler
- //---------------------------------------------------------------------------
- Fb2Handler::ContentHandler::ContentHandler(Fb2Handler &owner, const QString &name)
- : m_owner(owner)
- , m_handler(NULL)
- , m_name(name)
- {
- }
- Fb2Handler::ContentHandler::ContentHandler(ContentHandler &parent, const QString &name)
- : m_owner(parent.m_owner)
- , m_handler(NULL)
- , m_name(name)
- {
- }
- Fb2Handler::ContentHandler::~ContentHandler()
- {
- if (m_handler) delete m_handler;
- }
- bool Fb2Handler::ContentHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- m_handler = new ContentHandler(*this, name);
- }
- bool Fb2Handler::ContentHandler::doText(const QString &text)
- {
- if (m_handler) return m_handler->doText(text);
- return true;
- }
- bool Fb2Handler::ContentHandler::doEnd(const QString &name, bool & exit)
- {
- if (m_handler) {
- bool ok = m_handler->doEnd(name, exit);
- if (exit) {
- delete m_handler;
- m_handler = NULL;
- exit = false;
- }
- return ok;
- } else {
- if (name != m_name) qCritical() << QObject::tr("Conglict XML tags: <%1> - </%2>").arg(m_name).arg(name);
- exit = true;
- return true;
- }
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::RootHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(RootHandler)
- Fb2Handler::RootHandler::KeywordHash::KeywordHash()
- {
- insert("stylesheet", Style);
- insert("description", Descr);
- insert("body", Body);
- insert("binary", Binary);
- }
- Fb2Handler::RootHandler::RootHandler(Fb2Handler & owner, const QString &name)
- : ContentHandler(owner, name)
- {
- }
- bool Fb2Handler::RootHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- switch (toKeyword(name)) {
- case Descr : return m_handler = new DescrHandler(*this, name);
- case Body : return m_handler = new BodyHandler(*this, name);
- case Binary : return m_handler = new BinaryHandler(*this, name, attributes);
- default:
- qCritical() << QObject::tr("Unknown XML tag: %1").arg(name);
- return m_handler = new ContentHandler(*this, name);
- }
- return false;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::DescrHandler
- //---------------------------------------------------------------------------
- bool Fb2Handler::DescrHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- Q_UNUSED(name);
- Q_UNUSED(attributes);
- return true;
- }
- bool Fb2Handler::DescrHandler::doEnd(const QString &name, bool & exit)
- {
- Q_UNUSED(name);
- if (name == "description") exit = true;
- return true;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::BodyHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(BodyHandler)
- Fb2Handler::BodyHandler::KeywordHash::KeywordHash()
- {
- insert("image", Image);
- insert("title", Title);
- insert("epigraph", Epigraph);
- insert("section", Section);
- insert("p", Paragraph);
- insert("poem", Poem);
- insert("stanza", Stanza);
- insert("v", Verse);
- }
- Fb2Handler::BodyHandler::BodyHandler(ContentHandler &parent, const QString &name)
- : ContentHandler(parent, name)
- , m_feed(true)
- {
- QTextBlockFormat blockFormat;
- blockFormat.setTopMargin(4);
- blockFormat.setBottomMargin(4);
- cursor().setBlockFormat(blockFormat);
- }
- bool Fb2Handler::BodyHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- switch (toKeyword(name)) {
- case Paragraph : m_handler = new TextHandler(*this, name, attributes); break;
- case Image : m_handler = new ImageHandler(*this, name, attributes); break;
- case Section : m_handler = new SectionHandler(*this, name, attributes); break;
- case Title : m_handler = new TitleHandler(*this, name, attributes); break;
- case Poem : m_handler = new SectionHandler(*this, name, attributes); break;
- case Stanza : m_handler = new SectionHandler(*this, name, attributes); break;
- default : m_handler = new ContentHandler(*this, name); break;
- }
- return true;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::SectionHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(SectionHandler)
- Fb2Handler::SectionHandler::KeywordHash::KeywordHash()
- {
- insert("title", Title);
- insert("epigraph", Epigraph);
- insert("image", Image);
- insert("annotation", Annotation);
- insert("section", Section);
- insert("p", Paragraph);
- insert("poem", Poem);
- insert("subtitle", Subtitle);
- insert("cite", Cite);
- insert("empty-line", Emptyline);
- insert("table", Table);
- }
- Fb2Handler::SectionHandler::SectionHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- , m_frame(cursor().currentFrame())
- , m_feed(false)
- {
- QTextFrameFormat frameFormat;
- frameFormat.setBorder(1);
- frameFormat.setPadding(8);
- frameFormat.setTopMargin(4);
- frameFormat.setBottomMargin(4);
- if (name == "title") frameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
- cursor().insertFrame(frameFormat);
- QTextBlockFormat blockFormat;
- blockFormat.setTopMargin(4);
- blockFormat.setBottomMargin(4);
- cursor().setBlockFormat(blockFormat);
- }
- bool Fb2Handler::SectionHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- Keyword keyword = toKeyword(name);
- switch (keyword) {
- case Paragraph:
- case Emptyline:
- case Image:
- if (m_feed) cursor().insertBlock();
- m_feed = true;
- break;
- default:
- m_feed = false;
- }
- switch (keyword) {
- case Emptyline : m_handler = new ContentHandler(*this, name); break;
- case Paragraph : m_handler = new TextHandler(*this, name, attributes); break;
- case Section : m_handler = new SectionHandler(*this, name, attributes); break;
- case Title : m_handler = new TitleHandler(*this, name, attributes); break;
- case Poem : m_handler = new PoemHandler(*this, name, attributes); break;
- case Image : m_handler = new ImageHandler(*this, name, attributes); break;
- default : m_handler = new ContentHandler(*this, name); break;
- }
- return true;
- }
- bool Fb2Handler::SectionHandler::doEnd(const QString &name, bool & exit)
- {
- bool ok = ContentHandler::doEnd(name, exit);
- if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
- return ok;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::TitleHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(TitleHandler)
- Fb2Handler::TitleHandler::KeywordHash::KeywordHash()
- {
- insert("p", Paragraph);
- insert("empty-line", Emptyline);
- }
- Fb2Handler::TitleHandler::TitleHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- , m_frame(cursor().currentFrame())
- , m_table(NULL)
- , m_feed(false)
- {
- QTextTableFormat format;
- format.setBorder(0);
- format.setCellPadding(4);
- format.setCellSpacing(4);
- m_table = cursor().insertTable(1, 1, format);
- }
- bool Fb2Handler::TitleHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- if (m_feed) cursor().insertBlock(); else m_feed = true;
- switch (toKeyword(name)) {
- case Paragraph : m_handler = new TextHandler(*this, name, attributes); break;
- case Emptyline : m_handler = new TextHandler(*this, name, attributes); break;
- default: m_handler = new ContentHandler(*this, name); break;
- }
- return true;
- }
- bool Fb2Handler::TitleHandler::doEnd(const QString &name, bool & exit)
- {
- bool ok = ContentHandler::doEnd(name, exit);
- if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
- return ok;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::PoemHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(PoemHandler)
- Fb2Handler::PoemHandler::KeywordHash::KeywordHash()
- {
- insert("title", Title);
- insert("epigraph", Epigraph);
- insert("stanza", Stanza);
- insert("author", Author);
- insert("date", Date);
- }
- Fb2Handler::PoemHandler::PoemHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- , m_frame(cursor().currentFrame())
- , m_table(NULL)
- , m_feed(false)
- {
- QTextTableFormat format;
- format.setBorder(1);
- format.setCellPadding(4);
- format.setCellSpacing(4);
- format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
- m_table = cursor().insertTable(1, 1, format);
- }
- bool Fb2Handler::PoemHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- Keyword keyword = toKeyword(name);
- switch (keyword) {
- case Title:
- case Epigraph:
- case Stanza:
- case Author:
- if (m_feed) m_table->appendRows(1);
- m_feed = true;
- m_handler = new StanzaHandler(*this, name, attributes);
- break;
- default:
- break;
- }
- return true;
- }
- bool Fb2Handler::PoemHandler::doEnd(const QString &name, bool & exit)
- {
- bool ok = ContentHandler::doEnd(name, exit);
- if (exit && m_frame) cursor().setPosition(m_frame->lastPosition());
- return ok;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::StanzaHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(StanzaHandler)
- Fb2Handler::StanzaHandler::KeywordHash::KeywordHash()
- {
- insert("title", Title);
- insert("subtitle", Subtitle);
- insert("v", Verse);
- }
- Fb2Handler::StanzaHandler::StanzaHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- , m_feed(false)
- {
- }
- bool Fb2Handler::StanzaHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- Keyword keyword = toKeyword(name);
- switch (keyword) {
- // case Title:
- // case Subtitle:
- case Verse:
- if (m_feed) cursor().insertBlock();
- m_feed = true;
- m_handler = new TextHandler(*this, name, attributes); break;
- break;
- default:
- break;
- }
- return true;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::TextHandler
- //---------------------------------------------------------------------------
- FB2KEYHASH(TextHandler)
- Fb2Handler::TextHandler::TextHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- {
- QTextBlockFormat blockFormat;
- blockFormat.setTopMargin(4);
- blockFormat.setBottomMargin(4);
- cursor().setBlockFormat(blockFormat);
- }
- Fb2Handler::TextHandler::KeywordHash::KeywordHash()
- {
- insert("strong" , Strong);
- insert("emphasis" , Emphasis);
- insert("style" , Style);
- insert("a" , Anchor);
- insert("strikethrough" , Strikethrough);
- insert("sub" , Sub);
- insert("sup" , Sup);
- insert("code" , Code);
- insert("image" , Image);
- }
- bool Fb2Handler::TextHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- if (m_handler) return m_handler->doStart(name, attributes);
- switch (toKeyword(name)) {
- default : m_handler = new ContentHandler(*this, name); break;
- }
- return true;
- }
- bool Fb2Handler::TextHandler::doText(const QString &text)
- {
- if (m_handler) return m_handler->doText(text);
- cursor().insertText(text);
- return true;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::ImageHandler
- //---------------------------------------------------------------------------
- Fb2Handler::ImageHandler::ImageHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent, name)
- {
- QString image = Value(attributes, "href");
- while (image.left(1) == "#") image.remove(0, 1);
- if (!image.isEmpty()) cursor().insertImage(image);
- }
- bool Fb2Handler::ImageHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- Q_UNUSED(name);
- Q_UNUSED(attributes);
- return false;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler::BinaryHandler
- //---------------------------------------------------------------------------
- Fb2Handler::BinaryHandler::BinaryHandler(ContentHandler &parent, const QString &name, const QXmlAttributes &attributes)
- : ContentHandler(parent), m_file(Value(attributes, "id"))
- {
- }
- bool Fb2Handler::BinaryHandler::doStart(const QString &name, const QXmlAttributes &attributes)
- {
- Q_UNUSED(name);
- Q_UNUSED(attributes);
- return false;
- }
- bool Fb2Handler::BinaryHandler::doText(const QString &text)
- {
- m_text += text;
- return true;
- }
- bool Fb2Handler::BinaryHandler::doEnd(const QString &name, bool & exit)
- {
- Q_UNUSED(name);
- QByteArray in; in.append(m_text);
- QImage img = QImage::fromData(QByteArray::fromBase64(in));
- if (!m_file.isEmpty()) m_owner.document().addResource(QTextDocument::ImageResource, QUrl(m_file), img);
- exit = true;
- return true;
- }
- //---------------------------------------------------------------------------
- // Fb2Handler
- //---------------------------------------------------------------------------
- Fb2Handler::Fb2Handler(QTextDocument & document)
- : m_document(document)
- , m_cursor(&document)
- , m_handler(NULL)
- {
- m_cursor.beginEditBlock();
- document.clear();
- m_cursor.movePosition(QTextCursor::Start);
- }
- Fb2Handler::~Fb2Handler()
- {
- if (m_handler) delete m_handler;
- m_cursor.endEditBlock();
- }
- bool Fb2Handler::startElement(const QString & namespaceURI, const QString & localName, const QString &qName, const QXmlAttributes &attributes)
- {
- Q_UNUSED(namespaceURI);
- Q_UNUSED(localName);
- const QString name = qName.toLower();
- if (m_handler) return m_handler->doStart(name, attributes);
- if (name == "fictionbook") {
- m_handler = new RootHandler(*this, name);
- return true;
- } else {
- m_error = QObject::tr("The file is not an FB2 file.");
- return false;
- }
- }
- static bool isWhiteSpace(const QString &str)
- {
- return str.simplified().isEmpty();
- }
- bool Fb2Handler::characters(const QString &str)
- {
- QString s = str.simplified();
- if (s.isEmpty()) return true;
- if (isWhiteSpace(str.left(1))) s.prepend(" ");
- if (isWhiteSpace(str.right(1))) s.append(" ");
- return m_handler && m_handler->doText(s);
- }
- bool Fb2Handler::endElement(const QString & namespaceURI, const QString & localName, const QString &qName)
- {
- Q_UNUSED(namespaceURI);
- Q_UNUSED(localName);
- bool exit = false;
- return m_handler && m_handler->doEnd(qName.toLower(), exit);
- }
- bool Fb2Handler::fatalError(const QXmlParseException &exception)
- {
- qCritical() << QObject::tr("Parse error at line %1, column %2:\n%3")
- .arg(exception.lineNumber())
- .arg(exception.columnNumber())
- .arg(exception.message());
- return false;
- }
- QString Fb2Handler::errorString() const
- {
- return m_error;
- }
|