File: extractor.c

package info (click to toggle)
libextractor-java 1.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,616 kB
  • sloc: sh: 10,270; ansic: 749; java: 168; makefile: 33
file content (278 lines) | stat: -rw-r--r-- 7,008 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004, 2005, 2010, 2012 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <extractor.h>
#include <jni.h>

/* gcj's jni.h does not define JNIEXPORT/JNICALL (at least
 * not in my version).  Sun defines it to 'empty' on GNU/Linux,
 * so that should work */
#ifndef JNIEXPORT
#define JNIEXPORT
#endif
#ifndef JNICALL
#define JNICALL
#endif

#include "org_gnu_libextractor_Extractor.h"

#define HIGHEST_TYPE_NUMBER EXTRACTOR_metatype_get_max()

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    loadDefaultInternal
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL 
Java_org_gnu_libextractor_Extractor_loadDefaultInternal(JNIEnv * env,
							   jclass c) {
  return (jlong) (long) EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    unloadInternal
 * Signature: (J)V
 */
JNIEXPORT void JNICALL 
Java_org_gnu_libextractor_Extractor_unloadAllInternal(JNIEnv * env,
						         jclass c,
						        jlong arg) {
  EXTRACTOR_plugin_remove_all((struct EXTRACTOR_PluginList*) (long) arg);
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    getTypeAsStringInternal
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL 
Java_org_gnu_libextractor_Extractor_getTypeAsStringInternal(JNIEnv * env,
							       jclass c,
							       jint type) {
  const char * str;

  if ( (type < 0) || (type > HIGHEST_TYPE_NUMBER) )
    return NULL; /* error! */
  str = EXTRACTOR_metatype_to_string((enum EXTRACTOR_MetaType)type);
  if (str == NULL)
    return NULL;
  return (*env)->NewStringUTF(env, str);
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    getVersionInternal
 * Signature: ()I
 */
JNIEXPORT jint JNICALL 
Java_org_gnu_libextractor_Extractor_getVersionInternal(JNIEnv * env,
							  jclass c) {
  return EXTRACTOR_VERSION;
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    getMaxTypeInternal
 * Signature: ()I
 */
JNIEXPORT jint JNICALL 
Java_org_gnu_libextractor_Extractor_getMaxTypeInternal(JNIEnv * env,
							  jclass c) {
  return HIGHEST_TYPE_NUMBER;
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    unloadPlugin
 * Signature: (JLjava/lang/String;)J
 */
JNIEXPORT jlong JNICALL 
Java_org_gnu_libextractor_Extractor_unloadPluginInternal(JNIEnv * env,
							    jclass c,
							    jlong handle,
							    jstring name) {
  const char * lname;
  jboolean bo;
  jlong ret;

  bo = JNI_FALSE;
  lname = (const char*) (*env)->GetStringUTFChars(env, name, &bo);
  ret = (jlong) (long) EXTRACTOR_plugin_remove((struct EXTRACTOR_PluginList*) (long) handle,
					       lname);
  (*env)->ReleaseStringUTFChars(env, name, lname);
  return ret;
}

/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    loadPlugin
 * Signature: (JLjava/lang/String;Z)J
 */
JNIEXPORT jlong JNICALL 
Java_org_gnu_libextractor_Extractor_loadPluginInternal(JNIEnv * env,
							  jclass c,
							  jlong handle,
							  jstring name) {
  const char * lname;
  jboolean bo;
  jlong ret;

  bo = JNI_FALSE;
  lname = (const char*) (*env)->GetStringUTFChars(env, name, &bo);
  ret = (jlong) (long) EXTRACTOR_plugin_add((struct EXTRACTOR_PluginList*) (long) handle,
					    lname,
					    NULL,
					    EXTRACTOR_OPTION_DEFAULT_POLICY);
  (*env)->ReleaseStringUTFChars(env, name, lname);
  return ret;
}


/**
 * Closure of 'add_meta' function.
 */
struct AddMetaContext
{
  JNIEnv *env;
  jobject ret;
  jclass metadata;
  jmethodID meta_ctor;
  jmethodID alist_add;
};


/**
 * Function called by libextractor for each meta data item.
 */
static int
add_meta (void *cls,
	  const char *plugin_name,
	  enum EXTRACTOR_MetaType type,
	  enum EXTRACTOR_MetaFormat format,
	  const char *data_mime_type,
	  const char *data,
	  size_t data_len)
{
  struct AddMetaContext *ctx = cls;
  jbyteArray bdata;
  jstring mimestring;
  jobject metadata;
  
  mimestring = (*ctx->env)->NewStringUTF (ctx->env,
					  data_mime_type);
  bdata = (*ctx->env)->NewByteArray (ctx->env,
				     data_len);
  (*ctx->env)->SetByteArrayRegion (ctx->env,
				   bdata,
				   0,
				   data_len,
				   (jbyte*) data);
  metadata = (*ctx->env)->NewObject (ctx->env,
				     ctx->metadata,
				     ctx->meta_ctor,
				     type, format, bdata, 
				     mimestring);
  (*ctx->env)->CallBooleanMethod (ctx->env,
				  ctx->ret,
				  ctx->alist_add,
				  metadata);
  return 0;
}


/*
 * Class:     org_gnu_libextractor_Extractor
 * Method:    extractInternal
 * Signature: (JLjava/lang/String;[BLjava/util/ArrayList;)V
 */
JNIEXPORT void JNICALL Java_org_gnu_libextractor_Extractor_extractInternal
  (JNIEnv *env, jclass c, jlong arg, jstring f, jbyteArray ba, jobject ret)
{
  const char * fname;
  void * data;
  jboolean bo;
  jsize asize;
  struct AddMetaContext am_ctx;
  jclass alist;

  am_ctx.env = env;
  am_ctx.ret = ret;
  am_ctx.metadata = (*env)->FindClass (env, "org/gnu/libextractor/MetaData");
  if (am_ctx.metadata == 0)
    return;
  am_ctx.meta_ctor = (*env)->GetMethodID (env,
					  am_ctx.metadata, 
					  "<init>",
					  "(II[BLjava/lang/String;)V");
  if (am_ctx.meta_ctor == 0)
    return;
  alist = (*env)->FindClass (env, "java/util/ArrayList");
  if (alist == 0)
    return;
  am_ctx.alist_add = (*env)->GetMethodID (env, 
					  alist, 
					  "add",
					  "(Ljava/lang/Object;)Z");
  if (am_ctx.alist_add == 0)
    return;
  bo = JNI_FALSE;
  if (f != 0)
    {
      fname = (const char*) (*env)->GetStringUTFChars(env, 
						      f, 
						      &bo);
    }
  else
    {
      fname = NULL;
    }
  if (ba != 0)
    {
      asize = (*env)->GetArrayLength(env, ba);
      data = (*env)->GetPrimitiveArrayCritical(env, 
					       ba,
					       &bo);
    }
  else
    {
      asize = 0;
      data = 0;
    }
  if ( (data == NULL) && (fname == NULL) )
    return;
  EXTRACTOR_extract((struct EXTRACTOR_PluginList*) (long) arg,		    
		    fname,
		    data,
		    (size_t) asize,
		    &add_meta,
		    &am_ctx);
  if (fname != NULL)
    (*env)->ReleaseStringUTFChars(env, 
				  f,
				  fname);
  if (data != NULL)
    (*env)->ReleasePrimitiveArrayCritical(env, 
					  f, 
					  data,
					  JNI_ABORT);
}