Changes

Jump to navigation Jump to search
12,853 bytes added ,  16:04, 19 July 2021
This article is part of the [[XQuery|XQuery Portal]].It demonstrates two different ways to invoke Java code from XQuery, and an extension it presents extensions to make Java code aware of access the current query contextfrom Java.
The Java Binding feature is an extensibility mechanism which enables developers
to directly access Java variables and execute code from XQuery. Addressed Java classes are identified bynamespaces. The namespace URI code must simply contain either be contained in the fully qualified class name.The URI can optionally Java classpath, or it must be prefixed with located in the string {{Code|java:}} to enforce thatthe addressed code is written in Java[[Repository]].
If Please bear in mind that the addressed 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. {{Mark|Updated with Version 9.6:}}* 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 now be omitted.* Java objects are now wrapped into function items.* Results of constructor calls are always returned as function item.* A new option {{Option|WRAPJAVA}} was added to control how Java values are converted to XQuery.* The Mapping rules were refined and unified. The most important changes:** {{Code|array(*)}} type added.** {{Code|xs:integer}} values are converted to {{Code|long}} values.** {{Code|xs:unsignedShort}} values are converted to {{Code|char}} values.* All error messages were revised and improved. =Identification= ==Classes== A Java class is not found identified by a namespace URI. The original URI is rewritten as follows: # The [[#URI Rewriting|URI Rewriting]] steps are applied to the URI.# Slashes in the classpathresulting URI are replaced with dots.# 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 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>java:java.lang.String</code> → <code>java.lang.String</code>* <code>StringBuilder</code> → <code>java.lang.StringBuilder</code> ==Functions and Variables== 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, it first needs 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> (<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 bereferenced by their full path. Three adjacent dots can be used to address an array argument. {| class="wikitable"|- valign="top"! Addressed code! XQuery! Java|- valign="top"| Variable| <code>Q{Integer}MIN_VALUE()</code>| <code>Integer.MIN_VALUE</code>|- valign="top"| Function| <code>Q{Object}hash-code($object)</code>| <code>object.hashCode()</code>|- valign="top"| Function with argumentinstalled in | <code>Q{String}split·String·int($string, ';', xs:int(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 must be converted to equivalent Java values, and the result of a Java function is converted back to an XQuery value (see [[Repository#Data Types|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 {{Code|int}} value, you will need to convert your XQuery integers to {{Code|xs:int}}.
=Namespace Declarations=
In the following example, the Java classes can be declared via namespaces{{Code|Math}} class is referenced. The namespace can then be used tocall 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 functions contained in that class. Variables are represented asfunction with 0 parameters.variable via {{Code|PI()}}:
The following example uses Java’s {{Code|Math}} class to return the cosine of an angleby calling the static method {{Code|cos()}}, and the value of π by addressing the staticvariable via {{Code|PI()}}: <pre classsyntaxhighlight lang="brush:xquery">
declare namespace math = "java:java.lang.Math";
math:cos(xs:double(0)), math:PI()
</presyntaxhighlight>
The new With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0allows you to , the namespace can directly specify a namespace URI instead of be embedded in the prefixfunction call:
<pre classsyntaxhighlight lang="brush:xquery">
Q{java:java.lang.Math}cos(xs:double(0))
</presyntaxhighlightThe constructor of a class can be invoked by calling the virtualfunction {{Code|new()}}. Instance methods can then called bypassing on the resulting Java object as first argument.
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 {{Code|java:}} prefix is omitted inthe URI:
<pre classsyntaxhighlight lang="brush:xquery">declare namespace fw = "'java:java.io.FileWriter"';
let $file := fw:new('output.txt')
return (
fw:close($file)
)
</presyntaxhighlightFunction names with dashes will be rewritten to Java’s camel case notation:
<pre class="brush:xquery">XQuery: get-contents($x as xs:string) 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: getContents(String x)</pre>
Strings with invalid XML characters will be rejected by default. The validity check can be disabled by setting the [[Options#CHECKSTRINGS|CHECKSTRINGS]] option to false. The following query writes a file with a single 00-byte, which will then be successfully read via Java functions: <pre classsyntaxhighlight lang="brush:xquery">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))</presyntaxhighlightThe option can also be specified via a pragma:
Note that Java code cannot be pre-compiled<syntaxhighlight lang="xquery">(# db:checkstrings #) { br:new(fr:new('00.bin')) ! (br:readLine(.), and will often be evaluated slower than optimizedbr:close(.))XQuery code.}</syntaxhighlight>
=Module Imports=
A Java code classes can also be integrated instantiated by ''importing'' classes them as modules.a module: A new instance of the addressed class is createdwill be constructed, which can then be accessed referenced in the query body.
An In the (side-effecting) example (below, a HashSet instance is created, values are added, and the size of the boolean values set is returned by . As {{Code|set:add()}} are ignored)returns boolean values, {{Function|Profiling|prof:void}} is used to swallow the values:
<pre classsyntaxhighlight lang="brush:xquery">import module namespace set = "java:java.util.HashSet";let $loop prof:= void( set:addfor $s in ("checkone", "two"), set:add("whatone"), return set:add("happens"$s)),return set:size()</presyntaxhighlightThe 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.
Advantages of this approach are:* imported code can be executed faster than instances created at runtime via {{Code|new()}}.* the work on class instances ensures that queries run in parallel will not cause any concurrency issues (provided that the class contains no static variables or functions).=Integration=
A drawback is that no arguments Java classes can be passed on 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 constructor, the two variables [https://github.As a consequencecom/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 addressed class must provide global and static context of a constructor with no argumentsquery.
=ContextThe [https://github.com/BaseXdb/basex/blob/master/basex-Awareness=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.
{{Mark|Updated with Version 7.8}}: {{Code|context}} variable has been split into {{Code|queryContext}} and {{Code|staticContext}}.==Annotations==
Java classes can be coupled more closely to the BaseX core library.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. Additionally, the default The internal properties of functions can be changed assigned via annotations:
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You may can annotate a function with {{Code|@Requires(<Permission>)}} to also make it accessible to users with less 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 ''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}}
The In the following XQuery code invokes two Java methods. The first Java function retrieves , information from the static query contextis returned by the first function, and the second one throws a query exceptionis raised by the second function:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace context = 'org.basex.examples.query.ContextModule';
context:user()
},
try { element to-int { try { context:to-int('abc') }} catch basex:error { catch * element error { 'Error in line', $err:line-number description }
}
</presyntaxhighlight>
The imported Java class is shown below:
<pre classsyntaxhighlight lang="brush:java">
package org.basex.examples.query;
/**
* This example is inherited from inherits the {@link QueryModule} classand * implements the QueryResource interface.
*/
public class ContextModule extends QueryModule implements QueryResource {
/**
* Returns the name of the logged -in user. * @return userstring
*/
@Requires(Permission.NONE)
/**
* Converts the specified string to an integer.
* @param value string representationto be converted * @return resulting integer
* @throws QueryException query exception
*/
return Integer.parseInt(value);
} catch(NumberFormatException ex) {
throw new QueryException(ex.getMessage()"Integer conversion failed: " + value);
}
}
 
@Override
public void close() {
// defined in QueryResource interface, will be called after query evaluation
}
}
</presyntaxhighlight>
The result will look as follows:
<pre classsyntaxhighlight lang="brush:xml">
<user>admin</admin>
<to-interror>Error in line 6Integer conversion failed: abc</to-interror></presyntaxhighlight>
Please visit the XQuery 3.0 specification if you want to get more insight into
[httphttps://www.w3.org/TR/xpath-functions-3031/#properties-of-functions function properties]. ==Updates== The {{Code|@Updating}} annotation can be applied to mark Java functions that perform write or update operations: <syntaxhighlight lang="java"> @Updating public void backup() { // ... }</syntaxhighlight> 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. The annotation is particularly helpful if combined with a lock annotation. ==Locking== 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===
=Locking=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.
{{Mark|Introduced with Version 7.8:}}===XQuery Locks===
By default, a Java function will be executed in parallel with other code. However, if a Java function performs sensitive write operations, it is advisable If you want to explicitly lock synchronize the execution of your code. This with BaseX locks, you can be realized via locking annotationstake advantage of the {{Code|@Lock}} annotation:
<pre classsyntaxhighlight lang="brush:java"> @Lock(write = { "HEAVYIO" }) public void writeread() {
// ...
}
@Updating @Lock(read = { "HEAVYIO" }) public void readwrite() {
// ...
}
</presyntaxhighlight>
If an XQuery expression is run which calls the Java invokes {{Code|write()}} function, every any other query that calls {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. If a query calls the The {{Code|read()}} function, only those can be run in parallel; whereas queries are will be queued that call if {{Code|write()}}is called. More details on concurrent querying can be found in the article on [[Transaction Management]]. ==Data Types== ===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. ===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'', because this 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: <syntaxhighlight lang="xquery">declare namespace Scanner = 'java:java.util.Scanner';let $scanner := Scanner:new("A B C") => Scanner:useDelimiter(" ")return $scanner()</syntaxhighlight> If no conversion is defined, a string is returned, resulting from the {{Code|toString()}} method of the object. This method is also called is the string representation of a Java item is only annotated requested: <syntaxhighlight 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())</syntaxhighlight> 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|readvoid}} lock| Java values are ignored, and an empty sequence is returned instead. More details |} In the following example, the result of the first function – a char array – is wrapped and passed on parallel query execution 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: <syntaxhighlight 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()}</syntaxhighlight> The next example demonstrates a use case for the {{Code|instance}} option: <syntaxhighlight lang="xquery">(: Thanks to the pragma, the function calls can be found chained :) declare namespace set = 'java:java.util.HashSet';let $set := (# db:wrapjava instance #) { set:new() => set:add('1') => set:add('2')}return $set()</syntaxhighlight> The {{Code|void}} option is helpful if side-effecting methods return values that do not contribute to the final result: <syntaxhighlight 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 the article on 1 to 100 return set:add($set, $i) }, $set())</syntaxhighlight> The irrelevant results could also be swallowed with {{Function|Profiling|prof:void}}. {| class="wikitable"|- valign="top"! XQuery input! Expected or returned Java type! XQuery output|- 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>|- valign="top"| <code>xs:unsignedShort</code>| <code>char</code>, <code>Character</code>| <code>xs:unsignedShort</code>|- valign="top"| <code>xs:boolean</code>| <code>boolean</code>, <code>Boolean</code>| <code>xs:boolean</code>|- valign="top"| <code>xs:byte</code>| <code>byte</code>, <code>Byte</code>| <code>xs:byte</code>|- valign="top"| <code>xs:short</code>| <code>short</code>, <code>Short</code>| <code>xs:short</code>|- valign="top"| <code>xs:int</code>| <code>int</code>, <code>Integer</code>| <code>xs:int</code>|- valign="top"| <code>xs:integer</code>, <code>xs: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"| <code>xs:float</code>| <code>float</code>, <code>Float</code>| <code>xs:float</code>|- valign="top"| <code>xs:double</code>| <code>double</code>, <code>Double</code>| <code>xs:double</code>|- valign="top"| <code>xs:QName</code>| <code>javax.xml.namespace.QName</code>| <code>xs:QName</code>|- valign="top"| <code>xs:anyURI</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"| <code>node()</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[Transaction Management]</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>|} ==URI Rewriting== Before a Java class or module is accessed, its namespace URI will be normalized: # If the URI is a URL:## colons will be replaced with slashes,## in the URI authority, the order of all substrings separated by dots is reversed, and## dots in the authority and the path are replaced by slashes. If no path exists, a single slash is appended.# Otherwise, if the URI is a URN, colons will be replaced with slashes.# Characters other than letters, dots and slashes will be replaced with dashes.# If the resulting string ends with a slash, the {{Code|index}} string is appended. If the resulting path has no file suffix, it may point to either an XQuery module or a Java archive: * {{Code|<nowiki>http://basex.org/modules/hello/World</nowiki>}} → {{Code|org/basex/modules/hello/World}}* {{Code|<nowiki>http://www.example.com</nowiki>}} → {{Code|com/example/www/index}}* {{Code|a/little/example}} → {{Code|a/little/example}}* {{Code|a:b:c}} → {{Code|a/b/c}}
=Changelog=
 
; Version 9.6
* Updated: {{Anchor|Data Types}}: {{Code|array(*)}} type added; {{Code|xs:integer}} are converted to {{Code|long}} values.
 
; Version 9.4
* Added: Annotation for [[#Updates|updating functions]].
* Updated: Single annotation for read and write locks.
 
; Version 8.4
* Updated: Rewriting rules
 
;Version 8.2
* Added: [[#URI Rewriting|URI Rewriting]]: support for URNs
 
; Version 8.0
* Added: {{Code|QueryResource}} interface, called after a query has been fully evaluated.
; Version 7.8
 
* Added: Java locking annotations
* Updated: {{Code|context}} variable has been split into {{Code|queryContext}} and {{Code|staticContext}}.
; Version 7.2.1
 
* Added: import of Java modules, context awareness
 * Added: [[Category:XQuery#Packaging|Packaging]], [[Category:API#URI Rewriting|URI Rewriting]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu