Add --read-filters to read list of filters from file
[xapian.git] / xapian-bindings / java / java.i
blob6775505ad8993a745b8fa5759d6802d79ce028fe
1 %module(directors="1") xapian
2 %{
3 /* java.i: SWIG interface file for the Java bindings
5 * Copyright (c) 2007,2009,2011,2012,2014,2016,2017,2018 Olly Betts
6 * Copyright (c) 2012 Dan Colish
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
25 // Insert code to automatically load the JNI library.
26 %pragma(java) jniclasscode=%{
27 static {
28 System.loadLibrary("xapian_jni");
32 // Use SWIG directors for Java wrappers.
33 #define XAPIAN_SWIG_DIRECTORS
35 %include ../xapian-head.i
37 // Rename function and method names to match Java conventions (e.g. from
38 // get_description() to getDescription()).
39 %rename("%(lctitle)s",%$isfunction) "";
41 // Fix up API methods which aren't split by '_' on word boundaries.
43 %rename("getTermPos") get_termpos;
44 %rename("getTermFreq") get_termfreq;
45 %rename("getTermWeight") get_termweight;
46 %rename("getDocCount") get_doccount;
47 %rename("getDocId") get_docid;
48 %rename("getDocLength") get_doclength;
49 %rename("getDocumentId") get_document_id;
50 %rename("positionListBegin") positionlist_begin;
51 %rename("positionListEnd") positionlist_end;
52 %rename("getValueNo") get_valueno;
53 %rename("termListCount") termlist_count;
54 %rename("termListBegin") termlist_begin;
55 %rename("termListEnd") termlist_end;
56 %rename("getFirstItem") get_firstitem;
57 %rename("getSumPart") get_sumpart;
58 %rename("getMaxPart") get_maxpart;
59 %rename("getSumExtra") get_sumextra;
60 %rename("getMaxExtra") get_maxextra;
61 %rename("postListBegin") postlist_begin;
62 %rename("postListEnd") postlist_end;
63 %rename("allTermsBegin") allterms_begin;
64 %rename("allTermsEnd") allterms_end;
65 %rename("getLastDocId") get_lastdocid;
66 %rename("getAvLength") get_avlength;
67 %rename("stopListBegin") stoplist_begin;
68 %rename("stopListEnd") stoplist_end;
69 %rename("getMSet") get_mset;
70 %rename("getESet") get_eset;
72 // Avoid collision with Object.clone
73 %rename("cloneWeight") clone;
75 // toString is more Java-esque and also matches what the old JNI bindings did.
76 %rename("toString") get_description() const;
78 // The old JNI bindings wrapped operator() as accept() for MatchDecider and
79 // ExpandDecider.
80 %rename("accept") Xapian::MatchDecider::operator();
81 %rename("accept") Xapian::ExpandDecider::operator();
83 // By default, valueno is wrapped as long and "(long, bool)" collides with
84 // some of SWIG/Java's machinery, so for now we wrap valueno as int to avoid
85 // this problem.
86 %apply int { Xapian::valueno };
88 %inline {
89 namespace Xapian {
91 // Wrap Xapian::version_string as Xapian.Version.String() as Java can't have
92 // functions outside a class and we don't want Xapian.Xapian.versionString()!
93 class Version {
94 private:
95 Version();
96 ~Version();
97 public:
98 static const char * String() { return Xapian::version_string(); }
99 static int Major() { return Xapian::major_version(); }
100 static int Minor() { return Xapian::minor_version(); }
101 static int Revision() { return Xapian::revision(); }
108 #include <xapian/iterator.h>
111 namespace Xapian {
113 %ignore version_string;
114 %ignore major_version;
115 %ignore minor_version;
116 %ignore revision;
118 // For compatibility with the original JNI wrappers.
119 // FIXME: These make use of the fact that the default ctor for PostingIterator,
120 // TermIterator, and ValueIterator produces an end iterator.
121 %extend PostingIterator {
122 Xapian::docid next () {
123 Xapian::docid tmp;
124 if (Xapian::iterator_valid(*self)) {
125 tmp = (**self);
126 ++(*self);
127 } else {
128 tmp = -1;
130 return tmp;
133 bool hasNext() const { return Xapian::iterator_valid(*self); }
136 %extend TermIterator {
137 std::string next () {
138 std:string tmp;
139 if (Xapian::iterator_valid(*self)) {
140 tmp = (**self);
141 ++(*self);
142 } else {
143 tmp = "";
145 return tmp;
148 bool hasNext() const { return Xapian::iterator_valid(*self); }
151 %extend ValueIterator {
152 std::string next () {
153 std:string tmp;
154 if (Xapian::iterator_valid(*self)) {
155 tmp = (**self);
156 ++(*self);
157 } else {
158 tmp = "";
160 return tmp;
163 bool hasNext() const { return Xapian::iterator_valid(*self); }
166 %extend ESetIterator {
167 std::string next () {
168 std:string tmp;
169 if (Xapian::iterator_valid(*self)) {
170 tmp = (**self);
171 ++(*self);
172 } else {
173 tmp = "";
175 return tmp;
178 bool hasNext() const { return Xapian::iterator_valid(*self); }
181 %extend MSetIterator {
182 Xapian::docid next () {
183 Xapian::docid tmp;
184 if (Xapian::iterator_valid(*self)) {
185 tmp = (**self);
186 ++(*self);
187 } else {
188 tmp = -1;
190 return tmp;
193 bool hasNext() const { return Xapian::iterator_valid(*self); }
198 #define XAPIAN_MIXED_SUBQUERIES_BY_ITERATOR_TYPEMAP
201 class XapianSWIGStrItor {
202 JNIEnv * jenv;
204 jobjectArray array;
206 jsize i;
208 public:
209 typedef std::random_access_iterator_tag iterator_category;
210 typedef Xapian::Query value_type;
211 typedef Xapian::termcount_diff difference_type;
212 typedef Xapian::Query * pointer;
213 typedef Xapian::Query & reference;
215 XapianSWIGStrItor() { }
217 void begin(JNIEnv * jenv_, jobjectArray array_) {
218 jenv = jenv_;
219 array = array_;
220 i = 0;
223 void end(jsize len_) {
224 i = len_;
227 XapianSWIGStrItor & operator++() {
228 ++i;
229 return *this;
232 Xapian::Query operator*() const {
233 jstring term = (jstring)jenv->GetObjectArrayElement(array, i);
234 const char *c_term = jenv->GetStringUTFChars(term, 0);
235 Xapian::Query subq(c_term);
236 jenv->ReleaseStringUTFChars(term, c_term);
237 jenv->DeleteLocalRef(term);
238 return subq;
241 bool operator==(const XapianSWIGStrItor & o) {
242 return i == o.i;
245 bool operator!=(const XapianSWIGStrItor & o) {
246 return !(*this == o);
249 difference_type operator-(const XapianSWIGStrItor &o) const {
250 return i - o.i;
254 class XapianSWIGQueryItor {
255 jlong * p;
257 public:
258 typedef std::random_access_iterator_tag iterator_category;
259 typedef Xapian::Query value_type;
260 typedef Xapian::termcount_diff difference_type;
261 typedef Xapian::Query * pointer;
262 typedef Xapian::Query & reference;
264 XapianSWIGQueryItor() { }
266 void set_p(jlong * p_) { p = p_; }
268 XapianSWIGQueryItor & operator++() {
269 ++p;
270 return *this;
273 const Xapian::Query & operator*() const {
274 return **(Xapian::Query **)p;
277 bool operator==(const XapianSWIGQueryItor & o) {
278 return p == o.p;
281 bool operator!=(const XapianSWIGQueryItor & o) {
282 return !(*this == o);
285 difference_type operator-(const XapianSWIGQueryItor &o) const {
286 return p - o.p;
292 %typemap(in) (XapianSWIGStrItor qbegin, XapianSWIGStrItor qend) {
293 $1.begin(jenv, $input);
294 $2.end(jenv->GetArrayLength($input));
297 /* These 3 typemaps tell SWIG what JNI and Java types to use. */
298 %typemap(jni) XapianSWIGStrItor, XapianSWIGStrItor "jobjectArray"
299 %typemap(jtype) XapianSWIGStrItor, XapianSWIGStrItor "String[]"
300 %typemap(jstype) XapianSWIGStrItor, XapianSWIGStrItor "String[]"
302 /* This typemap handles the conversion of the jtype to jstype typemap type
303 * and vice versa. */
304 %typemap(javain) XapianSWIGStrItor, XapianSWIGStrItor "$javainput"
306 %typemap(in) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) %{
307 if (!$input)
308 SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
309 jlong * jarr = jenv->GetLongArrayElements($input, NULL);
310 if (!jarr) return $null;
311 $1.set_p(jarr);
312 $2.set_p(jarr + jenv->GetArrayLength($input));
315 %typemap(freearg) (XapianSWIGQueryItor qbegin, XapianSWIGQueryItor qend) %{
316 /* We don't change the array so use JNI_ABORT to avoid any work
317 * copying back non-existent changes if the JVM gave us a copy
318 * of the array data. */
319 jenv->ReleaseLongArrayElements($input, jarr, JNI_ABORT);
322 /* These 3 typemaps tell SWIG what JNI and Java types to use. */
323 %typemap(jni) XapianSWIGQueryItor, XapianSWIGQueryItor "jlongArray"
324 %typemap(jtype) XapianSWIGQueryItor, XapianSWIGQueryItor "long[]"
325 %typemap(jstype) XapianSWIGQueryItor, XapianSWIGQueryItor "Query[]"
327 /* This typemap handles the conversion of the jstype to the jtype. */
328 %typemap(javain) XapianSWIGQueryItor, XapianSWIGQueryItor "Query.cArrayUnwrap($javainput)"
330 %typemap(javacode) Xapian::Query %{
331 // For compatibility with the original JNI wrappers.
332 public final static op OP_AND = new op("OP_AND");
333 public final static op OP_OR = new op("OP_OR");
334 public final static op OP_AND_NOT = new op("OP_AND_NOT");
335 public final static op OP_XOR = new op("OP_XOR");
336 public final static op OP_AND_MAYBE = new op("OP_AND_MAYBE");
337 public final static op OP_FILTER = new op("OP_FILTER");
338 public final static op OP_NEAR = new op("OP_NEAR");
339 public final static op OP_PHRASE = new op("OP_PHRASE");
340 public final static op OP_ELITE_SET = new op("OP_ELITE_SET");
341 public final static op OP_VALUE_RANGE = new op("OP_VALUE_RANGE");
343 public final static Query MatchAll = new Query("");
344 public final static Query MatchNothing = new Query();
346 protected static long[] cArrayUnwrap(Query[] arrayWrapper) {
347 long[] cArray = new long[arrayWrapper.length];
348 for (int i=0; i<arrayWrapper.length; i++)
349 cArray[i] = Query.getCPtr(arrayWrapper[i]);
350 return cArray;
354 #define XAPIAN_TERMITERATOR_PAIR_OUTPUT_TYPEMAP
355 %typemap(out) std::pair<Xapian::TermIterator, Xapian::TermIterator> {
356 jobjectArray c_result;
357 jboolean jbool;
358 jstring temp_string;
359 int n = 0;
360 const jclass clazz = jenv->FindClass("java/lang/String");
361 const jclass arrayClass = jenv->FindClass("java/util/ArrayList");
362 if (arrayClass == NULL)
363 return NULL;
365 const jmethodID mid_init = jenv->GetMethodID(arrayClass, "<init>", "()V");
366 if (mid_init == NULL)
367 return NULL;
369 jobject objArr = jenv->NewObject(arrayClass, mid_init);
370 if (objArr == NULL)
371 return NULL;
373 const jmethodID mid_add = jenv->GetMethodID(arrayClass, "add",
374 "(Ljava/lang/Object;)Z");
375 if (mid_add == NULL)
376 return NULL;
378 const jmethodID mid_toArray = jenv->GetMethodID(arrayClass, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
379 if (mid_toArray == NULL) return NULL;
381 for (Xapian::TermIterator i = $1.first; i != $1.second; ++i) {
382 temp_string = jenv->NewStringUTF((*i).c_str());
383 jbool = jenv->CallBooleanMethod(objArr, mid_add, temp_string);
384 if (jbool == false) return NULL;
385 jenv->DeleteLocalRef(temp_string);
386 ++n;
389 c_result = jenv->NewObjectArray(n, clazz, NULL);
390 $result = (jobjectArray)jenv->CallObjectMethod(objArr, mid_toArray, c_result);
393 %typemap(jni) std::pair<Xapian::TermIterator, Xapian::TermIterator> "jobjectArray"
394 %typemap(jtype) std::pair<Xapian::TermIterator, Xapian::TermIterator> "String[]"
395 %typemap(jstype) std::pair<Xapian::TermIterator, Xapian::TermIterator> "String[]"
397 /* This typemap handles the conversion of the jstype to the jtype. */
398 %typemap(javaout) std::pair<Xapian::TermIterator, Xapian::TermIterator> { return $jnicall; }
400 // Typemaps for converting C++ std::string to/from Java byte[] for cases
401 // where the C++ API uses it for binary data.
403 // Terms, document data and user metadata can also be binary data, but for at
404 // least for now we won't worry about that.
406 %typemap(in) const binary_std_string & (jbyte * jarr, std::string c_arg) %{
407 if (!$input)
408 SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null array");
409 jarr = jenv->GetByteArrayElements($input, NULL);
410 if (!jarr) return $null;
411 c_arg.assign(reinterpret_cast<char*>(jarr), jenv->GetArrayLength($input));
412 jenv->ReleaseByteArrayElements($input, jarr, JNI_ABORT);
413 $1 = &c_arg;
416 %typemap(out) binary_std_string {
417 size_t len = $1.size();
418 jbyteArray c_result = jenv->NewByteArray(len);
419 const jbyte* data = reinterpret_cast<const jbyte*>($1.data());
420 // Final parameter was not const in Java 6 and earlier.
421 jbyte* data_nc = const_cast<jbyte*>(data);
422 jenv->SetByteArrayRegion(c_result, 0, len, data_nc);
423 $result = c_result;
426 %typemap(directorin, descriptor="B[", noblock=1) const binary_std_string & {
427 size_t $1_len = $1.size();
428 $input = jenv->NewByteArray($1_len);
429 Swig::LocalRefGuard $1_refguard(jenv, $input);
431 const jbyte* data = reinterpret_cast<const jbyte*>($1.data());
432 // Final parameter was not const in Java 6 and earlier.
433 jbyte* data_nc = const_cast<jbyte*>(data);
434 jenv->SetByteArrayRegion($input, 0, $1_len, data_nc);
438 %typemap(jni) binary_std_string, const binary_std_string & "jbyteArray"
439 %typemap(jtype) binary_std_string, const binary_std_string & "byte[]"
440 %typemap(jstype) binary_std_string, const binary_std_string & "byte[]"
442 %inline %{
443 typedef std::string binary_std_string;
446 %apply const binary_std_string & { const std::string & range_limit };
447 %apply const binary_std_string & { const std::string & range_lower };
448 %apply const binary_std_string & { const std::string & range_upper };
449 %apply const binary_std_string & { const std::string & serialised };
450 %apply const binary_std_string & { const std::string & value };
452 %apply binary_std_string { std::string serialise() };
453 %apply binary_std_string { std::string get_value() };
454 %apply binary_std_string { std::string get_value_lower_bound() };
455 %apply binary_std_string { std::string get_value_upper_bound() };
456 %apply binary_std_string { std::string Xapian::ValueIterator::operator*() };
457 %apply binary_std_string { std::string Xapian::sortable_serialise(double) };
459 #pragma SWIG nowarn=822 /* Suppress warning about covariant return types (FIXME - check if this is a problem!) */
461 %include ../generic/except.i
462 %include ../xapian-headers.i