Changes

Jump to navigation Jump to search
218 bytes added ,  14:14, 27 February 2020
no edit summary
==Accessing Modules==
Library modules can be imported with the {{Code|import module}} statement,followed by a freely choosable prefix and the namespace of the target module.The specified location may be absolute or relative; in the latter case, it is resolvedagainst the location (i.e., ''static base URI'') of the calling module.Import module statements must be placed at the beginning of a module:
'''Main Module''' <code>hello-universe.xq</code>:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace m = 'http://basex.org/modules/hello' at 'hello-world.xqm';
m:hello("Universe")
</presyntaxhighlight>
'''Library Module''' <code>hello-world.xqm</code> (in the same directory):
<pre classsyntaxhighlight lang="brush:xquery">
module namespace m = 'http://basex.org/modules/Hello';
declare function m:hello($world) {
'Hello ' || $world
};
</presyntaxhighlight>
If no location is supplied, modules will be looked up in the repository. Repository modules are stored in the {{Code|repo}} directory, which resides in your [[Configuration#Home Directory|home directory]]. XQuery modules can be manually copied to the repository directory or installed and deleted via [[#Commands|commands]].
If a module has been placed in the repository (see below), there is no need to specify the location. The following example calls a function from the FunctX modulein the repository:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace functx = 'http://www.functx.com';
functx:capitalize-first('test')
</presyntaxhighlight>
=Commands=
Importing the repository module:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</presyntaxhighlight>
==Java==
Contents of the file {{Code|Hello.java}} (comments removed):
<pre classsyntaxhighlight lang="brush:java">
package org.basex.modules;
public class Hello {
}
}
</presyntaxhighlight>
Installation (the file will be copied to {{Code|org/basex/modules/Hello.jar}}):
XQuery file <code>[http://files.basex.org/modules/org/basex/modules/Hello/HelloUniverse.xq HelloUniverse.xq]</code> (same as above):
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</presyntaxhighlight>
After having installed the module, all of the following URIs can be used in XQuery to import this module or call its functions (see [[Java Bindings#URI Rewriting|URI Rewriting]] for more information):
; Main Module {{Code|hello-universe.xq}}:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</presyntaxhighlight>
; Wrapper Module {{Code|Hello.xqm}}:
<pre classsyntaxhighlight lang="brush:xquery">
module namespace hello = 'http://basex.org/modules/Hello';
java:hello($world)
};
</presyntaxhighlight>
; Java class {{Code|Hello.java}}:
<pre classsyntaxhighlight lang="brush:java">
package org.basex.modules;
}
}
</presyntaxhighlight>
If the JAR file is installed, {{Code|Combined}} will be displayed as type:
Apart from the package descriptor, a {{Code|.xar}} archive contains a directory which includes the actual XQuery modules. For example, the [http://files.basex.org/modules/expath/functx-1.0.xar FunctX XAR archive] is packaged as follows:
<presyntaxhighlight>
expath-pkg.xml
functx/
functx.xql
functx.xsl
</presyntaxhighlight>
==Java==
* 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:
<pre classsyntaxhighlight lang="brush:xml">
<package xmlns="http://expath.org/ns/pkg">
<jar>...</jar>
....
</package>
</presyntaxhighlight>
* The jar file itself along with an XQuery file defining wrapper functions around the java methods has to reside in the module directory. The following example illustrates how java methods are wrapped with XQuery functions:
'''Example:'''<br>Suppose we have a simple class <code>Printer</code> having just one public method <code>print()</code>:
<pre classsyntaxhighlight lang="brush:java">
package test;
}
}
</presyntaxhighlight>
We want to extend BaseX with this class and use its method. In order to make this possible we have to define an XQuery function which wraps the <code>print</code> method of our class. This can be done in the following way:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace j="http://basex.org/lib/testJar";
return p:print($printer, $str)
};
</presyntaxhighlight>
As it can be seen, the class {{Code|Printer}} is declared with its binary name as a namespace prefixed with "java" and the XQuery function is implemented using the [http://docs.basex.org/wiki/Java_Bindings Java Bindings] offered by BaseX.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu