File: Extractor.java

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 (244 lines) | stat: -rw-r--r-- 6,155 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
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004, 2007, 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.
 */
package org.gnu.libextractor;

import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;

/**
 * Java Binding for libextractor.  Each Extractor instance
 * represents a set of meta data extraction plugins.
 *
 * @see Xtract
 * @see MetaData
 * @author Christian Grothoff
 */ 
public final class Extractor {
	

    private static final boolean warn_;

    /**
     * LE version.  0 if LE was compiled without JNI/Java support, in which
     * case we better not call any native methods...
     */
    private static final int version_;

    static {	
	// first, initialize warn_
	boolean warn = false;
	try {
	    if (System.getProperty("libextractor.warn") != null)
		warn = true;
	} catch (SecurityException se) {
	    // ignore
	} finally {
	    warn_ = true; // warn;
	}

	// next, load library and determine version_
	int ver = 0;
	try {
	    System.loadLibrary("extractor_java");
	} catch (UnsatisfiedLinkError ule) {
	    ver = -1;
	    warn("Did not find libextractor_java library: " + ule);
	}
	if (ver == 0) {
	    try {
		ver = getVersionInternal();
	    } catch (UnsatisfiedLinkError ule) {
		// warn: libextractor compiled without Java support
		warn("libextractor library compiled without Java support: " + ule);
	    }
	}
	version_ = ver;
    }    


    private static void warn(String warning) {
	if (warn_)
	    System.err.println("WARNING: " + warning);
    }


    /**
     * @return -1 if LE library was not found, 0 if LE library
     *  was found but compiled without JNI support, otherwise
     *  the LE version number
     */
    public static int getVersion() {
	return version_;
    }


    /**
     * Get the 'default' extractor, that is an extractor that loads
     * the default set of extractor plugins.
     */
    public static Extractor getDefault() {
	if (version_ > 0)
	    return new Extractor(loadDefaultInternal());
	return new Extractor(0);
    }


    /**
     * Get the 'empty' extractor, that is an extractor that does not
     * have any plugins loaded.  This is useful to manually construct
     * an Extractor from scratch.
     */
    public static Extractor getEmpty() {
	return new Extractor(0L);
    }


    /**
     * Handle to the list of plugins (a C pointer, long to support
     * 64-bit architectures!).
     */
    private long pluginHandle_;


    /**
     * Creates an extractor.
     *
     * @param pluginHandle the internal handle (C pointer!) refering
     *   to the list of plugins.  0 means no plugins.
     */
    private Extractor(long pluginHandle) {
	pluginHandle_ = pluginHandle;
    }


    /**
     * Unloads all loaded plugins on "exit".
     */
    protected void finalize() {
	if (pluginHandle_ != 0)
	    unloadAllInternal(pluginHandle_);
    }


    /**
     * Remove a plugin from the list of plugins.
     *
     * @param pluginName name of the plugin to unload
     */
    public void unloadPlugin(String pluginName) {
	if (pluginHandle_ != 0) 
	    pluginHandle_ = unloadPluginInternal(pluginHandle_,
						 pluginName);	
    }


    /**
     * Add an additional plugin to the list of plugins
     * used.
     *
     * @param pluginName name of the plugin to load
     */
    public void loadPlugin(String pluginName) {
	if (version_ <= 0)
	    return; 
	pluginHandle_ = loadPluginInternal(pluginHandle_,
					   pluginName);
    }


    /**
     * Extract keywords (meta-data) from the given file.
     *
     * @param f the file to extract meta-data from
     * @return extracted meta data (ArrayList<MetaData>)
     */
    public ArrayList extract(File f) {
	return extract(f.getAbsolutePath());
    }


    /**
     * Extract keywords (meta-data) from the given file.
     *
     * @param file the name of the file
     * @return extracted meta data (ArrayList<MetaData>)
     */
    public ArrayList extract(String filename) {
	ArrayList ret = new ArrayList(0);
	if (pluginHandle_ == 0)
	    return ret; // fast way out
	extractInternal(pluginHandle_,
			filename,
			null,
			ret);
	return ret;
    }

    
    /**
     * Extract keywords (meta-data) from the given block
     * of data.
     *
     * @param data the file data
     * @return extracted meta data (ArrayList<MetaData>)
     */
    public ArrayList extract(byte[] data) {
	ArrayList ret = new ArrayList(0);
	if (pluginHandle_ == 0)
	    return ret; // fast way out
	extractInternal(pluginHandle_,
			null,
			data,
			ret);	
	return ret;
    }

    
    /* ********************* native calls ******************** */

    private static native long unloadPluginInternal(long handle,
						    String pluginName);
    
    private static native long loadPluginInternal(long handle,
						  String pluginName);

    private static native long loadDefaultInternal();

    private static native void unloadAllInternal(long handle);
    
    private static native void extractInternal(long handle,
					       String filename,
					       byte[] data,
					       ArrayList result);

    private static native int getVersionInternal();

    /**
     * Not private since we use this from "MetaData".
     */
    static native String getTypeAsStringInternal(int type);

    /**
     * Not private since we use this from "MetaData".
     */
    static native int getMaxTypeInternal();

} // end of Extractor