Merge pull request #36 from kiwix/execption_handling

This commit is contained in:
Matthieu Gautier 2023-06-30 12:03:35 +02:00 committed by GitHub
commit 6797d0fb64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 462 additions and 414 deletions

View File

@ -342,7 +342,8 @@ String getLibzimFiles() {
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionIterator.java " +
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " +
"${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " +
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java"
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java " +
"${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java"
}
task buildLinuxBinding(type: Exec) {

View File

@ -32,19 +32,19 @@
METHOD0(void, allocate)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
}
} CATCH_EXCEPTION()
DISPOSE
METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook)
{
THIS->update(*getPtr<kiwix::Book>(env, otherBook));
}
} CATCH_EXCEPTION()
METHOD(void, update__Lorg_kiwix_libzim_Archive_2, jobject archive)
{
THIS->update(*getPtr<zim::Archive>(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<std::string>("", 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)

View File

@ -31,7 +31,7 @@
METHOD0(void, setNativeBookmark)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
}
} 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()

View File

@ -33,7 +33,7 @@
/* Kiwix Reader JNI functions */
METHOD0(void, allocate) {
SET_PTR(std::make_shared<NATIVE_TYPE>());
}
} 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<args_type>(value, env)); \
return thisObj; \
}
} CATCH_EXCEPTION(nullptr)

View File

@ -32,22 +32,22 @@
METHOD0(void, dispose)
{
dispose<NATIVE_TYPE>(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)

View File

@ -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");
}

View File

@ -34,15 +34,11 @@
/* Kiwix Reader JNI functions */
METHOD(void, setNativeServer, jobject jLibrary)
{
LOG("Attempting to create server");
try {
auto library = getPtr<kiwix::Library>(env, jLibrary);
SET_PTR(std::make_shared<NATIVE_TYPE>(library.get()));
} catch (std::exception& e) {
LOG("Error creating the server");
LOG("%s", e.what());
}
}
auto library = getPtr<kiwix::Library>(env, jLibrary);
SET_PTR(std::make_shared<NATIVE_TYPE>(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()

View File

@ -32,7 +32,7 @@
METHOD0(void, setNativeHandler)
{
SET_PTR(std::make_shared<NATIVE_TYPE>());
}
} CATCH_EXCEPTION()
DISPOSE
@ -41,43 +41,41 @@ METHOD(jboolean, addBook, jobject book)
{
auto cBook = getPtr<kiwix::Book>(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<kiwix::Filter>(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<kiwix::Bookmark>(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)

View File

@ -32,7 +32,7 @@ METHOD(void, allocate, jobject libraryObj)
{
auto lib = getPtr<kiwix::Library>(env, libraryObj);
SET_PTR(std::make_shared<NATIVE_TYPE>(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)

View File

@ -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<zim::Archive>(cPath);
SET_PTR(archive);
} catch (std::exception& e) {
LOG("Error opening ZIM file");
LOG("%s", e.what());
}
}
auto archive = std::make_shared<zim::Archive>(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<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(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<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(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<zim::Archive::iterator<zim::EntryOrder::efficientOrder>>(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<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(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<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
} CATCH_EXCEPTION(nullptr)

View File

@ -37,5 +37,5 @@ DISPOSE
METHOD0(jbyteArray, getData) {
return cArray2jni(THIS->data(), THIS->size(), env);
}
} CATCH_EXCEPTION(nullptr)
GETTER(jlong, size)

View File

@ -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)

View File

@ -60,7 +60,7 @@ METHOD0(void, dispose)
dispose<EFFICIENT_NATIVE_TYPE>(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)

View File

@ -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)

View File

@ -36,24 +36,19 @@
METHOD(void, setNativeQuery, jstring query)
{
auto cQuery = TO_C(query);
try {
auto query = std::make_shared<NATIVE_TYPE>(cQuery);
SET_PTR(query);
} catch (std::exception& e) {
LOG("Cannot create query");
LOG("%s", e.what());
}
}
auto zimQuery = std::make_shared<NATIVE_TYPE>(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)

View File

@ -44,6 +44,6 @@ METHOD(jobject, getResults, jint start, jint maxResults) {
auto end_ptr = std::make_shared<zim::SearchIterator>(results.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
} CATCH_EXCEPTION(nullptr)
GETTER(jlong, getEstimatedMatches)

View File

@ -41,7 +41,7 @@ METHOD0(void, dispose)
// Delete end iterator
dispose<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<NATIVE_TYPE>(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<NATIVE_TYPE>(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)

View File

@ -36,14 +36,9 @@
METHOD(void, setNativeSearcher, jobject archive)
{
auto cArchive = getPtr<zim::Archive>(env, archive);
try {
auto searcher = std::make_shared<zim::Searcher>(*cArchive);
SET_PTR(searcher);
} catch (std::exception& e) {
LOG("Cannot create searcher");
LOG("%s", e.what());
}
}
auto searcher = std::make_shared<zim::Searcher>(*cArchive);
SET_PTR(searcher);
} CATCH_EXCEPTION()
METHOD(void, setNativeSearcherMulti, jobjectArray archives)
{
@ -54,14 +49,9 @@ METHOD(void, setNativeSearcherMulti, jobjectArray archives)
auto cArchive = getPtr<zim::Archive>(env, archive);
cArchives.push_back(*cArchive);
}
try {
auto searcher = std::make_shared<zim::Searcher>(cArchives);
SET_PTR(searcher);
} catch (std::exception& e) {
LOG("Cannot create searcher");
LOG("%s", e.what());
}
}
auto searcher = std::make_shared<zim::Searcher>(cArchives);
SET_PTR(searcher);
} CATCH_EXCEPTION()
DISPOSE
@ -69,14 +59,14 @@ METHOD(jobject, addArchive, jobject archive) {
auto cArchive = getPtr<zim::Archive>(env, archive);
THIS->addArchive(*cArchive);
return thisObj;
}
} CATCH_EXCEPTION(nullptr)
METHOD(jobject, search, jobject query) {
auto cQuery = getPtr<zim::Query>(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()

View File

@ -39,18 +39,18 @@ METHOD0(void, dispose)
// Delete end iterator
dispose<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<NATIVE_TYPE>(env, thisObj);
}
} CATCH_EXCEPTION()
METHOD0(jboolean, hasNext) {
NATIVE_TYPE next(*THIS);
next++;
auto end = getPtr<NATIVE_TYPE>(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)

View File

@ -44,6 +44,6 @@ METHOD(jobject, getResults, jint start, jint maxResults) {
auto end_ptr = std::make_shared<zim::SuggestionIterator>(results.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
} CATCH_EXCEPTION(nullptr)
GETTER(jlong, getEstimatedMatches)

View File

@ -36,22 +36,17 @@
METHOD(void, setNativeSearcher, jobject archive)
{
auto cArchive = getPtr<zim::Archive>(env, archive);
try {
auto searcher = std::make_shared<zim::SuggestionSearcher>(*cArchive);
SET_PTR(searcher);
} catch (std::exception& e) {
LOG("Cannot create searcher");
LOG("%s", e.what());
}
}
auto searcher = std::make_shared<zim::SuggestionSearcher>(*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()

View File

@ -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<NATIVE_TYPE>(env, thisObj); }
#define DISPOSE METHOD0(void, dispose) { dispose<NATIVE_TYPE>(env, thisObj); } CATCH_EXCEPTION()
#include <zim/error.h>
#define CATCH_EXCEPTION(RET) \
catch(const zim::ZimFileFormatError& e) { \
throwException(env, "org/kiwix/libzim/ZimFileFormatException", e.what()); \
return RET; \
} catch(const zim::InvalidType& e) { \
throwException(env, "java/lang/Exception", e.what()); \
return RET; \
} catch(const zim::EntryNotFound& e) { \
throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \
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; \
}

View File

@ -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

View File

@ -31,26 +31,17 @@ public class Archive
public Archive(String filename) throws ZimFileFormatException
{
setNativeArchive(filename);
if (nativeHandle == 0) {
throw new ZimFileFormatException("Cannot open zimfile "+filename);
}
}
public Archive(FileDescriptor fd) throws ZimFileFormatException
{
setNativeArchiveByFD(fd);
if (nativeHandle == 0) {
throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString());
}
}
public Archive(FileDescriptor fd, long offset, long size)
throws ZimFileFormatException
{
setNativeArchiveEmbedded(fd, offset, size);
if (nativeHandle == 0) {
throw new ZimFileFormatException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size));
}
}
public native String getFilename();
@ -60,25 +51,25 @@ public class Archive
public native int getArticleCount();
public native int getMediaCount();
public native String getUuid();
public native String getMetadata(String name);
public native Item getMetadataItem(String name);
public native String getMetadata(String name) throws EntryNotFoundException;
public native Item getMetadataItem(String name) throws EntryNotFoundException;
public native String[] getMetadataKeys();
public native Item getIllustrationItem(int size);
public native boolean hasIllustration(int size);
public native long[] getIllustrationSizes();
public native Entry getEntryByPath(String path);
public native Entry getEntryByPath(int index);
public native Entry getEntryByPath(String path) throws EntryNotFoundException;
public native Entry getEntryByPath(int index) throws EntryNotFoundException;
public native boolean hasEntryByPath(String path);
public native Entry getEntryByTitle(String title);
public native Entry getEntryByTitle(int index);
public native Entry getEntryByTitle(String title) throws EntryNotFoundException;
public native Entry getEntryByTitle(int index) throws EntryNotFoundException;
public native boolean hasEntryByTitle(String title);
public native Entry getEntryByClusterOrder(int index);
public native Entry getEntryByClusterOrder(int index) throws EntryNotFoundException;
public native Entry getMainEntry();
public native Entry getMainEntry() throws EntryNotFoundException;
public native boolean hasMainEntry();
public native Entry getRandomEntry();

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.org>
*
* 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 3 of the License, or
* 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.kiwix.libzim;
public class EntryNotFoundException extends Exception
{
public EntryNotFoundException(String message) {
super(message);
}
}

View File

@ -54,25 +54,25 @@ public class TestArchive
public int getArticleCount() { return inner.getArticleCount(); }
public int getMediaCount() { return inner.getMediaCount(); }
public String getUuid() { return inner.getUuid(); }
public String getMetadata(String name) { return inner.getMetadata(name); }
public TestItem getMetadataItem(String name) { return new TestItem(inner.getMetadataItem(name)); }
public String getMetadata(String name) throws EntryNotFoundException { return inner.getMetadata(name); }
public TestItem getMetadataItem(String name) throws EntryNotFoundException { return new TestItem(inner.getMetadataItem(name)); }
public String[] getMetadataKeys() { return inner.getMetadataKeys(); }
public TestItem getIllustrationItem(int size) { return new TestItem(inner.getIllustrationItem(size)); }
public boolean hasIllustration(int size) { return inner.hasIllustration(size); }
public long[] getIllustrationSizes() { return inner.getIllustrationSizes(); }
public TestEntry getEntryByPath(String path) { return new TestEntry(inner.getEntryByPath(path)); }
public TestEntry getEntryByPath(int index) { return new TestEntry(inner.getEntryByPath(index)); }
public TestEntry getEntryByPath(String path) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(path)); }
public TestEntry getEntryByPath(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByPath(index)); }
public boolean hasEntryByPath(String path) { return inner.hasEntryByPath(path); }
public TestEntry getEntryByTitle(String title) { return new TestEntry(inner.getEntryByTitle(title)); }
public TestEntry getEntryByTitle(int index) { return new TestEntry(inner.getEntryByTitle(index)); }
public TestEntry getEntryByTitle(String title) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(title)); }
public TestEntry getEntryByTitle(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByTitle(index)); }
public boolean hasEntryByTitle(String title) { return inner.hasEntryByTitle(title); }
public TestEntry getEntryByClusterOrder(int index) { return new TestEntry(inner.getEntryByClusterOrder(index)); }
public TestEntry getEntryByClusterOrder(int index) throws EntryNotFoundException { return new TestEntry(inner.getEntryByClusterOrder(index)); }
public TestEntry getMainEntry() { return new TestEntry(inner.getMainEntry()); }
public TestEntry getMainEntry() throws EntryNotFoundException { return new TestEntry(inner.getMainEntry()); }
public boolean hasMainEntry() { return inner.hasMainEntry(); }
public TestEntry getRandomEntry() { return new TestEntry(inner.getRandomEntry()); }

View File

@ -22,8 +22,8 @@ public class test {
throws IOException {
File file = new File(path);
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
new BufferedInputStream(
new FileInputStream(file)));
byte[] data = new byte[(int) file.length()];
in.read(data);
return data;
@ -33,8 +33,8 @@ public class test {
throws IOException {
File file = new File(path);
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
new BufferedInputStream(
new FileInputStream(file)));
byte[] data = new byte[size];
in.skipBytes(offset);
in.read(data, 0, size);
@ -47,7 +47,7 @@ public class test {
}
private void testArchive(TestArchive archive)
throws IOException {
throws IOException, EntryNotFoundException {
// test the zim file main page title
TestEntry mainPage = archive.getMainEntry();
assertTrue(mainPage.isRedirect());
@ -94,8 +94,8 @@ public class test {
assertTrue(Arrays.equals(illuSizes, archive.getIllustrationSizes()));
String[] metaKeys = {"Counter", "Creator", "Date", "Description", "Illustration_48x48@1", "Language", "LongDescription", "Name", "Publisher", "Scraper", "Tags", "Title"};
assertTrue(Arrays.equals(
metaKeys,
archive.getMetadataKeys()
metaKeys,
archive.getMetadataKeys()
));
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", archive.getUuid());
assertEquals(1, archive.getMediaCount());
@ -116,86 +116,132 @@ public class test {
assertFalse(archive.getRandomEntry().getTitle().isEmpty());
// Test different iterators
{
TestEntryIterator iter = archive.iterByPath();
assertTrue(iter.hasNext());
assertEquals("favicon.png", iter.next().getPath());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
TestEntryIterator iter = archive.iterByPath();
assertTrue(iter.hasNext());
assertEquals("favicon.png", iter.next().getPath());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
}
{
TestEntryIterator iter = archive.iterByTitle();
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
// No favicon, because favicon is not a main article (no title)
assertFalse(iter.hasNext());
TestEntryIterator iter = archive.iterByTitle();
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
// No favicon, because favicon is not a main article (no title)
assertFalse(iter.hasNext());
}
{
TestEntryIterator iter = archive.iterEfficient();
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertEquals("favicon.png", iter.next().getPath());
assertFalse(iter.hasNext());
TestEntryIterator iter = archive.iterEfficient();
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertEquals("favicon.png", iter.next().getPath());
assertFalse(iter.hasNext());
}
{
TestEntryIterator iter = archive.findByPath("ma");
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
TestEntryIterator iter = archive.findByPath("ma");
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
}
{
TestEntryIterator iter = archive.findByTitle("Test");
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
TestEntryIterator iter = archive.findByTitle("Test");
assertTrue(iter.hasNext());
assertEquals("main.html", iter.next().getPath());
assertFalse(iter.hasNext());
}
}
// Test invalid path
try {
archive.getEntryByTitle("Wrong title");
} catch(EntryNotFoundException e) {
assertEquals("Cannot find entry", e.getMessage());
} catch(Exception e) {
fail("ERROR: Must be a EntryNotFoundException.");
}
try {
archive.getEntryByPath("wrong_path.html");
} catch(EntryNotFoundException e) {
assertEquals("Cannot find entry", e.getMessage());
} catch(Exception e) {
fail("ERROR: Must be a EntryNotFoundException.");
}
System.gc();
System.runFinalization();
}
@Test
public void testArchiveDirect()
throws JNIKiwixException, IOException, ZimFileFormatException {
TestArchive archive = new TestArchive("small.zim");
testArchive(archive);
assertTrue(archive.check());
assertEquals("small.zim", archive.getFilename());
archive.dispose();
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
{
TestArchive archive = new TestArchive("small.zim");
testArchive(archive);
assertTrue(archive.check());
assertEquals("small.zim", archive.getFilename());
}
System.gc();
System.runFinalization();
}
// test reader with invalid zim file
String zimFile = "test.zim";
@Test
public void testNonExistant() {
// test reader with non existant zim file
String zimFile = "non_existant.zim";
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());
}
}
@Test
public void testNotValid() {
// test reader with non existant zim file
String zimFile = "test.java";
try {
TestArchive archive1 = new TestArchive(zimFile);
fail("ERROR: Archive created with invalid Zim file!");
} catch (ZimFileFormatException e) {
assertEquals("Invalid magic number", e.getMessage());
} catch(Exception e) {
fail("ERROR: Must be a ZimFileFormatException.");
}
}
@Test
public void testArchiveByFd()
throws JNIKiwixException, IOException, ZimFileFormatException {
FileInputStream fis = new FileInputStream("small.zim");
TestArchive archive = new TestArchive(fis.getFD());
testArchive(archive);
assertTrue(archive.check());
assertEquals("", archive.getFilename());
archive.dispose();
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
{
FileInputStream fis = new FileInputStream("small.zim");
TestArchive archive = new TestArchive(fis.getFD());
testArchive(archive);
assertTrue(archive.check());
assertEquals("", archive.getFilename());
}
System.gc();
System.runFinalization();
}
@Test
public void testArchiveWithAnEmbeddedArchive()
throws JNIKiwixException, IOException, ZimFileFormatException {
File plainArchive = new File("small.zim");
FileInputStream fis = new FileInputStream("small.zim.embedded");
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
// This fails. See https://github.com/openzim/libzim/issues/812
//assertTrue(archive.check());
testArchive(archive);
assertEquals("", archive.getFilename());
archive.dispose();
throws JNIKiwixException, IOException, ZimFileFormatException, EntryNotFoundException {
{
File plainArchive = new File("small.zim");
FileInputStream fis = new FileInputStream("small.zim.embedded");
TestArchive archive = new TestArchive(fis.getFD(), 8, plainArchive.length());
// This fails. See https://github.com/openzim/libzim/issues/812
//assertTrue(archive.check());
testArchive(archive);
assertEquals("", archive.getFilename());
}
System.gc();
System.runFinalization();
}
private void testLibrary(TestLibrary lib)
@ -235,165 +281,191 @@ public class test {
@Test
public void testLibrarySimple() throws IOException {
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true);
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
{
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
manager.addBookFromPath("small.zim", "small.zim", "http://localhost/small.zim", true);
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
// remove book from library by id
lib.removeBookById(bookIds[0]);
bookIds = lib.getBooksIds();
assertEquals(bookIds.length, 0);
// remove book from library by id
lib.removeBookById(bookIds[0]);
bookIds = lib.getBooksIds();
assertEquals(bookIds.length, 0);
}
System.gc();
System.runFinalization();
}
@Test
public void testLibraryXml() throws IOException {
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
manager.readFile("library.xml");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
{
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
manager.readFile("library.xml");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
}
System.gc();
System.runFinalization();
}
@Test
public void testLibraryXmlContent() throws IOException {
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
String content = getTextFileContent("library.xml");
manager.readXml(content, "library.xml");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
{
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
String content = getTextFileContent("library.xml");
manager.readXml(content, "library.xml");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");
assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
}
System.gc();
System.runFinalization();
}
@Test
public void testLibraryOPDS() throws IOException {
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
String content = getTextFileContent("catalog.xml");
manager.readOpds(content, "http://localhost");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");
assertEquals(book.getPath(), "");
assertEquals(book.getHumanReadableIdFromPath(), "");
assertFalse(book.isPathValid());
{
TestLibrary lib = new TestLibrary();
TestManager manager = new TestManager(lib);
String content = getTextFileContent("catalog.xml");
manager.readOpds(content, "http://localhost");
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");
assertEquals(book.getPath(), "");
assertEquals(book.getHumanReadableIdFromPath(), "");
assertFalse(book.isPathValid());
}
System.gc();
System.runFinalization();
}
@Test
public void testServer() throws ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim");
TestLibrary lib = new TestLibrary();
TestBook book = new TestBook();
book.update(archive);
lib.addBook(book);
assertEquals(1, lib.getBookCount(true, true));
TestServer server = new TestServer(lib);
server.setPort(8080);
server.setRoot("FOO");
server.setAddress("127.0.0.1");
server.setNbThreads(1);
server.setBlockExternalLinks(true);
server.setTaskbar(true, true);
assertTrue(server.start());
server.stop();
{
TestArchive archive = new TestArchive("small.zim");
TestLibrary lib = new TestLibrary();
TestBook book = new TestBook();
book.update(archive);
lib.addBook(book);
assertEquals(1, lib.getBookCount(true, true));
TestServer server = new TestServer(lib);
server.setPort(8080);
server.setRoot("FOO");
server.setAddress("127.0.0.1");
server.setNbThreads(1);
server.setBlockExternalLinks(true);
server.setTaskbar(true, true);
assertTrue(server.start());
server.stop();
}
System.gc();
System.runFinalization();
}
@Test
public void testBookMark() throws ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim");
TestLibrary lib = new TestLibrary();
TestBook book = new TestBook();
book.update(archive);
lib.addBook(book);
TestBookmark bookmark = new TestBookmark();
bookmark.setBookId(book.getId());
bookmark.setTitle(book.getTitle());
bookmark.setUrl(book.getUrl());
bookmark.setLanguage(book.getLanguage());
bookmark.setDate(book.getDate());
bookmark.setBookTitle(book.getName());
// add bookmark to library
lib.addBookmark(bookmark);
TestBookmark[] bookmarkArray = lib.getBookmarks(true);
assertEquals(1, bookmarkArray.length);
bookmark = bookmarkArray[0];
// test saved bookmark
assertEquals(bookmark.getBookId(), book.getId());
assertEquals(bookmark.getTitle(), book.getTitle());
assertEquals(bookmark.getUrl(), book.getUrl());
assertEquals(bookmark.getLanguage(), book.getLanguage());
assertEquals(bookmark.getDate(), book.getDate());
assertEquals(bookmark.getBookTitle(), book.getName());
// remove bookmark from library
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
bookmarkArray = lib.getBookmarks(true);
assertEquals(0, bookmarkArray.length);
{
TestArchive archive = new TestArchive("small.zim");
TestLibrary lib = new TestLibrary();
TestBook book = new TestBook();
book.update(archive);
lib.addBook(book);
TestBookmark bookmark = new TestBookmark();
bookmark.setBookId(book.getId());
bookmark.setTitle(book.getTitle());
bookmark.setUrl(book.getUrl());
bookmark.setLanguage(book.getLanguage());
bookmark.setDate(book.getDate());
bookmark.setBookTitle(book.getName());
// add bookmark to library
lib.addBookmark(bookmark);
TestBookmark[] bookmarkArray = lib.getBookmarks(true);
assertEquals(1, bookmarkArray.length);
bookmark = bookmarkArray[0];
// test saved bookmark
assertEquals(bookmark.getBookId(), book.getId());
assertEquals(bookmark.getTitle(), book.getTitle());
assertEquals(bookmark.getUrl(), book.getUrl());
assertEquals(bookmark.getLanguage(), book.getLanguage());
assertEquals(bookmark.getDate(), book.getDate());
assertEquals(bookmark.getBookTitle(), book.getName());
// remove bookmark from library
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
bookmarkArray = lib.getBookmarks(true);
assertEquals(0, bookmarkArray.length);
}
System.gc();
System.runFinalization();
}
@Test
public void testSearcher() throws Exception, ZimFileFormatException, JNIKiwixException {
TestArchive archive = new TestArchive("small.zim");
{
TestArchive archive = new TestArchive("small.zim");
TestSearcher searcher = new TestSearcher(archive);
searcher.setVerbose(true);
TestQuery query = new TestQuery("test__");
query.setQuery("test");
TestSearcher searcher = new TestSearcher(archive);
searcher.setVerbose(true);
TestQuery query = new TestQuery("test__");
query.setQuery("test");
TestSearch search = searcher.search(query);
int estimatedMatches = (int) search.getEstimatedMatches();
assertEquals(1, estimatedMatches);
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
assertTrue(iterator.hasNext());
assertEquals("Test ZIM file", iterator.getTitle());
assertEquals("main.html", iterator.getPath());
assertEquals(100, iterator.getScore());
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
assertEquals(3, iterator.getWordCount());
assertEquals(0, iterator.getFileIndex());
assertEquals(-1, iterator.getSize());
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId());
TestEntry entry = iterator.next();
assertEquals("main.html", entry.getPath());
TestSearch search = searcher.search(query);
int estimatedMatches = (int) search.getEstimatedMatches();
assertEquals(1, estimatedMatches);
TestSearchIterator iterator = search.getResults(0, estimatedMatches);
assertTrue(iterator.hasNext());
assertEquals("Test ZIM file", iterator.getTitle());
assertEquals("main.html", iterator.getPath());
assertEquals(100, iterator.getScore());
assertEquals("<b>Test</b> ZIM file", iterator.getSnippet());
assertEquals(3, iterator.getWordCount());
assertEquals(0, iterator.getFileIndex());
assertEquals(-1, iterator.getSize());
assertEquals("e34f5109-ed0d-b93e-943d-06f7717c7340", iterator.getZimId());
TestEntry entry = iterator.next();
assertEquals("main.html", entry.getPath());
query.setGeorange(50,70,50);
assertEquals(0, searcher.search(query).getEstimatedMatches());
searcher.dispose();
query.setGeorange(50,70,50);
assertEquals(0, searcher.search(query).getEstimatedMatches());
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
searcher2.addArchive(archive);
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
TestSearcher searcher2 = new TestSearcher(new TestArchive[0]);
searcher2.addArchive(archive);
assertEquals(1, searcher2.search(new TestQuery("test")).getEstimatedMatches());
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
suggestionSearcher.setVerbose(true);
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
int matches = (int) suggestionSearch.getEstimatedMatches();
assertEquals(1, matches);
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
assertTrue(results.hasNext());
TestSuggestionItem suggestionItem = results.next();
assertFalse(results.hasNext());
assertEquals("Test ZIM file", suggestionItem.getTitle());
assertEquals("main.html", suggestionItem.getPath());
assertTrue(suggestionItem.hasSnippet());
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
suggestionSearcher.dispose();
TestSuggestionSearcher suggestionSearcher = new TestSuggestionSearcher(archive);
suggestionSearcher.setVerbose(true);
TestSuggestionSearch suggestionSearch = suggestionSearcher.suggest("test");
int matches = (int) suggestionSearch.getEstimatedMatches();
assertEquals(1, matches);
TestSuggestionIterator results = suggestionSearch.getResults(0, matches);
assertTrue(results.hasNext());
TestSuggestionItem suggestionItem = results.next();
assertFalse(results.hasNext());
assertEquals("Test ZIM file", suggestionItem.getTitle());
assertEquals("main.html", suggestionItem.getPath());
assertTrue(suggestionItem.hasSnippet());
assertEquals("<b>Test</b> ZIM file", suggestionItem.getSnippet());
}
System.gc();
System.runFinalization();
}
static