diff options
Diffstat (limited to 'fbreader/src/formats/docbook')
-rw-r--r-- | fbreader/src/formats/docbook/DocBookBookReader.cpp | 111 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookBookReader.h | 45 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookDescriptionReader.cpp | 137 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookDescriptionReader.h | 56 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookPlugin.cpp | 43 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookPlugin.h | 41 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookReader.cpp | 71 | ||||
-rw-r--r-- | fbreader/src/formats/docbook/DocBookReader.h | 95 |
8 files changed, 599 insertions, 0 deletions
diff --git a/fbreader/src/formats/docbook/DocBookBookReader.cpp b/fbreader/src/formats/docbook/DocBookBookReader.cpp new file mode 100644 index 0000000..eada90c --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookBookReader.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include <ZLInputStream.h> + +#include "DocBookBookReader.h" + +#include "../../bookmodel/BookModel.h" +#include "../../model/Paragraph.h" + +DocBookBookReader::DocBookBookReader(BookModel &model) : BookReader(model) { + setMainTextModel(); + + myReadText = false; +} + +void DocBookBookReader::characterDataHandler(const char *text, std::size_t len) { + addDataToBuffer(text, len); +} + +void DocBookBookReader::startElementHandler(int tag, const char **) { + switch (tag) { + case _SECT1: + myReadText = true; + pushKind(REGULAR); + beginContentsParagraph(); + break; + case _PARA: + if (myReadText) { + beginParagraph(); + } + break; + case _TITLE: + enterTitle(); + pushKind(SECTION_TITLE); + if (myReadText) { + beginParagraph(); + } + break; + case _EMPHASIS: + addControl(EMPHASIS, true); + break; + case _CITETITLE: + addControl(CITE, true); + break; + case _ULINK: + case _EMAIL: + addControl(CODE, true); + break; + case _BLOCKQUOTE: + pushKind(STRONG); + break; + default: + break; + } +} + +void DocBookBookReader::endElementHandler(int tag) { + switch (tag) { + case _SECT1: + myReadText = false; + popKind(); + endContentsParagraph(); + insertEndOfSectionParagraph(); + break; + case _PARA: + endParagraph(); + break; + case _TITLE: + endParagraph(); + popKind(); + endContentsParagraph(); + exitTitle(); + break; + case _EMPHASIS: + addControl(EMPHASIS, false); + break; + case _CITETITLE: + addControl(CITE, false); + break; + case _ULINK: + case _EMAIL: + addControl(CODE, false); + break; + case _BLOCKQUOTE: + popKind(); + break; + default: + break; + } +} + +void DocBookBookReader::readBook(shared_ptr<ZLInputStream> stream) { + readDocument(stream); +} diff --git a/fbreader/src/formats/docbook/DocBookBookReader.h b/fbreader/src/formats/docbook/DocBookBookReader.h new file mode 100644 index 0000000..c226184 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookBookReader.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __DOCBOOKBOOKREADER_H__ +#define __DOCBOOKBOOKREADER_H__ + +#include "DocBookReader.h" +#include "../../bookmodel/BookReader.h" + +class BookModel; + +class DocBookBookReader : public BookReader, public DocBookReader { + +public: + DocBookBookReader(BookModel &model); + ~DocBookBookReader(); + void readBook(shared_ptr<ZLInputStream> stream); + + void startElementHandler(int tag, const char **attributes); + void endElementHandler(int tag); + void characterDataHandler(const char *text, std::size_t len); + +private: + bool myReadText; +}; + +inline DocBookBookReader::~DocBookBookReader() {} + +#endif /* __DOCBOOKBOOKREADER_H__ */ diff --git a/fbreader/src/formats/docbook/DocBookDescriptionReader.cpp b/fbreader/src/formats/docbook/DocBookDescriptionReader.cpp new file mode 100644 index 0000000..bcd4ae4 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookDescriptionReader.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include <ZLInputStream.h> +#include <ZLUnicodeUtil.h> + +#include "DocBookDescriptionReader.h" + +#include "../../library/Book.h" +#include "../../library/Author.h" + +DocBookDescriptionReader::DocBookDescriptionReader(Book &book) : myBook(book) { + myReadTitle = false; + myReadAuthor = false; + for (int i = 0; i < 3; ++i) { + myReadAuthorName[i] = false; + } + myBook.setLanguage("en"); + myDepth = 0; +} + +void DocBookDescriptionReader::characterDataHandler(const char *text, std::size_t len) { + if (myReadTitle) { + myBook.setTitle(myBook.title() + std::string(text, len)); + } else { + for (int i = 0; i < 3; ++i) { + if (myReadAuthorName[i]) { + myAuthorNames[i].append(text, len); + break; + } + } + } +} + +void DocBookDescriptionReader::startElementHandler(int tag, const char **) { + ++myDepth; + switch (tag) { + case _SECT1: + myReturnCode = true; + myDoBreak = true; + break; + case _TITLE: + if (myDepth == 2) { + myReadTitle = true; + } + break; + case _AUTHOR: + if (myDepth == 3) { + myReadAuthor = true; + } + break; + case _FIRSTNAME: + if (myReadAuthor) { + myReadAuthorName[0] = true; + } + break; + case _OTHERNAME: + if (myReadAuthor) { + myReadAuthorName[1] = true; + } + break; + case _SURNAME: + if (myReadAuthor) { + myReadAuthorName[2] = true; + } + break; + default: + break; + } +} + +void DocBookDescriptionReader::endElementHandler(int tag) { + --myDepth; + switch (tag) { + case _TITLE: + myReadTitle = false; + break; + case _AUTHOR: { + ZLUnicodeUtil::utf8Trim(myAuthorNames[0]); + ZLUnicodeUtil::utf8Trim(myAuthorNames[1]); + ZLUnicodeUtil::utf8Trim(myAuthorNames[2]); + std::string fullName = myAuthorNames[0]; + if (!fullName.empty() && !myAuthorNames[1].empty()) { + fullName += ' '; + } + fullName += myAuthorNames[1]; + if (!fullName.empty() && !myAuthorNames[2].empty()) { + fullName += ' '; + } + fullName += myAuthorNames[2]; + shared_ptr<Author> author = Author::create(fullName, myAuthorNames[2]); + if (!author.isNull()) { + myBook.authors().add( author ); + } + } + myAuthorNames[0].erase(); + myAuthorNames[1].erase(); + myAuthorNames[2].erase(); + myReadAuthor = false; + break; + case _FIRSTNAME: + myReadAuthorName[0] = false; + break; + case _OTHERNAME: + myReadAuthorName[1] = false; + break; + case _SURNAME: + myReadAuthorName[2] = false; + break; + default: + break; + } +} + +bool DocBookDescriptionReader::readMetaInfo(shared_ptr<ZLInputStream> stream) { + bool code = readDocument(stream); + if (myBook.authors().empty()) { + myBook.authors().push_back( new Author() ); + } + return code; +} diff --git a/fbreader/src/formats/docbook/DocBookDescriptionReader.h b/fbreader/src/formats/docbook/DocBookDescriptionReader.h new file mode 100644 index 0000000..d9f4aa3 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookDescriptionReader.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __DOCBOOKDESCRIPTIONREADER_H__ +#define __DOCBOOKDESCRIPTIONREADER_H__ + +#include <string> + +#include "DocBookReader.h" + +class Book; + +class DocBookDescriptionReader : public DocBookReader { + +public: + DocBookDescriptionReader(Book &book); + ~DocBookDescriptionReader(); + bool readMetaInfo(shared_ptr<ZLInputStream> stream); + + void startElementHandler(int tag, const char **attributes); + void endElementHandler(int tag); + void characterDataHandler(const char *text, std::size_t len); + +private: + Book &myBook; + + bool myReturnCode; + + bool myReadTitle; + bool myReadAuthor; + bool myReadAuthorName[3]; + + std::string myAuthorNames[3]; + + int myDepth; +}; + +inline DocBookDescriptionReader::~DocBookDescriptionReader() {} + +#endif /* __DOCBOOKDESCRIPTIONREADER_H__ */ diff --git a/fbreader/src/formats/docbook/DocBookPlugin.cpp b/fbreader/src/formats/docbook/DocBookPlugin.cpp new file mode 100644 index 0000000..1b890a6 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookPlugin.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include <ZLFile.h> +#include <ZLInputStream.h> + +#include "DocBookPlugin.h" +#include "DocBookDescriptionReader.h" +#include "DocBookBookReader.h" +#include "../../library/Book.h" + +bool DocBookPlugin::acceptsFile(const std::string &extension) const { + return extension == "xml"; +} + +bool DocBookPlugin::readMetaInfo(Book &book) const { + return DocBookDescriptionReader(book).readMetaInfo(ZLFile(path).inputStream()); +} + +bool DocBookPlugin::readLanguageAndEncoding(Book &book) const { + (void)book; + return true; +} + +bool DocBookPlugin::readModel(BookModel &model) const { + return DocBookBookReader(model).readDocument(ZLFile(book.fileName()).inputStream()); +} diff --git a/fbreader/src/formats/docbook/DocBookPlugin.h b/fbreader/src/formats/docbook/DocBookPlugin.h new file mode 100644 index 0000000..324b2be --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookPlugin.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __DOCBOOKPLUGIN_H__ +#define __DOCBOOKPLUGIN_H__ + +#include "../FormatPlugin.h" + +class DocBookPlugin : public FormatPlugin { + +public: + DocBookPlugin(); + ~DocBookPlugin(); + bool providesMetaInfo() const; + bool acceptsFile(const std::string &extension) const; + bool readMetaInfo(Book &book) const; + bool readLanguageAndEncoding(Book &book) const; + bool readModel(BookModel &model) const; +}; + +inline DocBookPlugin::DocBookPlugin() {} +inline DocBookPlugin::~DocBookPlugin() {} +inline bool DocBookPlugin::providesMetaInfo() const { return true; } + +#endif /* __DOCBOOKPLUGIN_H__ */ diff --git a/fbreader/src/formats/docbook/DocBookReader.cpp b/fbreader/src/formats/docbook/DocBookReader.cpp new file mode 100644 index 0000000..73c17d1 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookReader.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include <ZLStringUtil.h> +#include <ZLFile.h> +#include <ZLDir.h> + +#include "DocBookReader.h" + +static const DocBookReader::Tag TAGS[] = { + {"article", DocBookReader::_ARTICLE}, + {"title", DocBookReader::_TITLE}, + {"articleinfo", DocBookReader::_ARTICLEINFO}, + {"author", DocBookReader::_AUTHOR}, + {"firstname", DocBookReader::_FIRSTNAME}, + {"othername", DocBookReader::_OTHERNAME}, + {"surname", DocBookReader::_SURNAME}, + {"affiliation", DocBookReader::_AFFILIATION}, + {"orgname", DocBookReader::_ORGNAME}, + {"ulink", DocBookReader::_ULINK}, + {"address", DocBookReader::_ADDRESS}, + {"email", DocBookReader::_EMAIL}, + {"pubdate", DocBookReader::_PUBDATE}, + {"releaseinfo", DocBookReader::_RELEASEINFO}, + {"copyright", DocBookReader::_COPYRIGHT}, + {"year", DocBookReader::_YEAR}, + {"holder", DocBookReader::_HOLDER}, + {"legalnotice", DocBookReader::_LEGALNOTICE}, + {"para", DocBookReader::_PARA}, + {"revhistory", DocBookReader::_REVHISTORY}, + {"revision", DocBookReader::_REVISION}, + {"revnumber", DocBookReader::_REVNUMBER}, + {"date", DocBookReader::_DATE}, + {"authorinitials", DocBookReader::_AUTHORINITIALS}, + {"revremark", DocBookReader::_REVREMARK}, + {"abstract", DocBookReader::_ABSTRACT}, + {"sect1", DocBookReader::_SECT1}, + {"emphasis", DocBookReader::_EMPHASIS}, + {"blockquote", DocBookReader::_BLOCKQUOTE}, + {"citetitle", DocBookReader::_CITETITLE}, + {"link", DocBookReader::_LINK}, + {"foreignphrase", DocBookReader::_FOREIGNPHRASE}, + {"part", DocBookReader::_PART}, + {"preface", DocBookReader::_PREFACE}, + {"chapter", DocBookReader::_CHAPTER}, + {0, DocBookReader::_UNKNOWN} +}; + +const DocBookReader::Tag *DocBookReader::tags() const { + return TAGS; +} + +const std::vector<std::string> &DocBookReader::externalDTDs() const { + return EntityFilesCollector::Instance().externalDTDs("docbook"); +} diff --git a/fbreader/src/formats/docbook/DocBookReader.h b/fbreader/src/formats/docbook/DocBookReader.h new file mode 100644 index 0000000..a18f358 --- /dev/null +++ b/fbreader/src/formats/docbook/DocBookReader.h @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2004-2012 Geometer Plus <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef __DOCBOOKREADER_H__ +#define __DOCBOOKREADER_H__ + +#include <ZLXMLReader.h> + +class DocBookReader : public ZLXMLReader { + +public: + static std::string DTDDirectory; + +public: + struct Tag { + const char *tagName; + int tagCode; + }; + +public: +//protected: + enum TagCode { + _ARTICLE, + _TITLE, + _ARTICLEINFO, + _AUTHOR, + _FIRSTNAME, + _OTHERNAME, + _SURNAME, + _AFFILIATION, + _ORGNAME, + _ULINK, + _ADDRESS, + _EMAIL, + _PUBDATE, + _RELEASEINFO, + _COPYRIGHT, + _YEAR, + _HOLDER, + _LEGALNOTICE, + _PARA, + _REVHISTORY, + _REVISION, + _REVNUMBER, + _DATE, + _AUTHORINITIALS, + _REVREMARK, + _ABSTRACT, + _SECT1, + _EMPHASIS, + _BLOCKQUOTE, + _CITETITLE, + _LINK, + _FOREIGNPHRASE, + _FIRSTTERM, + _FILENAME, + _ITEMIZEDLIST, + _LISTITEM, + _PART, + _PREFACE, + _CHAPTER, + _UNKNOWN + }; + +protected: + DocBookReader(); + +public: + ~DocBookReader(); + const Tag *tags() const; + +protected: + const std::vector<std::string> &externalDTDs() const; +}; + +inline DocBookReader::DocBookReader() {} +inline DocBookReader::~DocBookReader() {} + +#endif /* __DOCBOOKREADER_H__ */ |