| 1 |
/* |
| 2 |
* Copyright(C) 2007 Jiro Nishiguchi <jiro@cpan.org> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include "senna_java.h" |
| 21 |
#include "senna_Snippet.h" |
| 22 |
|
| 23 |
static sen_snip * |
| 24 |
this_sen_snip(JNIEnv *env, jobject obj) |
| 25 |
{ |
| 26 |
jclass clazz; |
| 27 |
jfieldID id; |
| 28 |
clazz = (*env)->FindClass(env, "senna/Snippet"); |
| 29 |
id = (*env)->GetFieldID(env, clazz, "ptr", "I"); |
| 30 |
return (sen_snip *) (*env)->GetIntField(env, obj, id); |
| 31 |
} |
| 32 |
|
| 33 |
JNIEXPORT void JNICALL |
| 34 |
Java_senna_Snippet_open(JNIEnv *env, jobject obj, jint encoding, jint flags, jint width, |
| 35 |
jint max_results, jstring defaultopentag, jstring defaultclosetag, jint mapping) |
| 36 |
{ |
| 37 |
sen_snip *snip; |
| 38 |
const char *open_str, *close_str; |
| 39 |
unsigned int open_len, close_len; |
| 40 |
jclass clazz; |
| 41 |
jfieldID fieldID; |
| 42 |
flags = flags | SEN_SNIP_COPY_TAG | SEN_SNIP_SKIP_LEADING_SPACES; |
| 43 |
open_str = (*env)->GetStringUTFChars(env, defaultopentag, NULL); |
| 44 |
close_str = (*env)->GetStringUTFChars(env, defaultclosetag, NULL); |
| 45 |
open_len = (*env)->GetStringUTFLength(env, defaultopentag); |
| 46 |
close_len = (*env)->GetStringUTFLength(env, defaultclosetag); |
| 47 |
snip = sen_snip_open(encoding, flags, width, max_results, open_str, |
| 48 |
open_len, close_str, close_len, (sen_snip_mapping *) mapping); |
| 49 |
(*env)->ReleaseStringUTFChars(env, defaultopentag, open_str); |
| 50 |
(*env)->ReleaseStringUTFChars(env, defaultclosetag, close_str); |
| 51 |
if (snip == NULL) |
| 52 |
ThrowNewSennaException(env, "Failed to call sen_snip_open().", 0); |
| 53 |
clazz = (*env)->FindClass(env, "senna/Snippet"); |
| 54 |
fieldID = (*env)->GetFieldID(env, clazz, "ptr", "I"); |
| 55 |
(*env)->SetIntField(env, obj, fieldID, (jint) snip); |
| 56 |
} |
| 57 |
|
| 58 |
JNIEXPORT void JNICALL |
| 59 |
Java_senna_Snippet_addCond(JNIEnv *env, jobject obj, jstring keyword_str, jstring opentag_str, |
| 60 |
jstring closetag_str) |
| 61 |
{ |
| 62 |
sen_snip *snip; |
| 63 |
const char *keyword, *opentag = NULL, *closetag = NULL; |
| 64 |
unsigned int keyword_len, opentag_len, closetag_len; |
| 65 |
sen_rc rc; |
| 66 |
snip = this_sen_snip(env, obj); |
| 67 |
keyword = (*env)->GetStringUTFChars(env, keyword_str, NULL); |
| 68 |
keyword_len = (*env)->GetStringUTFLength(env, keyword_str); |
| 69 |
if (opentag_str != NULL) { |
| 70 |
opentag = (*env)->GetStringUTFChars(env, opentag_str, NULL); |
| 71 |
opentag_len = (*env)->GetStringUTFLength(env, opentag_str); |
| 72 |
} |
| 73 |
if (closetag_str != NULL) { |
| 74 |
closetag = (*env)->GetStringUTFChars(env, closetag_str, NULL); |
| 75 |
closetag_len = (*env)->GetStringUTFLength(env, closetag_str); |
| 76 |
} |
| 77 |
rc = sen_snip_add_cond(snip, keyword, keyword_len, opentag, opentag_len, closetag, |
| 78 |
closetag_len); |
| 79 |
(*env)->ReleaseStringUTFChars(env, keyword_str, keyword); |
| 80 |
(*env)->ReleaseStringUTFChars(env, opentag_str, opentag); |
| 81 |
(*env)->ReleaseStringUTFChars(env, closetag_str, closetag); |
| 82 |
if (rc != sen_success) |
| 83 |
ThrowNewSennaException(env, "Failed to call sen_snip_add_cond().", rc); |
| 84 |
} |
| 85 |
|
| 86 |
JNIEXPORT jobjectArray JNICALL |
| 87 |
Java_senna_Snippet_exec(JNIEnv *env, jobject obj, jstring jstr) |
| 88 |
{ |
| 89 |
sen_snip *snip; |
| 90 |
const char *string; |
| 91 |
unsigned int i, string_len, nresults, max_tagged_len; |
| 92 |
sen_rc rc; |
| 93 |
jclass string_class; |
| 94 |
jobjectArray array; |
| 95 |
char **results; |
| 96 |
|
| 97 |
snip = this_sen_snip(env, obj); |
| 98 |
string = (*env)->GetStringUTFChars(env, jstr, NULL); |
| 99 |
string_len = (*env)->GetStringUTFLength(env, jstr); |
| 100 |
rc = sen_snip_exec(snip, string, string_len, &nresults, &max_tagged_len); |
| 101 |
if (rc != sen_success) |
| 102 |
ThrowNewSennaException(env, "Failed to call sen_snip_exec().", rc); |
| 103 |
|
| 104 |
string_class = (*env)->FindClass(env, "java/lang/String"); |
| 105 |
array = (*env)->NewObjectArray(env, nresults, string_class, NULL); |
| 106 |
if (array == NULL) |
| 107 |
ThrowNewSennaException(env, "Failed to create Result Array.", 0); |
| 108 |
results = (char **) malloc(sizeof(char *) * nresults); |
| 109 |
if (results == NULL) |
| 110 |
ThrowNewSennaException(env, "Failed to allocate memory.", 0); |
| 111 |
|
| 112 |
for (i = 0; i < nresults; i++) { |
| 113 |
results[i] = (char *) malloc(sizeof(char) * max_tagged_len); |
| 114 |
if (results[i] == NULL) |
| 115 |
ThrowNewSennaException(env, "Failed to allocate memory.", 0); |
| 116 |
rc = sen_snip_get_result(snip, i, results[i], NULL); |
| 117 |
if (rc != sen_success) |
| 118 |
ThrowNewSennaException(env, "Failed to call sen_snip_get_result().", rc); |
| 119 |
(*env)->SetObjectArrayElement(env, array, i, (*env)->NewStringUTF(env, results[i])); |
| 120 |
} |
| 121 |
free(results); |
| 122 |
(*env)->ReleaseStringUTFChars(env, jstr, string); |
| 123 |
return array; |
| 124 |
} |
| 125 |
|
| 126 |
JNIEXPORT void JNICALL |
| 127 |
Java_senna_Snippet_close(JNIEnv *env, jobject obj) |
| 128 |
{ |
| 129 |
sen_snip *snip; |
| 130 |
sen_rc rc; |
| 131 |
snip = this_sen_snip(env, obj); |
| 132 |
rc = sen_snip_close(snip); |
| 133 |
if (rc != sen_success) |
| 134 |
ThrowNewSennaException(env, "Failed to call sen_snip_close().", rc); |
| 135 |
} |
| 136 |
|