mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
BookReadingException: handle reading error more accurately
This commit is contained in:
parent
2976b4c8fa
commit
be37623fcf
33 changed files with 239 additions and 134 deletions
|
@ -82,7 +82,7 @@ static void fillMetaInfo(JNIEnv* env, jobject javaBook, Book &book) {
|
|||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readMetaInfo(JNIEnv* env, jobject thiz, jobject javaBook) {
|
||||
JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readMetaInfoNative(JNIEnv* env, jobject thiz, jobject javaBook) {
|
||||
shared_ptr<FormatPlugin> plugin = findCppPlugin(env, thiz);
|
||||
if (plugin.isNull()) {
|
||||
return JNI_FALSE;
|
||||
|
@ -228,7 +228,7 @@ static bool initTOC(JNIEnv *env, jobject javaModel, BookModel &model) {
|
|||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readModel(JNIEnv* env, jobject thiz, jobject javaModel) {
|
||||
JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPlugin_readModelNative(JNIEnv* env, jobject thiz, jobject javaModel) {
|
||||
shared_ptr<FormatPlugin> plugin = findCppPlugin(env, thiz);
|
||||
if (plugin.isNull()) {
|
||||
return JNI_FALSE;
|
||||
|
|
|
@ -136,8 +136,8 @@ public abstract class DictionaryUtil {
|
|||
if (ourInfos.isEmpty()) {
|
||||
final Thread initThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
new InfoReader().read(ZLFile.createFileByPath("dictionaries/main.xml"));
|
||||
new ParagonInfoReader(context).read(ZLFile.createFileByPath("dictionaries/paragon.xml"));
|
||||
new InfoReader().readQuietly(ZLFile.createFileByPath("dictionaries/main.xml"));
|
||||
new ParagonInfoReader(context).readQuietly(ZLFile.createFileByPath("dictionaries/paragon.xml"));
|
||||
}
|
||||
});
|
||||
initThread.setPriority(Thread.MIN_PRIORITY);
|
||||
|
|
|
@ -27,10 +27,10 @@ import org.geometerplus.fbreader.library.Book;
|
|||
import org.geometerplus.fbreader.formats.*;
|
||||
|
||||
public abstract class BookModel {
|
||||
public static BookModel createModel(Book book) {
|
||||
public static BookModel createModel(Book book) throws BookReadingException {
|
||||
final FormatPlugin plugin = PluginCollection.Instance().getPlugin(book.File);
|
||||
if (plugin == null) {
|
||||
return null;
|
||||
throw new BookReadingException("pluginNotFound");
|
||||
}
|
||||
|
||||
final BookModel model;
|
||||
|
@ -42,13 +42,11 @@ public abstract class BookModel {
|
|||
model = new JavaBookModel(book);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
throw new BookReadingException("unknownPluginType", plugin.type().toString());
|
||||
}
|
||||
|
||||
if (plugin.readModel(model)) {
|
||||
return model;
|
||||
}
|
||||
return null;
|
||||
plugin.readModel(model);
|
||||
return model;
|
||||
}
|
||||
|
||||
public final Book Book;
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (C) 2012 Geometer Plus <contact@geometerplus.com>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.geometerplus.fbreader.bookmodel;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
public final class BookReadingException extends Exception {
|
||||
private static String getResourceText(String resourceId) {
|
||||
return ZLResource.resource("bookReadingException").getResource(resourceId).getValue();
|
||||
}
|
||||
|
||||
public BookReadingException(String resourceId) {
|
||||
super(getResourceText(resourceId));
|
||||
}
|
||||
|
||||
public BookReadingException(String resourceId, String param) {
|
||||
super(getResourceText(resourceId).replace("%s", param));
|
||||
}
|
||||
|
||||
public BookReadingException(IOException e, ZLFile file) {
|
||||
super(e.getMessage(), e);
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ import org.geometerplus.zlibrary.text.hyphenation.ZLTextHyphenator;
|
|||
import org.geometerplus.zlibrary.text.view.*;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.bookmodel.TOCTree;
|
||||
import org.geometerplus.fbreader.library.*;
|
||||
|
||||
|
@ -243,8 +244,8 @@ public final class FBReaderApp extends ZLApplication {
|
|||
Model = null;
|
||||
System.gc();
|
||||
System.gc();
|
||||
Model = BookModel.createModel(book);
|
||||
if (Model != null) {
|
||||
try {
|
||||
Model = BookModel.createModel(book);
|
||||
ZLTextHyphenator.Instance().load(book.getLanguage());
|
||||
BookTextView.setModel(Model.getTextModel());
|
||||
BookTextView.gotoPosition(book.getStoredPosition());
|
||||
|
@ -265,6 +266,8 @@ public final class FBReaderApp extends ZLApplication {
|
|||
title.append(")");
|
||||
}
|
||||
setTitle(title.toString());
|
||||
} catch (BookReadingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
getViewWidget().repaint();
|
||||
|
|
|
@ -46,7 +46,7 @@ public class TapZoneMap {
|
|||
final ZLFile mapFile = ZLFile.createFileByPath(
|
||||
"default/tapzones/" + name.toLowerCase() + ".xml"
|
||||
);
|
||||
new Reader().read(mapFile);
|
||||
new Reader().readQuietly(mapFile);
|
||||
}
|
||||
|
||||
public String getActionByCoordinates(int x, int y, int width, int height, Tap tap) {
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|||
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
|
||||
public abstract class FormatPlugin {
|
||||
|
@ -36,9 +37,9 @@ public abstract class FormatPlugin {
|
|||
return myFileType;
|
||||
}
|
||||
|
||||
public abstract boolean readMetaInfo(Book book);
|
||||
public abstract void readMetaInfo(Book book) throws BookReadingException;
|
||||
public abstract void readModel(BookModel model) throws BookReadingException;
|
||||
public abstract boolean readLanguageAndEncoding(Book book);
|
||||
public abstract boolean readModel(BookModel model);
|
||||
public abstract ZLImage readCover(ZLFile file);
|
||||
public abstract String readAnnotation(ZLFile file);
|
||||
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.geometerplus.zlibrary.core.image.*;
|
|||
import org.geometerplus.zlibrary.core.util.MimeType;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
|
||||
public class NativeFormatPlugin extends FormatPlugin {
|
||||
|
@ -37,13 +38,25 @@ public class NativeFormatPlugin extends FormatPlugin {
|
|||
}
|
||||
|
||||
@Override
|
||||
public native boolean readMetaInfo(Book book);
|
||||
public void readMetaInfo(Book book) throws BookReadingException {
|
||||
if (!readMetaInfoNative(book)) {
|
||||
throw new BookReadingException("errorReadingFile", book.File.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private native boolean readMetaInfoNative(Book book);
|
||||
|
||||
@Override
|
||||
public native boolean readLanguageAndEncoding(Book book);
|
||||
|
||||
@Override
|
||||
public native boolean readModel(BookModel model);
|
||||
public void readModel(BookModel model) throws BookReadingException {
|
||||
if (!readModelNative(model)) {
|
||||
throw new BookReadingException("errorReadingFile", model.Book.File.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private native boolean readModelNative(BookModel model);
|
||||
|
||||
@Override
|
||||
public ZLImage readCover(final ZLFile file) {
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package org.geometerplus.fbreader.formats.fb2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.xml.*;
|
||||
|
||||
|
@ -92,7 +94,12 @@ public class FB2AnnotationReader extends ZLXMLReaderAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean readDocument(ZLFile file) {
|
||||
return ZLXMLProcessor.read(this, file, 512);
|
||||
private boolean readDocument(ZLFile file) {
|
||||
try {
|
||||
ZLXMLProcessor.read(this, file, 512);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ class FB2CoverImage extends ZLImageProxy {
|
|||
Base64EncodedImage readCover(ZLFile file) {
|
||||
myReadCoverPage = false;
|
||||
myImageReference = null;
|
||||
read(file);
|
||||
readQuietly(file);
|
||||
return myImage;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,10 +20,12 @@
|
|||
package org.geometerplus.fbreader.formats.fb2;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.xml.*;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.library.Tag;
|
||||
|
||||
|
@ -54,13 +56,17 @@ public class FB2MetaInfoReader extends ZLXMLReaderAdapter {
|
|||
return true;
|
||||
}
|
||||
|
||||
public boolean readMetaInfo() {
|
||||
public void readMetaInfo() throws BookReadingException {
|
||||
myReadState = READ_NOTHING;
|
||||
myAuthorNames[0] = "";
|
||||
myAuthorNames[1] = "";
|
||||
myAuthorNames[2] = "";
|
||||
myBuffer.delete(0, myBuffer.length());
|
||||
return readDocument(myBook.File);
|
||||
try {
|
||||
ZLXMLProcessor.read(this, myBook.File, 512);
|
||||
} catch (IOException e) {
|
||||
throw new BookReadingException(e, myBook.File);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean startElementHandler(String tagName, ZLStringMap attributes) {
|
||||
|
@ -222,8 +228,4 @@ public class FB2MetaInfoReader extends ZLXMLReaderAdapter {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean readDocument(ZLFile file) {
|
||||
return ZLXMLProcessor.read(this, file, 512);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.formats.fb2;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.formats.JavaFormatPlugin;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
@ -31,13 +32,13 @@ public class FB2Plugin extends JavaFormatPlugin {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean readMetaInfo(Book book) {
|
||||
return new FB2MetaInfoReader(book).readMetaInfo();
|
||||
public void readMetaInfo(Book book) throws BookReadingException {
|
||||
new FB2MetaInfoReader(book).readMetaInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readModel(BookModel model) {
|
||||
return new FB2Reader(model).readBook();
|
||||
public void readModel(BookModel model) throws BookReadingException {
|
||||
new FB2Reader(model).readBook();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -20,8 +20,10 @@
|
|||
package org.geometerplus.fbreader.formats.fb2;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.constants.XMLNamespaces;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.library.ZLibrary;
|
||||
import org.geometerplus.zlibrary.core.util.*;
|
||||
import org.geometerplus.zlibrary.core.xml.*;
|
||||
|
@ -58,9 +60,14 @@ public final class FB2Reader extends ZLXMLReaderAdapter {
|
|||
myBookReader = new BookReader(model);
|
||||
}
|
||||
|
||||
boolean readBook() {
|
||||
void readBook() throws BookReadingException {
|
||||
Base64EncodedImage.resetCounter();
|
||||
return ZLXMLProcessor.read(this, myBookReader.Model.Book.File);
|
||||
final ZLFile file = myBookReader.Model.Book.File;
|
||||
try {
|
||||
ZLXMLProcessor.read(this, file);
|
||||
} catch (IOException e) {
|
||||
throw new BookReadingException(e, file);
|
||||
}
|
||||
}
|
||||
|
||||
public void startDocumentHandler() {
|
||||
|
|
|
@ -31,7 +31,7 @@ abstract class FB2TagManager {
|
|||
|
||||
static ArrayList<Tag> humanReadableTags(String id) {
|
||||
if (ourMap.isEmpty()) {
|
||||
new FB2TagInfoReader().read(
|
||||
new FB2TagInfoReader().readQuietly(
|
||||
ZLResourceFile.createResourceFile("formats/fb2/fb2genres.xml")
|
||||
);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.formats.oeb;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLArchiveEntryFile;
|
||||
|
@ -57,10 +58,14 @@ class NCXReader extends ZLXMLReaderAdapter {
|
|||
NCXReader(BookReader modelReader) {
|
||||
}
|
||||
|
||||
boolean readFile(String filePath) {
|
||||
void readFile(String filePath) throws BookReadingException {
|
||||
final ZLFile file = ZLFile.createFileByPath(filePath);
|
||||
myLocalPathPrefix = MiscUtil.archiveEntryName(MiscUtil.htmlDirectoryPrefix(file));
|
||||
return read(file);
|
||||
try {
|
||||
read(file);
|
||||
} catch (IOException e) {
|
||||
throw new BookReadingException(e, file);
|
||||
}
|
||||
}
|
||||
|
||||
Map<Integer,NavPoint> navigationMap() {
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.formats.oeb;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.constants.XMLNamespaces;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
@ -38,7 +39,8 @@ class OEBAnnotationReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
myReadState = READ_NONE;
|
||||
myBuffer.delete(0, myBuffer.length());
|
||||
|
||||
if (ZLXMLProcessor.read(this, file, 512)) {
|
||||
try {
|
||||
ZLXMLProcessor.read(this, file, 512);
|
||||
final int len = myBuffer.length();
|
||||
if (len > 1) {
|
||||
if (myBuffer.charAt(len - 1) == '\n') {
|
||||
|
@ -46,8 +48,10 @@ class OEBAnnotationReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
}
|
||||
return myBuffer.toString();
|
||||
}
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.formats.oeb;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.constants.XMLNamespaces;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
@ -70,7 +71,7 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
private HashMap<String,String> myFileNumbers = new HashMap<String,String>();
|
||||
private HashMap<String,Integer> myTOCLabels = new HashMap<String,Integer>();
|
||||
|
||||
boolean readBook(ZLFile file) {
|
||||
void readBook(ZLFile file) throws BookReadingException {
|
||||
myFilePrefix = MiscUtil.htmlDirectoryPrefix(file);
|
||||
|
||||
myIdToHref.clear();
|
||||
|
@ -80,8 +81,10 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
myGuideTOC.clear();
|
||||
myState = READ_NONE;
|
||||
|
||||
if (!read(file)) {
|
||||
return false;
|
||||
try {
|
||||
read(file);
|
||||
} catch (IOException e) {
|
||||
throw new BookReadingException(e, file);
|
||||
}
|
||||
|
||||
myModelReader.setMainTextModel();
|
||||
|
@ -92,7 +95,7 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
final ZLFile xhtmlFile = ZLFile.createFileByPath(myFilePrefix + name);
|
||||
if (xhtmlFile == null) {
|
||||
// NPE fix: null for bad attributes in .opf XML file
|
||||
return false;
|
||||
throw new BookReadingException("fileNotFound", myFilePrefix + name);
|
||||
}
|
||||
if (count++ == 0 && xhtmlFile.getPath().equals(myCoverFileName)) {
|
||||
continue;
|
||||
|
@ -102,13 +105,16 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
|
||||
myModelReader.addHyperlinkLabel(referenceName);
|
||||
myTOCLabels.put(referenceName, myModelReader.Model.BookTextModel.getParagraphsNumber());
|
||||
reader.readFile(xhtmlFile, referenceName + '#');
|
||||
try {
|
||||
reader.readFile(xhtmlFile, referenceName + '#');
|
||||
} catch (IOException e) {
|
||||
// skip
|
||||
e.printStackTrace();
|
||||
}
|
||||
myModelReader.insertEndOfSectionParagraph();
|
||||
}
|
||||
|
||||
generateTOC();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private BookModel.Label getTOCLabel(String id) {
|
||||
|
@ -128,33 +134,32 @@ class OEBBookReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
return myModelReader.Model.getLabel(num + id.substring(index));
|
||||
}
|
||||
|
||||
private void generateTOC() {
|
||||
private void generateTOC() throws BookReadingException {
|
||||
if (myNCXTOCFileName != null) {
|
||||
final NCXReader ncxReader = new NCXReader(myModelReader);
|
||||
if (ncxReader.readFile(myFilePrefix + myNCXTOCFileName)) {
|
||||
final Map<Integer,NCXReader.NavPoint> navigationMap = ncxReader.navigationMap();
|
||||
if (!navigationMap.isEmpty()) {
|
||||
int level = 0;
|
||||
for (NCXReader.NavPoint point : navigationMap.values()) {
|
||||
final BookModel.Label label = getTOCLabel(point.ContentHRef);
|
||||
int index = (label != null) ? label.ParagraphIndex : -1;
|
||||
while (level > point.Level) {
|
||||
myModelReader.endContentsParagraph();
|
||||
--level;
|
||||
}
|
||||
while (++level <= point.Level) {
|
||||
myModelReader.beginContentsParagraph(-2);
|
||||
myModelReader.addContentsData(Dots);
|
||||
}
|
||||
myModelReader.beginContentsParagraph(index);
|
||||
myModelReader.addContentsData(point.Text.toCharArray());
|
||||
}
|
||||
while (level > 0) {
|
||||
ncxReader.readFile(myFilePrefix + myNCXTOCFileName);
|
||||
final Map<Integer,NCXReader.NavPoint> navigationMap = ncxReader.navigationMap();
|
||||
if (!navigationMap.isEmpty()) {
|
||||
int level = 0;
|
||||
for (NCXReader.NavPoint point : navigationMap.values()) {
|
||||
final BookModel.Label label = getTOCLabel(point.ContentHRef);
|
||||
int index = (label != null) ? label.ParagraphIndex : -1;
|
||||
while (level > point.Level) {
|
||||
myModelReader.endContentsParagraph();
|
||||
--level;
|
||||
}
|
||||
return;
|
||||
while (++level <= point.Level) {
|
||||
myModelReader.beginContentsParagraph(-2);
|
||||
myModelReader.addContentsData(Dots);
|
||||
}
|
||||
myModelReader.beginContentsParagraph(index);
|
||||
myModelReader.addContentsData(point.Text.toCharArray());
|
||||
}
|
||||
while (level > 0) {
|
||||
myModelReader.endContentsParagraph();
|
||||
--level;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ class OEBCoverBackgroundReader extends ZLXMLReaderAdapter implements XMLNamespac
|
|||
myPathPrefix = MiscUtil.htmlDirectoryPrefix(file);
|
||||
myReadGuide = false;
|
||||
myImage = null;
|
||||
read(file);
|
||||
readQuietly(file);
|
||||
return myImage;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,12 +20,14 @@
|
|||
package org.geometerplus.fbreader.formats.oeb;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.constants.XMLNamespaces;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
import org.geometerplus.zlibrary.core.xml.*;
|
||||
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
|
||||
class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
||||
private final Book myBook;
|
||||
|
@ -52,12 +54,14 @@ class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
myBook.setLanguage(null);
|
||||
}
|
||||
|
||||
boolean readMetaInfo(ZLFile file) {
|
||||
void readMetaInfo(ZLFile file) throws BookReadingException {
|
||||
myReadMetaData = false;
|
||||
myReadState = READ_NONE;
|
||||
|
||||
if (!ZLXMLProcessor.read(this, file, 512)) {
|
||||
return false;
|
||||
try {
|
||||
ZLXMLProcessor.read(this, file, 512);
|
||||
} catch (IOException e) {
|
||||
throw new BookReadingException(e, file);
|
||||
}
|
||||
|
||||
final ArrayList<String> authors = myAuthorList.isEmpty() ? myAuthorList2 : myAuthorList;
|
||||
|
@ -70,8 +74,6 @@ class OEBMetaInfoReader extends ZLXMLReaderAdapter implements XMLNamespaces {
|
|||
}
|
||||
myBook.addAuthor(a);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final int READ_NONE = 0;
|
||||
|
|
|
@ -19,18 +19,20 @@
|
|||
|
||||
package org.geometerplus.fbreader.formats.oeb;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.formats.JavaFormatPlugin;
|
||||
import org.geometerplus.zlibrary.core.filesystem.*;
|
||||
import org.geometerplus.zlibrary.core.image.ZLImage;
|
||||
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.formats.JavaFormatPlugin;
|
||||
|
||||
public class OEBPlugin extends JavaFormatPlugin {
|
||||
public OEBPlugin() {
|
||||
super("ePub");
|
||||
}
|
||||
|
||||
private ZLFile getOpfFile(ZLFile oebFile) {
|
||||
private ZLFile getOpfFile(ZLFile oebFile) throws BookReadingException {
|
||||
if ("opf".equals(oebFile.getExtension())) {
|
||||
return oebFile;
|
||||
}
|
||||
|
@ -38,7 +40,7 @@ public class OEBPlugin extends JavaFormatPlugin {
|
|||
final ZLFile containerInfoFile = ZLFile.createFile(oebFile, "META-INF/container.xml");
|
||||
if (containerInfoFile.exists()) {
|
||||
final ContainerFileReader reader = new ContainerFileReader();
|
||||
reader.read(containerInfoFile);
|
||||
reader.readQuietly(containerInfoFile);
|
||||
final String opfPath = reader.getRootPath();
|
||||
if (opfPath != null) {
|
||||
return ZLFile.createFile(oebFile, opfPath);
|
||||
|
@ -50,31 +52,35 @@ public class OEBPlugin extends JavaFormatPlugin {
|
|||
return child;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
throw new BookReadingException("opfFileNotFound");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readMetaInfo(Book book) {
|
||||
final ZLFile opfFile = getOpfFile(book.File);
|
||||
return (opfFile != null) ? new OEBMetaInfoReader(book).readMetaInfo(opfFile) : false;
|
||||
public void readMetaInfo(Book book) throws BookReadingException {
|
||||
new OEBMetaInfoReader(book).readMetaInfo(getOpfFile(book.File));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readModel(BookModel model) {
|
||||
public void readModel(BookModel model) throws BookReadingException {
|
||||
model.Book.File.setCached(true);
|
||||
final ZLFile opfFile = getOpfFile(model.Book.File);
|
||||
return (opfFile != null) ? new OEBBookReader(model).readBook(opfFile) : false;
|
||||
new OEBBookReader(model).readBook(getOpfFile(model.Book.File));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZLImage readCover(ZLFile file) {
|
||||
final ZLFile opfFile = getOpfFile(file);
|
||||
return (opfFile != null) ? new OEBCoverReader().readCover(opfFile) : null;
|
||||
try {
|
||||
return new OEBCoverReader().readCover(getOpfFile(file));
|
||||
} catch (BookReadingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readAnnotation(ZLFile file) {
|
||||
final ZLFile opfFile = getOpfFile(file);
|
||||
return (opfFile != null) ? new OEBAnnotationReader().readAnnotation(opfFile) : null;
|
||||
try {
|
||||
return new OEBAnnotationReader().readAnnotation(getOpfFile(file));
|
||||
} catch (BookReadingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ class XHTMLImageFinder extends ZLXMLReaderAdapter {
|
|||
ZLFileImage readImage(ZLFile file) {
|
||||
myXHTMLPathPrefix = MiscUtil.htmlDirectoryPrefix(file);
|
||||
myImage = null;
|
||||
read(file);
|
||||
readQuietly(file);
|
||||
return myImage;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.geometerplus.zlibrary.core.util.MimeType;
|
|||
|
||||
import org.geometerplus.fbreader.library.Book;
|
||||
import org.geometerplus.fbreader.bookmodel.BookModel;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
import org.geometerplus.fbreader.formats.JavaFormatPlugin;
|
||||
|
||||
public class MobipocketPlugin extends JavaFormatPlugin {
|
||||
|
@ -38,14 +39,14 @@ public class MobipocketPlugin extends JavaFormatPlugin {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean readMetaInfo(Book book) {
|
||||
public void readMetaInfo(Book book) throws BookReadingException {
|
||||
InputStream stream = null;
|
||||
try {
|
||||
stream = book.File.getInputStream();
|
||||
final PdbHeader header = new PdbHeader(stream);
|
||||
PdbUtil.skip(stream, header.Offsets[0] + 16 - header.length());
|
||||
if (PdbUtil.readInt(stream) != 0x4D4F4249) /* "MOBI" */ {
|
||||
return false;
|
||||
throw new BookReadingException("unsupportedFileFormat");
|
||||
}
|
||||
final int length = (int)PdbUtil.readInt(stream);
|
||||
PdbUtil.skip(stream, 4);
|
||||
|
@ -111,9 +112,8 @@ public class MobipocketPlugin extends JavaFormatPlugin {
|
|||
final byte[] titleBuffer = new byte[fullNameLength];
|
||||
stream.read(titleBuffer);
|
||||
book.setTitle(new String(titleBuffer, encodingName));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
throw new BookReadingException(e, book.File);
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
|
@ -125,12 +125,11 @@ public class MobipocketPlugin extends JavaFormatPlugin {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean readModel(BookModel model) {
|
||||
public void readModel(BookModel model) throws BookReadingException {
|
||||
try {
|
||||
return new MobipocketHtmlBookReader(model).readBook();
|
||||
new MobipocketHtmlBookReader(model).readBook();
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
return false;
|
||||
throw new BookReadingException(e, model.Book.File);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.geometerplus.fbreader.formats.xhtml;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.xml.*;
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
@ -162,7 +163,7 @@ public class XHTMLReader extends ZLXMLReaderAdapter {
|
|||
return num;
|
||||
}
|
||||
|
||||
public boolean readFile(ZLFile file, String referencePrefix) {
|
||||
public void readFile(ZLFile file, String referencePrefix) throws IOException {
|
||||
fillTagTable();
|
||||
|
||||
myReferencePrefix = referencePrefix;
|
||||
|
@ -177,7 +178,7 @@ public class XHTMLReader extends ZLXMLReaderAdapter {
|
|||
myPreformatted = false;
|
||||
myInsideBody = false;
|
||||
|
||||
return read(file);
|
||||
read(file);
|
||||
}
|
||||
|
||||
private final HashMap<String,XHTMLTagAction> myActions = new HashMap<String,XHTMLTagAction>();
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.geometerplus.zlibrary.core.image.ZLImage;
|
|||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||
|
||||
import org.geometerplus.fbreader.formats.*;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
|
||||
import org.geometerplus.fbreader.Paths;
|
||||
|
||||
|
@ -150,7 +151,12 @@ public class Book {
|
|||
myIsSaved = false;
|
||||
|
||||
final FormatPlugin plugin = PluginCollection.Instance().getPlugin(File);
|
||||
if (plugin == null || !plugin.readMetaInfo(this)) {
|
||||
if (plugin == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
plugin.readMetaInfo(this);
|
||||
} catch (BookReadingException e) {
|
||||
return false;
|
||||
}
|
||||
if (myTitle == null || myTitle.length() == 0) {
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.geometerplus.fbreader.network.opds;
|
|||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLPhysicalFile;
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
|
||||
|
@ -101,9 +102,9 @@ public class OPDSLinkReader {
|
|||
}
|
||||
|
||||
try {
|
||||
xmlReader.read(new FileInputStream(catalogsFile));
|
||||
xmlReader.read(new ZLPhysicalFile(catalogsFile));
|
||||
return xmlReader.links();
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (IOException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ public class TipsManager {
|
|||
final ZLFile file = ZLFile.createFileByPath(getLocalFilePath());
|
||||
if (file.exists()) {
|
||||
final TipsFeedHandler handler = new TipsFeedHandler();
|
||||
new ATOMXMLReader(handler, false).read(file);
|
||||
new ATOMXMLReader(handler, false).readQuietly(file);
|
||||
final List<Tip> tips = Collections.unmodifiableList(handler.Tips);
|
||||
if (tips.size() > 0) {
|
||||
myTips = tips;
|
||||
|
|
|
@ -44,14 +44,14 @@ public final class ZLKeyBindings {
|
|||
public ZLKeyBindings(String name) {
|
||||
myName = name;
|
||||
final Set<String> keys = new TreeSet<String>();
|
||||
new Reader(keys).read(ZLFile.createFileByPath("default/keymap.xml"));
|
||||
new Reader(keys).readQuietly(ZLFile.createFileByPath("default/keymap.xml"));
|
||||
try {
|
||||
new Reader(keys).read(ZLFile.createFileByPath(Paths.systemShareDirectory() + "/keymap.xml"));
|
||||
new Reader(keys).readQuietly(ZLFile.createFileByPath(Paths.systemShareDirectory() + "/keymap.xml"));
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
new Reader(keys).read(ZLFile.createFileByPath(Paths.BooksDirectoryOption().getValue() + "/keymap.xml"));
|
||||
new Reader(keys).readQuietly(ZLFile.createFileByPath(Paths.BooksDirectoryOption().getValue() + "/keymap.xml"));
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ public final class ZLEncodingCollection {
|
|||
private final HashMap<String,String> myEncodingByAlias = new HashMap<String,String>();
|
||||
|
||||
private ZLEncodingCollection() {
|
||||
new ZLEncodingCollectionReader().read(
|
||||
new ZLEncodingCollectionReader().readQuietly(
|
||||
ZLResourceFile.createResourceFile("encodings/Encodings.xml")
|
||||
);
|
||||
}
|
||||
|
|
|
@ -195,7 +195,7 @@ final class ZLTreeResource extends ZLResource {
|
|||
public void readDocument(ZLTreeResource root, ZLFile file) {
|
||||
myStack.clear();
|
||||
myStack.add(root);
|
||||
read(file);
|
||||
readQuietly(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -33,42 +33,33 @@ public abstract class ZLXMLProcessor {
|
|||
}
|
||||
}
|
||||
|
||||
public static boolean read(ZLXMLReader reader, InputStream stream, int bufferSize) {
|
||||
public static void read(ZLXMLReader reader, InputStream stream, int bufferSize) throws IOException {
|
||||
ZLXMLParser parser = null;
|
||||
try {
|
||||
parser = new ZLXMLParser(reader, stream, bufferSize);
|
||||
reader.startDocumentHandler();
|
||||
parser.doIt();
|
||||
reader.endDocumentHandler();
|
||||
} catch (IOException e) {
|
||||
//System.out.println(e);
|
||||
return false;
|
||||
} finally {
|
||||
if (parser != null) {
|
||||
parser.finish();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean read(ZLXMLReader xmlReader, ZLFile file) {
|
||||
return read(xmlReader, file, 65536);
|
||||
public static void read(ZLXMLReader xmlReader, ZLFile file) throws IOException {
|
||||
read(xmlReader, file, 65536);
|
||||
}
|
||||
|
||||
public static boolean read(ZLXMLReader xmlReader, ZLFile file, int bufferSize) {
|
||||
InputStream stream = null;
|
||||
public static void read(ZLXMLReader xmlReader, ZLFile file, int bufferSize) throws IOException {
|
||||
InputStream stream = file.getInputStream();
|
||||
try {
|
||||
stream = file.getInputStream();
|
||||
} catch (IOException e) {
|
||||
read(xmlReader, stream, bufferSize);
|
||||
} finally {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
if (stream == null) {
|
||||
return false;
|
||||
}
|
||||
boolean code = read(xmlReader, stream, bufferSize);
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,18 +21,28 @@ package org.geometerplus.zlibrary.core.xml;
|
|||
|
||||
import java.util.*;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
||||
public abstract class ZLXMLReaderAdapter implements ZLXMLReader {
|
||||
private Map<String,String> myNamespaceMap = Collections.emptyMap();
|
||||
|
||||
public boolean read(ZLFile file) {
|
||||
return ZLXMLProcessor.read(this, file);
|
||||
public boolean readQuietly(ZLFile file) {
|
||||
try {
|
||||
ZLXMLProcessor.read(this, file);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean read(InputStream stream) {
|
||||
return ZLXMLProcessor.read(this, stream, 65536);
|
||||
public void read(ZLFile file) throws IOException {
|
||||
ZLXMLProcessor.read(this, file);
|
||||
}
|
||||
|
||||
public void read(InputStream stream) throws IOException {
|
||||
ZLXMLProcessor.read(this, stream, 65536);
|
||||
}
|
||||
|
||||
public boolean dontCacheAttributeValues() {
|
||||
|
|
|
@ -61,7 +61,7 @@ final class ZLTextTeXHyphenator extends ZLTextHyphenator {
|
|||
unload();
|
||||
|
||||
if (language != null) {
|
||||
new ZLTextHyphenationReader(this).read(ZLResourceFile.createResourceFile(
|
||||
new ZLTextHyphenationReader(this).readQuietly(ZLResourceFile.createResourceFile(
|
||||
"hyphenationPatterns/" + language + ".pattern"
|
||||
));
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class ZLTextStyleCollection {
|
|||
private final ZLTextStyleDecoration[] myDecorationMap = new ZLTextStyleDecoration[256];
|
||||
|
||||
private ZLTextStyleCollection() {
|
||||
new TextStyleReader(this).read(ZLResourceFile.createResourceFile("default/styles.xml"));
|
||||
new TextStyleReader(this).readQuietly(ZLResourceFile.createResourceFile("default/styles.xml"));
|
||||
}
|
||||
|
||||
public static ZLTextStyleCollection Instance() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue