Changes

Jump to navigation Jump to search
No change in size ,  15:13, 26 May 2012
m
Text replace - "{{Mono|" to "{{Code|"
to directly access Java variables and execute code from XQuery. Java classes are identified by
namespaces. The namespace URI must simply contain the fully qualified class name.
The URI can optionally be prefixed with the string {{MonoCode|java:}} to enforce that
the addressed code is written in Java.
call static functions contained in that class.
The following example uses Java’s {{MonoCode|Math}} class to return the cosine of an angleby calling the static method {{MonoCode|cos()}}:
<pre class="brush:xquery">
The constructor of a class can be invoked by calling the virtual
function {{MonoCode|new()}}. Instance methods can then called by
passing on the resulting Java object as first argument.
The following example writes 256 bytes to the file {{MonoCode|output.txt}}.First, a new {{MonoCode|FileWriter}} instance is created, and its{{MonoCode|write()}} function is called in the next step:
<pre class="brush:xquery">declare namespace fw = "java.io.FileWriter";
A new instance of the addressed class is created, which can then be accessed in the query body.
An example (the boolean values returned by {{MonoCode|set:add()}} are ignored):
<pre class="brush:xquery">
Advantages of this approach are:
* imported code can be executed faster than instances created at runtime via {{MonoCode|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).
Java classes can be coupled more closely to the BaseX core library.
If an instantiated class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/query/QueryModule.java QueryModule] class of BaseX, it will get access to the {{MonoCode|context}} variable, which is an instance of the [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/query/QueryContext.java QueryContext] class. It provides access to all static and dynamic properties of the current query. Additionally, the default properties of functions can be changed via annotations:
* Java functions can only be executed by users with [[User_Management|Admin permissions]]. You may annotate a function with {{MonoCode|@Requires(<Permission>)}} to also make it accessible to users with less privileges.* Java code is treated as ''non-deterministic'', as its behavior cannot be predicted by the XQuery processor. You may annotate a function as {{MonoCode|@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 {{MonoCode|@ContextDependent}}* Java code is treated as ''focus-independent''. If a function accesses the current context item, position or size, it should be annotated as {{MonoCode|@FocusDependent}}
The following XQuery code invokes two Java methods. The first Java function retrieves information from the static query context, and the second one throws a query exception:
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu