diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index 9470e02..1c8e8b0 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -32,19 +32,19 @@ METHOD0(void, allocate) { SET_PTR(std::make_shared()); -} +} CATCH_EXCEPTION() DISPOSE METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) { THIS->update(*getPtr(env, otherBook)); -} +} CATCH_EXCEPTION() METHOD(void, update__Lorg_kiwix_libzim_Archive_2, jobject archive) { THIS->update(*getPtr(env, archive)); -} +} CATCH_EXCEPTION() GETTER(jstring, getId) @@ -76,7 +76,7 @@ GETTER(jstring, getCategory) GETTER(jstring, getTags) -METHOD(jstring, getTagStr, jstring tagName) try { +METHOD(jstring, getTagStr, jstring tagName) { return TO_JNI(THIS->getTagStr(TO_C(tagName))); } catch(...) { return c2jni("", env); @@ -97,8 +97,8 @@ METHOD0(jobjectArray, getIllustrations) { env->SetObjectArrayElement(retArray, index++, wrapper); } return retArray; -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, getIllustration, jint size) { return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size))); -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libkiwix/bookmark.cpp b/lib/src/main/cpp/libkiwix/bookmark.cpp index f14300c..fe61d18 100644 --- a/lib/src/main/cpp/libkiwix/bookmark.cpp +++ b/lib/src/main/cpp/libkiwix/bookmark.cpp @@ -31,7 +31,7 @@ METHOD0(void, setNativeBookmark) { SET_PTR(std::make_shared()); -} +} CATCH_EXCEPTION() DISPOSE @@ -49,24 +49,24 @@ GETTER(jstring, getDate) METHOD(void, setBookId, jstring bookId) { THIS->setBookId(TO_C(bookId)); -} +} CATCH_EXCEPTION() METHOD(void, setBookTitle, jstring bookTitle) { THIS->setBookTitle(TO_C(bookTitle)); -} +} CATCH_EXCEPTION() METHOD(void, setUrl, jstring url) { THIS->setUrl(TO_C(url)); -} +} CATCH_EXCEPTION() METHOD(void, setTitle, jstring title) { THIS->setTitle(TO_C(title)); -} +} CATCH_EXCEPTION() METHOD(void, setLanguage, jstring lang) { THIS->setLanguage(TO_C(lang)); -} +} CATCH_EXCEPTION() METHOD(void, setDate, jstring date) { THIS->setDate(TO_C(date)); -} +} CATCH_EXCEPTION() diff --git a/lib/src/main/cpp/libkiwix/filter.cpp b/lib/src/main/cpp/libkiwix/filter.cpp index 4e4a5f3..3b818ba 100644 --- a/lib/src/main/cpp/libkiwix/filter.cpp +++ b/lib/src/main/cpp/libkiwix/filter.cpp @@ -33,7 +33,7 @@ /* Kiwix Reader JNI functions */ METHOD0(void, allocate) { SET_PTR(std::make_shared()); -} +} CATCH_EXCEPTION() DISPOSE @@ -41,13 +41,13 @@ DISPOSE METHOD(jobject, name, args_type value) { \ THIS->name(jni2c(value, env)); \ return thisObj; \ -} +} CATCH_EXCEPTION(nullptr) #define FORWARDA(name, args_type) \ METHOD(jobject, name, jobjectArray value) { \ THIS->name(jni2c(value, env)); \ return thisObj; \ -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libkiwix/illustration.cpp b/lib/src/main/cpp/libkiwix/illustration.cpp index 5fa51d8..78ede04 100644 --- a/lib/src/main/cpp/libkiwix/illustration.cpp +++ b/lib/src/main/cpp/libkiwix/illustration.cpp @@ -32,22 +32,22 @@ METHOD0(void, dispose) { dispose(env, thisObj); -} +} CATCH_EXCEPTION() METHOD0(jint, width) { return TO_JNI(THIS->width); -} +} CATCH_EXCEPTION(0) METHOD0(jint, height) { return TO_JNI(THIS->height); -} +} CATCH_EXCEPTION(0) METHOD0(jstring, mimeType) { return TO_JNI(THIS->mimeType); -} +} CATCH_EXCEPTION(nullptr) METHOD0(jstring, url) { return TO_JNI(THIS->url); -} +} CATCH_EXCEPTION(nullptr) GETTER(jstring, getData) diff --git a/lib/src/main/cpp/libkiwix/kiwixicu.cpp b/lib/src/main/cpp/libkiwix/kiwixicu.cpp index bc94cf6..546f183 100644 --- a/lib/src/main/cpp/libkiwix/kiwixicu.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixicu.cpp @@ -28,11 +28,9 @@ #include "zim/tools.h" JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( - JNIEnv* env, jclass kclass, jstring dirStr) + JNIEnv* env, jclass kclass, jstring dirStr) try { - try { - zim::setICUDataDirectory(TO_C(dirStr)); - } catch (...) { - std::cerr << "Unable to set data directory " << TO_C(dirStr) << std::endl; - } + zim::setICUDataDirectory(TO_C(dirStr)); +} catch(...) { + throwException(env, "java/lang/Exception", "Unable to set data directory"); } diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp index 289e203..ae46afa 100644 --- a/lib/src/main/cpp/libkiwix/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -34,15 +34,11 @@ /* Kiwix Reader JNI functions */ METHOD(void, setNativeServer, jobject jLibrary) { - LOG("Attempting to create server"); - try { - auto library = getPtr(env, jLibrary); - SET_PTR(std::make_shared(library.get())); - } catch (std::exception& e) { - LOG("Error creating the server"); - LOG("%s", e.what()); - } -} + auto library = getPtr(env, jLibrary); + SET_PTR(std::make_shared(library.get())); +} catch (std::exception& e) { + throwException(env, "java/lang/Exception", "Error creating the server"); +} CATCH_EXCEPTION() DISPOSE @@ -51,39 +47,39 @@ DISPOSE METHOD(void, setRoot, jstring root) { THIS->setRoot(TO_C(root)); -} +} CATCH_EXCEPTION() METHOD(void, setAddress, jstring address) { THIS->setAddress(TO_C(address)); -} +} CATCH_EXCEPTION() METHOD(void, setPort, int port) { THIS->setPort(TO_C(port)); -} +} CATCH_EXCEPTION() METHOD(void, setNbThreads, int threads) { THIS->setNbThreads(TO_C(threads)); -} +} CATCH_EXCEPTION() METHOD(void, setTaskbar, jboolean withTaskbar, jboolean withLibraryButton) { THIS->setTaskbar(TO_C(withTaskbar), TO_C(withLibraryButton)); -} +} CATCH_EXCEPTION() METHOD(void, setBlockExternalLinks, jboolean blockExternalLinks) { THIS->setBlockExternalLinks(TO_C(blockExternalLinks)); -} +} CATCH_EXCEPTION() METHOD0(jboolean, start) { return THIS->start(); -} +} CATCH_EXCEPTION(false) METHOD0(void, stop) { THIS->stop(); -} +} CATCH_EXCEPTION() diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 0eb04af..1ce3ccd 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -32,7 +32,7 @@ METHOD0(void, setNativeHandler) { SET_PTR(std::make_shared()); -} +} CATCH_EXCEPTION() DISPOSE @@ -41,43 +41,41 @@ METHOD(jboolean, addBook, jobject book) { auto cBook = getPtr(env, book); - try { - return THIS->addBook(*cBook); - } catch (std::exception& e) { - LOG("Unable to add the book"); - LOG("%s", e.what()); } - return false; -} + return THIS->addBook(*cBook); +} CATCH_EXCEPTION(false) METHOD(jobject, getBookById, jstring id) { return BUILD_WRAPPER2("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, getArchiveById, jstring id) { - return BUILD_WRAPPER("org/kiwix/libzim/Archive", THIS->getArchiveById(TO_C(id))); -} + auto archive = THIS->getArchiveById(TO_C(id)); + std::cout << "archive is " << archive << std::endl; + return BUILD_WRAPPER2("org/kiwix/libzim/Archive", archive); +} CATCH_EXCEPTION(nullptr) METHOD(jboolean, removeBookById, jstring id) { return TO_JNI(THIS->removeBookById(TO_C(id))); -} +} CATCH_EXCEPTION(false) METHOD(jboolean, writeToFile, jstring path) { return TO_JNI(THIS->writeToFile(TO_C(path))); -} +} CATCH_EXCEPTION(false) + METHOD(jboolean, writeBookmarksToFile, jstring path) { return TO_JNI(THIS->writeBookmarksToFile(TO_C(path))); -} +} CATCH_EXCEPTION(false) METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { return TO_JNI(THIS->getBookCount(TO_C(localBooks), TO_C(remoteBooks))); -} +} CATCH_EXCEPTION(0) GETTER(jobjectArray, getBooksIds) METHOD(jobjectArray, filter, jobject filterObj) { auto filter = getPtr(env, filterObj); return c2jni(THIS->filter(*filter), env); -} +} CATCH_EXCEPTION(nullptr) GETTER(jobjectArray, getBooksLanguages) GETTER(jobjectArray, getBooksCategories) @@ -87,11 +85,11 @@ GETTER(jobjectArray, getBooksPublishers) METHOD(void, addBookmark, jobject bookmark) { auto cBookmark = getPtr(env, bookmark); THIS->addBookmark(*cBookmark); -} +} CATCH_EXCEPTION() METHOD(jboolean, removeBookmark, jstring zimId, jstring url) { return TO_JNI(THIS->removeBookmark(TO_C(zimId), TO_C(url))); -} +} CATCH_EXCEPTION(false) METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) { auto bookmarks = THIS->getBookmarks(TO_C(onlyValidBookmarks)); @@ -108,4 +106,4 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) { env->SetObjectArrayElement(retArray, index++, wrapper); } return retArray; -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libkiwix/manager.cpp b/lib/src/main/cpp/libkiwix/manager.cpp index cc9a9bd..01da4da 100644 --- a/lib/src/main/cpp/libkiwix/manager.cpp +++ b/lib/src/main/cpp/libkiwix/manager.cpp @@ -32,7 +32,7 @@ METHOD(void, allocate, jobject libraryObj) { auto lib = getPtr(env, libraryObj); SET_PTR(std::make_shared(lib.get())); -} +} CATCH_EXCEPTION() DISPOSE @@ -41,58 +41,30 @@ METHOD(jboolean, readFile, jstring path) { auto cPath = TO_C(path); - try { - return THIS->readFile(cPath); - } catch (std::exception& e) { - LOG("Unable to get readFile"); - LOG("%s", e.what()); - } - return false; -} + return THIS->readFile(cPath); +} CATCH_EXCEPTION(false) METHOD(jboolean, readXml, jstring content, jstring libraryPath) { auto cContent = TO_C(content); auto cPath = TO_C(libraryPath); - try { - return THIS->readXml(cContent, false, cPath); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG("%s", e.what()); - } - - return false; -} + return THIS->readXml(cContent, false, cPath); +} CATCH_EXCEPTION(false) METHOD(jboolean, readOpds, jstring content, jstring urlHost) { auto cContent = TO_C(content); auto cUrl = TO_C(urlHost); - try { - return THIS->readOpds(cContent, cUrl); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG("%s", e.what()); - } - - return false; -} + return THIS->readOpds(cContent, cUrl); +} CATCH_EXCEPTION(false) METHOD(jboolean, readBookmarkFile, jstring path) { auto cPath = TO_C(path); - - try { - return THIS->readBookmarkFile(cPath); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG("%s", e.what()); - } - - return false; -} + return THIS->readBookmarkFile(cPath); +} CATCH_EXCEPTION(false) METHOD(jstring, addBookFromPath, jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) { @@ -101,15 +73,10 @@ METHOD(jstring, addBookFromPath, jstring pathToOpen, jstring pathToSave, jstring auto cUrl = TO_C(url); jstring id = NULL; - try { - auto cId = THIS->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); - if ( !cId.empty() ) { - id = c2jni(cId, env); - } - } catch (std::exception& e) { - LOG("Unable to get ZIM file size"); - LOG("%s", e.what()); + auto cId = THIS->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); + if ( !cId.empty() ) { + id = c2jni(cId, env); } return id; -} +} CATCH_EXCEPTION(0) diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 870d535..5d61a19 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -40,15 +40,9 @@ METHOD(void, setNativeArchive, jstring filename) { std::string cPath = TO_C(filename); - LOG("Attempting to create reader with: %s", cPath.c_str()); - try { - auto archive = std::make_shared(cPath); - SET_PTR(archive); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG("%s", e.what()); - } -} + auto archive = std::make_shared(cPath); + SET_PTR(archive); +} CATCH_EXCEPTION() namespace { @@ -71,7 +65,7 @@ int jni2fd(const jobject& fdObj, JNIEnv* env) } // unnamed namespace JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD( - JNIEnv* env, jobject thisObj, jobject fdObj) + JNIEnv* env, jobject thisObj, jobject fdObj) try { #ifndef _WIN32 int fd = jni2fd(fdObj, env); @@ -88,10 +82,10 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD( jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows"); #endif -} +} CATCH_EXCEPTION() JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( - JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size) + JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size) try { #ifndef _WIN32 int fd = jni2fd(fdObj, env); @@ -108,7 +102,7 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows"); #endif -} +} CATCH_EXCEPTION() DISPOSE @@ -121,65 +115,65 @@ GETTER(jint, getMediaCount) METHOD0(jstring, getUuid) { return TO_JNI(std::string(THIS->getUuid())); -} +} CATCH_EXCEPTION(nullptr) METHOD(jstring, getMetadata, jstring name) { return TO_JNI(THIS->getMetadata(TO_C(name))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, getMetadataItem, jstring name) { return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getMetadataItem(TO_C(name))); -} +} CATCH_EXCEPTION(nullptr) GETTER(jobjectArray, getMetadataKeys) METHOD(jobject, getIllustrationItem, jint size) { return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getIllustrationItem(TO_C(size))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jboolean, hasIllustration, jint size) { return TO_JNI(THIS->hasIllustration(TO_C(size))); -} +} CATCH_EXCEPTION(false) GETTER(jlongArray, getIllustrationSizes) METHOD(jobject, getEntryByPath__Ljava_lang_String_2, jstring path) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(path))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, getEntryByPath__I, jint index) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(index))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jboolean, hasEntryByPath, jstring path) { return TO_JNI(THIS->hasEntryByPath(TO_C(path))); -} +} CATCH_EXCEPTION(false) METHOD(jobject, getEntryByTitle__Ljava_lang_String_2, jstring title) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(title))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, getEntryByTitle__I, jint index) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(index))); -} +} CATCH_EXCEPTION(nullptr) METHOD(jboolean, hasEntryByTitle, jstring title) { return TO_JNI(THIS->hasEntryByTitle(TO_C(title))); -} +} CATCH_EXCEPTION(false) METHOD(jobject, getEntryByClusterOrder, jint index) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByClusterOrder(TO_C(index))); -} +} CATCH_EXCEPTION(nullptr) METHOD0(jobject, getMainEntry) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getMainEntry()); -} +} CATCH_EXCEPTION(nullptr) GETTER(jboolean, hasMainEntry) METHOD0(jobject, getRandomEntry) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRandomEntry()); -} +} CATCH_EXCEPTION(nullptr) GETTER(jboolean, hasFulltextIndex) GETTER(jboolean, hasTitleIndex) @@ -202,7 +196,7 @@ METHOD0(jobject, iterByPath) { auto end_ptr = std::make_shared>(range.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) METHOD0(jobject, iterByTitle) { auto range = THIS->iterByTitle(); @@ -214,7 +208,7 @@ METHOD0(jobject, iterByTitle) { auto end_ptr = std::make_shared>(range.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) METHOD0(jobject, iterEfficient) { auto range = THIS->iterEfficient(); @@ -226,7 +220,7 @@ METHOD0(jobject, iterEfficient) { auto end_ptr = std::make_shared>(range.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, findByPath, jstring path) { auto range = THIS->findByPath(TO_C(path)); @@ -238,7 +232,7 @@ METHOD(jobject, findByPath, jstring path) { auto end_ptr = std::make_shared>(range.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, findByTitle, jstring title) { auto range = THIS->findByTitle(TO_C(title)); @@ -250,4 +244,4 @@ METHOD(jobject, findByTitle, jstring title) { auto end_ptr = std::make_shared>(range.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 2bbaca4..510dbfe 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -37,5 +37,5 @@ DISPOSE METHOD0(jbyteArray, getData) { return cArray2jni(THIS->data(), THIS->size(), env); -} +} CATCH_EXCEPTION(nullptr) GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 2652e25..9f72a0d 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -42,12 +42,12 @@ GETTER(jstring, getTitle) GETTER(jstring, getPath) METHOD(jobject, getItem, jboolean follow) { return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getItem(TO_C(follow))); -} +} CATCH_EXCEPTION(nullptr) METHOD0(jobject, getRedirect) { return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getRedirect()); -} +} CATCH_EXCEPTION(nullptr) METHOD0(jobject, getRedirectEntry) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRedirectEntry()); -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp index 78aa840..4834d74 100644 --- a/lib/src/main/cpp/libzim/entry_iterator.cpp +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -60,7 +60,7 @@ METHOD0(void, dispose) dispose(env, thisObj); break; } -} +} CATCH_EXCEPTION() METHOD0(jboolean, hasNext) { @@ -84,7 +84,7 @@ METHOD0(jboolean, hasNext) { // unreachable!() return false; } -} +} CATCH_EXCEPTION(false) METHOD0(jobject, next) { switch (get_order(env, thisObj)) { @@ -107,5 +107,5 @@ METHOD0(jobject, next) { // unreachable!() return nullptr; } -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index 500797f..784f36b 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -41,7 +41,7 @@ GETTER(jstring, getMimetype) METHOD0(jobject, getData) { return BUILD_WRAPPER("org/kiwix/libzim/Blob", THIS->getData()); -} +} CATCH_EXCEPTION(nullptr) GETTER(jlong, getSize) @@ -53,4 +53,4 @@ METHOD0(jobject, getDirectAccessInformation) { auto cDirectObjInfo = THIS->getDirectAccessInformation(); setDaiObjValue(cDirectObjInfo.first, cDirectObjInfo.second, directObjInfo, env); return directObjInfo; -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp index 874da9e..c7c6370 100644 --- a/lib/src/main/cpp/libzim/query.cpp +++ b/lib/src/main/cpp/libzim/query.cpp @@ -36,24 +36,19 @@ METHOD(void, setNativeQuery, jstring query) { auto cQuery = TO_C(query); - try { - auto query = std::make_shared(cQuery); - SET_PTR(query); - } catch (std::exception& e) { - LOG("Cannot create query"); - LOG("%s", e.what()); - } -} + auto zimQuery = std::make_shared(cQuery); + SET_PTR(zimQuery); +} CATCH_EXCEPTION() DISPOSE METHOD(jobject, setQuery, jstring query) { THIS->setQuery(TO_C(query)); return thisObj; -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance)); return thisObj; -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp index 5e26433..7531b02 100644 --- a/lib/src/main/cpp/libzim/search.cpp +++ b/lib/src/main/cpp/libzim/search.cpp @@ -44,6 +44,6 @@ METHOD(jobject, getResults, jint start, jint maxResults) { auto end_ptr = std::make_shared(results.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index c449406..f2746f5 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -41,7 +41,7 @@ METHOD0(void, dispose) // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); dispose(env, thisObj); -} +} CATCH_EXCEPTION() GETTER(jstring, getPath) @@ -54,16 +54,16 @@ GETTER(jint, getSize) METHOD0(jstring, getZimId) { return TO_JNI(std::string(THIS->getZimId())); -} +} CATCH_EXCEPTION(0) METHOD0(jboolean, hasNext) { auto end = getPtr(env, thisObj, "nativeHandleEnd"); return *THIS != *end; -} +} CATCH_EXCEPTION(false) METHOD0(jobject, next) { zim::Entry entry = **THIS; (*THIS)++; return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index 9810ba2..fe85318 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -36,14 +36,9 @@ METHOD(void, setNativeSearcher, jobject archive) { auto cArchive = getPtr(env, archive); - try { - auto searcher = std::make_shared(*cArchive); - SET_PTR(searcher); - } catch (std::exception& e) { - LOG("Cannot create searcher"); - LOG("%s", e.what()); - } -} + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); +} CATCH_EXCEPTION() METHOD(void, setNativeSearcherMulti, jobjectArray archives) { @@ -54,14 +49,9 @@ METHOD(void, setNativeSearcherMulti, jobjectArray archives) auto cArchive = getPtr(env, archive); cArchives.push_back(*cArchive); } - try { - auto searcher = std::make_shared(cArchives); - SET_PTR(searcher); - } catch (std::exception& e) { - LOG("Cannot create searcher"); - LOG("%s", e.what()); - } -} + auto searcher = std::make_shared(cArchives); + SET_PTR(searcher); +} CATCH_EXCEPTION() DISPOSE @@ -69,14 +59,14 @@ METHOD(jobject, addArchive, jobject archive) { auto cArchive = getPtr(env, archive); THIS->addArchive(*cArchive); return thisObj; -} +} CATCH_EXCEPTION(nullptr) METHOD(jobject, search, jobject query) { auto cQuery = getPtr(env, query); return BUILD_WRAPPER("org/kiwix/libzim/Search", THIS->search(*cQuery)); -} +} CATCH_EXCEPTION(nullptr) METHOD(void, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); -} +} CATCH_EXCEPTION() diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index 2d2c410..9fe6cd8 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -39,18 +39,18 @@ METHOD0(void, dispose) // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); dispose(env, thisObj); -} +} CATCH_EXCEPTION() METHOD0(jboolean, hasNext) { NATIVE_TYPE next(*THIS); next++; auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; -} +} CATCH_EXCEPTION(false) METHOD0(jobject, next) { zim::SuggestionItem item = **THIS; (*THIS)++; return BUILD_WRAPPER("org/kiwix/libzim/SuggestionItem", item); -} +} CATCH_EXCEPTION(nullptr) diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp index 27507d8..15c9ac9 100644 --- a/lib/src/main/cpp/libzim/suggestion_search.cpp +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -44,6 +44,6 @@ METHOD(jobject, getResults, jint start, jint maxResults) { auto end_ptr = std::make_shared(results.end()); setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); return obj; -} +} CATCH_EXCEPTION(nullptr) GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 792447e..b68b9d5 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -36,22 +36,17 @@ METHOD(void, setNativeSearcher, jobject archive) { auto cArchive = getPtr(env, archive); - try { - auto searcher = std::make_shared(*cArchive); - SET_PTR(searcher); - } catch (std::exception& e) { - LOG("Cannot create searcher"); - LOG("%s", e.what()); - } -} + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); +} CATCH_EXCEPTION() DISPOSE METHOD(jobject, suggest, jstring query) { return BUILD_WRAPPER("org/kiwix/libzim/SuggestionSearch", THIS->suggest(TO_C(query))); -} +} CATCH_EXCEPTION(nullptr) METHOD(void, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); -} +} CATCH_EXCEPTION() diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h index bef647b..7ce1543 100644 --- a/lib/src/main/cpp/macros.h +++ b/lib/src/main/cpp/macros.h @@ -29,14 +29,34 @@ #define METHOD0(retType, name) \ JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME, name) ( \ - JNIEnv* env, jobject thisObj) + JNIEnv* env, jobject thisObj) try #define METHOD(retType, name, ...) \ JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME ,name) ( \ - JNIEnv* env, jobject thisObj, __VA_ARGS__) + JNIEnv* env, jobject thisObj, __VA_ARGS__) try #define GETTER(retType, name) METHOD0(retType, name) { \ return TO_JNI(THIS->name()); \ -} +} CATCH_EXCEPTION(0) -#define DISPOSE METHOD0(void, dispose) { dispose(env, thisObj); } +#define DISPOSE METHOD0(void, dispose) { dispose(env, thisObj); } CATCH_EXCEPTION() + +#include + +#define CATCH_EXCEPTION(RET) \ +catch(const zim::ZimFileFormatError& e) { \ + throwException(env, "org/kiwix/libzim/ZimFileFormatException", "Zim file format error."); \ + return RET; \ +} catch(const zim::InvalidType& e) { \ + throwException(env, "java/lang/Exception", "Invalid type."); \ + return RET; \ +} catch(const zim::EntryNotFound& e) { \ + throwException(env, "java/lang/Exception", "Entry Not Found."); \ + return RET; \ +} catch (const std::ios_base::failure& e) { \ + throwException(env, "java/io/IOException", e.what()); \ + return RET; \ +} catch(const std::exception& e) { \ + throwException(env, "java/lang/Exception", e.what()); \ + return RET; \ +} diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index f6afd89..e5aee6f 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -400,4 +400,8 @@ inline void setDaiObjValue(const std::string& filename, const long offset, env->SetLongField(obj, offsetFid, offset); } +inline int throwException(JNIEnv* env, const char* exception, const char* message) { + return env->ThrowNew(env->FindClass(exception), message); +} + #endif // _ANDROID_JNI_UTILS_H diff --git a/lib/src/test/test.java b/lib/src/test/test.java index db50102..c177824 100644 --- a/lib/src/test/test.java +++ b/lib/src/test/test.java @@ -169,8 +169,8 @@ public class test { try { TestArchive archive1 = new TestArchive(zimFile); fail("ERROR: Archive created with invalid Zim file!"); - } catch (ZimFileFormatException zimFileFormatException) { - assertEquals("Cannot open zimfile " + zimFile, zimFileFormatException.getMessage()); + } catch (Exception e) { + assertEquals("error 2 opening file \"" + zimFile, e.getMessage()); } }