source: indexers/trunk/mgpp/jni/MGPPRetrieveWrapperImpl.cpp@ 14910

Last change on this file since 14910 was 14910, checked in by davidb, 16 years ago

Standardisation of Windows config file to lowercase (included from this source file). Was causing a problem when trying to compile on Unix filesystem mounted under Windows.

File size: 8.5 KB
Line 
1/*
2 * MGPPRetrieveWrapperImpl.c
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#ifdef __WIN32__
20#include <win32cfg.h>
21#include <strstream>
22#include <sstream>
23#else
24#ifdef __APPLE__
25#include <strstream>
26#include <sstream>
27#else
28#include <sstream>
29#endif
30#endif
31
32#include <jni.h>
33#include "org_greenstone_mgpp_MGPPRetrieveWrapper.h"
34#include "MGPPWrapperImpl.h"
35#include "TextGet.h"
36#include "GSDLQueryParser.h"
37#include "MGQuery.h"
38
39MGPPWrapperData::MGPPWrapperData() {
40 indexData = new IndexData();
41 queryInfo = new QueryInfo();
42
43 if (queryInfo==NULL) {
44 cerr<<"couldn't allocate new query info\n";
45 if (indexData!=NULL) {
46 delete indexData;
47 }
48 }
49
50 // set all the default params
51 //SetCStr(queryInfo->docLevel, "Document"); // the level to search at
52 queryInfo->maxDocs = 50;
53 queryInfo->sortByRank = true;
54 queryInfo->exactWeights = false;
55 queryInfo->needRankInfo = true;
56 queryInfo->needTermFreqs = true;
57
58 UCArrayClear(level);
59 SetCStr(level, "Document"); // the level to return docs at
60 defaultStemMethod=0;
61 defaultBoolCombine=0;
62 maxNumeric = 4;
63}
64
65MGPPWrapperData::~MGPPWrapperData() {
66 if (indexData !=NULL) {
67 delete indexData;
68 }
69 if (queryInfo !=NULL) {
70 delete queryInfo;
71 }
72}
73
74// ********************************************
75// initialisation stuff
76// ********************************************
77
78// cached ids for java stuff
79jfieldID FID_mgpp_data = NULL; // MGPPWrapperData
80jfieldID FID_query_result = NULL; // MGPPQueryResult
81jmethodID MID_addDoc=NULL; // MGPPQueryResult.addDoc()
82jmethodID MID_addTerm=NULL; // MGPPQueryResult.addTerm()
83jmethodID MID_setTotalDocs=NULL; // MGPPQueryResult.setTotalDocs()
84jmethodID MID_clearResult=NULL; //MGPPQueryResult.clear()
85jmethodID MID_setSyntaxError=NULL; // MGPPQueryResult.setSyntaxError()
86jclass CID_String=NULL; // class ID of String
87
88/* to access objects and methods on java side, need their field/method ids -
89 this initialises them at the start to avoid recalculating them each time they
90 are needed
91Note: the descriptors need to be exactly right, otherwise you get an error
92saying "no such field" but no reference to the fact that it has the right
93name but the wrong type.
94Note: apparently the jclass is a local ref and should only work
95in the method that created it. It seems to work ok, but I'll make it
96 global cos the book said I should, and it may avoid future hassles.
97*/
98JNIEXPORT void JNICALL
99Java_org_greenstone_mgpp_MGPPRetrieveWrapper_initIDs (JNIEnv *j_env, jclass j_cls) {
100
101 FID_mgpp_data = j_env->GetFieldID(j_cls, "mgpp_data_ptr_", "J"); //a long-"J"
102 if (FID_mgpp_data==NULL) {
103 cerr <<"MGPP JNI: field mgpp_data_ptr_ not found"<<endl;
104 }
105
106 FID_query_result = j_env->GetFieldID(j_cls, "mgpp_query_result_", "Lorg/greenstone/mgpp/MGPPQueryResult;"); // an object -"L<class name>;"
107 if (FID_query_result==NULL) {
108 cerr <<"MGPP JNI: field mgpp_query_result_ not found"<<endl;
109 }
110 // the methods we want to use
111
112 // addDoc(long doc, float rank)
113 jclass JC_MGPPQueryResult = j_env->FindClass("org/greenstone/mgpp/MGPPQueryResult");
114 MID_addDoc = j_env->GetMethodID(JC_MGPPQueryResult, "addDoc", "(JF)V");
115 if (MID_addDoc==NULL) {
116 cerr <<"MGPP JNI: addDoc method not found"<<endl;
117 }
118 // addTerm(String term, String tag, int stem_method, long match_docs,
119 // long term_freq, String[] equiv_terms)
120 MID_addTerm = j_env->GetMethodID(JC_MGPPQueryResult, "addTerm", "(Ljava/lang/String;Ljava/lang/String;IJJ[Ljava/lang/String;)V");
121 if (MID_addTerm==NULL) {
122 cerr <<"MGPP JNI: method addTerm not found"<<endl;
123 }
124
125 // setTotalDocs(long)
126 MID_setTotalDocs = j_env->GetMethodID(JC_MGPPQueryResult, "setTotalDocs", "(J)V");
127 if (MID_setTotalDocs==NULL) {
128 cerr <<"MGPP JNI: method setTotalDocs not found"<<endl;
129 }
130
131 MID_clearResult = j_env->GetMethodID(JC_MGPPQueryResult, "clear", "()V");
132 if (MID_clearResult==NULL) {
133 cerr <<"MGPP JNI: method clear not found"<<endl;
134 }
135 MID_setSyntaxError = j_env->GetMethodID(JC_MGPPQueryResult, "setSyntaxError", "(Z)V");
136 if (MID_clearResult==NULL) {
137 cerr <<"MGPP JNI: method setSyntaxError not found"<<endl;
138 }
139
140 // get the class for String to use in NewObjectArray in runQuery()
141 // FindClass returns a local reference - have to convert it to a global one
142 jclass local_CID_String = j_env->FindClass("java/lang/String");
143 if (local_CID_String==NULL) {
144 cerr <<"MGPP JNI: java String class not found"<<endl;
145 } else {
146 /* create a global ref */
147 CID_String = (jclass)j_env->NewGlobalRef(local_CID_String);
148 /* The local reference is no longer useful */
149 j_env->DeleteLocalRef(local_CID_String);
150
151 /* Is the global reference created successfully? */
152 if (CID_String == NULL) {
153 return; /* out of memory exception thrown */
154 }
155 }
156
157}
158
159/* the java side MGPPWrapper has a pointer to a C++ object - MGPPWrapperData
160 initialise this and set the pointer
161*/
162JNIEXPORT jboolean JNICALL
163Java_org_greenstone_mgpp_MGPPRetrieveWrapper_initCppSide (JNIEnv *j_env, jobject j_obj){
164
165 MGPPWrapperData * data = new MGPPWrapperData();
166 j_env->SetIntField(j_obj, FID_mgpp_data, (long)data);
167
168 return true;
169
170}
171
172//****************************************************
173// retrieve a document
174//****************************************************
175
176/* returns a document from mgpp as a string
177Note: TextData isn't cached - just reloaded each time
178*/
179JNIEXPORT jstring JNICALL
180Java_org_greenstone_mgpp_MGPPRetrieveWrapper_getDocument (JNIEnv *j_env,
181 jobject j_obj, jstring j_text_name, jstring j_level, jlong j_docnum) {
182
183#ifdef __WIN32__
184 const char* base_dir = "";
185#else
186 const char* base_dir = "/";
187#endif
188
189 const char * text_name = j_env->GetStringUTFChars(j_text_name, NULL);
190 if (text_name==NULL) {
191 return NULL;
192 }
193
194 const char * level = j_env->GetStringUTFChars( j_level, NULL);
195 if (level==NULL) {
196 j_env->ReleaseStringUTFChars(j_text_name, text_name);
197 return NULL;
198 }
199
200 // does this work alright? j_docnum is a long (64 bit)
201 unsigned long docnum = j_docnum;
202 TextData td;
203
204 // cast to char* otherwise complains about const
205 td.LoadData((char *)base_dir, (char *)text_name);
206
207 UCArray mg_level;
208 SetCStr(mg_level, level);
209 UCArray docText;
210 docText.clear();
211 // get the actual text
212 if (!GetDocText(td, mg_level, docnum, docText)) {
213 cerr <<"MGPP JNI: couldn't retrieve doc text"<<endl;
214 }
215
216 td.UnloadData();
217
218 char * doc = GetCStr(docText); // do I need to free this char *??
219 jstring result = j_env->NewStringUTF(doc);
220 // release any gets
221 j_env->ReleaseStringUTFChars(j_text_name, text_name);
222 j_env->ReleaseStringUTFChars(j_level, level);
223
224 // free any C++ stuff
225 delete doc;
226
227 return result;
228
229}
230
231//******************************************
232// do a query
233// ****************************************
234
235/* load the IndexData - cached for querying
236 */
237JNIEXPORT jboolean JNICALL
238Java_org_greenstone_mgpp_MGPPRetrieveWrapper_loadIndexData (JNIEnv *j_env, jobject j_obj, jstring j_index_name) {
239
240 jint data_ptr = j_env->GetIntField(j_obj, FID_mgpp_data);
241 MGPPWrapperData * data = (MGPPWrapperData *)data_ptr;
242
243#ifdef __WIN32__
244 const char* base_dir = "";
245#else
246 const char* base_dir = "/";
247#endif
248
249 const char * index_name = j_env->GetStringUTFChars( j_index_name, NULL);
250 if (index_name==NULL) {
251 return false;
252 }
253
254 jboolean j_result=false;
255
256 // why doesn't this complain about const??
257 if (data->indexData->LoadData(base_dir, index_name)) {
258 j_result=true;
259 }
260
261 // release any gets
262 j_env->ReleaseStringUTFChars(j_index_name, index_name);
263
264 return j_result;
265}
266
267/* unload the data
268 */
269JNIEXPORT jboolean JNICALL
270Java_org_greenstone_mgpp_MGPPRetrieveWrapper_unloadIndexData (JNIEnv *j_env, jobject j_obj) {
271
272 jint data_ptr = j_env->GetIntField(j_obj, FID_mgpp_data);
273 MGPPWrapperData * data = (MGPPWrapperData *)data_ptr;
274
275 data->indexData->UnloadData();
276 return true;
277
278}
Note: See TracBrowser for help on using the repository browser.