pull/913/head
gedoor 4 years ago
parent 008654e445
commit b422c64cf4
  1. 26
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  2. 13
      epublib/src/main/java/me/ag2s/epublib/browsersupport/NavigationEvent.java
  3. 8
      epublib/src/main/java/me/ag2s/epublib/browsersupport/NavigationHistory.java
  4. 13
      epublib/src/main/java/me/ag2s/epublib/browsersupport/Navigator.java
  5. 2
      epublib/src/main/java/me/ag2s/epublib/domain/EpubBook.java
  6. 4
      epublib/src/main/java/me/ag2s/epublib/epub/BookProcessor.java
  7. 12
      epublib/src/main/java/me/ag2s/epublib/epub/BookProcessorPipeline.java
  8. 79
      epublib/src/main/java/me/ag2s/epublib/epub/EpubReader.java
  9. 31
      epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java
  10. 53
      epublib/src/main/java/me/ag2s/epublib/epub/NCXDocument.java
  11. 36
      epublib/src/main/java/me/ag2s/epublib/epub/NCXDocumentV2.java
  12. 18
      epublib/src/main/java/me/ag2s/epublib/epub/NCXDocumentV3.java
  13. 23
      epublib/src/main/java/me/ag2s/epublib/epub/PackageDocumentMetadataWriter.java
  14. 56
      epublib/src/main/java/me/ag2s/epublib/epub/PackageDocumentReader.java
  15. 88
      epublib/src/main/java/me/ag2s/epublib/epub/PackageDocumentWriter.java

@ -5,11 +5,11 @@ import android.graphics.BitmapFactory
import android.net.Uri
import android.text.TextUtils
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.EpubChapter
import io.legado.app.utils.*
import me.ag2s.epublib.domain.Book
import me.ag2s.epublib.domain.EpubBook
import me.ag2s.epublib.domain.MediaTypes
import me.ag2s.epublib.domain.Resources
import me.ag2s.epublib.domain.TOCReference
@ -26,13 +26,13 @@ import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
class EPUBFile(var book: io.legado.app.data.entities.Book) {
class EPUBFile(var book: Book) {
companion object {
private var eFile: EPUBFile? = null
@Synchronized
private fun getEFile(book: io.legado.app.data.entities.Book): EPUBFile {
private fun getEFile(book: Book): EPUBFile {
if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) {
eFile = EPUBFile(book)
return eFile!!
@ -42,30 +42,30 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
}
@Synchronized
fun getChapterList(book: io.legado.app.data.entities.Book): ArrayList<BookChapter> {
fun getChapterList(book: Book): ArrayList<BookChapter> {
return getEFile(book).getChapterList()
}
@Synchronized
fun getContent(book: io.legado.app.data.entities.Book, chapter: BookChapter): String? {
fun getContent(book: Book, chapter: BookChapter): String? {
return getEFile(book).getContent(chapter)
}
@Synchronized
fun getImage(
book: io.legado.app.data.entities.Book,
book: Book,
href: String
): InputStream? {
return getEFile(book).getImage(href)
}
@Synchronized
fun upBookInfo(book: io.legado.app.data.entities.Book) {
fun upBookInfo(book: Book) {
return getEFile(book).upBookInfo()
}
}
private var epubBook: Book? = null
private var epubBook: EpubBook? = null
private var mCharset: Charset = Charset.defaultCharset()
init {
@ -102,7 +102,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
}
/*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/
private fun readEpub(input: InputStream?): Book? {
private fun readEpub(input: InputStream?): EpubBook? {
if (input == null) return null
try {
val inZip = ZipInputStream(input)
@ -156,7 +156,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
}
/*选择去除正文中的H标签,部分书籍标题与阅读标题重复待优化*/
var tag = io.legado.app.data.entities.Book.hTag
var tag = Book.hTag
if (book.getDelTag(tag)) {
body.getElementsByTag("h1")?.remove()
body.getElementsByTag("h2")?.remove()
@ -168,7 +168,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
}
/*选择去除正文中的img标签,目前图片支持效果待优化*/
tag = io.legado.app.data.entities.Book.imgTag
tag = Book.imgTag
if (book.getDelTag(tag)) {
body.getElementsByTag("img")?.remove()
}
@ -177,7 +177,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
elements.select("script").remove()
elements.select("style").remove()
/*选择去除正文中的ruby标签,目前注释支持效果待优化*/
tag = io.legado.app.data.entities.Book.rubyTag
tag = Book.rubyTag
var html = elements.outerHtml()
if (book.getDelTag(tag)) {
html = html.replace("<ruby>\\s?([\\u4e00-\\u9fa5])\\s?.*?</ruby>".toRegex(), "$1")

@ -1,9 +1,10 @@
package me.ag2s.epublib.browsersupport;
import me.ag2s.epublib.domain.Book;
import java.util.EventObject;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.util.StringUtil;
import java.util.EventObject;
/**
* Used to tell NavigationEventListener just what kind of navigation action
@ -19,7 +20,7 @@ public class NavigationEvent extends EventObject {
private Resource oldResource;
private int oldSpinePos;
private Navigator navigator;
private Book oldBook;
private EpubBook oldBook;
private int oldSectionPos;
private String oldFragmentId;
@ -59,7 +60,7 @@ public class NavigationEvent extends EventObject {
this.oldFragmentId = oldFragmentId;
}
public Book getOldBook() {
public EpubBook getOldBook() {
return oldBook;
}
@ -122,11 +123,11 @@ public class NavigationEvent extends EventObject {
}
public void setOldBook(Book oldBook) {
public void setOldBook(EpubBook oldBook) {
this.oldBook = oldBook;
}
public Book getCurrentBook() {
public EpubBook getCurrentBook() {
return getNavigator().getBook();
}

@ -1,15 +1,15 @@
package me.ag2s.epublib.browsersupport;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.Resource;
import java.util.ArrayList;
import java.util.List;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Resource;
/**
* A history of the user's locations with the epub.
*
* @author paul.siegmann
*
*/
public class NavigationHistory implements NavigationEventListener {
@ -58,7 +58,7 @@ public class NavigationHistory implements NavigationEventListener {
return currentSize;
}
public void initBook(Book book) {
public void initBook(EpubBook book) {
if (book == null) {
return;
}

@ -1,11 +1,12 @@
package me.ag2s.epublib.browsersupport;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Resource;
/**
* A helper class for epub browser applications.
*
@ -18,7 +19,7 @@ import java.util.List;
public class Navigator implements Serializable {
private static final long serialVersionUID = 1076126986424925474L;
private Book book;
private EpubBook book;
private int currentSpinePos;
private Resource currentResource;
private int currentPagePos;
@ -30,7 +31,7 @@ public class Navigator implements Serializable {
this(null);
}
public Navigator(Book book) {
public Navigator(EpubBook book) {
this.book = book;
this.currentSpinePos = 0;
if (book != null) {
@ -158,7 +159,7 @@ public class Navigator implements Serializable {
return gotoSpineSection(book.getSpine().size() - 1, source);
}
public void gotoBook(Book book, Object source) {
public void gotoBook(EpubBook book, Object source) {
NavigationEvent navigationEvent = new NavigationEvent(source, this);
this.book = book;
this.currentFragmentId = null;
@ -193,7 +194,7 @@ public class Navigator implements Serializable {
this.currentResource = book.getSpine().getResource(currentIndex);
}
public Book getBook() {
public EpubBook getBook() {
return book;
}

@ -39,7 +39,7 @@ import java.util.Map;
* @author paul
* @author jake
*/
public class Book implements Serializable {
public class EpubBook implements Serializable {
private static final long serialVersionUID = 2068355170895770100L;

@ -1,6 +1,6 @@
package me.ag2s.epublib.epub;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.EpubBook;
/**
* Post-processes a book.
@ -16,5 +16,5 @@ public interface BookProcessor {
*/
BookProcessor IDENTITY_BOOKPROCESSOR = book -> book;
Book processBook(Book book);
EpubBook processBook(EpubBook book);
}

@ -2,15 +2,17 @@ package me.ag2s.epublib.epub;
import android.util.Log;
import me.ag2s.epublib.domain.Book;
//import io.documentnode.minilog.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import me.ag2s.epublib.domain.EpubBook;
//import io.documentnode.minilog.Logger;
/**
* A book processor that combines several other bookprocessors
*
* <p>
* Fixes coverpage/coverimage.
* Cleans up the XHTML.
*
@ -30,7 +32,7 @@ public class BookProcessorPipeline implements BookProcessor {
}
@Override
public Book processBook(Book book) {
public EpubBook processBook(EpubBook book) {
if (bookProcessors == null) {
return book;
}
@ -38,7 +40,7 @@ public class BookProcessorPipeline implements BookProcessor {
try {
book = bookProcessor.processBook(book);
} catch (Exception e) {
Log.e(TAG,e.getMessage(), e);
Log.e(TAG, e.getMessage(), e);
}
}
return book;

@ -2,15 +2,9 @@ package me.ag2s.epublib.epub;
import android.util.Log;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.MediaType;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.Resources;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
@ -18,8 +12,16 @@ import java.util.List;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.MediaType;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.Resources;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
/**
* Reads an epub file.
@ -32,27 +34,27 @@ public class EpubReader {
private static String TAG= EpubReader.class.getName();
private BookProcessor bookProcessor = BookProcessor.IDENTITY_BOOKPROCESSOR;
public Book readEpub(InputStream in) throws IOException {
public EpubBook readEpub(InputStream in) throws IOException {
return readEpub(in, Constants.CHARACTER_ENCODING);
}
public Book readEpub(ZipInputStream in) throws IOException {
public EpubBook readEpub(ZipInputStream in) throws IOException {
return readEpub(in, Constants.CHARACTER_ENCODING);
}
public Book readEpub(ZipFile zipfile) throws IOException {
public EpubBook readEpub(ZipFile zipfile) throws IOException {
return readEpub(zipfile, Constants.CHARACTER_ENCODING);
}
/**
* Read epub from inputstream
*
* @param in the inputstream from which to read the epub
* @param in the inputstream from which to read the epub
* @param encoding the encoding to use for the html files within the epub
* @return the Book as read from the inputstream
* @throws IOException
*/
public Book readEpub(InputStream in, String encoding) throws IOException {
public EpubBook readEpub(InputStream in, String encoding) throws IOException {
return readEpub(new ZipInputStream(in), encoding);
}
@ -60,54 +62,53 @@ public class EpubReader {
/**
* Reads this EPUB without loading any resources into memory.
*
* @param zipFile the file to load
* @param zipFile the file to load
* @param encoding the encoding for XHTML files
*
* @return this Book without loading all resources into memory.
* @throws IOException
*/
public Book readEpubLazy(ZipFile zipFile, String encoding)
throws IOException {
public EpubBook readEpubLazy(ZipFile zipFile, String encoding)
throws IOException {
return readEpubLazy(zipFile, encoding,
Arrays.asList(MediaTypes.mediaTypes));
Arrays.asList(MediaTypes.mediaTypes));
}
public Book readEpub(ZipInputStream in, String encoding) throws IOException {
public EpubBook readEpub(ZipInputStream in, String encoding) throws IOException {
return readEpub(ResourcesLoader.loadResources(in, encoding));
}
public Book readEpub(ZipFile in, String encoding) throws IOException {
public EpubBook readEpub(ZipFile in, String encoding) throws IOException {
return readEpub(ResourcesLoader.loadResources(in, encoding));
}
/**
* Reads this EPUB without loading all resources into memory.
*
* @param zipFile the file to load
* @param encoding the encoding for XHTML files
* @param zipFile the file to load
* @param encoding the encoding for XHTML files
* @param lazyLoadedTypes a list of the MediaType to load lazily
* @return this Book without loading all resources into memory.
* @throws IOException
*/
public Book readEpubLazy(ZipFile zipFile, String encoding,
List<MediaType> lazyLoadedTypes) throws IOException {
public EpubBook readEpubLazy(ZipFile zipFile, String encoding,
List<MediaType> lazyLoadedTypes) throws IOException {
Resources resources = ResourcesLoader
.loadResources(zipFile, encoding, lazyLoadedTypes);
.loadResources(zipFile, encoding, lazyLoadedTypes);
return readEpub(resources);
}
public Book readEpub(Resources resources) throws IOException {
return readEpub(resources, new Book());
public EpubBook readEpub(Resources resources) throws IOException {
return readEpub(resources, new EpubBook());
}
public Book readEpub(Resources resources, Book result) throws IOException {
public EpubBook readEpub(Resources resources, EpubBook result) throws IOException {
if (result == null) {
result = new Book();
result = new EpubBook();
}
handleMimeType(result, resources);
String packageResourceHref = getPackageResourceHref(resources);
Resource packageResource = processPackageResource(packageResourceHref,
result, resources);
result, resources);
result.setOpfResource(packageResource);
Resource ncxResource = processNcxResource(packageResource, result);
result.setNcxResource(ncxResource);
@ -116,21 +117,21 @@ public class EpubReader {
}
private Book postProcessBook(Book book) {
private EpubBook postProcessBook(EpubBook book) {
if (bookProcessor != null) {
book = bookProcessor.processBook(book);
}
return book;
}
private Resource processNcxResource(Resource packageResource, Book book) {
Log.d(TAG,"OPF:getHref()"+packageResource.getHref());
private Resource processNcxResource(Resource packageResource, EpubBook book) {
Log.d(TAG, "OPF:getHref()" + packageResource.getHref());
return NCXDocument.read(book, this);
}
private Resource processPackageResource(String packageResourceHref, Book book,
Resources resources) {
private Resource processPackageResource(String packageResourceHref, EpubBook book,
Resources resources) {
Resource packageResource = resources.remove(packageResourceHref);
try {
PackageDocumentReader.read(packageResource, this, book, resources);
@ -163,7 +164,7 @@ public class EpubReader {
return result;
}
private void handleMimeType(Book result, Resources resources) {
private void handleMimeType(EpubBook result, Resources resources) {
resources.remove("mimetype");
}
}

@ -2,11 +2,8 @@ package me.ag2s.epublib.epub;
import android.util.Log;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.util.IOUtil;
//import io.documentnode.minilog.Logger;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -15,7 +12,13 @@ import java.io.Writer;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.xmlpull.v1.XmlSerializer;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.util.IOUtil;
//import io.documentnode.minilog.Logger;
/**
* Generates an epub file. Not thread-safe, single use object.
@ -41,7 +44,7 @@ public class EpubWriter {
}
public void write(Book book, OutputStream out) throws IOException {
public void write(EpubBook book, OutputStream out) throws IOException {
book = processBook(book);
ZipOutputStream resultStream = new ZipOutputStream(out);
writeMimeType(resultStream);
@ -52,14 +55,14 @@ public class EpubWriter {
resultStream.close();
}
private Book processBook(Book book) {
private EpubBook processBook(EpubBook book) {
if (bookProcessor != null) {
book = bookProcessor.processBook(book);
}
return book;
}
private void initTOCResource(Book book) {
private void initTOCResource(EpubBook book) {
Resource tocResource;
try {
tocResource = NCXDocument.createNCXResource(book);
@ -77,8 +80,8 @@ public class EpubWriter {
}
private void writeResources(Book book, ZipOutputStream resultStream)
throws IOException {
private void writeResources(EpubBook book, ZipOutputStream resultStream)
throws IOException {
for (Resource resource : book.getResources().getAll()) {
writeResource(resource, resultStream);
}
@ -107,11 +110,11 @@ public class EpubWriter {
}
private void writePackageDocument(Book book, ZipOutputStream resultStream)
throws IOException {
private void writePackageDocument(EpubBook book, ZipOutputStream resultStream)
throws IOException {
resultStream.putNextEntry(new ZipEntry("OEBPS/content.opf"));
XmlSerializer xmlSerializer = EpubProcessorSupport
.createXmlSerializer(resultStream);
.createXmlSerializer(resultStream);
PackageDocumentWriter.write(this, xmlSerializer, book);
xmlSerializer.flush();
// String resultAsString = result.toString();

@ -2,31 +2,30 @@ package me.ag2s.epublib.epub;
import android.util.Log;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlSerializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Author;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Identifier;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.TOCReference;
import me.ag2s.epublib.domain.TableOfContents;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
//import javax.xml.stream.FactoryConfigurationError;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlSerializer;
/**
* Writes the ncx document as defined by namespace http://www.daisy.org/z3986/2005/ncx/
@ -75,7 +74,7 @@ public class NCXDocument {
}
public static Resource read(Book book, EpubReader epubReader) {
public static Resource read(EpubBook book, EpubReader epubReader) {
Log.d(TAG, book.getVersion());
String version = book.getVersion();
if (version.startsWith("2.")) {
@ -89,28 +88,28 @@ public class NCXDocument {
}
private static List<TOCReference> readTOCReferences(NodeList navpoints,
Book book) {
EpubBook book) {
Log.d(TAG, book.getVersion());
String version = book.getVersion();
if (version.startsWith("2.")) {
return NCXDocumentV2.readTOCReferences(navpoints,book);
return NCXDocumentV2.readTOCReferences(navpoints, book);
} else if (version.startsWith("3.")) {
return NCXDocumentV3.readTOCReferences(navpoints,book);
return NCXDocumentV3.readTOCReferences(navpoints, book);
} else {
return NCXDocumentV2.readTOCReferences(navpoints,book);
return NCXDocumentV2.readTOCReferences(navpoints, book);
}
}
static TOCReference readTOCReference(Element navpointElement, Book book) {
static TOCReference readTOCReference(Element navpointElement, EpubBook book) {
Log.d(TAG, book.getVersion());
String version = book.getVersion();
if (version.startsWith("2.")) {
return NCXDocumentV2.readTOCReference(navpointElement,book);
return NCXDocumentV2.readTOCReference(navpointElement, book);
} else if (version.startsWith("3.")) {
return NCXDocumentV3.readTOCReference(navpointElement,book);
return NCXDocumentV3.readTOCReference(navpointElement, book);
} else {
return NCXDocumentV2.readTOCReference(navpointElement,book);
return NCXDocumentV2.readTOCReference(navpointElement, book);
}
}
@ -138,7 +137,7 @@ public class NCXDocument {
}
public static void write(EpubWriter epubWriter, Book book,
public static void write(EpubWriter epubWriter, EpubBook book,
ZipOutputStream resultStream) throws IOException {
resultStream
.putNextEntry(new ZipEntry(book.getSpine().getTocResource().getHref()));
@ -158,13 +157,13 @@ public class NCXDocument {
* @throws IllegalArgumentException
* @1throws FactoryConfigurationError
*/
public static void write(XmlSerializer xmlSerializer, Book book)
public static void write(XmlSerializer xmlSerializer, EpubBook book)
throws IllegalArgumentException, IllegalStateException, IOException {
write(xmlSerializer, book.getMetadata().getIdentifiers(), book.getTitle(),
book.getMetadata().getAuthors(), book.getTableOfContents());
}
public static Resource createNCXResource(Book book)
public static Resource createNCXResource(EpubBook book)
throws IllegalArgumentException, IllegalStateException, IOException {
return createNCXResource(book.getMetadata().getIdentifiers(),
book.getTitle(), book.getMetadata().getAuthors(),

@ -19,7 +19,7 @@ import java.util.zip.ZipOutputStream;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Author;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Identifier;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
@ -75,10 +75,10 @@ public class NCXDocumentV2 extends NCXDocument{
}
public static Resource read(Book book, EpubReader epubReader) {
public static Resource read(EpubBook book, EpubReader epubReader) {
Resource ncxResource = null;
if (book.getSpine().getTocResource() == null) {
Log.e(TAG,"Book does not contain a table of contents file");
Log.e(TAG, "Book does not contain a table of contents file");
return ncxResource;
}
try {
@ -102,12 +102,12 @@ public class NCXDocumentV2 extends NCXDocument{
}
static List<TOCReference> readTOCReferences(NodeList navpoints,
Book book) {
EpubBook book) {
if (navpoints == null) {
return new ArrayList<>();
}
List<TOCReference> result = new ArrayList<>(
navpoints.getLength());
navpoints.getLength());
for (int i = 0; i < navpoints.getLength(); i++) {
Node node = navpoints.item(i);
if (node.getNodeType() != Document.ELEMENT_NODE) {
@ -122,13 +122,13 @@ public class NCXDocumentV2 extends NCXDocument{
return result;
}
static TOCReference readTOCReference(Element navpointElement, Book book) {
static TOCReference readTOCReference(Element navpointElement, EpubBook book) {
String label = readNavLabel(navpointElement);
//Log.d(TAG,"label:"+label);
String tocResourceRoot = StringUtil
.substringBeforeLast(book.getSpine().getTocResource().getHref(), '/');
.substringBeforeLast(book.getSpine().getTocResource().getHref(), '/');
if (tocResourceRoot.length() == book.getSpine().getTocResource().getHref()
.length()) {
.length()) {
tocResourceRoot = "";
} else {
tocResourceRoot = tocResourceRoot + "/";
@ -174,10 +174,10 @@ public class NCXDocumentV2 extends NCXDocument{
}
public static void write(EpubWriter epubWriter, Book book,
ZipOutputStream resultStream) throws IOException {
public static void write(EpubWriter epubWriter, EpubBook book,
ZipOutputStream resultStream) throws IOException {
resultStream
.putNextEntry(new ZipEntry(book.getSpine().getTocResource().getHref()));
.putNextEntry(new ZipEntry(book.getSpine().getTocResource().getHref()));
XmlSerializer out = EpubProcessorSupport.createXmlSerializer(resultStream);
write(out, book);
out.flush();
@ -195,17 +195,17 @@ public class NCXDocumentV2 extends NCXDocument{
* @throws IllegalStateException
* @throws IllegalArgumentException
*/
public static void write(XmlSerializer xmlSerializer, Book book)
throws IllegalArgumentException, IllegalStateException, IOException {
public static void write(XmlSerializer xmlSerializer, EpubBook book)
throws IllegalArgumentException, IllegalStateException, IOException {
write(xmlSerializer, book.getMetadata().getIdentifiers(), book.getTitle(),
book.getMetadata().getAuthors(), book.getTableOfContents());
book.getMetadata().getAuthors(), book.getTableOfContents());
}
public static Resource createNCXResource(Book book)
throws IllegalArgumentException, IllegalStateException, IOException {
public static Resource createNCXResource(EpubBook book)
throws IllegalArgumentException, IllegalStateException, IOException {
return createNCXResource(book.getMetadata().getIdentifiers(),
book.getTitle(), book.getMetadata().getAuthors(),
book.getTableOfContents());
book.getTitle(), book.getMetadata().getAuthors(),
book.getTableOfContents());
}
public static Resource createNCXResource(List<Identifier> identifiers,

@ -6,22 +6,14 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlSerializer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Author;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.Identifier;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.TOCReference;
import me.ag2s.epublib.domain.TableOfContents;
@ -69,7 +61,7 @@ public class NCXDocumentV3 extends NCXDocument {
}
public static Resource read(Book book, EpubReader epubReader) {
public static Resource read(EpubBook book, EpubReader epubReader) {
Resource ncxResource = null;
if (book.getSpine().getTocResource() == null) {
Log.e(TAG, "Book does not contain a table of contents file");
@ -99,7 +91,7 @@ public class NCXDocumentV3 extends NCXDocument {
return ncxResource;
}
public static List<TOCReference> doToc(Node n, Book book) {
public static List<TOCReference> doToc(Node n, EpubBook book) {
List<TOCReference> result = new ArrayList<>();
if (n == null || n.getNodeType() != Document.ELEMENT_NODE) {
@ -118,7 +110,7 @@ public class NCXDocumentV3 extends NCXDocument {
static List<TOCReference> readTOCReferences(NodeList navpoints,
Book book) {
EpubBook book) {
if (navpoints == null) {
return new ArrayList<>();
}
@ -146,7 +138,7 @@ public class NCXDocumentV3 extends NCXDocument {
}
static TOCReference readTOCReference(Element navpointElement, Book book) {
static TOCReference readTOCReference(Element navpointElement, EpubBook book) {
String label = readNavLabel(navpointElement);
//Log.d(TAG, "label:" + label);
String tocResourceRoot = StringUtil

@ -1,16 +1,19 @@
package me.ag2s.epublib.epub;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Author;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.Date;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Identifier;
import me.ag2s.epublib.util.StringUtil;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import org.xmlpull.v1.XmlSerializer;
public class PackageDocumentMetadataWriter extends PackageDocumentBase {
@ -23,17 +26,17 @@ public class PackageDocumentMetadataWriter extends PackageDocumentBase {
* @throws IllegalStateException
* @throws IllegalArgumentException
*/
public static void writeMetaData(Book book, XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
public static void writeMetaData(EpubBook book, XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
serializer.startTag(NAMESPACE_OPF, OPFTags.metadata);
serializer.setPrefix(PREFIX_DUBLIN_CORE, NAMESPACE_DUBLIN_CORE);
serializer.setPrefix(PREFIX_OPF, NAMESPACE_OPF);
writeIdentifiers(book.getMetadata().getIdentifiers(), serializer);
writeSimpleMetdataElements(DCTags.title, book.getMetadata().getTitles(),
serializer);
serializer);
writeSimpleMetdataElements(DCTags.subject, book.getMetadata().getSubjects(),
serializer);
serializer);
writeSimpleMetdataElements(DCTags.description,
book.getMetadata().getDescriptions(), serializer);
writeSimpleMetdataElements(DCTags.publisher,

@ -1,21 +1,12 @@
package me.ag2s.epublib.epub;
import android.util.Log;
import android.widget.Toast;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.Guide;
import me.ag2s.epublib.domain.GuideReference;
import me.ag2s.epublib.domain.MediaType;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.Resources;
import me.ag2s.epublib.domain.Spine;
import me.ag2s.epublib.domain.SpineReference;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@ -27,11 +18,23 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Guide;
import me.ag2s.epublib.domain.GuideReference;
import me.ag2s.epublib.domain.MediaType;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.Resources;
import me.ag2s.epublib.domain.Spine;
import me.ag2s.epublib.domain.SpineReference;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
/**
* Reads the opf package document as defined by namespace http://www.idpf.org/2007/opf
@ -47,8 +50,8 @@ public class PackageDocumentReader extends PackageDocumentBase {
public static void read(
Resource packageResource, EpubReader epubReader, Book book,
Resources resources)
Resource packageResource, EpubReader epubReader, EpubBook book,
Resources resources)
throws UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException {
Document packageDocument = ResourceUtil.getAsDocument(packageResource);
String packageHref = packageResource.getHref();
@ -151,16 +154,16 @@ public class PackageDocumentReader extends PackageDocumentBase {
* @param resources
*/
private static void readGuide(Document packageDocument,
EpubReader epubReader, Book book, Resources resources) {
EpubReader epubReader, EpubBook book, Resources resources) {
Element guideElement = DOMUtil
.getFirstElementByTagNameNS(packageDocument.getDocumentElement(),
NAMESPACE_OPF, OPFTags.guide);
.getFirstElementByTagNameNS(packageDocument.getDocumentElement(),
NAMESPACE_OPF, OPFTags.guide);
if (guideElement == null) {
return;
}
Guide guide = book.getGuide();
NodeList guideReferences = guideElement
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.reference);
.getElementsByTagNameNS(NAMESPACE_OPF, OPFTags.reference);
for (int i = 0; i < guideReferences.getLength(); i++) {
Element referenceElement = (Element) guideReferences.item(i);
String resourceHref = DOMUtil
@ -400,17 +403,18 @@ public class PackageDocumentReader extends PackageDocumentBase {
/**
* Finds the cover resource in the packageDocument and adds it to the book if found.
* Keeps the cover resource in the resources map
*
* @param packageDocument
* @param book
* @1param resources
*/
private static void readCover(Document packageDocument, Book book) {
private static void readCover(Document packageDocument, EpubBook book) {
Collection<String> coverHrefs = findCoverHrefs(packageDocument);
for (String coverHref : coverHrefs) {
Resource resource = book.getResources().getByHref(coverHref);
if (resource == null) {
Log.e(TAG,"Cover resource " + coverHref + " not found");
Log.e(TAG, "Cover resource " + coverHref + " not found");
continue;
}
if (resource.getMediaType() == MediaTypes.XHTML) {

@ -2,24 +2,26 @@ package me.ag2s.epublib.epub;
import android.util.Log;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import me.ag2s.epublib.Constants;
import me.ag2s.epublib.domain.Book;
import me.ag2s.epublib.domain.EpubBook;
import me.ag2s.epublib.domain.Guide;
import me.ag2s.epublib.domain.GuideReference;
import me.ag2s.epublib.domain.MediaTypes;
import me.ag2s.epublib.domain.Resource;
import me.ag2s.epublib.domain.Spine;
import me.ag2s.epublib.domain.SpineReference;
import me.ag2s.epublib.util.ResourceUtil;
import me.ag2s.epublib.util.StringUtil;
//import io.documentnode.minilog.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//import javax.xml.stream.XMLStreamException;
import org.xmlpull.v1.XmlSerializer;
/**
* Writes the opf package document as defined by namespace http://www.idpf.org/2007/opf
@ -33,17 +35,17 @@ public class PackageDocumentWriter extends PackageDocumentBase {
private static String TAG= PackageDocumentWriter.class.getName();
public static void write(EpubWriter epubWriter, XmlSerializer serializer,
Book book) throws IOException {
EpubBook book) throws IOException {
try {
serializer.startDocument(Constants.CHARACTER_ENCODING, false);
serializer.setPrefix(PREFIX_OPF, NAMESPACE_OPF);
serializer.setPrefix(PREFIX_DUBLIN_CORE, NAMESPACE_DUBLIN_CORE);
serializer.startTag(NAMESPACE_OPF, OPFTags.packageTag);
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.version,
"2.0");
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.version,
"2.0");
serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX,
OPFAttributes.uniqueIdentifier, BOOK_ID_ID);
OPFAttributes.uniqueIdentifier, BOOK_ID_ID);
PackageDocumentMetadataWriter.writeMetaData(book, serializer);
@ -71,26 +73,26 @@ public class PackageDocumentWriter extends PackageDocumentBase {
* @throws IllegalArgumentException
* 1@throws XMLStreamException
*/
private static void writeSpine(Book book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
private static void writeSpine(EpubBook book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
serializer.startTag(NAMESPACE_OPF, OPFTags.spine);
Resource tocResource = book.getSpine().getTocResource();
String tocResourceId = tocResource.getId();
serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.toc,
tocResourceId);
tocResourceId);
if (book.getCoverPage() != null // there is a cover page
&& book.getSpine().findFirstResourceById(book.getCoverPage().getId())
< 0) { // cover page is not already in the spine
&& book.getSpine().findFirstResourceById(book.getCoverPage().getId())
< 0) { // cover page is not already in the spine
// write the cover html file
serializer.startTag(NAMESPACE_OPF, OPFTags.itemref);
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.idref,
book.getCoverPage().getId());
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.linear,
"no");
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.linear,
"no");
serializer.endTag(NAMESPACE_OPF, OPFTags.itemref);
}
writeSpineItems(book.getSpine(), serializer);
@ -98,18 +100,18 @@ public class PackageDocumentWriter extends PackageDocumentBase {
}
private static void writeManifest(Book book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
private static void writeManifest(EpubBook book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
serializer.startTag(NAMESPACE_OPF, OPFTags.manifest);
serializer.startTag(NAMESPACE_OPF, OPFTags.item);
serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.id,
epubWriter.getNcxId());
epubWriter.getNcxId());
serializer.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.href,
epubWriter.getNcxHref());
epubWriter.getNcxHref());
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.media_type,
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.media_type,
epubWriter.getNcxMediaType());
serializer.endTag(NAMESPACE_OPF, OPFTags.item);
@ -122,9 +124,9 @@ public class PackageDocumentWriter extends PackageDocumentBase {
serializer.endTag(NAMESPACE_OPF, OPFTags.manifest);
}
private static List<Resource> getAllResourcesSortById(Book book) {
private static List<Resource> getAllResourcesSortById(EpubBook book) {
List<Resource> allResources = new ArrayList<Resource>(
book.getResources().getAll());
book.getResources().getAll());
Collections.sort(allResources, new Comparator<Resource>() {
@Override
@ -137,25 +139,25 @@ public class PackageDocumentWriter extends PackageDocumentBase {
/**
* Writes a resources as an item element
*
* @param resource
* @param serializer
* @throws IOException
* @throws IllegalStateException
* @throws IllegalArgumentException
* 1@throws XMLStreamException
* @throws IllegalArgumentException 1@throws XMLStreamException
*/
private static void writeItem(Book book, Resource resource,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
private static void writeItem(EpubBook book, Resource resource,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
if (resource == null ||
(resource.getMediaType() == MediaTypes.NCX
&& book.getSpine().getTocResource() != null)) {
(resource.getMediaType() == MediaTypes.NCX
&& book.getSpine().getTocResource() != null)) {
return;
}
if (StringUtil.isBlank(resource.getId())) {
// log.error("resource id must not be empty (href: " + resource.getHref()
// + ", mediatype:" + resource.getMediaType() + ")");
Log.e(TAG,"resource id must not be empty (href: " + resource.getHref()
Log.e(TAG, "resource id must not be empty (href: " + resource.getHref()
+ ", mediatype:" + resource.getMediaType() + ")");
return;
}
@ -196,22 +198,22 @@ public class PackageDocumentWriter extends PackageDocumentBase {
serializer.startTag(NAMESPACE_OPF, OPFTags.itemref);
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.idref,
spineReference.getResourceId());
spineReference.getResourceId());
if (!spineReference.isLinear()) {
serializer
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.linear,
OPFValues.no);
.attribute(EpubWriter.EMPTY_NAMESPACE_PREFIX, OPFAttributes.linear,
OPFValues.no);
}
serializer.endTag(NAMESPACE_OPF, OPFTags.itemref);
}
}
private static void writeGuide(Book book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
private static void writeGuide(EpubBook book, EpubWriter epubWriter,
XmlSerializer serializer)
throws IllegalArgumentException, IllegalStateException, IOException {
serializer.startTag(NAMESPACE_OPF, OPFTags.guide);
ensureCoverPageGuideReferenceWritten(book.getGuide(), epubWriter,
serializer);
serializer);
for (GuideReference reference : book.getGuide().getReferences()) {
writeGuideReference(reference, serializer);
}

Loading…
Cancel
Save