Difference between revisions of "Repository"

From BaseX Documentation
Jump to navigation Jump to search
Line 96: Line 96:
 
  Main-Class:org.basex.modules.hello.World
 
  Main-Class:org.basex.modules.hello.World
  
Repository destination path:
+
Installation (the file will be copied to {{Mono|org/basex/modules/hello/World.jar}}):
  
  org/basex/modules/hello/World.jar
+
  REPO INSTALL HelloWorld.jar
  
 
After installing the module, all of the following URIs can then be used in XQuery to import this module or call its functions:
 
After installing the module, all of the following URIs can then be used in XQuery to import this module or call its functions:

Revision as of 09:09, 1 April 2012

This article is part of the XQuery Portal. It describes how external XQuery and Java code can be installed and used from XQuery, and how new packages are built and deployed.

Motivation

One of the reasons why languages such as Java or Perl have been so successful is the vast amount of libraries that are available to developers. XQuery is a Turing complete language, but it just provides around 100 pre-defined functions, which cannot meet all requirements. This is why additional libraries arise – such as FunctX – that extend the language with new features.

BaseX offers two mechanisms to make new packages accessible to the XQuery processor:

  1. With Version 7.2.1, we offer a simple packaging mechanism to directly install single XQuery and Java modules to the repository.
  2. The EXPath Packaging system provides a generic mechanism for adding XQuery modules to query processors. A package is defined as a .xar archive, which encapsulates one or more extension libraries.

Usage

All packages are stored in the package repository. The repository is a directory named BaseXRepo or repo, which resides in your home directory. BaseX provides three commands for interaction with the package repository: REPO INSTALL, REPO DELETE, and REPO LIST. Packages can also be managed from within XQuery, using the Repository Module.

Installation

A module or package can be installed with the REPO INSTALL command. The path to the file has to be given as a parameter, as the following two examples demonstrate:

REPO INSTALL http://files.basex.org/modules/functx-1.0.xar
REPO INSTALL hello-world.xqm

The installation will only succeed if the specified file conforms to the constraints described below. If you know that your input is valid, you may as well copy the files directly to the repository directory, or edit its contents in the repository without deleting and reinstalling them.

Querying

Installed packages can be addressed by importing them as modules. Since we have the package repository in which all packages are located, it is sufficient to just specify the namespace URI of a module:

import module namespace functx = "http://www.functx.com";

When this statement is parsed, the query processor will check if the namespace "http://www.functx.com" is used in any of the installed packages and, if yes, will load and parse the modules. In the remaining query, you can call the parsed module functions in the standard way, e.g.:

functx:capitalize-first("test")

Package encapsulating Java archives can be imported in the same way as pure XQuery modules (see below).

Listing

All currently installed packages can be listed with the REPO LIST command. It will return the names of all packages, their version, and the directory in which they are installed:

URI                    Version  Directory
-------------------------------------------------------
http://www.functx.com  1.0      http-www.functx.com-1.0

1 package(s).

Removal

A package can be deleted with the command REPO DELETE and by specifying either its name or the name of its directory:

REPO DELETE http://www.functx.com  ...or...
REPO DELETE functx-1.0

Packaging

With Version 7.2.1, XQuery modules and JAR archives can be installed to the repository by following just a few simple rules:

XQuery

If an XQuery file is specified as input for the install command, it will be parsed as XQuery module. If parsing was successful, the module URI will be rewritten to a file path and attached with the .xqm file suffix, and the original file will be copied to that path into the repository.

Example:

Contents of the file hello-world.xq:

module namespace m = 'http://basex.org/modules/hello/World';
declare function m:hello($world) { 'Hello ' || $world };

Installation (the file will be copied to org/basex/modules/hello/World.xqm):

REPO INSTALL hello-world.xq

XQuery importing the module:

import module namespace m = 'http://basex.org/modules/hello/World';
m:hello('Universe') 

Java

Suitable JAR archives may contain one or more class files. One of them will be chosen as main class, which must be specified in a Main-Class entry in the manifest file (META-INF/MANIFEST.MF). This fully qualified Java class name will be rewritten to a file path by replacing the dots with slashes and attached with the .jar file suffix, and the original file will be copied to that path into the repository.

The public functions of this class can then be addressed from XQuery, using the class or file path as namespace URI, or an alternative writing that can be rewritten to the module file path.

Example:

Contents of the file META-INF/MANIFEST.mf in HelloWorld.jar

Manifest-Version: 1.0
Main-Class:org.basex.modules.hello.World

Installation (the file will be copied to org/basex/modules/hello/World.jar):

REPO INSTALL HelloWorld.jar

After installing the module, all of the following URIs can then be used in XQuery to import this module or call its functions:

http://basex.org/modules/hello/World
org/basex/modules/hello/World
org.basex.modules.hello.World

The article on Java Bindings gives more insight on how Java code is handled from the XQuery processor.

EXPath Packaging

The EXPath specification defines how the structure of a .xar archive shall look like. The package contains at its root a package descriptor named expath-pkg.xml. This descriptor presents some meta data about the package as well as the libraries which it contains and their dependencies on other libraries or processors.

XQuery

Apart from the package descriptor, a .xar archive contains a directory which includes the actual XQuery modules. For example, the FunctX XQuery Library is packaged as follows:

expath-pkg.xml
functx/
  functx.xql
  functx.xsl

Java

In case you want to extend BaseX with a Java archive, some additional requirements have to be fulfilled:

  • Apart from the package descriptor expath-pkg.xml, 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 basex.xml and must conform to the following structure:
<package xmlns="http://expath.org/ns/pkg">
  <jar>...</jar>
    ....
    <class>...</class>
    <class>...</class>
    ....
</package>
  • 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:
Suppose we have a simple class Printer having just one public method print():

package test;

public final class Printer {
  public String print(final String s) {
    return new Writer(s).write();
  }
}

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 print method of our class. This can be done in the following way:

import module namespace j="http://basex.org/lib/testJar";

declare namespace p="java:test.Printer";

declare function j:print($str as xs:string) as xs:string {
  let $printer := p:new()
  return p:print($printer, $str)
};

As it can be seen, the class Printer is declared with its binary name as a namespace prefixed with "java" and the XQuery function is implemented using the Java Bindings offered by BaseX.

On our 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 Rewriting

If modules are looked up in the repository, their URIs are rewritten to a local file path. The URI transformation has been inspired by Zorba:

1. If a URI authority exists, it is reversed, and its dots are replaced by slashes.
2. The URI path is appended. If no path exists, a single slash is appended instead.
3. If the resulting string ends with a slash, the index string is appended.

If the resulting path has no file suffix, it may point to either an XQuery module or a Java archive. The following examples show some rewritings:

  • http://basex.org/modules/hello/World → org/basex/modules/hello/World
  • http://www.example.com → com/example/www/index
  • a/little/example.xq → a/little/example.xq

Changelog

Version 7.2.1

Version 7.1

Version 7.0