Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
(Added category "Language Bindings")
Line 11: Line 11:
 
Note: To call a method directly it has to be set as <code>static</code> in you java code.  
 
Note: To call a method directly it has to be set as <code>static</code> in you 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>.
 +
To call the <code>write</code> method of the FileWriter class,
 +
you have to pass a class instance as first argument:
 
<pre class="brush:xquery">declare namespace fw = &quot;java:java.io.FileWriter&quot;;
 
<pre class="brush:xquery">declare namespace fw = &quot;java:java.io.FileWriter&quot;;
 
   
 
   
Line 21: Line 23:
 
  )
 
  )
 
</pre>
 
</pre>
 
Note: To call the <code>write</code> method of the filewriter class you have to pass a
 
instantiated filewriter-object as first parameter to the method.
 
  
 
[[Category:XQuery]]
 
[[Category:XQuery]]
 
[[Category:Language Bindings]]
 
[[Category:Language Bindings]]

Revision as of 19:14, 21 January 2011

The Java Binding feature is an extensibility mechanism which allows 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 namespace URI must be of the form java:fullyQualifiedClassName.

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))

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)
 )