Example: Create a Custom Java Library

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Functions > Referencing Java, C# and C++ Libraries Manually >

Example: Create a Custom Java Library

This topic describes how to create a sample Java library and configure a .mff file for it so that the library appears in the Libraries window of MapForce.

 

1.Create a new Java project in your preferred development environment (for example, Eclipse).
2.Add to the project a new package called com.hello.functions which consists of a class called Hello.  In the code listing below, notice that the HelloFunction function has been defined as public static.

 

    package com.hello.functions;
 
    public class Hello {
        public static String HelloFunction ( boolean greetingType ) {
          if( greetingType )
              return "Hello World!";
          return "Hello User!";
        }
    }

 

3.Optionally, if your project needs support for special schema types such as date, time, and duration, import the com.altova.types package. To obtain this package, generate Java code from a mapping without custom functions.

 

import com.altova.types.*;

 

4.Compile your custom library to a class file, and add it to the Java classpath.
5.Using an XML editor, create a new .mff file and validate it against the ..\Program Files\MapForceLibraries\mff.xsd folder. Make sure that the text highlighted below points to the namespace and class defined previously in the Java code. For more information, see Configuring the .mff File.

 

<?xml version="1.0" encoding="UTF-8"?>
<mapping version="9" library="helloworld" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mff.xsd">
  <implementations>
    <implementation language="java">
        <setting name="namespace" value="com.hello.functions"/>
        <setting name="class" value="Hello"/>        
    </implementation>
  </implementations>
  <group name="Greetings">
    <component name="HelloFunction">
        <sources>
          <datapoint name="greeting_type" type="xs:boolean"/>
        </sources>
        <targets>
          <datapoint name="result" type="xs:string"/>
        </targets>
        <implementations>
          <implementation language="java">
              <function name="HelloFunction"/>
          </implementation>
        </implementations>
        <description>
          <short>result = hello(greeting_type)</short>
          <long>Returns a greeting according to the given greeting type.</long>
        </description>
    </component>
  </group>
</mapping>

 

You have now finished creating a custom library and the .mff file which adapts it to MapForce. The custom .mff file can now be used in MapForce (see Importing the .mff File Into MapForce).


© 2019 Altova GmbH