Difference between revisions of "Java Bindings"
Line 6: | Line 6: | ||
namespaces; the namespace URI must be of the form {{Mono|java:fully.qualified.ClassName}}. | namespaces; the namespace URI must be of the form {{Mono|java:fully.qualified.ClassName}}. | ||
− | The following example uses Java’s | + | The following example uses Java’s {{Mono|Math}} class and returns the cosine of an angle. |
+ | The {{Mono|cos()}} method can be directly called, as it is a {{Mono|static}} method: | ||
+ | |||
<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> | ||
− | + | The next example writes 256 bytes to the file {{Mono|output.txt}}. | |
− | + | First, a new {{Mono|FileWriter}} instance is created: by calling the | |
− | The next example writes 256 bytes to the file | + | {{Mono|new()}} function, the class constructor is invoked. Instance |
− | + | methods are called by passing on the resulting Java object as | |
− | + | first argument: | |
+ | |||
<pre class="brush:xquery">declare namespace fw = "java:java.io.FileWriter"; | <pre class="brush:xquery">declare namespace fw = "java:java.io.FileWriter"; | ||
− | + | let $file := fw:new('output.txt') | |
− | let $file := fw:new('output.txt') | ||
return ( | return ( | ||
for $i in 0 to 255 | for $i in 0 to 255 | ||
Line 26: | Line 28: | ||
</pre> | </pre> | ||
− | + | In general, we recommend everyone to use XQuery expressions and functions whenever possible, | |
− | as Java code cannot be pre-compiled | + | as Java code cannot be pre-compiled, and will often be evaluated slower than optimized |
− | slower than | + | XQuery code. Next, Java code can only be executed with [[User_Management|admin permissions]]. |
− | Next, [[User_Management|admin permissions]] | ||
[[Category:XQuery]] | [[Category:XQuery]] | ||
[[Category:API]] | [[Category:API]] |
Revision as of 18:33, 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.
The cos()
method can be directly called, as it is a static
method:
declare namespace math = "java:java.lang.Math"; math:cos(xs:double(0))
The next example writes 256 bytes to the file output.txt
.
First, a new FileWriter
instance is created: by calling the
new()
function, the class constructor is invoked. Instance
methods are called by passing on the resulting Java object 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) )
In general, we recommend everyone to use XQuery expressions and functions whenever possible, as Java code cannot be pre-compiled, and will often be evaluated slower than optimized XQuery code. Next, Java code can only be executed with admin permissions.