Java binding - variable arguments
Tuesday, 23 April 2019 · 5 messages
LB
Luan Bui23 Apr 2019 06:29 #
Hello all,
How can I call a method with variable arguments in BaseX with Java binding? I am trying to do this:
|let $string := 'Format string: %1$d - %2$s'||
|| let $i := 1||
|| let $j := 'text'||
|| return
Q{java.lang.String}format·java.lang.String·java.lang.Object*($string,
$string, $i, $j)|However, I got the error: [XPDY0002]
{java.lang.String}format·java.lang.Stri...: Context is undeclared.
Thank you for your time.
JB
Johannes Bauer23 Apr 2019 06:41 #
Hi Luan,
try this:
declare namespace java = "java:java.lang.String";
let $string := 'Format string: %1$d - %2$s'
let $i := 1
let $j := 'text'
let $args := ($i, $j)return java:format($string, $args)
Best regards
Johannes
Quoted text · Luan Bui · 15 lines
Am 23.04.2019 um 06:29 schrieb Luan Bui: > Hello all, > How can I call a method with variable arguments in BaseX with Java > binding? I am trying to do this: > > |let $string := 'Format string: %1$d - %2$s'|| > || let $i := 1|| > || let $j := 'text'|| > || return > Q{java.lang.String}format·java.lang.String·java.lang.Object*($string, > $string, $i, $j)| > > However, I got the error: [XPDY0002] > {java.lang.String}format·java.lang.Stri...: Context is undeclared. > Thank you for your time.
CG
Christian Grün23 Apr 2019 08:51 #
And a complementary remark: You can also use the built-in function out:format (it will be optimized better than Java functions):
let $string := 'Format string: %1$d - %2$s'
let $i := 1
let $j := 'text'
return out:format($string, $i, $j)For formatting numbers, XQuery provides the function
fn:format-number: format-number(1, '0000')Best,
Christian
Quoted text · Johannes Bauer · 32 lines
On Tue, Apr 23, 2019 at 6:41 AM Johannes Bauer <johannes.bauer at tanner.de> wrote: > > Hi Luan, > > try this: > > declare namespace java = "java:java.lang.String"; > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > let $args := ($i, $j) > > return java:format($string, $args) > > Best regards > Johannes > > Am 23.04.2019 um 06:29 schrieb Luan Bui: > > Hello all, > How can I call a method with variable arguments in BaseX with Java binding? I am trying to do this: > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > return Q{java.lang.String}format·java.lang.String·java.lang.Object*($string, $string, $i, $j) > > However, I got the error: [XPDY0002] {java.lang.String}format·java.lang.Stri...: Context is undeclared. > Thank you for your time. > >
LB
Luan Bui23 Apr 2019 09:00 #
Hi all,
Thanks for your answers. Actually I have to use Java binding to call some methods, the code which I provided is just an example. I wanted to call this method:
publicProcessingResult
<https://get.carrot2.org/stable/javadoc/org/carrot2/core/ProcessingResult.html> process(List
<http://docs.oracle.com/javase/7/docs/api/java/util/List.html?is-external=true><Document
<https://get.carrot2.org/stable/javadoc/org/carrot2/core/Document.html>> documents,
String
<http://docs.oracle.com/javase/7/docs/api/java/lang/String.html?is-external=true> queryHint,
Class
<http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html?is-external=true><?>... processingComponentClasses)
throwsProcessingException
<https://get.carrot2.org/stable/javadoc/org/carrot2/core/ProcessingException.html>Quoted text · Christian Grün · 49 lines
On 4/23/2019 13:51, Christian Grün wrote: > And a complementary remark: You can also use the built-in function > out:format (it will be optimized better than Java functions): > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > return out:format($string, $i, $j) > > For formatting numbers, XQuery provides the function fn:format-number: > > format-number(1, '0000') > > Best, > Christian > > > > On Tue, Apr 23, 2019 at 6:41 AM Johannes Bauer <johannes.bauer at tanner.de> wrote: >> Hi Luan, >> >> try this: >> >> declare namespace java = "java:java.lang.String"; >> >> let $string := 'Format string: %1$d - %2$s' >> let $i := 1 >> let $j := 'text' >> let $args := ($i, $j) >> >> return java:format($string, $args) >> >> Best regards >> Johannes >> >> Am 23.04.2019 um 06:29 schrieb Luan Bui: >> >> Hello all, >> How can I call a method with variable arguments in BaseX with Java binding? I am trying to do this: >> >> let $string := 'Format string: %1$d - %2$s' >> let $i := 1 >> let $j := 'text' >> return Q{java.lang.String}format·java.lang.String·java.lang.Object*($string, $string, $i, $j) >> >> However, I got the error: [XPDY0002] {java.lang.String}format·java.lang.Stri...: Context is undeclared. >> Thank you for your time. >> >>
--
With best regards,
Bui The Luan (Mr.)
Lead Software Developer
----------------------------------------------------
TANNER Vietnam Ltd.
43D/8, Ho Van Hue Street
Ward 9, Phu Nhuan Dist
Ho Chi Minh City, Vietnam
Tax Code: 0303 620 482
tel. +84 28 3997 3452 - 103
fax. +84 28 3997 3465
mobi. +84 168 688 0001
luan.bui at tanner.de <mailto:luan.bui at tanner.de>
http://www.tanner.vn
TANNER Vietnam
------------------------------------------------------------------------------
CG
Christian Grün23 Apr 2019 09:24 #
In that case Johannes’ approach might be the solution.
I say “might”, though, because it’s not always possible to uniquely address Java functions and process their result (addressing varargs is just one example). If you have more than a single Java call in your code, and if it turns that it cannot be written in XQuery, it might be a better option to write a dedicated Java class that does the detail work and returns a single result to XQuery.
Quoted text · Luan Bui · 87 lines
On Tue, Apr 23, 2019 at 9:01 AM Luan Bui <luan.bui at tanner.de> wrote: > > Hi all, > Thanks for your answers. Actually I have to use Java binding to call some methods, the code which I provided is just an example. I wanted to call this method: > > public ProcessingResult process(List<Document> documents, > String queryHint, > Class<?>... processingComponentClasses) > throws ProcessingException > > (https://get.carrot2.org/stable/javadoc/org/carrot2/core/Controller.html#process-java.util.List-java.lang.String-java.lang.Class...-) > > On 4/23/2019 13:51, Christian Grün wrote: > > And a complementary remark: You can also use the built-in function > out:format (it will be optimized better than Java functions): > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > return out:format($string, $i, $j) > > For formatting numbers, XQuery provides the function fn:format-number: > > format-number(1, '0000') > > Best, > Christian > > > > On Tue, Apr 23, 2019 at 6:41 AM Johannes Bauer <johannes.bauer at tanner.de> wrote: > > Hi Luan, > > try this: > > declare namespace java = "java:java.lang.String"; > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > let $args := ($i, $j) > > return java:format($string, $args) > > Best regards > Johannes > > Am 23.04.2019 um 06:29 schrieb Luan Bui: > > Hello all, > How can I call a method with variable arguments in BaseX with Java binding? I am trying to do this: > > let $string := 'Format string: %1$d - %2$s' > let $i := 1 > let $j := 'text' > return Q{java.lang.String}format·java.lang.String·java.lang.Object*($string, $string, $i, $j) > > However, I got the error: [XPDY0002] {java.lang.String}format·java.lang.Stri...: Context is undeclared. > Thank you for your time. > > > > > -- > With best regards, > > Bui The Luan (Mr.) > Lead Software Developer > > ---------------------------------------------------- > TANNER Vietnam Ltd. > 43D/8, Ho Van Hue Street > Ward 9, Phu Nhuan Dist > Ho Chi Minh City, Vietnam > > Tax Code: 0303 620 482 > > tel. +84 28 3997 3452 - 103 > fax. +84 28 3997 3465 > mobi. +84 168 688 0001 > luan.bui at tanner.de > http://www.tanner.vn > > > ------------------------------------------------------------------------------
Written for BaseX 9.2. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery