|
|
|
@ -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,15 +34,15 @@ 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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -52,7 +54,7 @@ public class EpubReader { |
|
|
|
|
* @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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -62,21 +64,20 @@ public class EpubReader { |
|
|
|
|
* |
|
|
|
|
* @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) |
|
|
|
|
public EpubBook readEpubLazy(ZipFile zipFile, String encoding) |
|
|
|
|
throws IOException { |
|
|
|
|
return readEpubLazy(zipFile, encoding, |
|
|
|
|
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)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -89,20 +90,20 @@ public class EpubReader { |
|
|
|
|
* @return this Book without loading all resources into memory. |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
public Book readEpubLazy(ZipFile zipFile, String encoding, |
|
|
|
|
public EpubBook readEpubLazy(ZipFile zipFile, String encoding, |
|
|
|
|
List<MediaType> lazyLoadedTypes) throws IOException { |
|
|
|
|
Resources resources = ResourcesLoader |
|
|
|
|
.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); |
|
|
|
@ -116,20 +117,20 @@ 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) { |
|
|
|
|
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, |
|
|
|
|
private Resource processPackageResource(String packageResourceHref, EpubBook book, |
|
|
|
|
Resources resources) { |
|
|
|
|
Resource packageResource = resources.remove(packageResourceHref); |
|
|
|
|
try { |
|
|
|
@ -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"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|