Changes

Jump to navigation Jump to search
360 bytes added ,  15:30, 19 July 2021
no edit summary
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.
 
{{Mark|Updated with Version 9.6:}}
* Java objects are now wrapped into function items.
* A new option {{Option|WRAPJAVA}} was added to control how Java values are converted to XQuery.
* 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.
* 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=
# 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==
* 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>[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. Since {{Version|9.6}}, three Three adjacent dots can be used to address an array argument.
{| class="wikitable"
|- valign="top"
| Variable
| <code>Q{java.lang.Integer}MIN_VALUE()</code>
| <code>Integer.MIN_VALUE</code>
|- valign="top"
| Function
| <code>Q{java.lang.Object}hash-code($object)</code>
| <code>object.hashCode()</code>
|- valign="top"
| Function with argument
| <code>Q{java.lang.String}split·java.lang.String·intsplit·String·int($string, ';', xs:int(3))</code>
| <code>string.split(";", 3)</code>
|- valign="top"
| Function with array argument
| <code>Q{java.lang.String}new·byte...(xs:hexBinary('414243'))</code>
| <code>new String(new byte[] { 41, 42, 43 })</code>
|}
=Namespace Declarations=
In the following example, Java’s 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">
</syntaxhighlight>
With the [[XQuery 3.0#Expanded QNames|Expanded QName]] notation of XQuery 3.0,the namespace can directly be embedded in the function call:
<syntaxhighlight lang="xquery">
<syntaxhighlight lang="xquery">
declare namespace fw = "'java:java.io.FileWriter"';
let $file := fw:new('output.txt')
return (
<syntaxhighlight lang="xquery">
declare namespace br = 'java:java.io.BufferedReader';declare namespace fr = 'java:java.io.FileReader';
declare option db:checkstrings 'false';
==Data Types==
 
{{Mark|Updated with Version 9.6:}}
* Java objects are now wrapped into function items.
* New option {{Option|WRAPJAVA}}.
* Mapping rules were refined. 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.
===Conversion to Java===
(# db:wrapjava all #) {
Q{java.lang.Character}toChars(xs:int(33))
=> Q{java.nio.CharBuffer}wrap()
}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu