Changes

Jump to navigation Jump to search
2,692 bytes added ,  08:43, 8 April 2016
no edit summary
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]].
{{Mark|Updated Please bear in mind that the execution of Java code may cause side effects that conflict with Version 8.4}}: A Java class is identified by a namespace URIthe functional nature of XQuery, or may introduce new security risks to your project. The original URI is rewritten as follows:
* First, the [[Repository#URI_Rewriting|URI Rewriting]] steps are applied to the URI.* Next, slashes in the resulting URI are replaced with dots.* Next, the URI is rewritten to the camel notation: Dashes are removed, and the following characters are transformed to upper case.=Identification=
==Classes== A Java class is identified by a namespace URI. The original URI is rewritten as follows: # The [[Repository#URI_Rewriting|URI Rewriting]] steps are applied to the URI.# Slashes in the resulting URI are replaced with dots.# The last path segment of the URI is capitalized and rewritten to [https://en.wikipedia.org/wiki/CamelCase camel case]. The normalization steps can be are skipped by prefixing with if the URI is prefixed with {{Code|java:}}. See the following examples:
* <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>
Function names are ==Functions and Variables== Java 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 Java’s camel case notation, identifies a variable or function of that class.* The middle dot character (<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"! Type! XQuery! Java|- 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"| Function| <code>Q{java.lang.Object}hash-code()</code>| <code>[https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode() object.hashCode()]</code>|- valign="top"| Function with types| <code>Q{java.lang.String}split·java.lang.String·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>|} 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]]).
* <code>get-contents($x as 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:string)</code> → <code>getContents(String x)</code><br/>int}}.
=Namespace Declarations=
Java classes can be declared via namespaces. The namespace can then be used to call static functions contained in that class. Variables are represented as function with 0 parameters. The In the following example uses , Java’s {{Code|Math}} class to return 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()}}:
<pre class="brush:xquery">
</pre>
The new With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,allows you to the namespace can directly specify a namespace URI instead of be embedded in the prefixfunction call:
<pre class="brush:xquery">
</pre>
The 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. 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:
<pre class="brush:xquery">declare namespace fw = "java.io.FileWriter";
</pre>
Note that Java code cannot be pre-compiled, and will often as such be evaluated slower than optimizedXQuery code.
=Module Imports=
Java code can also be integrated by ''importing'' classes as modules.A new instance of the addressed class is created, which can then be accessed in the query body.
An The following, side-effecting example returns the number of distinct values added to a hash set (the boolean values returned by {{Code|set:add()}} are ignoredwill be swallowed):
<pre class="brush:xquery">
import module namespace set = "java.util.HashSet";
let $loop prof:= void( set:addfor $s in ("checkone", "two"), set:add("whatone"), return set:add("happens"$s)),return set:size()
</pre>
Advantages The advantages of this approach are:* is that imported code can be is 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). A drawback is that no arguments can be passed on to the class constructor.As a consequence, the addressed import will fail if the class must provide a has at least one constructor with arguments and no argumentsdefault constructor.
=Context-Awareness=
If an XQuery expression is run which calls the Java {{Code|write()}} function, every other query that calls {{Code|write()}} or {{Code|read()}} needs to wait for the query to be finished. If a query calls the {{Code|read()}} function, only those queries are queued that call {{Code|write()}}, because this function is only annotated with a {{Code|read}} lock. More details on parallel query execution can be found in the article on [[Transaction Management]].
 
=Data Types=
 
The following table lists the mappings of XQuery and Java types:
 
{| class="wikitable"
|- valign="top"
! XQuery Type
! Java Type
|- valign="top"
| <code>xs:string</code>
| <code>String</code>, <code>char</code>, <code>Character</code>
|- valign="top"
| <code>xs:boolean</code>
| <code>boolean</code>, <code>Boolean</code>
|- valign="top"
| <code>xs:byte</code>
| <code>byte</code>, <code>Byte</code>
|- valign="top"
| <code>xs:short</code>
| <code>short</code>, <code>Short</code>
|- valign="top"
| <code>xs:int</code>
| <code>int</code>, <code>Integer</code>
|- valign="top"
| <code>xs:long</code>
| <code>long</code>, <code>Long</code>
|- valign="top"
| <code>xs:float</code>
| <code>float</code>, <code>Float</code>
|- valign="top"
| <code>xs:double</code>
| <code>double</code>, <code>Double</code>
|- valign="top"
| <code>xs:decimal</code>
| <code>java.math.BigDecimal</code>
|- valign="top"
| <code>xs:integer</code>
| <code>java.math.BigInteger</code>
|- valign="top"
| <code>xs:QName</code>
| <code>javax.xml.namespace.QName</code>
|- valign="top"
| <code>xs:anyURI</code>
| <code>java.net.URI</code>, <code>java.net.URL</code>
|- valign="top"
| ''empty sequence''
| <code>null</code>
|}
=Changelog=
; Version 8.4
* UpdatesUpdated: Rewriting rules
; Version 8.0
* Added: import of Java modules, context awareness
 
[[Category:XQuery]]
[[Category:API]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu