Java Extension Functions
A Java extension function can be used within an XPath or XQuery expression to invoke a Java constructor or call a Java method (static or instance).
A field in a Java class is considered to be a method without any argument. A field can be static or instance. How to access fields is described in the respective sub-sections, static and instance.
This section is organized into the following sub-sections:
•Java: Static Methods and Static Fields
•Java: Instance Methods and Instance Fields
•Datatypes: XPath/XQuery to Java
•Datatypes: Java to XPath/XQuery
Note the following
•If you are using an Altova desktop product, the Altova application attempts to detect the path to the Java virtual machine automatically, by reading (in this order): (i) the Windows registry, and (ii) the JAVA_HOME environment variable. You can also add a custom path in the Options dialog of the application; this entry will take priority over any other Java VM path detected automatically.
•If you are running an Altova server product on a Windows machine, the path to the Java virtual machine will be read first from the Windows registry; if this is not successful the JAVA_HOME environment variable will be used.
•If you are running an Altova server product on a Linux or macOS machine, then make sure that the JAVA_HOME environment variable is properly set and that the Java Virtual Machines library (on Windows, the jvm.dll file) can be located in either the \bin\server or \bin\client directory.
Form of the extension function
The extension function in the XPath/XQuery expression must have the form prefix:fname().
•The prefix: part identifies the extension function as a Java function. It does so by associating the extension function with an in-scope namespace declaration, the URI of which must begin with java: (see below for examples). The namespace declaration should identify a Java class, for example: xmlns:myns="java:java.lang.Math". However, it could also simply be: xmlns:myns="java" (without a colon), with the identification of the Java class being left to the fname() part of the extension function.
•The fname() part identifies the Java method being called, and supplies the arguments for the method (see below for examples). However, if the namespace URI identified by the prefix: part does not identify a Java class (see preceding point), then the Java class should be identified in the fname() part, before the class and separated from the class by a period (see the second XSLT example below).
|Note:||The class being called must be on the classpath of the machine.|
Here are two examples of how a static method can be called. In the first example, the class name (java.lang.Math) is included in the namespace URI and, therefore, must not be in the fname() part. In the second example, the prefix: part supplies the prefix java: while the fname() part identifies the class as well as the method.
The method named in the extension function (cos() in the example above) must match the name of a public static method in the named Java class (java.lang.Math in the example above).
Here is an XQuery example similar to the XSLT example above:
User-defined Java classes
If you have created your own Java classes, methods in these classes are called differently according to: (i) whether the classes are accessed via a JAR file or a class file, and (ii) whether these files (JAR or class) are located in the current directory (the same directory as the XSLT or XQuery document) or not. How to locate these files is described in the sections User-Defined Class Files and User-Defined Jar Files. Note that paths to class files not in the current directory and to all JAR files must be specified.