Changes

Jump to navigation Jump to search
189 bytes added ,  18:42, 17 October 2017
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.
=Identification=
 
{{Mark|Updated with Version 8.4}}: address Java functions with specific types
==Classes==
* 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 (<code>·</code> ([http://www.fileformat.info/info/unicode/char/b7/index.htm ·/&amp;#xB7;]</code>) is , a valid character in XQuery names, but not in Java. It ) can be used to append exact Java parameter types to the function name. Class types must be referenced by their full path.
{| class="wikitable"
|- valign="top"
| Variable
| <code>Q{java.lang.Integer}MIN_VALUE()</code>
| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#MAX_VALUE Integer.MIN_VALUE]</code>
|- valign="top"
=Module Imports=
Java code can also be integrated accessed by ''importing'' classes as modules. A new instance of the addressed class is will be created, which can then be accessed referenced in the query body.
The following, (side-effecting ) example returns the number of distinct values added to a hash set (the . The boolean values returned by {{Code|set:add()}} will be swallowed):
<pre class="brush:xquery">
import module namespace set = "java:java.util.HashSet";
prof:void(
for $s in ("one", "two", "one")
</pre>
The advantages of this approach is that the execution of imported code classes is executed faster 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 only works fails if the addressed class provides a has no default constructor, but at least one constructor with no arguments.
=Context-Awareness=
Java classes can be coupled even 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 properties of functions can be changed via annotations:
; Version 8.4
* UpdatesUpdated: Rewriting rules
; Version 8.0
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu