Changes

Jump to navigation Jump to search
2,547 bytes added ,  14:14, 27 February 2020
no edit summary
=Introduction=
One of the reasons why things that makes languages such as Java or Perl have been so successful is the vast amount availability of external libraries that are available to developers.As XQuery comes with only 150 pre-defined functions, which cannot meet all requirements, there is some need for additional library modules exist – such as [http://www.functx.com/ FunctX] – that which extend the language with new features.
BaseX offers the following mechanisms to make external modules accessible to the XQuery processor:
# The default internal [[#Packaging|Packaging]] mechanism will install single XQuery and Java JAR modules in the repository.
# The [[#EXPath Packaging|EXPath Packaging]] system provides a generic mechanism for adding XQuery modules to query processors. A package is defined as a {{Code|.xar}} archive, which encapsulates one or more extension libraries.
==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>[http://files.basex.org/modules/org/basex/modules/Hello/HelloUniverse.xq HelloUniversehello-universe.xq]</code>:
<pre classsyntaxhighlight lang="brush:xquery">import module namespace m = 'http://basex.org/modules/Hellohello' at 'HelloWorldhello-world.xqm';
m:hello("Universe")
</presyntaxhighlight>
'''Library Module''' <code>[http://files.basex.org/modules/org/basex/modules/Hello/HelloWorld.xqm HelloWorldhello-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 a directory named {{Code|BaseXRepo}} or 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=
There are various ways to handle organize your packages:
* Execute BaseX REPO commands (listed below)
* Use the GUI (''Options'' → ''Packages'')
You can even manually add and remove packages in the repository directory; all changes will automatically be detected by the query processorBaseX.
==Installation==
==Listing==
All currently installed packages can be listed with {{Command|REPO LIST}}. It will return the The names of all packagesare listed, along with their version, their package type, and the directory in which they are installedrepository path:
URI Name Version DirectoryType Path ----------------------------------------------------------------- <nowiki>http://www.functx.com</nowiki> 1.0 EXPath http-www.functx.com-1.0 1 package(s).
==Removal==
A package can be deleted with {{Command|REPO DELETE}} and an additional argument, containing its name or the name suffixed with a hyphen and the package version:
REPO DELETE <nowiki>http://www.functx.com</nowiki> ...or...
REPO DELETE <nowiki>http://www.functx.com-1.0</nowiki>
==XQuery==
If an XQuery file is specified as input for the install command, it will be parsed as XQuery library module. If parsing was successfulthe file can successfully be parsed, the module URI will be [[Java Bindings#URI Rewriting|rewritten]] to a file path and attached with the {{Code|.xqm}} file suffix, and the original file will possibly be renamed and copied to that path into the repository.
'''Example:'''
Importing the repository module:
<pre classsyntaxhighlight lang="brush:xquery">
import module namespace m = 'http://basex.org/modules/Hello';
m:hello("Universe")
</presyntaxhighlight>
==Java==
Suitable JAR archives may contain one or more class files. One of them will be chosen as main classFor general notes on importing Java classes, which must be specified in a {{Code|Main-Class}} entry in please read 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 {{CodeBindings article on [[Java Bindings#Module_Imports|.jar}} file suffix, and the original file will be renamed and copied to that path into the repositoryModule Imports]].
The public functions Java archives (JARs) may contain one or more class files. One of this them will be chosen as main class can then , which must be addressed from XQuery, using specified in a {{Code|Main-Class}} entry in the manifest file ({{Code|META-INF/MANIFEST.MF}}). This fully qualified Java class or name will be rewritten to a file path as namespace URIby replacing the dots with slashes and attaching the {{Code|.jar}} file suffix, or an alternative writing and the original file will be renamed and copied to that can path into the repository. If the class will be [[#URI Rewriting|rewritten]] to imported in the prolog of the XQuery module file path, an instance of it will be created, and its public functions can then be addressed from XQuery. Moreover, a A class may extend the {{Code|QueryModule}} class to get access to the current query context and to be enriched by some helpful annotations (please consult see [[Java_Bindings#Context-AwarenessAnnotations|Context Awareness of Java BindingsAnnotations]] for more information).
'''Example:'''
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):
<nowiki>http://basex.org/modules/Hello</nowiki>
org.basex.modules.Hello
Please ===Additional Libraries=== A Java class may depend on additional libraries. The dependencies can be aware that resolved by creating a fat JAR file, i.e., extracting all files of the library archives and producing a single, flat JAR package. Another solution is to copy the libraries into a {{Code|lib}} directory of the JAR package. If the package will be installed, the additional library archives will be extracted and copied to a hidden sub-directory in the repository. If the package will be deleted, the hidden sub-directory will be removed as well. ; Examplary contents of {{Code|Image.jar}}  lib/ Images.jar META-INF/ MANIFEST.MF org/basex/modules/ Image.class ; Directory structure of the repository directory after installing the package  org/basex/modules/ Image.class .Images/ Images.jar ==Combined== It makes sense to combine the execution advantages of XQuery and Java packages: * Instead of directly calling Java code , a wrapper module can cause side effects be provided. This module contains functions that conflict with invoke the Java functions.* These functions can be strictly typed. This reduces the functional nature danger of erroneous or unexpected conversions between XQuery and Java code.* In addition, the entry functions can have properly maintained XQuery comments. XQueryand Java can be combined as follows: * First, or may introduce a JAR package is created (as described above).* A new security risksXQuery wrapper module is created, which is named identically to the Java main class. * The article on [[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 Bindings]] gives more insight on how Java code main class. If the resulting JAR file is handled from installed, the embedded XQuery processormodule will be extracted, and will be called first if the module will be imported. ; Main Module {{Code|hello-universe.xq}}: <syntaxhighlight lang="xquery">import module namespace m = 'http://basex.org/modules/Hello';m:hello("Universe")</syntaxhighlight> ; Wrapper Module {{Code|Hello.xqm}}: <syntaxhighlight lang="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)};</syntaxhighlight> ; Java class {{Code|Hello.java}}: <syntaxhighlight lang="java">package org.basex.modules; public class Hello { public String hello(final String world) { return "Hello " + world; }}</syntaxhighlight> If the JAR file is 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=
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==
In case If you want to extend BaseX package an EXPath archive with a Java archivecode, 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:
<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.
On our [http://files.basex.org/modules/ file server], you can find some example libraries packaged as XML archives (xar files). You can use them to try our packaging API or just as a reference for creating your own packages.
=URI RewritingPerformanceIf a module is looked up in the repository, the namespace URI is rewritten to a local file path: # If the URI is a URL:## colons will be replaced with slashes,## in the URI authority, the order of all substrings separated by dots is reversed, and## dots in the authority and the path are replaced by slashes. If no path exists, a single slash is appended.# Otherwise, if the URI is a URN, colons will be replaced with slashes.# Characters other than letters, dots and slashes will be replaced with dashes.# If the resulting string ends with a slash, the {{Code|index}} string is appended.
If Importing XQuery modules that are located in the resulting path has no file suffix, it may point to either an XQuery module or repository is just as fast as importing any other modules. Modules that are imported several times in a Java archiveproject will only be compiled once.The following examples show some rewritings:
* {{Code|<nowiki>http://basexImported Java archives will be dynamically added to the classpath and unregistered after query execution. This requires some constant overhead and may lead to unexpected effects in scenarios with highly concurrent read operations.org/modules/hello/World</nowiki>}} → If you want to get optimal performance, it is recommendable to move your JAR files into the {{Code|org/basex/modules/hellolib/Worldcustom}}* {{Code|<nowiki>http://wwwdirectory of BaseX.exampleThis way, the archive will be added to the classpath if BaseX is started.com</nowiki>}} → {{Code|com/example/www/index}}* {{Code|If you have installed a/little/example}} → {{Code[[#Combined|a/little/example}}* {{Code|a:b:c}} → {{Code|a/b/c}}Combined Package]], you can simply keep your XQuery module in the repository, and the Java classes will be automatically detected.
=Changelog=
;Version 89.20
* Added: [[#URI RewritingCombined|URI RewritingCombined]]XQuery and Java packages* Added: support for URNs[[#Additional Libraries|Additional Libraries]]
;Version 7.2.1
* Updated: [[#Installation|Installation]]: existing packages will be replaced without raising an error
* Updated: [[#Removal|Removal]]: remove specific version of a package
* Added: [[#Packaging|Packaging]], [[#URI Rewriting|URI Rewriting]]
;Version 7.1
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu