Getting started

This page shows how to translate EFX with the components that come with the toolkit: an expression into XPath, and a rules file into Schematron. Both translations use only these components, so you write no generator of your own.

Prerequisites

  • Java 21 or later.

  • Maven, or another build tool that reads Maven repositories.

  • The files of the eForms SDK whose EFX you want to translate, on disk (see Obtaining the SDK).

Adding the toolkit to your project

The toolkit is published on Maven Central:

<dependency>
  <groupId>eu.europa.ted.eforms</groupId>
  <artifactId>efx-toolkit-java</artifactId>
  <version>2.0.0-alpha.10</version>
</dependency>

The 2.0.0 versions of the toolkit translate EFX-2. They are pre-releases until SDK 2 is released. Use the latest one. This guide describes the version in development, so a few methods can differ in the latest pre-release.

Obtaining the SDK

The toolkit reads the metadata of the fields, nodes and codelists from the files of the SDK. It expects a folder with one subfolder for each version, named after the version, such as eforms-sdk/2.0.0. You can extract the SDK into this folder yourself. You can also let the eForms Core Library, which the toolkit already uses, download it from Maven Central:

SdkDownloader.downloadSdk(new SdkVersion("2.0.0"), Path.of("eforms-sdk"));

Your first translation

A translator receives its components from a TranslatorDependencyFactory. The factory below gives the components of the toolkit: the symbol resolver that reads the SDK, the XPath script generator, and the Schematron generator.

import java.nio.file.Path;
import java.util.Map;
import org.antlr.v4.runtime.BaseErrorListener;
import eu.europa.ted.eforms.sdk.ComponentFactory;
import eu.europa.ted.eforms.sdk.schematron.SchematronGenerator;
import eu.europa.ted.efx.EfxTranslator;
import eu.europa.ted.efx.exceptions.ThrowingErrorListener;
import eu.europa.ted.efx.interfaces.*;

public class FirstTranslation {

  static class ToolkitDependencyFactory implements TranslatorDependencyFactory {
    private final Path sdkRoot;

    ToolkitDependencyFactory(Path sdkRoot) {
      this.sdkRoot = sdkRoot;
    }

    @Override
    public SymbolResolver createSymbolResolver(String sdkVersion, String qualifier) {
      try {
        return ComponentFactory.getSymbolResolver(sdkVersion, qualifier, sdkRoot);
      } catch (InstantiationException e) {
        throw new IllegalStateException(e);
      }
    }

    @Override
    public ScriptGenerator createScriptGenerator(String sdkVersion, String qualifier,
        TranslatorOptions options) {
      try {
        return ComponentFactory.getScriptGenerator(sdkVersion, qualifier, options);
      } catch (InstantiationException e) {
        throw new IllegalStateException(e);
      }
    }

    @Override
    public MarkupGenerator createMarkupGenerator(String sdkVersion, String qualifier,
        TranslatorOptions options) {
      throw new UnsupportedOperationException("No markup generator in this example");
    }

    @Override
    public ValidatorGenerator createValidatorGenerator(String sdkVersion, String qualifier,
        TranslatorOptions options) {
      return new SchematronGenerator(options);
    }

    @Override
    public BaseErrorListener createErrorListener() {
      return ThrowingErrorListener.INSTANCE;
    }
  }

  public static void main(String[] args) throws Exception {
    Path sdkRoot = Path.of("eforms-sdk");
    TranslatorDependencyFactory factory = new ToolkitDependencyFactory(sdkRoot);

    // An expression, translated to XPath
    String xpath = EfxTranslator.translateExpression(factory, "2.0.0",
        "WITH ND-Root COMPUTE BT-01-notice == 'other'");
    System.out.println(xpath);

    // A rules file, translated to Schematron
    Map<String, String> files = EfxTranslator.translateRules(factory, "2.0.0",
        sdkRoot.resolve("2.0.0/validation/stage-1a.efx"));
    System.out.println(files.size() + " files");
  }
}

The program prints:

cbc:RegulatoryDomain/normalize-space(text()) = 'other'
103 files

The XPath reads the legal basis of the notice, the field BT-01-notice, at its path in the XML. The translation of the rules file gives a map from file names to their content:

  • the Schematron pattern files, under static/ and dynamic/;

  • in each of these folders, a complete-validation.sch that includes the pattern files;

  • a schematrons.json that describes the files.

Write the files to disk, or keep them in memory for your validator.

When an expression or a file has an error, the translator throws an exception with the line and the column, such as line 1:21 Type mismatch. Operator '==' cannot take 'text' and 'number'. Its right operand must be 'text'. for WITH ND-Root COMPUTE BT-01-notice == 1.

Translating templates

Template files need one more component, a MarkupGenerator, which writes the output in your target markup language. The toolkit does not provide one. Write your own (see Implementing MarkupGenerator), or start from the XSLT generator of the Notice Viewer sample application. Then return it from createMarkupGenerator and call EfxTranslator.translateTemplate.

Further reading


See also: