diff --git a/jni/NativeFormats/JavaNativeFormatPlugin.cpp b/jni/NativeFormats/JavaNativeFormatPlugin.cpp index 0378ca893..104e8475e 100644 --- a/jni/NativeFormats/JavaNativeFormatPlugin.cpp +++ b/jni/NativeFormats/JavaNativeFormatPlugin.cpp @@ -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 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 plugin = findCppPlugin(env, thiz); if (plugin.isNull()) { return JNI_FALSE; diff --git a/src/org/geometerplus/android/fbreader/DictionaryUtil.java b/src/org/geometerplus/android/fbreader/DictionaryUtil.java index 720be9114..0b0dae39f 100644 --- a/src/org/geometerplus/android/fbreader/DictionaryUtil.java +++ b/src/org/geometerplus/android/fbreader/DictionaryUtil.java @@ -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); diff --git a/src/org/geometerplus/fbreader/bookmodel/BookModel.java b/src/org/geometerplus/fbreader/bookmodel/BookModel.java index 43c7a392b..7edc15ed2 100644 --- a/src/org/geometerplus/fbreader/bookmodel/BookModel.java +++ b/src/org/geometerplus/fbreader/bookmodel/BookModel.java @@ -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; diff --git a/src/org/geometerplus/fbreader/bookmodel/BookReadingException.java b/src/org/geometerplus/fbreader/bookmodel/BookReadingException.java new file mode 100644 index 000000000..cf0a299b4 --- /dev/null +++ b/src/org/geometerplus/fbreader/bookmodel/BookReadingException.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2012 Geometer Plus + * + * 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); + } +} diff --git a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java index 332517621..bc5602fb9 100644 --- a/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java +++ b/src/org/geometerplus/fbreader/fbreader/FBReaderApp.java @@ -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(); diff --git a/src/org/geometerplus/fbreader/fbreader/TapZoneMap.java b/src/org/geometerplus/fbreader/fbreader/TapZoneMap.java index 1392d90fc..d0a62ee61 100644 --- a/src/org/geometerplus/fbreader/fbreader/TapZoneMap.java +++ b/src/org/geometerplus/fbreader/fbreader/TapZoneMap.java @@ -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) { diff --git a/src/org/geometerplus/fbreader/formats/FormatPlugin.java b/src/org/geometerplus/fbreader/formats/FormatPlugin.java index f21fbdcc3..606da6d7f 100644 --- a/src/org/geometerplus/fbreader/formats/FormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/FormatPlugin.java @@ -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); diff --git a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java index 8847fdd7c..6c764a35c 100644 --- a/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java +++ b/src/org/geometerplus/fbreader/formats/NativeFormatPlugin.java @@ -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) { diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2AnnotationReader.java b/src/org/geometerplus/fbreader/formats/fb2/FB2AnnotationReader.java index 3fe688287..5a764d2fe 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2AnnotationReader.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2AnnotationReader.java @@ -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; + } } } diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2CoverImage.java b/src/org/geometerplus/fbreader/formats/fb2/FB2CoverImage.java index cbc650fd5..435c157cc 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2CoverImage.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2CoverImage.java @@ -56,7 +56,7 @@ class FB2CoverImage extends ZLImageProxy { Base64EncodedImage readCover(ZLFile file) { myReadCoverPage = false; myImageReference = null; - read(file); + readQuietly(file); return myImage; } diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2MetaInfoReader.java b/src/org/geometerplus/fbreader/formats/fb2/FB2MetaInfoReader.java index 1f7657e59..0040dcca2 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2MetaInfoReader.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2MetaInfoReader.java @@ -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); - } } diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java b/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java index 6fa49abcf..e65868385 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2Plugin.java @@ -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 diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2Reader.java b/src/org/geometerplus/fbreader/formats/fb2/FB2Reader.java index 9f8195d46..fd4199d14 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2Reader.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2Reader.java @@ -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() { diff --git a/src/org/geometerplus/fbreader/formats/fb2/FB2TagManager.java b/src/org/geometerplus/fbreader/formats/fb2/FB2TagManager.java index 71b35d3be..82da7e65c 100644 --- a/src/org/geometerplus/fbreader/formats/fb2/FB2TagManager.java +++ b/src/org/geometerplus/fbreader/formats/fb2/FB2TagManager.java @@ -31,7 +31,7 @@ abstract class FB2TagManager { static ArrayList humanReadableTags(String id) { if (ourMap.isEmpty()) { - new FB2TagInfoReader().read( + new FB2TagInfoReader().readQuietly( ZLResourceFile.createResourceFile("formats/fb2/fb2genres.xml") ); } diff --git a/src/org/geometerplus/fbreader/formats/oeb/NCXReader.java b/src/org/geometerplus/fbreader/formats/oeb/NCXReader.java index 9007f612e..97e5cdf1d 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/NCXReader.java +++ b/src/org/geometerplus/fbreader/formats/oeb/NCXReader.java @@ -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 navigationMap() { diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBAnnotationReader.java b/src/org/geometerplus/fbreader/formats/oeb/OEBAnnotationReader.java index e59d51ecc..a61483c6e 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBAnnotationReader.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBAnnotationReader.java @@ -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 diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBBookReader.java b/src/org/geometerplus/fbreader/formats/oeb/OEBBookReader.java index 50263bdc8..e13b314b5 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBBookReader.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBBookReader.java @@ -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 myFileNumbers = new HashMap(); private HashMap myTOCLabels = new HashMap(); - 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 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 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; } } diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBCoverBackgroundReader.java b/src/org/geometerplus/fbreader/formats/oeb/OEBCoverBackgroundReader.java index 03656342b..272a35a96 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBCoverBackgroundReader.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBCoverBackgroundReader.java @@ -36,7 +36,7 @@ class OEBCoverBackgroundReader extends ZLXMLReaderAdapter implements XMLNamespac myPathPrefix = MiscUtil.htmlDirectoryPrefix(file); myReadGuide = false; myImage = null; - read(file); + readQuietly(file); return myImage; } diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBMetaInfoReader.java b/src/org/geometerplus/fbreader/formats/oeb/OEBMetaInfoReader.java index 7654ba71f..3c83bf0ba 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBMetaInfoReader.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBMetaInfoReader.java @@ -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 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; diff --git a/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java b/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java index 95f080a38..2bb6a30c3 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java +++ b/src/org/geometerplus/fbreader/formats/oeb/OEBPlugin.java @@ -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; + } } } diff --git a/src/org/geometerplus/fbreader/formats/oeb/XHTMLImageFinder.java b/src/org/geometerplus/fbreader/formats/oeb/XHTMLImageFinder.java index 6a0b1a69d..51f3a6680 100644 --- a/src/org/geometerplus/fbreader/formats/oeb/XHTMLImageFinder.java +++ b/src/org/geometerplus/fbreader/formats/oeb/XHTMLImageFinder.java @@ -47,7 +47,7 @@ class XHTMLImageFinder extends ZLXMLReaderAdapter { ZLFileImage readImage(ZLFile file) { myXHTMLPathPrefix = MiscUtil.htmlDirectoryPrefix(file); myImage = null; - read(file); + readQuietly(file); return myImage; } diff --git a/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java b/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java index 821d0127b..2120b2cf6 100644 --- a/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java +++ b/src/org/geometerplus/fbreader/formats/pdb/MobipocketPlugin.java @@ -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); } } diff --git a/src/org/geometerplus/fbreader/formats/xhtml/XHTMLReader.java b/src/org/geometerplus/fbreader/formats/xhtml/XHTMLReader.java index afc21bf59..95d9f20f2 100644 --- a/src/org/geometerplus/fbreader/formats/xhtml/XHTMLReader.java +++ b/src/org/geometerplus/fbreader/formats/xhtml/XHTMLReader.java @@ -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 myActions = new HashMap(); diff --git a/src/org/geometerplus/fbreader/library/Book.java b/src/org/geometerplus/fbreader/library/Book.java index 75e16c9a7..62a57fc73 100644 --- a/src/org/geometerplus/fbreader/library/Book.java +++ b/src/org/geometerplus/fbreader/library/Book.java @@ -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) { diff --git a/src/org/geometerplus/fbreader/network/opds/OPDSLinkReader.java b/src/org/geometerplus/fbreader/network/opds/OPDSLinkReader.java index 03cc2b3ca..f4c517d16 100644 --- a/src/org/geometerplus/fbreader/network/opds/OPDSLinkReader.java +++ b/src/org/geometerplus/fbreader/network/opds/OPDSLinkReader.java @@ -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(); } } diff --git a/src/org/geometerplus/fbreader/tips/TipsManager.java b/src/org/geometerplus/fbreader/tips/TipsManager.java index 899d0db36..b6a1a35be 100644 --- a/src/org/geometerplus/fbreader/tips/TipsManager.java +++ b/src/org/geometerplus/fbreader/tips/TipsManager.java @@ -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 tips = Collections.unmodifiableList(handler.Tips); if (tips.size() > 0) { myTips = tips; diff --git a/src/org/geometerplus/zlibrary/core/application/ZLKeyBindings.java b/src/org/geometerplus/zlibrary/core/application/ZLKeyBindings.java index 4243a7753..d95f3c6ef 100644 --- a/src/org/geometerplus/zlibrary/core/application/ZLKeyBindings.java +++ b/src/org/geometerplus/zlibrary/core/application/ZLKeyBindings.java @@ -44,14 +44,14 @@ public final class ZLKeyBindings { public ZLKeyBindings(String name) { myName = name; final Set keys = new TreeSet(); - 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 } diff --git a/src/org/geometerplus/zlibrary/core/encoding/ZLEncodingCollection.java b/src/org/geometerplus/zlibrary/core/encoding/ZLEncodingCollection.java index 74c6ac771..bab1676d3 100644 --- a/src/org/geometerplus/zlibrary/core/encoding/ZLEncodingCollection.java +++ b/src/org/geometerplus/zlibrary/core/encoding/ZLEncodingCollection.java @@ -38,7 +38,7 @@ public final class ZLEncodingCollection { private final HashMap myEncodingByAlias = new HashMap(); private ZLEncodingCollection() { - new ZLEncodingCollectionReader().read( + new ZLEncodingCollectionReader().readQuietly( ZLResourceFile.createResourceFile("encodings/Encodings.xml") ); } diff --git a/src/org/geometerplus/zlibrary/core/resources/ZLTreeResource.java b/src/org/geometerplus/zlibrary/core/resources/ZLTreeResource.java index 0e73c8390..8ccb2b63c 100644 --- a/src/org/geometerplus/zlibrary/core/resources/ZLTreeResource.java +++ b/src/org/geometerplus/zlibrary/core/resources/ZLTreeResource.java @@ -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 diff --git a/src/org/geometerplus/zlibrary/core/xml/ZLXMLProcessor.java b/src/org/geometerplus/zlibrary/core/xml/ZLXMLProcessor.java index ad27e33c1..e9a5bc67c 100644 --- a/src/org/geometerplus/zlibrary/core/xml/ZLXMLProcessor.java +++ b/src/org/geometerplus/zlibrary/core/xml/ZLXMLProcessor.java @@ -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; } } diff --git a/src/org/geometerplus/zlibrary/core/xml/ZLXMLReaderAdapter.java b/src/org/geometerplus/zlibrary/core/xml/ZLXMLReaderAdapter.java index 4fe1c0023..3011627f7 100644 --- a/src/org/geometerplus/zlibrary/core/xml/ZLXMLReaderAdapter.java +++ b/src/org/geometerplus/zlibrary/core/xml/ZLXMLReaderAdapter.java @@ -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 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() { diff --git a/src/org/geometerplus/zlibrary/text/hyphenation/ZLTextTeXHyphenator.java b/src/org/geometerplus/zlibrary/text/hyphenation/ZLTextTeXHyphenator.java index 5d9a9ea1f..ac1a0b6e9 100644 --- a/src/org/geometerplus/zlibrary/text/hyphenation/ZLTextTeXHyphenator.java +++ b/src/org/geometerplus/zlibrary/text/hyphenation/ZLTextTeXHyphenator.java @@ -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" )); } diff --git a/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java b/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java index e3ff43233..5e97ad01d 100644 --- a/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java +++ b/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java @@ -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() {