Difference between revisions of "Java Bindings"
Line 2: | Line 2: | ||
It shows how to call Java code from XQuery. | It shows how to call Java code from XQuery. | ||
− | The Java Binding [[Querying|query]] feature is an extensibility mechanism which | + | The Java Binding [[Querying|query]] feature is an extensibility mechanism which enables developers |
− | + | to directly access Java variables and execute code from XQuery. Java classes are identified by | |
− | + | namespaces; the namespace URI must be of the form {{Mono|java:fully.qualified.ClassName}}. | |
− | + | The following example uses Java’s <code>Math</code> class and returns the cosine of an angle: | |
− | |||
− | |||
<pre class="brush:xquery">declare namespace math = "java:java.lang.Math"; | <pre class="brush:xquery">declare namespace math = "java:java.lang.Math"; | ||
math:cos(xs:double(0)) | math:cos(xs:double(0)) | ||
</pre> | </pre> | ||
− | '''Please Note''': To call a method | + | '''Please Note''': To directly call a method it has to be set as <code>'''static'''</code> in your java code. |
The next example writes 256 bytes to the file <code>output.txt</code>. | The next example writes 256 bytes to the file <code>output.txt</code>. | ||
Line 31: | Line 29: | ||
as Java code cannot be pre-compiled by BaseX, and will most probably be evaluated | as Java code cannot be pre-compiled by BaseX, and will most probably be evaluated | ||
slower than pure XQuery code. | slower than pure XQuery code. | ||
+ | Next, [[User_Management|admin permissions]] are required to execute Java code. | ||
[[Category:XQuery]] | [[Category:XQuery]] | ||
[[Category:API]] | [[Category:API]] |
Revision as of 18:24, 28 January 2012
This article is part of the Query Portal. It shows how to call Java code from XQuery.
The Java Binding query feature is an extensibility mechanism which enables developers
to directly access Java variables and execute code from XQuery. Java classes are identified by
namespaces; the namespace URI must be of the form java:fully.qualified.ClassName
.
The following example uses Java’s Math
class and returns the cosine of an angle:
declare namespace math = "java:java.lang.Math"; math:cos(xs:double(0))
Please Note: To directly call a method it has to be set as static
in your java code.
The next example writes 256 bytes to the file output.txt
.
To call the write
method of the FileWriter class,
you have to pass a class instance as first argument:
declare namespace fw = "java:java.io.FileWriter"; let $file := fw:new('output.txt') return ( for $i in 0 to 255 return fw:write($file, xs:int($i)), fw:close($file) )
Note that we recommend to use XQuery expressions and functions whenever possible, as Java code cannot be pre-compiled by BaseX, and will most probably be evaluated slower than pure XQuery code. Next, admin permissions are required to execute Java code.