Changes

Jump to navigation Jump to search
2,116 bytes added ,  16:58, 20 October 2017
no edit summary
Java archives (JARs) may contain one or more class files. One of them will be chosen as main class, which must be specified in a {{Code|Main-Class}} entry in the manifest file ({{Code|META-INF/MANIFEST.MF}}). This fully qualified Java class name will be rewritten to a file path by replacing the dots with slashes and attaching the {{Code|.jar}} file suffix, and the original file will be renamed and copied to that path into the repository.
If the class will be importedin the prolog of the XQuery module, an instance of it will be created, and its public functions can then be addressed from XQuery. A class may extend the {{Code|QueryModule}} class to get access to the current query context and to be enriched by some helpful annotations (see [[Java_Bindings#Context-Awareness|Context-Awareness]]).
'''Example:'''
org/basex/modules/Hello
org.basex.modules.Hello
 
===Combined===
 
{{Mark|Introduced with Version 9.0}}:
 
It makes sense to combine the advantages of XQuery and Java packages by providing wrapper functions for the Java class. XQuery functions can be properly typed; this reduces the danger of erroneous or unexpected conversions between XQuery and Java code.
 
XQuery and Java can be combined as follows:
 
* A JAR file must be created, as described above.
* The XQuery wrapper module must be named identically to the Java main class.
* The URL of the {{Code|import module}} statement in the wrapper module must start with the {{Code|java:}} prefix.
* The finalized XQuery module must be copied into the JAR file, and placed in the same directory as the Java main class.
 
If the resulting JAR file is installed, the embedded XQuery module will be extracted, and will be called first if the module will be imported.
 
; Main Module {{Code|HelloUniverse.xq}}:
 
<pre class="brush:xquery">
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</pre>
 
; Wrapper Module {{Code|Hello.xqm}}:
 
<pre class="brush:xquery">
module namespace hello = 'http://basex.org/modules/Hello';
 
(: Import JAR file :)
import module namespace java = 'java:org.basex.modules.Hello';
 
(:~
: Say hello to someone.
: @param $world the one to be greeted
: @return welcome string
:)
declare function hello:hello(
$world as xs:string
) as xs:string {
java:hello($world)
};
<pre>
 
; Java class {{Code|Hello.java}}:
 
<pre class="brush:java">
package org.basex.modules;
 
public class Hello {
public String hello(final String world) {
return "Hello " + world;
}
}
</pre>
 
If the JAR file will be installed, {{Code|Combined}} will be displayed as type:
 
REPO INSTALL http://files.basex.org/modules/org/basex/modules/Hello.jar
REPO LIST
 
Name Version Type Path
---------------------------------------------------------------------------------------
org.basex.modules.Hello - Combined org/basex/modules/Hello.xqm
=EXPath Packaging=
==Java==
If you want to extend package an EXPath archive with a JAR fileJava code, some additional requirements have to be fulfilled:
* Apart from the package descriptor <code>expath-pkg.xml</code>, the package has to contain a descriptor file at its root, defining the included jars and the binary names of their public classes. It must be named <code>basex.xml</code> and must conform to the following structure:
=Changelog=
 
;Version 9.0
 
* Added: [[#Combined|Combined]] XQuery and Java packages
;Version 8.2
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu