libextractor-java

GNU libextractor
Log | Files | Refs | README | LICENSE

extractor.c (7008B)


      1 /*
      2      This file is part of libextractor.
      3      (C) 2002, 2003, 2004, 2005, 2010, 2012 Vidyut Samanta and Christian Grothoff
      4 
      5      libextractor is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      libextractor is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with libextractor; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #include "config.h"
     22 #include <extractor.h>
     23 #include <jni.h>
     24 
     25 /* gcj's jni.h does not define JNIEXPORT/JNICALL (at least
     26  * not in my version).  Sun defines it to 'empty' on GNU/Linux,
     27  * so that should work */
     28 #ifndef JNIEXPORT
     29 #define JNIEXPORT
     30 #endif
     31 #ifndef JNICALL
     32 #define JNICALL
     33 #endif
     34 
     35 #include "org_gnu_libextractor_Extractor.h"
     36 
     37 #define HIGHEST_TYPE_NUMBER EXTRACTOR_metatype_get_max()
     38 
     39 /*
     40  * Class:     org_gnu_libextractor_Extractor
     41  * Method:    loadDefaultInternal
     42  * Signature: ()J
     43  */
     44 JNIEXPORT jlong JNICALL 
     45 Java_org_gnu_libextractor_Extractor_loadDefaultInternal(JNIEnv * env,
     46 							   jclass c) {
     47   return (jlong) (long) EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
     48 }
     49 
     50 /*
     51  * Class:     org_gnu_libextractor_Extractor
     52  * Method:    unloadInternal
     53  * Signature: (J)V
     54  */
     55 JNIEXPORT void JNICALL 
     56 Java_org_gnu_libextractor_Extractor_unloadAllInternal(JNIEnv * env,
     57 						         jclass c,
     58 						        jlong arg) {
     59   EXTRACTOR_plugin_remove_all((struct EXTRACTOR_PluginList*) (long) arg);
     60 }
     61 
     62 /*
     63  * Class:     org_gnu_libextractor_Extractor
     64  * Method:    getTypeAsStringInternal
     65  * Signature: (I)Ljava/lang/String;
     66  */
     67 JNIEXPORT jstring JNICALL 
     68 Java_org_gnu_libextractor_Extractor_getTypeAsStringInternal(JNIEnv * env,
     69 							       jclass c,
     70 							       jint type) {
     71   const char * str;
     72 
     73   if ( (type < 0) || (type > HIGHEST_TYPE_NUMBER) )
     74     return NULL; /* error! */
     75   str = EXTRACTOR_metatype_to_string((enum EXTRACTOR_MetaType)type);
     76   if (str == NULL)
     77     return NULL;
     78   return (*env)->NewStringUTF(env, str);
     79 }
     80 
     81 /*
     82  * Class:     org_gnu_libextractor_Extractor
     83  * Method:    getVersionInternal
     84  * Signature: ()I
     85  */
     86 JNIEXPORT jint JNICALL 
     87 Java_org_gnu_libextractor_Extractor_getVersionInternal(JNIEnv * env,
     88 							  jclass c) {
     89   return EXTRACTOR_VERSION;
     90 }
     91 
     92 /*
     93  * Class:     org_gnu_libextractor_Extractor
     94  * Method:    getMaxTypeInternal
     95  * Signature: ()I
     96  */
     97 JNIEXPORT jint JNICALL 
     98 Java_org_gnu_libextractor_Extractor_getMaxTypeInternal(JNIEnv * env,
     99 							  jclass c) {
    100   return HIGHEST_TYPE_NUMBER;
    101 }
    102 
    103 /*
    104  * Class:     org_gnu_libextractor_Extractor
    105  * Method:    unloadPlugin
    106  * Signature: (JLjava/lang/String;)J
    107  */
    108 JNIEXPORT jlong JNICALL 
    109 Java_org_gnu_libextractor_Extractor_unloadPluginInternal(JNIEnv * env,
    110 							    jclass c,
    111 							    jlong handle,
    112 							    jstring name) {
    113   const char * lname;
    114   jboolean bo;
    115   jlong ret;
    116 
    117   bo = JNI_FALSE;
    118   lname = (const char*) (*env)->GetStringUTFChars(env, name, &bo);
    119   ret = (jlong) (long) EXTRACTOR_plugin_remove((struct EXTRACTOR_PluginList*) (long) handle,
    120 					       lname);
    121   (*env)->ReleaseStringUTFChars(env, name, lname);
    122   return ret;
    123 }
    124 
    125 /*
    126  * Class:     org_gnu_libextractor_Extractor
    127  * Method:    loadPlugin
    128  * Signature: (JLjava/lang/String;Z)J
    129  */
    130 JNIEXPORT jlong JNICALL 
    131 Java_org_gnu_libextractor_Extractor_loadPluginInternal(JNIEnv * env,
    132 							  jclass c,
    133 							  jlong handle,
    134 							  jstring name) {
    135   const char * lname;
    136   jboolean bo;
    137   jlong ret;
    138 
    139   bo = JNI_FALSE;
    140   lname = (const char*) (*env)->GetStringUTFChars(env, name, &bo);
    141   ret = (jlong) (long) EXTRACTOR_plugin_add((struct EXTRACTOR_PluginList*) (long) handle,
    142 					    lname,
    143 					    NULL,
    144 					    EXTRACTOR_OPTION_DEFAULT_POLICY);
    145   (*env)->ReleaseStringUTFChars(env, name, lname);
    146   return ret;
    147 }
    148 
    149 
    150 /**
    151  * Closure of 'add_meta' function.
    152  */
    153 struct AddMetaContext
    154 {
    155   JNIEnv *env;
    156   jobject ret;
    157   jclass metadata;
    158   jmethodID meta_ctor;
    159   jmethodID alist_add;
    160 };
    161 
    162 
    163 /**
    164  * Function called by libextractor for each meta data item.
    165  */
    166 static int
    167 add_meta (void *cls,
    168 	  const char *plugin_name,
    169 	  enum EXTRACTOR_MetaType type,
    170 	  enum EXTRACTOR_MetaFormat format,
    171 	  const char *data_mime_type,
    172 	  const char *data,
    173 	  size_t data_len)
    174 {
    175   struct AddMetaContext *ctx = cls;
    176   jbyteArray bdata;
    177   jstring mimestring;
    178   jobject metadata;
    179   
    180   mimestring = (*ctx->env)->NewStringUTF (ctx->env,
    181 					  data_mime_type);
    182   bdata = (*ctx->env)->NewByteArray (ctx->env,
    183 				     data_len);
    184   (*ctx->env)->SetByteArrayRegion (ctx->env,
    185 				   bdata,
    186 				   0,
    187 				   data_len,
    188 				   (jbyte*) data);
    189   metadata = (*ctx->env)->NewObject (ctx->env,
    190 				     ctx->metadata,
    191 				     ctx->meta_ctor,
    192 				     type, format, bdata, 
    193 				     mimestring);
    194   (*ctx->env)->CallBooleanMethod (ctx->env,
    195 				  ctx->ret,
    196 				  ctx->alist_add,
    197 				  metadata);
    198   return 0;
    199 }
    200 
    201 
    202 /*
    203  * Class:     org_gnu_libextractor_Extractor
    204  * Method:    extractInternal
    205  * Signature: (JLjava/lang/String;[BLjava/util/ArrayList;)V
    206  */
    207 JNIEXPORT void JNICALL Java_org_gnu_libextractor_Extractor_extractInternal
    208   (JNIEnv *env, jclass c, jlong arg, jstring f, jbyteArray ba, jobject ret)
    209 {
    210   const char * fname;
    211   void * data;
    212   jboolean bo;
    213   jsize asize;
    214   struct AddMetaContext am_ctx;
    215   jclass alist;
    216 
    217   am_ctx.env = env;
    218   am_ctx.ret = ret;
    219   am_ctx.metadata = (*env)->FindClass (env, "org/gnu/libextractor/MetaData");
    220   if (am_ctx.metadata == 0)
    221     return;
    222   am_ctx.meta_ctor = (*env)->GetMethodID (env,
    223 					  am_ctx.metadata, 
    224 					  "<init>",
    225 					  "(II[BLjava/lang/String;)V");
    226   if (am_ctx.meta_ctor == 0)
    227     return;
    228   alist = (*env)->FindClass (env, "java/util/ArrayList");
    229   if (alist == 0)
    230     return;
    231   am_ctx.alist_add = (*env)->GetMethodID (env, 
    232 					  alist, 
    233 					  "add",
    234 					  "(Ljava/lang/Object;)Z");
    235   if (am_ctx.alist_add == 0)
    236     return;
    237   bo = JNI_FALSE;
    238   if (f != 0)
    239     {
    240       fname = (const char*) (*env)->GetStringUTFChars(env, 
    241 						      f, 
    242 						      &bo);
    243     }
    244   else
    245     {
    246       fname = NULL;
    247     }
    248   if (ba != 0)
    249     {
    250       asize = (*env)->GetArrayLength(env, ba);
    251       data = (*env)->GetPrimitiveArrayCritical(env, 
    252 					       ba,
    253 					       &bo);
    254     }
    255   else
    256     {
    257       asize = 0;
    258       data = 0;
    259     }
    260   if ( (data == NULL) && (fname == NULL) )
    261     return;
    262   EXTRACTOR_extract((struct EXTRACTOR_PluginList*) (long) arg,		    
    263 		    fname,
    264 		    data,
    265 		    (size_t) asize,
    266 		    &add_meta,
    267 		    &am_ctx);
    268   if (fname != NULL)
    269     (*env)->ReleaseStringUTFChars(env, 
    270 				  f,
    271 				  fname);
    272   if (data != NULL)
    273     (*env)->ReleasePrimitiveArrayCritical(env, 
    274 					  f, 
    275 					  data,
    276 					  JNI_ABORT);
    277 }
    278