Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
m (wikify)
Line 5: Line 5:
 
The following examples introduce the behavior. Please note that the namespace URI
 
The following examples introduce the behavior. Please note that the namespace URI
 
must be of the form <code>java:fullyQualifiedClassName</code>.
 
must be of the form <code>java:fullyQualifiedClassName</code>.
<blockquote>
+
<p>This example uses the java <code>Math</code> class and returns the cosine of an angle:</p>
+
This example uses the java <code>Math</code> class and returns the cosine of an angle:  
 
<pre>declare namespace math = "java:java.lang.Math";
 
<pre>declare namespace math = "java:java.lang.Math";
 
math:cos(xs:double(0))
 
math:cos(xs:double(0))
 
</pre>  
 
</pre>  
 
   
 
   
<p>The next example writes 256 bytes to the file <code>output.txt</code>:</p>
+
The next example writes 256 bytes to the file <code>output.txt</code>:  
 
<pre>declare namespace fw = &quot;java:java.io.FileWriter&quot;;
 
<pre>declare namespace fw = &quot;java:java.io.FileWriter&quot;;
 
   
 
   
Line 21: Line 21:
 
  )
 
  )
 
</pre>  
 
</pre>  
</blockquote>
+
 
   
 
   
 
==Try/Catch==
 
==Try/Catch==
<p>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
 
run-time errors. This feature will also be part of the new
 
run-time errors. This feature will also be part of the new
<a href="http://www.w3.org/TR/xquery-11/#id-try-catch">XQuery 1.1 Recommendation</a>.<br/>
+
[http://www.w3.org/TR/xquery-11/#id-try-catch XQuery 1.1 Recommendation].  
</p>
+
<blockquote>
+
<p><b>Example:</b></p>
+
'''Example:'''
 
<pre>try {
 
<pre>try {
 
   1 + '2'
 
   1 + '2'
Line 35: Line 35:
 
   concat('Error [', $code, ']: ', $desc)
 
   concat('Error [', $code, ']: ', $desc)
 
  }</pre>  
 
  }</pre>  
<p><b>Result:</b> <code>Error [XPTY0004]: '+' operator: number expected, string found.</code></p>  
+
'''Result:''' <code>Error [XPTY0004]: '+' operator: number expected, string found.</code>  
</blockquote>
+
 
 
[[Category:XQuery]]
 
[[Category:XQuery]]
 
[[Category:Wikify]]
 
[[Category:Wikify]]

Revision as of 22:07, 9 December 2010

Java Binding

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.