/* * MGPPRetrieveWrapperImpl.c * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.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 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef __WIN32__ #include #include #include #else #ifdef __APPLE__ #include #include #else #include #endif #endif #include #include "org_greenstone_mgpp_MGPPRetrieveWrapper.h" #include "MGPPWrapperImpl.h" #include "TextGet.h" #include "GSDLQueryParser.h" #include "MGQuery.h" MGPPWrapperData::MGPPWrapperData() { indexData = new IndexData(); queryInfo = new QueryInfo(); if (queryInfo==NULL) { cerr<<"couldn't allocate new query info\n"; if (indexData!=NULL) { delete indexData; } } // set all the default params //SetCStr(queryInfo->docLevel, "Document"); // the level to search at queryInfo->maxDocs = 50; queryInfo->sortByRank = true; queryInfo->exactWeights = false; queryInfo->needRankInfo = true; queryInfo->needTermFreqs = true; UCArrayClear(level); SetCStr(level, "Document"); // the level to return docs at defaultStemMethod=0; defaultBoolCombine=0; maxNumeric = 4; } MGPPWrapperData::~MGPPWrapperData() { if (indexData !=NULL) { delete indexData; } if (queryInfo !=NULL) { delete queryInfo; } } // ******************************************** // initialisation stuff // ******************************************** // cached ids for java stuff jfieldID FID_mgpp_data = NULL; // MGPPWrapperData jfieldID FID_query_result = NULL; // MGPPQueryResult jmethodID MID_addDoc=NULL; // MGPPQueryResult.addDoc() jmethodID MID_addTerm=NULL; // MGPPQueryResult.addTerm() jmethodID MID_setTotalDocs=NULL; // MGPPQueryResult.setTotalDocs() jmethodID MID_clearResult=NULL; //MGPPQueryResult.clear() jmethodID MID_setSyntaxError=NULL; // MGPPQueryResult.setSyntaxError() jclass CID_String=NULL; // class ID of String /* to access objects and methods on java side, need their field/method ids - this initialises them at the start to avoid recalculating them each time they are needed Note: the descriptors need to be exactly right, otherwise you get an error saying "no such field" but no reference to the fact that it has the right name but the wrong type. Note: apparently the jclass is a local ref and should only work in the method that created it. It seems to work ok, but I'll make it global cos the book said I should, and it may avoid future hassles. */ JNIEXPORT void JNICALL Java_org_greenstone_mgpp_MGPPRetrieveWrapper_initIDs (JNIEnv *j_env, jclass j_cls) { FID_mgpp_data = j_env->GetFieldID(j_cls, "mgpp_data_ptr_", "J"); //a long-"J" if (FID_mgpp_data==NULL) { cerr <<"MGPP JNI: field mgpp_data_ptr_ not found"<GetFieldID(j_cls, "mgpp_query_result_", "Lorg/greenstone/mgpp/MGPPQueryResult;"); // an object -"L;" if (FID_query_result==NULL) { cerr <<"MGPP JNI: field mgpp_query_result_ not found"<FindClass("org/greenstone/mgpp/MGPPQueryResult"); MID_addDoc = j_env->GetMethodID(JC_MGPPQueryResult, "addDoc", "(JF)V"); if (MID_addDoc==NULL) { cerr <<"MGPP JNI: addDoc method not found"<GetMethodID(JC_MGPPQueryResult, "addTerm", "(Ljava/lang/String;Ljava/lang/String;IJJ[Ljava/lang/String;)V"); if (MID_addTerm==NULL) { cerr <<"MGPP JNI: method addTerm not found"<GetMethodID(JC_MGPPQueryResult, "setTotalDocs", "(J)V"); if (MID_setTotalDocs==NULL) { cerr <<"MGPP JNI: method setTotalDocs not found"<GetMethodID(JC_MGPPQueryResult, "clear", "()V"); if (MID_clearResult==NULL) { cerr <<"MGPP JNI: method clear not found"<GetMethodID(JC_MGPPQueryResult, "setSyntaxError", "(Z)V"); if (MID_clearResult==NULL) { cerr <<"MGPP JNI: method setSyntaxError not found"<FindClass("java/lang/String"); if (local_CID_String==NULL) { cerr <<"MGPP JNI: java String class not found"<NewGlobalRef(local_CID_String); /* The local reference is no longer useful */ j_env->DeleteLocalRef(local_CID_String); /* Is the global reference created successfully? */ if (CID_String == NULL) { return; /* out of memory exception thrown */ } } } /* the java side MGPPWrapper has a pointer to a C++ object - MGPPWrapperData initialise this and set the pointer */ JNIEXPORT jboolean JNICALL Java_org_greenstone_mgpp_MGPPRetrieveWrapper_initCppSide (JNIEnv *j_env, jobject j_obj){ MGPPWrapperData * data = new MGPPWrapperData(); j_env->SetIntField(j_obj, FID_mgpp_data, (long)data); return true; } //**************************************************** // retrieve a document //**************************************************** /* returns a document from mgpp as a string Note: TextData isn't cached - just reloaded each time */ JNIEXPORT jstring JNICALL Java_org_greenstone_mgpp_MGPPRetrieveWrapper_getDocument (JNIEnv *j_env, jobject j_obj, jstring j_text_name, jstring j_level, jlong j_docnum) { #ifdef __WIN32__ const char* base_dir = ""; #else const char* base_dir = "/"; #endif const char * text_name = j_env->GetStringUTFChars(j_text_name, NULL); if (text_name==NULL) { return NULL; } const char * level = j_env->GetStringUTFChars( j_level, NULL); if (level==NULL) { j_env->ReleaseStringUTFChars(j_text_name, text_name); return NULL; } // does this work alright? j_docnum is a long (64 bit) unsigned long docnum = j_docnum; TextData td; // cast to char* otherwise complains about const td.LoadData((char *)base_dir, (char *)text_name); UCArray mg_level; SetCStr(mg_level, level); UCArray docText; docText.clear(); // get the actual text if (!GetDocText(td, mg_level, docnum, docText)) { cerr <<"MGPP JNI: couldn't retrieve doc text"<NewStringUTF(doc); // release any gets j_env->ReleaseStringUTFChars(j_text_name, text_name); j_env->ReleaseStringUTFChars(j_level, level); // free any C++ stuff delete doc; return result; } //****************************************** // do a query // **************************************** /* load the IndexData - cached for querying */ JNIEXPORT jboolean JNICALL Java_org_greenstone_mgpp_MGPPRetrieveWrapper_loadIndexData (JNIEnv *j_env, jobject j_obj, jstring j_index_name) { jint data_ptr = j_env->GetIntField(j_obj, FID_mgpp_data); MGPPWrapperData * data = (MGPPWrapperData *)data_ptr; #ifdef __WIN32__ const char* base_dir = ""; #else const char* base_dir = "/"; #endif const char * index_name = j_env->GetStringUTFChars( j_index_name, NULL); if (index_name==NULL) { return false; } jboolean j_result=false; // why doesn't this complain about const?? if (data->indexData->LoadData(base_dir, index_name)) { j_result=true; } // release any gets j_env->ReleaseStringUTFChars(j_index_name, index_name); return j_result; } /* unload the data */ JNIEXPORT jboolean JNICALL Java_org_greenstone_mgpp_MGPPRetrieveWrapper_unloadIndexData (JNIEnv *j_env, jobject j_obj) { jint data_ptr = j_env->GetIntField(j_obj, FID_mgpp_data); MGPPWrapperData * data = (MGPPWrapperData *)data_ptr; data->indexData->UnloadData(); return true; }