Changes

Jump to navigation Jump to search
324 bytes added ,  19:40, 21 December 2015
no edit summary
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 the addressed {{Mark|Updated with Version 8.4}}: A Java code class is identified by a namespace URI. The original URI is not found in the classpathrewritten as follows: * First, it first needs to beinstalled in the [[Repository#URI_Rewriting|URI_Rewriting]]steps will be applied to the URI.* Next, slashes in the resulting URI will be replaced with dots.* Next, the URI will be rewritten to the camel notation. The normalization steps can be skipped by prefixing with the URI with {{Code|java:}}: * <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 rewritten to Java’s camel case notation: * <code>get-contents($x as xs:string)</code> → <code>getContents(String x)</code>
=Namespace Declarations=
Java classes can be declared via namespaces. The namespace can then be used tocall static functions contained in that class. Variables are represented asfunction with 0 parameters.
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 class="brush:xquery">
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 class="brush:xquery">declare namespace fw = "java.io.FileWriter";
</pre>
Function 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 : getContents(String x)</pre> Strings with call contains invalid XML characters , it 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 class="brush:xquery">
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu