Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
Line 1: Line 1:
==Java Binding==
 
 
 
The Java Binding feature is an extensibility mechanism which allows direct calling
 
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.
 
of Java methods bound as XQuery functions and manipulation of wrapped Java objects.
Line 20: Line 18:
 
   fw:close($file)
 
   fw:close($file)
 
  )
 
  )
</pre>  
+
</pre>
+
 
 
==Try/Catch==
 
==Try/Catch==
 
The well-known try/catch construct can be used in BaseX to intercept
 
The well-known try/catch construct can be used in BaseX to intercept

Revision as of 22:19, 12 December 2010

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

The next example writes 256 bytes to the file output.txt:

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

Try/Catch

The well-known try/catch construct can be used in BaseX to intercept run-time errors. This feature will also be part of the new XQuery 1.1 Recommendation.

Example:

try {
   1 + '2'
 } catch *($code, $desc) {
   concat('Error [', $code, ']: ', $desc)
 }

Result: Error [XPTY0004]: '+' operator: number expected, string found.