1 | /*
|
---|
2 | * MGPassesWrapperImpl.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 |
|
---|
20 |
|
---|
21 | #include <jni.h>
|
---|
22 |
|
---|
23 | #ifdef __MINGW32__
|
---|
24 |
|
---|
25 | /* Cross compiling for Windows
|
---|
26 | Want the type definitions in *win32* version of jni_md.h but
|
---|
27 | this then leads to C-mangled style functions which we *don't*
|
---|
28 | want. The following achieves this */
|
---|
29 |
|
---|
30 | #undef JNIEXPORT
|
---|
31 | #undef JNIIMPORT
|
---|
32 | #undef JNICALL
|
---|
33 |
|
---|
34 | #define JNIEXPORT
|
---|
35 | #define JNIIMPORT
|
---|
36 | #define JNICALL
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include "sysfuncs.h"
|
---|
42 | #include "org_greenstone_mg_MGPassesWrapper.h"
|
---|
43 |
|
---|
44 | #include "mg_passes_4jni.h"
|
---|
45 | #include "mg_files.h"
|
---|
46 |
|
---|
47 | /* if we need to use java objects, we should initialise their field ids here*/
|
---|
48 | JNIEXPORT void JNICALL
|
---|
49 | Java_org_greenstone_mg_MGPassesWrapper_initIDs(JNIEnv *j_env, jclass j_cls)
|
---|
50 | {
|
---|
51 | return;
|
---|
52 | }
|
---|
53 |
|
---|
54 | JNIEXPORT jboolean JNICALL
|
---|
55 | Java_org_greenstone_mg_MGPassesWrapper_initCSide(JNIEnv *j_env, jobject j_obj)
|
---|
56 | {
|
---|
57 | clear_variables();
|
---|
58 | return 1; /* true - no errors */
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* add a pass type T1, T2, I1, I2, S */
|
---|
62 | JNIEXPORT void JNICALL
|
---|
63 | Java_org_greenstone_mg_MGPassesWrapper_addPass(JNIEnv *j_env,
|
---|
64 | jobject j_obj,
|
---|
65 | jchar j_pass_type,
|
---|
66 | jchar j_pass_num)
|
---|
67 | {
|
---|
68 | /* get the level as a c char */
|
---|
69 | const char pass_type = j_pass_type;
|
---|
70 | const char pass_num = j_pass_num;
|
---|
71 | add_pass(pass_type, pass_num);
|
---|
72 |
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Set the filename */
|
---|
76 | JNIEXPORT void JNICALL
|
---|
77 | Java_org_greenstone_mg_MGPassesWrapper_setFileName(JNIEnv *j_env,
|
---|
78 | jobject j_obj,
|
---|
79 | jstring j_filename)
|
---|
80 | {
|
---|
81 | /* Get the filename as a C string */
|
---|
82 | const char* filename = (*j_env)->GetStringUTFChars(j_env, j_filename, NULL);
|
---|
83 |
|
---|
84 | assert(filename != NULL);
|
---|
85 | set_filename(filename);
|
---|
86 |
|
---|
87 | /* Release the string */
|
---|
88 | (*j_env)->ReleaseStringUTFChars(j_env, j_filename, filename);
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Set the base path */
|
---|
93 | JNIEXPORT void JNICALL
|
---|
94 | Java_org_greenstone_mg_MGPassesWrapper_setBasePath(JNIEnv *j_env,
|
---|
95 | jobject j_obj,
|
---|
96 | jstring j_basepath)
|
---|
97 | {
|
---|
98 | /* Get the base_path as a C string */
|
---|
99 | const char* basepath = (*j_env)->GetStringUTFChars(j_env, j_basepath, NULL);
|
---|
100 | assert(basepath != NULL);
|
---|
101 |
|
---|
102 | set_basepath(basepath);
|
---|
103 |
|
---|
104 | /* Release the string */
|
---|
105 | (*j_env)->ReleaseStringUTFChars(j_env, j_basepath, basepath);
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* set the level for the inverted file */
|
---|
110 | JNIEXPORT void JNICALL
|
---|
111 | Java_org_greenstone_mg_MGPassesWrapper_setInvfLevel(JNIEnv *j_env,
|
---|
112 | jobject j_obj,
|
---|
113 | jchar j_level)
|
---|
114 | {
|
---|
115 | /* get the level as a c char */
|
---|
116 | const char level = j_level;
|
---|
117 | set_invf_level(level);
|
---|
118 |
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* set the stemmer and stem method */
|
---|
122 | JNIEXPORT void JNICALL
|
---|
123 | Java_org_greenstone_mg_MGPassesWrapper_setStemOptions(JNIEnv *j_env,
|
---|
124 | jobject j_obj,
|
---|
125 | jstring j_stemmer,
|
---|
126 | jint j_method)
|
---|
127 | {
|
---|
128 |
|
---|
129 | const char* stemmer = (*j_env)->GetStringUTFChars(j_env, j_stemmer, NULL);
|
---|
130 | int method = j_method;
|
---|
131 |
|
---|
132 | assert(stemmer != NULL);
|
---|
133 | set_stem_options(stemmer, method);
|
---|
134 |
|
---|
135 | /* Release the string */
|
---|
136 | (*j_env)->ReleaseStringUTFChars(j_env, j_stemmer, stemmer);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /** Specify the size of the document buffer in kilobytes.
|
---|
140 | If any document is larger than bufsize, the program
|
---|
141 | will abort with an error message.
|
---|
142 | */
|
---|
143 | JNIEXPORT void JNICALL
|
---|
144 | Java_org_greenstone_mg_MGPassesWrapper_setBufferSize(JNIEnv *j_env,
|
---|
145 | jobject j_obj,
|
---|
146 | jlong j_bufsize){
|
---|
147 | long buffer = j_bufsize;
|
---|
148 | set_buffer_size(buffer);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Maximum amount of memory to use for the index pass-2 file
|
---|
152 | inversion in megabytes.
|
---|
153 | */
|
---|
154 | JNIEXPORT void JNICALL
|
---|
155 | Java_org_greenstone_mg_MGPassesWrapper_setInversionMemLimit(JNIEnv *j_env,
|
---|
156 | jobject j_obj,
|
---|
157 | jint j_limit) {
|
---|
158 | int limit = j_limit;
|
---|
159 | set_inversion_limit(limit);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** If true, treat SGML tags as non-words when building the
|
---|
163 | inverted file.
|
---|
164 | */
|
---|
165 | JNIEXPORT void JNICALL
|
---|
166 | Java_org_greenstone_mg_MGPassesWrapper_ignoreSGMLTags(JNIEnv *j_env,
|
---|
167 | jobject j_obj,
|
---|
168 | jboolean j_ignore){
|
---|
169 | int ignore = j_ignore;
|
---|
170 | ignore_sgml_tags(ignore);
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** if mg_passes fails, the document that caused the failure will be
|
---|
174 | output to the trace file or STDERR.
|
---|
175 | */
|
---|
176 | JNIEXPORT void JNICALL
|
---|
177 | Java_org_greenstone_mg_MGPassesWrapper_dumpFailedDocument(JNIEnv *j_env,
|
---|
178 | jobject j_obj,
|
---|
179 | jboolean j_dump) {
|
---|
180 | int dump = j_dump;
|
---|
181 | dump_failed_document(dump);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** output statistics on the compression performance to a file
|
---|
185 | called *.compression.stats. frequency specifies the interval
|
---|
186 | (in kilobytes of source text) between outputting each line of
|
---|
187 | statistics.
|
---|
188 | */
|
---|
189 | JNIEXPORT void JNICALL
|
---|
190 | Java_org_greenstone_mg_MGPassesWrapper_outputCompStats(JNIEnv *j_env,
|
---|
191 | jobject j_obj,
|
---|
192 | jint j_frequency){
|
---|
193 | int comp_stat_point = j_frequency;
|
---|
194 | set_comp_stat_point(comp_stat_point);
|
---|
195 |
|
---|
196 | }
|
---|
197 | /** activate tracing, a line will be output every tracepos input bytes */
|
---|
198 | JNIEXPORT void JNICALL
|
---|
199 | Java_org_greenstone_mg_MGPassesWrapper_enableTracing(JNIEnv *j_env,
|
---|
200 | jobject j_obj,
|
---|
201 | jint j_tracepos){
|
---|
202 | int tracepos = j_tracepos;
|
---|
203 | set_trace_point(tracepos);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /** specify the name of the trace file */
|
---|
207 | JNIEXPORT void JNICALL
|
---|
208 | Java_org_greenstone_mg_MGPassesWrapper_setTraceFile(JNIEnv *j_env,
|
---|
209 | jobject j_obj,
|
---|
210 | jstring j_tracefile){
|
---|
211 |
|
---|
212 | const char* tracefile = (*j_env)->GetStringUTFChars(j_env, j_tracefile, NULL);
|
---|
213 | assert(tracefile != NULL);
|
---|
214 | set_trace_file(tracefile);
|
---|
215 | /* Release the string */
|
---|
216 | (*j_env)->ReleaseStringUTFChars(j_env, j_tracefile, tracefile);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /* initialise the pass through the documents. must be called after all
|
---|
220 | the set methods
|
---|
221 | */
|
---|
222 | JNIEXPORT jboolean JNICALL
|
---|
223 | Java_org_greenstone_mg_MGPassesWrapper_init(JNIEnv *j_env,
|
---|
224 | jobject j_obj) {
|
---|
225 |
|
---|
226 | init_driver();
|
---|
227 | return 1;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /* process one document */
|
---|
232 | JNIEXPORT jboolean JNICALL
|
---|
233 | Java_org_greenstone_mg_MGPassesWrapper_processMGDocument(JNIEnv *j_env,
|
---|
234 | jobject j_obj,
|
---|
235 | jbyteArray j_doc_text) {
|
---|
236 | /* Get the text as a C string */
|
---|
237 | int length = (*j_env)->GetArrayLength(j_env, j_doc_text);
|
---|
238 | u_char * text_buffer = (u_char *)(*j_env)->GetByteArrayElements(j_env, j_doc_text, NULL);
|
---|
239 | process_document(text_buffer, length);
|
---|
240 | /* Release the string */
|
---|
241 | (*j_env)->ReleaseByteArrayElements(j_env, j_doc_text, text_buffer,0);
|
---|
242 | return 1;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* finalise the pass through the documents */
|
---|
246 | JNIEXPORT jboolean JNICALL
|
---|
247 | Java_org_greenstone_mg_MGPassesWrapper_finish(JNIEnv *j_env,
|
---|
248 | jobject j_obj) {
|
---|
249 |
|
---|
250 | finalise_driver();
|
---|
251 | return 1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** get the exit value once finished */
|
---|
255 | JNIEXPORT jint JNICALL
|
---|
256 | Java_org_greenstone_mg_MGPassesWrapper_exitValue(JNIEnv *j_env,
|
---|
257 | jobject j_obj) {
|
---|
258 |
|
---|
259 | return get_exit_value();
|
---|
260 | }
|
---|
261 |
|
---|