1 | /*
|
---|
2 | * MGPPRetrieveWrapperImpl.c
|
---|
3 | * Copyright (C) 2007 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 | #ifdef __WIN32__
|
---|
21 | #include <win32cfg.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <jni.h>
|
---|
25 | #include "org_greenstone_mgpp_MGPPRetrieveWrapper.h"
|
---|
26 | #include "TextGet.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | //****************************************************
|
---|
30 | // retrieve a document
|
---|
31 | //****************************************************
|
---|
32 |
|
---|
33 | /* returns a document from mgpp as a string
|
---|
34 | Note: TextData isn't cached - just reloaded each time
|
---|
35 | */
|
---|
36 | JNIEXPORT jstring JNICALL
|
---|
37 | Java_org_greenstone_mgpp_MGPPRetrieveWrapper_getDocument (JNIEnv *j_env,
|
---|
38 | jobject j_obj, jstring j_text_name, jstring j_level, jlong j_docnum) {
|
---|
39 |
|
---|
40 | #ifdef __WIN32__
|
---|
41 | const char* base_dir = "";
|
---|
42 | #else
|
---|
43 | const char* base_dir = "/";
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | const char * text_name = j_env->GetStringUTFChars(j_text_name, NULL);
|
---|
47 | if (text_name==NULL) {
|
---|
48 | return NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | const char * level = j_env->GetStringUTFChars( j_level, NULL);
|
---|
52 | if (level==NULL) {
|
---|
53 | j_env->ReleaseStringUTFChars(j_text_name, text_name);
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // does this work alright? j_docnum is a long (64 bit)
|
---|
58 | mg_u_long docnum = j_docnum;
|
---|
59 | TextData td;
|
---|
60 |
|
---|
61 | // cast to char* otherwise complains about const
|
---|
62 | td.LoadData((char *)base_dir, (char *)text_name);
|
---|
63 |
|
---|
64 | UCArray mg_level;
|
---|
65 | SetCStr(mg_level, level);
|
---|
66 | UCArray docText;
|
---|
67 | docText.clear();
|
---|
68 | // get the actual text
|
---|
69 | if (!GetDocText(td, mg_level, docnum, docText)) {
|
---|
70 | cerr <<"MGPP JNI: couldn't retrieve doc text"<<endl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | td.UnloadData();
|
---|
74 |
|
---|
75 | char * doc = GetCStr(docText);
|
---|
76 | jstring result = j_env->NewStringUTF(doc);
|
---|
77 | // release any gets
|
---|
78 | j_env->ReleaseStringUTFChars(j_text_name, text_name);
|
---|
79 | j_env->ReleaseStringUTFChars(j_level, level);
|
---|
80 |
|
---|
81 | // free any C++ stuff
|
---|
82 | delete doc;
|
---|
83 |
|
---|
84 | return result;
|
---|
85 |
|
---|
86 | }
|
---|