1 %module
(directors
="1") xapian
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
25 // Insert code to automatically load the JNI library.
26 %pragma
(java
) jniclasscode
=%{
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
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
86 %apply int
{ Xapian
::valueno
};
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
()!
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
>
113 %ignore version_string
;
114 %ignore major_version
;
115 %ignore minor_version
;
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
() {
124 if
(Xapian
::iterator_valid
(*self
)) {
133 bool hasNext
() const
{ return Xapian
::iterator_valid
(*self
); }
136 %extend TermIterator
{
137 std
::string next
() {
139 if
(Xapian
::iterator_valid
(*self
)) {
148 bool hasNext
() const
{ return Xapian
::iterator_valid
(*self
); }
151 %extend ValueIterator
{
152 std
::string next
() {
154 if
(Xapian
::iterator_valid
(*self
)) {
163 bool hasNext
() const
{ return Xapian
::iterator_valid
(*self
); }
166 %extend ESetIterator
{
167 std
::string next
() {
169 if
(Xapian
::iterator_valid
(*self
)) {
178 bool hasNext
() const
{ return Xapian
::iterator_valid
(*self
); }
181 %extend MSetIterator
{
182 Xapian
::docid next
() {
184 if
(Xapian
::iterator_valid
(*self
)) {
193 bool hasNext
() const
{ return Xapian
::iterator_valid
(*self
); }
198 #define XAPIAN_MIXED_SUBQUERIES_BY_ITERATOR_TYPEMAP
201 class XapianSWIGStrItor
{
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_
) {
223 void end
(jsize len_
) {
227 XapianSWIGStrItor
& operator++() {
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
);
241 bool operator
==(const XapianSWIGStrItor
& o) {
245 bool operator
!=(const XapianSWIGStrItor
& o) {
246 return
!(*this
== o
);
249 difference_type operator-
(const XapianSWIGStrItor
&o) const {
254 class XapianSWIGQueryItor
{
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++() {
273 const Xapian
::Query
& operator*() const {
274 return
**(Xapian
::Query
**)p
;
277 bool operator
==(const XapianSWIGQueryItor
& o) {
281 bool operator
!=(const XapianSWIGQueryItor
& o) {
282 return
!(*this
== o
);
285 difference_type operator-
(const XapianSWIGQueryItor
&o) const {
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
304 %typemap
(javain
) XapianSWIGStrItor
, XapianSWIGStrItor
"$javainput"
306 %typemap
(in
) (XapianSWIGQueryItor qbegin
, XapianSWIGQueryItor qend
) %{
308 SWIG_JavaThrowException
(jenv
, SWIG_JavaNullPointerException
, "null array");
309 jlong
* jarr
= jenv-
>GetLongArrayElements
($input
, NULL);
310 if
(!jarr
) return $null
;
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
]);
354 #define XAPIAN_TERMITERATOR_PAIR_OUTPUT_TYPEMAP
355 %typemap
(out
) std
::pair
<Xapian
::TermIterator
, Xapian
::TermIterator
> {
356 jobjectArray c_result
;
360 const jclass clazz
= jenv-
>FindClass
("java/lang/String");
361 const jclass arrayClass
= jenv-
>FindClass
("java/util/ArrayList");
362 if
(arrayClass
== NULL)
365 const jmethodID mid_init
= jenv-
>GetMethodID
(arrayClass
, "<init>", "()V");
366 if
(mid_init
== NULL)
369 jobject objArr
= jenv-
>NewObject
(arrayClass
, mid_init
);
373 const jmethodID mid_add
= jenv-
>GetMethodID
(arrayClass
, "add",
374 "(Ljava/lang/Object;)Z");
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
);
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) %{
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
);
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
);
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[]"
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