Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
Line 1: Line 1:
 
The Java Binding [[Querying|query]] feature is an extensibility mechanism which allows
 
The Java Binding [[Querying|query]] feature is an extensibility mechanism which allows
 
direct calling of Java methods bound as XQuery functions and manipulation of wrapped
 
direct calling of Java methods bound as XQuery functions and manipulation of wrapped
Java objects. The following examples introduce the behavior. Please note that the
+
Java objects. For using the Java Binding feature the user needs [[User_Management|admin rights]].<br />
namespace URI must be of the form <code>java:fullyQualifiedClassName</code> and the
+
Please note that the namespace URI must be of the form <code>java:fullyQualifiedClassName</code>.<br />
user needs to have admin rights to perfom queries with java bindings.
+
The following examples introduce the behavior.
 
   
 
   
 
This example uses the java <code>Math</code> class and returns the cosine of an angle:  
 
This example uses the java <code>Math</code> class and returns the cosine of an angle:  

Revision as of 17:50, 27 June 2011

The Java Binding query feature is an extensibility mechanism which allows direct calling of Java methods bound as XQuery functions and manipulation of wrapped Java objects. For using the Java Binding feature the user needs admin rights.
Please note that the namespace URI must be of the form java:fullyQualifiedClassName.
The following examples introduce the behavior.

This example uses the java Math class and returns the cosine of an angle:

declare namespace math = "java:java.lang.Math";
math:cos(xs:double(0))

Please Note: To call a method directly it has to be set as static in you 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.