Difference between revisions of "Java Bindings"

From BaseX Documentation
Jump to navigation Jump to search
 
(40 intermediate revisions by 2 users not shown)
Line 5: Line 5:
  
 
Please bear in mind that the execution of Java code may cause side effects that conflict with the functional nature of XQuery, or may introduce new security risks to your project.
 
Please bear in mind that the execution of Java code may cause side effects that conflict with the functional nature of XQuery, or may introduce new security risks to your project.
 +
 +
Some more notes:
 +
* With the middle dot notation, three adjacent dots can be used to specify array types.
 +
* The path to the standard package {{Code|java.lang.}} can be omitted.
 +
* Java objects are wrapped into function items.
 +
* Results of constructor calls are always returned as function item.
 +
* With {{Option|WRAPJAVA}}, it can be controlled how Java values are converted to XQuery.
  
 
=Identification=
 
=Identification=
Line 16: Line 23:
 
# The last path segment of the URI is capitalized and rewritten to [https://en.wikipedia.org/wiki/CamelCase CamelCase].
 
# The last path segment of the URI is capitalized and rewritten to [https://en.wikipedia.org/wiki/CamelCase CamelCase].
  
The normalization steps are skipped if the URI is prefixed with {{Code|java:}}:
+
The normalization steps are skipped if the URI is prefixed with {{Code|java:}}. The path to the standard package {{Code|java.lang.}} can be omitted:
  
 
* <code><nowiki>http://basex.org/modules/meta-data</nowiki></code> → <code>org.basex.modules.MetaData</code>
 
* <code><nowiki>http://basex.org/modules/meta-data</nowiki></code> → <code>org.basex.modules.MetaData</code>
 
* <code>java:java.lang.String</code> → <code>java.lang.String</code>
 
* <code>java:java.lang.String</code> → <code>java.lang.String</code>
 +
* <code>StringBuilder</code> → <code>java.lang.StringBuilder</code>
  
 
==Functions and Variables==
 
==Functions and Variables==
  
Java functions and variables can be referenced and evaluated by the existing XQuery function syntax:
+
Java constructors, functions and variables can be referenced and evaluated by the existing XQuery function syntax:
  
 
* The namespace of the function name identifies the Java class.
 
* The namespace of the function name identifies the Java class.
 
* The local part of the name, which is rewritten to camel case, identifies a variable or function of that class.
 
* The local part of the name, which is rewritten to camel case, identifies a variable or function of that class.
* The middle dot character <code>[https://www.fileformat.info/info/unicode/char/b7/index.htm ·]</code> (&amp;#xB7;, a valid character in XQuery names, but not in Java) can be used to append exact Java parameter types to the function name. Class types must be referenced by their full path.
+
* The middle dot character <code>[https://www.fileformat.info/info/unicode/char/b7/index.htm ·]</code> (<code>&amp;#xB7;</code>), a valid character in XQuery names, but not in Java) can be used to append exact Java parameter types to the function name. Class types must be referenced by their full path. Three adjacent dots can be used to address an array argument.
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|- valign="top"
 
|- valign="top"
! Type
+
! Addressed code
 
! XQuery
 
! XQuery
 
! Java
 
! Java
 
|- valign="top"
 
|- valign="top"
 
| Variable
 
| Variable
| <code>Q{java.lang.Integer}MIN_VALUE()</code>
+
| <code>Q{Integer}MIN_VALUE()</code>
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#MAX_VALUE Integer.MIN_VALUE]</code>
+
| <code>Integer.MIN_VALUE</code>
 
|- valign="top"
 
|- valign="top"
 
| Function
 
| Function
| <code>Q{java.lang.Object}hash-code($object)</code>
+
| <code>Q{Object}hash-code($object)</code>
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode() object.hashCode()]</code>
+
| <code>object.hashCode()</code>
 
|- valign="top"
 
|- valign="top"
| Function with types
+
| Function with argument
| <code>Q{java.lang.String}split·java.lang.String·int($string, ';', xs:int(3))</code>
+
| <code>Q{String}split·String·int($string, ';', xs:int(3))</code>
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int- string.split(";", 3)]</code>
+
| <code>string.split(";", 3)</code>
 +
|- valign="top"
 +
| Constructor with array argument
 +
| <code>Q{String}new·byte...(xs:hexBinary('414243'))</code>
 +
| <code>new String(new byte[] { 41, 42, 43 })</code>
 
|}
 
|}
  
As XQuery and Java have different type systems, XQuery arguments are converted to equivalent Java values, and the result of a Java function is converted back to an XQuery value (see [[#Data Types|Data Types]]).
+
As XQuery and Java have different type systems, XQuery arguments must be converted to equivalent Java values, and the result of a Java function is converted back to an XQuery value (see [[#Data Types|Data Types]]).
  
If a Java function is not found, XQuery values may need to be cast the target type. For example, if a Java function expects a primitive {{Code|int}} value, you will need to convert your XQuery integers to {{Code|xs:int}}.
+
If the Java function you want to address is not detected, you may need to cast your values to the target type. For example, if a Java function expects a primitive {{Code|int}} value, you will need to convert your XQuery integers to {{Code|xs:int}}.
  
 
=Namespace Declarations=
 
=Namespace Declarations=
  
In the following example, Java’s {{Code|Math}} class is referenced. When executed, the query returns the cosine of an angle by calling the static method {{Code|cos()}}, and the value of π by addressing the static variable via {{Code|PI()}}:
+
In the following example, the Java {{Code|Math}} class is referenced. When executed, the query returns the cosine of an angle by calling the static method {{Code|cos()}}, and the value of π by addressing the static variable via {{Code|PI()}}:
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
declare namespace math = "java:java.lang.Math";
 
declare namespace math = "java:java.lang.Math";
 
math:cos(xs:double(0)), math:PI()
 
math:cos(xs:double(0)), math:PI()
</syntaxhighlight>
+
</pre>
  
With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,
+
With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0, the namespace can directly be embedded in the function call:
the namespace can directly be embedded in the function call:
 
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
Q{java:java.lang.Math}cos(xs:double(0))
 
Q{java:java.lang.Math}cos(xs:double(0))
</syntaxhighlight>
+
</pre>
  
 
The constructor of a class can be invoked by calling the virtual function {{Code|new()}}. Instance methods can then called by passing on the resulting Java object as first argument. In the following example, 256 bytes are written to the file {{Code|output.txt}}. First, a new {{Code|FileWriter}} instance is created, and its {{Code|write()}} function is called in the next step:
 
The constructor of a class can be invoked by calling the virtual function {{Code|new()}}. Instance methods can then called by passing on the resulting Java object as first argument. In the following example, 256 bytes are written to the file {{Code|output.txt}}. First, a new {{Code|FileWriter}} instance is created, and its {{Code|write()}} function is called in the next step:
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
declare namespace fw = "java.io.FileWriter";
+
declare namespace fw = 'java:java.io.FileWriter';
 
let $file := fw:new('output.txt')
 
let $file := fw:new('output.txt')
 
return (
 
return (
Line 78: Line 89:
 
   fw:close($file)
 
   fw:close($file)
 
)
 
)
</syntaxhighlight>
+
</pre>
  
 
If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting {{Option|CHECKSTRINGS}} to false. In the example below, a file with a single {{Code|00}} byte is written, and this file will then be accessed by via Java functions:
 
If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting {{Option|CHECKSTRINGS}} to false. In the example below, a file with a single {{Code|00}} byte is written, and this file will then be accessed by via Java functions:
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
declare namespace br = 'java.io.BufferedReader';
+
declare namespace br = 'java:java.io.BufferedReader';
declare namespace fr = 'java.io.FileReader';
+
declare namespace fr = 'java:java.io.FileReader';
  
 
declare option db:checkstrings 'false';
 
declare option db:checkstrings 'false';
  
 +
(: write file :)
 
file:write-binary('00.bin', xs:hexBinary('00')),
 
file:write-binary('00.bin', xs:hexBinary('00')),
br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.))
+
(: read file :)
</syntaxhighlight>
+
let $br := br:new(fr:new('00.bin'))
 +
return (
 +
  br:readLine($br),  
 +
  br:close($br)
 +
)
 +
</pre>
 +
 
 +
The option can also be specified via a pragma:
  
Note that Java code cannot be pre-compiled, and will as such be evaluated slower than optimized XQuery code.
+
<pre lang='xquery'>
 +
(# db:checkstrings #) {
 +
  br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.))
 +
}
 +
</pre>
  
 
=Module Imports=
 
=Module Imports=
  
An alternative solution is to access Java code by ''importing'' classes as modules. A new instance of the addressed class will be created, which can then be referenced in the query body.
+
A Java class can be instantiated by ''importing'' them as a module: A new instance of the addressed class will be constructed, which can then be referenced in the query body.
  
In the (side-effecting) example below, the size of a Java hash set is returned. The boolean values that are returned by {{Code|set:add()}} are swallowed:
+
In the (side-effecting) example below, a HashSet instance is created, values are added, and the size of the set is returned. As {{Code|set:add()}} returns boolean values, {{Function|Profiling|prof:void}} is used to swallow the values:
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
import module namespace set = "java:java.util.HashSet";
 
import module namespace set = "java:java.util.HashSet";
 
prof:void(
 
prof:void(
Line 107: Line 130:
 
),
 
),
 
set:size()
 
set:size()
</syntaxhighlight>
+
</pre>
  
The execution of imported classes is more efficient than the execution of instances that are created at runtime via {{Code|new()}}. A drawback is that no arguments can be passed on to the class constructor. As a consequence, the import fails if the addressed class has no default constructor, but at least one constructor with arguments.
+
The execution of imported classes is more efficient than the execution of instances that have been created via {{Code|new()}}. In turn, no arguments can be supplied in the import statement, and the construction will only be successful if the class can be instantiated without arguments.
  
 
=Integration=
 
=Integration=
  
Java classes can be coupled even more closely to the BaseX core library.
+
Java classes can be coupled more closely to BaseX. If a class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryModule.java QueryModule] class, the two variables [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryContext.java queryContext] and [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/StaticContext.java staticContext] get available, which provide access to the global and static context of a query.
If a class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryModule.java QueryModule] class, the two variables [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryContext.java queryContext] and [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/StaticContext.java staticContext] get available, which provide access to the global and static context of a query.
 
  
 
The [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryResource.java QueryResource] interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its {{Code|close()}} method will be called after the XQuery expression has been fully evaluated.
 
The [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryResource.java QueryResource] interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its {{Code|close()}} method will be called after the XQuery expression has been fully evaluated.
Line 122: Line 144:
 
The internal properties of functions can be assigned via annotations:
 
The internal properties of functions can be assigned via annotations:
  
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You may annotate a function with {{Code|@Requires(<Permission>)}} to also make it accessible to users with less privileges.
+
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You can annotate a function with {{Code|@Requires(<Permission>)}} to also make it accessible to users with fewer privileges.
* Java code is treated as ''non-deterministic'', as its behavior cannot be predicted by the XQuery processor. You may annotate a function as {{Code|@Deterministic}} if you know that it will have no side-effects and will always yield the same result.
+
* Java code is treated as ''nondeterministic'', as its behavior cannot be predicted by the XQuery processor. You may annotate a function as {{Code|@Deterministic}} if you know that it will have no side effects and will always yield the same result.
 
* Java code is treated as ''context-independent''. If a function accesses the query context, it should be annotated as {{Code|@ContextDependent}}
 
* Java code is treated as ''context-independent''. If a function accesses the query context, it should be annotated as {{Code|@ContextDependent}}
 
* Java code is treated as ''focus-independent''. If a function accesses the current context item, position or size, it should be annotated as {{Code|@FocusDependent}}
 
* Java code is treated as ''focus-independent''. If a function accesses the current context item, position or size, it should be annotated as {{Code|@FocusDependent}}
Line 129: Line 151:
 
In the following code, information from the static query context is returned by the first function, and a query exception is raised by the second function:
 
In the following code, information from the static query context is returned by the first function, and a query exception is raised by the second function:
  
<syntaxhighlight lang="xquery">
+
<pre lang='xquery'>
 
import module namespace context = 'org.basex.examples.query.ContextModule';
 
import module namespace context = 'org.basex.examples.query.ContextModule';
  
Line 140: Line 162:
 
   element error { $err:description }
 
   element error { $err:description }
 
}
 
}
</syntaxhighlight>
+
</pre>
  
 
The imported Java class is shown below:
 
The imported Java class is shown below:
  
<syntaxhighlight lang="java">
+
<pre lang="java">
 
package org.basex.examples.query;
 
package org.basex.examples.query;
  
Line 157: Line 179:
 
public class ContextModule extends QueryModule implements QueryResource {
 
public class ContextModule extends QueryModule implements QueryResource {
 
   /**
 
   /**
   * Returns the name of the logged in user.
+
   * Returns the name of the logged-in user.
 
   * @return user string
 
   * @return user string
 
   */
 
   */
Line 188: Line 210:
 
   }
 
   }
 
}
 
}
</syntaxhighlight>
+
</pre>
  
 
The result will look as follows:
 
The result will look as follows:
  
<syntaxhighlight lang="xml">
+
<pre lang="xml">
 
<user>admin</admin>
 
<user>admin</admin>
 
<error>Integer conversion failed: abc</error>
 
<error>Integer conversion failed: abc</error>
</syntaxhighlight>
+
</pre>
  
 
Please visit the XQuery 3.0 specification if you want to get more insight into
 
Please visit the XQuery 3.0 specification if you want to get more insight into
Line 204: Line 226:
 
The {{Code|@Updating}} annotation can be applied to mark Java functions that perform write or update operations:
 
The {{Code|@Updating}} annotation can be applied to mark Java functions that perform write or update operations:
  
<syntaxhighlight lang="java">
+
<pre lang="java">
 
   @Updating
 
   @Updating
 
   public void backup() {
 
   public void backup() {
 
     // ...
 
     // ...
 
   }
 
   }
</syntaxhighlight>
+
</pre>
  
 
An XQuery expression will be handled as an [[XQuery Update#Updating Expressions|updating expression]] if it calls an updating Java function. In contrast to XQuery update operations, the Java code will immediately be executed, but the result will be cached as if {{Function|Update|update:output}} was called.
 
An XQuery expression will be handled as an [[XQuery Update#Updating Expressions|updating expression]] if it calls an updating Java function. In contrast to XQuery update operations, the Java code will immediately be executed, but the result will be cached as if {{Function|Update|update:output}} was called.
Line 221: Line 243:
 
===Java Locks===
 
===Java Locks===
  
Java provides a handful of mechanism to control the execution of code. The concurrent execution of functions can be avoided with the {{Code|synchronized}} keyword. For more complex scenarios, Lock and Semaphor and Atomic classes exist.
+
Java provides a handful of mechanism to control the execution of code. The concurrent execution of functions can be avoided with the {{Code|synchronized}} keyword. For more complex scenarios, the Lock, Semaphore and Atomic classes can be brought into play.
  
 
===XQuery Locks===
 
===XQuery Locks===
Line 227: Line 249:
 
If you want to synchronize the execution of your code with BaseX locks, you can take advantage of the {{Code|@Lock}} annotation:
 
If you want to synchronize the execution of your code with BaseX locks, you can take advantage of the {{Code|@Lock}} annotation:
  
<syntaxhighlight lang="java">
+
<pre lang="java">
 
   @Lock("HEAVYIO")
 
   @Lock("HEAVYIO")
 
   public void read() {
 
   public void read() {
Line 238: Line 260:
 
     // ...
 
     // ...
 
   }
 
   }
</syntaxhighlight>
+
</pre>
  
If an XQuery expression invokes {{Code|write()}}, any other query that call {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. The {{Code|read()}} function can be run in parallel; only those queries are queued that call {{Code|write()}}.  
+
If an XQuery expression invokes {{Code|write()}}, any other query that calls {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. The {{Code|read()}} function can be run in parallel; whereas queries will be queued if {{Code|write()}} is called.
  
 
More details on concurrent querying can be found in the article on [[Transaction Management]].
 
More details on concurrent querying can be found in the article on [[Transaction Management]].
Line 246: Line 268:
 
==Data Types==
 
==Data Types==
  
XQuery and Java types are mapped as follows:
+
===Conversion to Java===
 +
 
 +
Before Java code is executed, the arguments are converted to Java values, depending on the addressed function or constructor parameters. The accepted Java types and the original XQuery types are depicted in the second and first column of the table below.
 +
 
 +
If a numeric value is supplied for which no exact matching is defined, it is cast to the appropriate type unless it exceeds its limits. The following two function calls are equivalent:
 +
 
 +
<pre lang='xquery'>
 +
(: exact match :)
 +
Q{String}codePointAt('ABC', xs:int(1)),
 +
(: xs:byte and xs:integer casts :)
 +
Q{String}codePointAt('ABC', xs:byte(1)),
 +
Q{String}codePointAt('ABC', 1)
 +
</pre>
 +
 
 +
===Conversion to XQuery===
 +
 
 +
By default, Java values with the most common types (as shown in the second and third column of the table) are converted to XQuery values. All other values are returned as ''Java items'', which are function items with a wrapped Java value. The results of constructor calls are always returned as Java items.
 +
 
 +
The conversion of the wrapped Java value to XQuery is enforced by invoking the function item: Values in {{Code|Iterator}} and {{Code|Iterable}} instances (Lists, Sets and Collections) are converted to items, and maps are converted to XQuery maps:
 +
 
 +
<pre lang='xquery'>
 +
declare namespace Scanner = 'java:java.util.Scanner';
 +
let $scanner := Scanner:new("A B C") => Scanner:useDelimiter(" ")
 +
return $scanner()
 +
</pre>
 +
 
 +
If no conversion is defined, a string is returned, resulting from the {{Code|toString()}} method of the object. This method is also called if the string representation of a Java item is requested:
 +
 
 +
<pre lang='xquery'>
 +
(: returns the string representations of a HashMap and an ArrayList instance :)
 +
'Map: ' || Q{java.util.HashMap}new(),
 +
string(Q{java:java.util.ArrayList}new())
 +
</pre>
 +
 
 +
The conversion can be further controlled with the {{Option|WRAPJAVA}} option. The following values exist:
 +
 
 +
{| class="wikitable"
 +
|- valign="top"
 +
! Value
 +
! Description
 +
|- valign="top"
 +
| {{Code|some}}
 +
| The default: Java values of the most common types are converted, others are wrapped into Java items.
 +
|- valign="top"
 +
| {{Code|none}}
 +
| All Java values are converted. If no conversion is defined, a string is returned, resulting from the {{Code|toString()}} method.
 +
|- valign="top"
 +
| {{Code|all}}
 +
| Java values are wrapped into Java items (excluding those inheriting the internal type {{Code|org.basex.query.value.Value}}).
 +
|- valign="top"
 +
| {{Code|instance}}
 +
| If the method of a class instance was called, the Java value is ignored and the instance is wrapped into a Java item. Otherwise, the Java value is returned.
 +
|- valign="top"
 +
| {{Code|void}}
 +
| Java values are ignored, and an empty sequence is returned instead.
 +
|}
 +
 
 +
In the following example, the result of the first function – a char array – is wrapped and passed on to a {{Code|CharBuffer}} function. Without the option, the single-value array would be converted to an {{Code|xs:unsignedShort}} item and the second function call would fail:
 +
 
 +
<pre lang='xquery'>
 +
(: Without the pragma, the result of toChars would be converted to an xs:unsignedShort item, and the second function call would fail :)
 +
 
 +
(# db:wrapjava all #) {
 +
  Q{Character}toChars(xs:int(33))
 +
  => Q{java.nio.CharBuffer}wrap()
 +
}
 +
</pre>
 +
 
 +
The next example demonstrates a use case for the {{Code|instance}} option:
 +
 
 +
<pre lang='xquery'>
 +
(: Thanks to the pragma, the function calls can be chained :)
 +
 
 +
declare namespace set = 'java:java.util.HashSet';
 +
let $set := (# db:wrapjava instance #) {
 +
  set:new()
 +
  => set:add('1')
 +
  => set:add('2')
 +
}
 +
return $set()
 +
</pre>
 +
 
 +
The {{Code|void}} option is helpful if side-effecting methods return values that do not contribute to the final result:
 +
 
 +
<pre lang='xquery'>
 +
(: Without the pragma, 100 booleans would be returned by the FLWOR expression :)
 +
 
 +
declare namespace set = 'java:java.util.HashSet';
 +
let $set := set:new()
 +
return (
 +
  (# db:wrapjava void #) {
 +
    for $i in 1 to 100
 +
    return set:add($set, $i)
 +
  },
 +
  $set()
 +
)
 +
</pre>
 +
 
 +
The irrelevant results could also be swallowed with {{Function|Profiling|prof:void}}.
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|- valign="top"
 
|- valign="top"
! XQuery Type
+
! XQuery input
! Java Type
+
! Expected or returned Java type
 +
! XQuery output
 
|- valign="top"
 
|- valign="top"
 +
| <code>item()*</code> (no conversion)
 +
| <code>org.basex.query.value.Value</code>
 +
| <code>item()*</code> (no conversion)
 +
|- valign="top"
 +
| <code>empty-sequence()</code>
 +
| <code>null</code>
 +
| <code>empty-sequence()</code>
 +
|- valign="top"
 +
| <code>xs:string</code>, <code>xs:untypedAtomic</code>
 +
| <code>String</code>
 
| <code>xs:string</code>
 
| <code>xs:string</code>
| <code>String</code>, <code>char</code>, <code>Character</code>
+
|- valign="top"
 +
| <code>xs:unsignedShort</code>
 +
| <code>char</code>, <code>Character</code>
 +
| <code>xs:unsignedShort</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:boolean</code>
 
| <code>xs:boolean</code>
 
| <code>boolean</code>, <code>Boolean</code>
 
| <code>boolean</code>, <code>Boolean</code>
 +
| <code>xs:boolean</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:byte</code>
 
| <code>xs:byte</code>
 
| <code>byte</code>, <code>Byte</code>
 
| <code>byte</code>, <code>Byte</code>
 +
| <code>xs:byte</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:short</code>
 
| <code>xs:short</code>
 
| <code>short</code>, <code>Short</code>
 
| <code>short</code>, <code>Short</code>
 +
| <code>xs:short</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:int</code>
 
| <code>xs:int</code>
 
| <code>int</code>, <code>Integer</code>
 
| <code>int</code>, <code>Integer</code>
 +
| <code>xs:int</code>
 
|- valign="top"
 
|- valign="top"
| <code>xs:long</code>
+
| <code>xs:integer</code>, <code>xs:long</code>
 
| <code>long</code>, <code>Long</code>
 
| <code>long</code>, <code>Long</code>
 +
| <code>xs:integer</code>
 +
|- valign="top"
 +
| <code>xs:unsignedLong</code>
 +
| <code>java.math.BigInteger</code>
 +
| <code>xs:unsignedLong</code> (lossy)
 +
|- valign="top"
 +
| <code>xs:decimal</code>
 +
| <code>java.math.BigDecimal</code>
 +
| <code>xs:decimal</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:float</code>
 
| <code>xs:float</code>
 
| <code>float</code>, <code>Float</code>
 
| <code>float</code>, <code>Float</code>
 +
| <code>xs:float</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:double</code>
 
| <code>xs:double</code>
 
| <code>double</code>, <code>Double</code>
 
| <code>double</code>, <code>Double</code>
|- valign="top"
+
| <code>xs:double</code>
| <code>xs:decimal</code>
 
| <code>java.math.BigDecimal</code>
 
|- valign="top"
 
| <code>xs:integer</code>
 
| <code>java.math.BigInteger</code>
 
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:QName</code>
 
| <code>xs:QName</code>
 
| <code>javax.xml.namespace.QName</code>
 
| <code>javax.xml.namespace.QName</code>
 +
| <code>xs:QName</code>
 
|- valign="top"
 
|- valign="top"
 
| <code>xs:anyURI</code>
 
| <code>xs:anyURI</code>
 
| <code>java.net.URI</code>, <code>java.net.URL</code>
 
| <code>java.net.URI</code>, <code>java.net.URL</code>
 +
| <code>xs:anyURI</code>
 +
|- valign="top"
 +
| <code>xs:date</code>
 +
| <code>javax.xml.datatype.XMLGregorianCalendar</code>
 +
| <code>xs:date</code>
 +
|- valign="top"
 +
| <code>xs:duration</code>
 +
| <code>javax.xml.datatype.Duration</code>
 +
| <code>xs:duration</code>
 
|- valign="top"
 
|- valign="top"
| ''empty sequence''
+
| <code>node()</code>
| <code>null</code>
+
| <code>org.w3c.dom.Node</code>
 +
| <code>node()</code>
 +
|- valign="top"
 +
| <code>array(xs:boolean)</code>
 +
| <code>boolean[]</code>
 +
| <code>xs:boolean*</code>
 +
|- valign="top"
 +
| <code>array(xs:string)</code>
 +
| <code>String[]</code>
 +
| <code>xs:string*</code>
 +
|- valign="top"
 +
| <code>array(xs:unsignedShort)</code>
 +
| <code>char[]</code>
 +
| <code>xs:unsignedShort*</code>
 +
|- valign="top"
 +
| <code>array(xs:short)</code>
 +
| <code>short[]</code>
 +
| <code>xs:short*</code>
 +
|- valign="top"
 +
| <code>array(xs:int)</code>
 +
| <code>int[]</code>
 +
| <code>xs:int*</code>
 +
|- valign="top"
 +
| <code>array(xs:integer)</code>, <code>array(xs:long)</code>
 +
| <code>long[]</code>
 +
| <code>xs:integer*</code>
 +
|- valign="top"
 +
| <code>array(xs:float)</code>
 +
| <code>float[]</code>
 +
| <code>xs:float*</code>
 +
|- valign="top"
 +
| <code>array(xs:double)</code>
 +
| <code>double[]</code>
 +
| <code>xs:double*</code>
 +
|- valign="top"
 +
| <code>Object[]</code> (others)
 +
| <code>item()*</code>
 +
| <code>array(*)</code> (others)
 +
|- valign="top"
 +
| <code>map(*)</code>
 +
| java.util.HashMap
 +
| <code>Wrapped Java object</code>
 
|}
 
|}
  
Line 311: Line 505:
 
* {{Code|a/little/example}} → {{Code|a/little/example}}
 
* {{Code|a/little/example}} → {{Code|a/little/example}}
 
* {{Code|a:b:c}} → {{Code|a/b/c}}
 
* {{Code|a:b:c}} → {{Code|a/b/c}}
 +
 +
Note that the mapping is not unique: Different URIs may result in the same path.
  
 
=Changelog=
 
=Changelog=
 +
 +
; Version 9.6
 +
* Updated: Java Bindings revised (new mappings, Java functiom items, {{Option|WRAPJAVA}} option).
  
 
; Version 9.4
 
; Version 9.4

Latest revision as of 13:23, 22 January 2024

This article is part of the XQuery Portal. It demonstrates different ways to invoke Java code from XQuery, and it presents extensions to access the current query context from Java.

The Java Binding feature is an extensibility mechanism which enables developers to directly access Java variables and execute code from XQuery. Addressed Java code must either be contained in the Java classpath, or it must be located in the Repository.

Please bear in mind that the execution of Java code may cause side effects that conflict with the functional nature of XQuery, or may introduce new security risks to your project.

Some more notes:

  • With the middle dot notation, three adjacent dots can be used to specify array types.
  • The path to the standard package java.lang. can be omitted.
  • Java objects are wrapped into function items.
  • Results of constructor calls are always returned as function item.
  • With WRAPJAVA, it can be controlled how Java values are converted to XQuery.

Identification[edit]

Classes[edit]

A Java class is identified by a namespace URI. The original URI is rewritten as follows:

  1. The URI Rewriting steps are applied to the URI.
  2. Slashes in the resulting URI are replaced with dots.
  3. The last path segment of the URI is capitalized and rewritten to CamelCase.

The normalization steps are skipped if the URI is prefixed with java:. The path to the standard package java.lang. can be omitted:

  • http://basex.org/modules/meta-dataorg.basex.modules.MetaData
  • java:java.lang.Stringjava.lang.String
  • StringBuilderjava.lang.StringBuilder

Functions and Variables[edit]

Java constructors, functions and variables can be referenced and evaluated by the existing XQuery function syntax:

  • The namespace of the function name identifies the Java class.
  • The local part of the name, which is rewritten to camel case, identifies a variable or function of that class.
  • The middle dot character · (&#xB7;), a valid character in XQuery names, but not in Java) can be used to append exact Java parameter types to the function name. Class types must be referenced by their full path. Three adjacent dots can be used to address an array argument.
Addressed code XQuery Java
Variable Q{Integer}MIN_VALUE() Integer.MIN_VALUE
Function Q{Object}hash-code($object) object.hashCode()
Function with argument Q{String}split·String·int($string, ';', xs:int(3)) string.split(";", 3)
Constructor with array argument Q{String}new·byte...(xs:hexBinary('414243')) new String(new byte[] { 41, 42, 43 })

As XQuery and Java have different type systems, XQuery arguments must be converted to equivalent Java values, and the result of a Java function is converted back to an XQuery value (see Data Types).

If the Java function you want to address is not detected, you may need to cast your values to the target type. For example, if a Java function expects a primitive int value, you will need to convert your XQuery integers to xs:int.

Namespace Declarations[edit]

In the following example, the Java Math class is referenced. When executed, the query returns the cosine of an angle by calling the static method cos(), and the value of π by addressing the static variable via PI():

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

With the Expanded QName notation of XQuery 3.0, the namespace can directly be embedded in the function call:

Q{java:java.lang.Math}cos(xs:double(0))

The constructor of a class can be invoked by calling the virtual function new(). Instance methods can then called by passing on the resulting Java object as first argument. In the following example, 256 bytes are written to the file output.txt. First, a new FileWriter instance is created, and its write() function is called in the next step:

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

If the result of a Java call contains invalid XML characters, it will be rejected. The validity check can be disabled by setting CHECKSTRINGS to false. In the example below, a file with a single 00 byte is written, and this file will then be accessed by via Java functions:

declare namespace br = 'java:java.io.BufferedReader';
declare namespace fr = 'java:java.io.FileReader';

declare option db:checkstrings 'false';

(: write file :)
file:write-binary('00.bin', xs:hexBinary('00')),
(: read file :)
let $br := br:new(fr:new('00.bin'))
return (
  br:readLine($br), 
  br:close($br)
)

The option can also be specified via a pragma:

(# db:checkstrings #) {
  br:new(fr:new('00.bin')) ! (br:readLine(.), br:close(.))
}

Module Imports[edit]

A Java class can be instantiated by importing them as a module: A new instance of the addressed class will be constructed, which can then be referenced in the query body.

In the (side-effecting) example below, a HashSet instance is created, values are added, and the size of the set is returned. As set:add() returns boolean values, prof:void is used to swallow the values:

import module namespace set = "java:java.util.HashSet";
prof:void(
  for $s in ("one", "two", "one")
  return set:add($s)
),
set:size()

The execution of imported classes is more efficient than the execution of instances that have been created via new(). In turn, no arguments can be supplied in the import statement, and the construction will only be successful if the class can be instantiated without arguments.

Integration[edit]

Java classes can be coupled more closely to BaseX. If a class inherits the abstract QueryModule class, the two variables queryContext and staticContext get available, which provide access to the global and static context of a query.

The QueryResource interface can be implemented to enforce finalizing operations, such as the closing of opened connections or resources in a module. Its close() method will be called after the XQuery expression has been fully evaluated.

Annotations[edit]

The internal properties of functions can be assigned via annotations:

  • Java functions can only be executed by users with Admin permissions. You can annotate a function with @Requires(<Permission>) to also make it accessible to users with fewer privileges.
  • Java code is treated as nondeterministic, as its behavior cannot be predicted by the XQuery processor. You may annotate a function as @Deterministic if you know that it will have no side effects and will always yield the same result.
  • Java code is treated as context-independent. If a function accesses the query context, it should be annotated as @ContextDependent
  • Java code is treated as focus-independent. If a function accesses the current context item, position or size, it should be annotated as @FocusDependent

In the following code, information from the static query context is returned by the first function, and a query exception is raised by the second function:

import module namespace context = 'org.basex.examples.query.ContextModule';

element user {
  context:user()
},
try {
  element to-int { context:to-int('abc') }
} catch basex:error {
  element error { $err:description }
}

The imported Java class is shown below:

package org.basex.examples.query;

import org.basex.query.*;
import org.basex.query.value.item.*;
import org.basex.util.*;

/**
 * This example inherits the {@link QueryModule} class and
 * implements the QueryResource interface.
 */
public class ContextModule extends QueryModule implements QueryResource {
  /**
   * Returns the name of the logged-in user.
   * @return user string
   */
  @Requires(Permission.NONE)
  @Deterministic
  @ContextDependent
  public String user() {
    return queryContext.context.user.name;
  }

  /**
   * Converts the specified string to an integer.
   * @param value string to be converted
   * @return resulting integer
   * @throws QueryException query exception
   */
  @Requires(Permission.NONE)
  @Deterministic
  public int toInt(final String value) throws QueryException {
    try {
      return Integer.parseInt(value);
    } catch(NumberFormatException ex) {
      throw new QueryException("Integer conversion failed: " + value);
    }
  }

  @Override
  public void close() {
    // defined in QueryResource interface, will be called after query evaluation
  }
}

The result will look as follows:

<user>admin</admin>
<error>Integer conversion failed: abc</error>

Please visit the XQuery 3.0 specification if you want to get more insight into function properties.

Updates[edit]

The @Updating annotation can be applied to mark Java functions that perform write or update operations:

  @Updating
  public void backup() {
    // ...
  }

An XQuery expression will be handled as an updating expression if it calls an updating Java function. In contrast to XQuery update operations, the Java code will immediately be executed, but the result will be cached as if update:output was called.

The annotation is particularly helpful if combined with a lock annotation.

Locking[edit]

By default, a Java function will be executed in parallel with other code. If a Java function performs sensitive operations, it is advisable to explicitly lock the code.

Java Locks[edit]

Java provides a handful of mechanism to control the execution of code. The concurrent execution of functions can be avoided with the synchronized keyword. For more complex scenarios, the Lock, Semaphore and Atomic classes can be brought into play.

XQuery Locks[edit]

If you want to synchronize the execution of your code with BaseX locks, you can take advantage of the @Lock annotation:

  @Lock("HEAVYIO")
  public void read() {
    // ...
  }

  @Updating
  @Lock("HEAVYIO")
  public void write() {
    // ...
  }

If an XQuery expression invokes write(), any other query that calls write() or read() needs to wait for the query to be finished. The read() function can be run in parallel; whereas queries will be queued if write() is called.

More details on concurrent querying can be found in the article on Transaction Management.

Data Types[edit]

Conversion to Java[edit]

Before Java code is executed, the arguments are converted to Java values, depending on the addressed function or constructor parameters. The accepted Java types and the original XQuery types are depicted in the second and first column of the table below.

If a numeric value is supplied for which no exact matching is defined, it is cast to the appropriate type unless it exceeds its limits. The following two function calls are equivalent:

(: exact match :)
Q{String}codePointAt('ABC', xs:int(1)),
(: xs:byte and xs:integer casts :)
Q{String}codePointAt('ABC', xs:byte(1)),
Q{String}codePointAt('ABC', 1)

Conversion to XQuery[edit]

By default, Java values with the most common types (as shown in the second and third column of the table) are converted to XQuery values. All other values are returned as Java items, which are function items with a wrapped Java value. The results of constructor calls are always returned as Java items.

The conversion of the wrapped Java value to XQuery is enforced by invoking the function item: Values in Iterator and Iterable instances (Lists, Sets and Collections) are converted to items, and maps are converted to XQuery maps:

declare namespace Scanner = 'java:java.util.Scanner';
let $scanner := Scanner:new("A B C") => Scanner:useDelimiter(" ")
return $scanner()

If no conversion is defined, a string is returned, resulting from the toString() method of the object. This method is also called if the string representation of a Java item is requested:

(: returns the string representations of a HashMap and an ArrayList instance :)
'Map: ' || Q{java.util.HashMap}new(),
string(Q{java:java.util.ArrayList}new())

The conversion can be further controlled with the WRAPJAVA option. The following values exist:

Value Description
some The default: Java values of the most common types are converted, others are wrapped into Java items.
none All Java values are converted. If no conversion is defined, a string is returned, resulting from the toString() method.
all Java values are wrapped into Java items (excluding those inheriting the internal type org.basex.query.value.Value).
instance If the method of a class instance was called, the Java value is ignored and the instance is wrapped into a Java item. Otherwise, the Java value is returned.
void Java values are ignored, and an empty sequence is returned instead.

In the following example, the result of the first function – a char array – is wrapped and passed on to a CharBuffer function. Without the option, the single-value array would be converted to an xs:unsignedShort item and the second function call would fail:

(: Without the pragma, the result of toChars would be converted to an xs:unsignedShort item, and the second function call would fail :)

(# db:wrapjava all #) {
  Q{Character}toChars(xs:int(33))
  => Q{java.nio.CharBuffer}wrap()
}

The next example demonstrates a use case for the instance option:

(: Thanks to the pragma, the function calls can be chained :)

declare namespace set = 'java:java.util.HashSet';
let $set := (# db:wrapjava instance #) {
  set:new()
  => set:add('1')
  => set:add('2')
}
return $set()

The void option is helpful if side-effecting methods return values that do not contribute to the final result:

(: Without the pragma, 100 booleans would be returned by the FLWOR expression :)

declare namespace set = 'java:java.util.HashSet';
let $set := set:new()
return (
  (# db:wrapjava void #) {
    for $i in 1 to 100
    return set:add($set, $i)
  },
  $set()
)

The irrelevant results could also be swallowed with prof:void.

XQuery input Expected or returned Java type XQuery output
item()* (no conversion) org.basex.query.value.Value item()* (no conversion)
empty-sequence() null empty-sequence()
xs:string, xs:untypedAtomic String xs:string
xs:unsignedShort char, Character xs:unsignedShort
xs:boolean boolean, Boolean xs:boolean
xs:byte byte, Byte xs:byte
xs:short short, Short xs:short
xs:int int, Integer xs:int
xs:integer, xs:long long, Long xs:integer
xs:unsignedLong java.math.BigInteger xs:unsignedLong (lossy)
xs:decimal java.math.BigDecimal xs:decimal
xs:float float, Float xs:float
xs:double double, Double xs:double
xs:QName javax.xml.namespace.QName xs:QName
xs:anyURI java.net.URI, java.net.URL xs:anyURI
xs:date javax.xml.datatype.XMLGregorianCalendar xs:date
xs:duration javax.xml.datatype.Duration xs:duration
node() org.w3c.dom.Node node()
array(xs:boolean) boolean[] xs:boolean*
array(xs:string) String[] xs:string*
array(xs:unsignedShort) char[] xs:unsignedShort*
array(xs:short) short[] xs:short*
array(xs:int) int[] xs:int*
array(xs:integer), array(xs:long) long[] xs:integer*
array(xs:float) float[] xs:float*
array(xs:double) double[] xs:double*
Object[] (others) item()* array(*) (others)
map(*) java.util.HashMap Wrapped Java object

URI Rewriting[edit]

Before a Java class or module is accessed, its namespace URI will be normalized:

  1. If the URI is a URL:
    1. colons will be replaced with slashes,
    2. in the URI authority, the order of all substrings separated by dots is reversed, and
    3. dots in the authority and the path are replaced by slashes. If no path exists, a single slash is appended.
  2. Otherwise, if the URI is a URN, colons will be replaced with slashes.
  3. Characters other than letters, dots and slashes will be replaced with dashes.
  4. If the resulting string ends with a slash, the index string is appended.

If the resulting path has no file suffix, it may point to either an XQuery module or a Java archive:

  • http://basex.org/modules/hello/Worldorg/basex/modules/hello/World
  • http://www.example.comcom/example/www/index
  • a/little/examplea/little/example
  • a:b:ca/b/c

Note that the mapping is not unique: Different URIs may result in the same path.

Changelog[edit]

Version 9.6
  • Updated: Java Bindings revised (new mappings, Java functiom items, WRAPJAVA option).
Version 9.4
  • Added: Annotation for updating functions.
  • Updated: Single annotation for read and write locks.
Version 8.4
  • Updated: Rewriting rules
Version 8.2
Version 8.0
  • Added: QueryResource interface, called after a query has been fully evaluated.
Version 7.8
  • Added: Java locking annotations
  • Updated: context variable has been split into queryContext and staticContext.
Version 7.2.1