Assembling and running a translator
When you have chosen or implemented the components (see Choosing a translator), this page shows how to put them together into a translator and run it. Getting started has a complete program that you can start from.
Instantiating a translator
The dependency factory
A translator does not create its components itself. It receives them from a TranslatorDependencyFactory, which you
implement, and which has one method for each component:
createSymbolResolver(String sdkVersion, String qualifier)-
The symbol resolver (see Using the symbol resolver).
createScriptGenerator(String sdkVersion, String qualifier, TranslatorOptions options)-
The script generator.
createMarkupGenerator(String sdkVersion, String qualifier, TranslatorOptions options)-
The markup generator. Only a template translator calls this method.
createValidatorGenerator(String sdkVersion, String qualifier, TranslatorOptions options)-
The validator generator. Only a rules translator calls this method.
createErrorListener()-
The ANTLR error listener that receives the syntax errors. Return
ThrowingErrorListener.INSTANCE, fromeu.europa.ted.efx.exceptions, so that the translator throws them as exceptions with their line and column. With a plainBaseErrorListener, the translator does not report a syntax error, and the translation fails later with an exception that does not explain it.
A method for a component that your application does not use can throw UnsupportedOperationException. The
sdkVersion is the version of the SDK whose EFX you translate. The qualifier selects a variant of a component when
several are registered. The translators pass an empty qualifier unless you give one.
Component registration
The toolkit finds components through the @SdkComponent annotation. This annotation states the major versions of
the SDK, the role, and the qualifier, for which a class is written:
@SdkComponent(versions = {"2"}, componentType = SdkComponentType.SCRIPT_GENERATOR, qualifier = "js")
public class JavaScriptScriptGenerator implements ScriptGenerator {
// ...
}
ComponentFactory.getSymbolResolver, getScriptGenerator, getMarkupGenerator and getValidatorGenerator look up
the annotated class for the version and qualifier, and create it. ComponentFactory creates the symbol resolver once
for each version and qualifier, and then reuses it.
Each combination of role, major version and qualifier can have only one class. The toolkit already registers
SdkSymbolResolver, XPathScriptGenerator and SchematronGenerator for SDK 2, without a qualifier. So give a
component of your own for the same role a qualifier, such as "js" above. Otherwise, the toolkit stops with an error
such as "More than one components of type [SCRIPT_GENERATOR] have been found for SDK version [2]". The annotation is
inherited, so this also applies to a class that extends one of the toolkit’s components. The Notice Viewer, for
example, registers its own variant of XPathScriptGenerator with the qualifier "json".
ComponentFactory has no fallback: a request with the qualifier "js" finds only the classes registered with it.
So, in your factory, ask for each component with its own qualifier, for example "" for SdkSymbolResolver and
"js" for your script generator. Your factory can also create the components directly, as the example in Getting
started does for SchematronGenerator. A class that your factory creates directly does not need the annotation.
Translator options
TranslatorOptions holds the settings that the generators read. The implementation of the toolkit is
EfxTranslatorOptions:
-
EfxTranslatorOptions.DEFAULT: English, the decimal format of XSL, and the namespaceefx-udffor user-defined functions. -
new EfxTranslatorOptions(DecimalFormat, String primaryLanguage, String… otherLanguages): other languages, for example for the labels of a template. Other constructors also set the namespace of user-defined functions, the profiler, and anIncludedFileResolver. TheIncludedFileResolverdecides where the translator reads the files named in the#includedirectives of rules files, for example from a database instead of the file system.
Obtaining a translator
EfxTranslator has static methods that create a translator and translate in one call, as in Getting started. To
translate many files, get a translator once from EfxTranslatorFactory, and reuse it:
TranslatorOptions options = EfxTranslatorOptions.DEFAULT;
EfxExpressionTranslator expressions =
EfxTranslatorFactory.getEfxExpressionTranslator("2.0.0", factory, options);
EfxTemplateTranslator templates =
EfxTranslatorFactory.getEfxTemplateTranslator("2.0.0", factory, options);
EfxRulesTranslator rules =
EfxTranslatorFactory.getEfxRulesTranslator("2.0.0", factory, options);
Each method also has a form with a qualifier after the version.
Translating expressions
String script = expressions.translateExpression(
"WITH ND-Root, number:$limit COMPUTE count(BT-21-Procedure) > $limit", "3");
// count(cac:ProcurementProject/cbc:Name/normalize-space(text())) > 3
The expression is a single expression or a selector (see Expression forms). The arguments after it are the values of its parameters, in the order of their declaration, written as EFX literals.
Translating templates
String markup = templates.renderTemplate(Path.of("eforms-sdk/2.0.0/view-templates/16.efx"));
renderTemplate also takes the template as a String or an InputStream, and optionally other
TranslatorOptions. It gives the output file that your markup generator put together in composeOutputFile, such
as a complete XSLT stylesheet (see Implementing MarkupGenerator). Write it to a file
and store it with the version of the SDK that it comes from.
Translating rules
Map<String, String> files = rules.translateRules(
Path.of("eforms-sdk/2.0.0/validation/stage-1a.efx"));
The result maps file names to their content. With SchematronGenerator, the files are:
-
static/validation-stage-1a.schanddynamic/validation-stage-1a.sch, with the rules of the stage, and more pattern files for groups of notice subtypes, such asstatic/validation-stage-1a-E2.sch; -
static/complete-validation.schanddynamic/complete-validation.sch, which include all of them; -
schematrons.json, which lists the Schematron files, each with its name, its type (static or dynamic) and its file name. The entries of the pattern files also give their stage.
The static files check a notice on its own. The dynamic files also contain the rules that call external services
(see Dynamic function declaration). A
pattern file whose rules all call external services exists only under dynamic/.
Write the files to disk under the same relative paths, so that the include statements of the complete files find
them.
Integration patterns
- Translating everything when you import the SDK (recommended)
-
The EFX of an SDK version never changes. Translate all its templates and rules once, when your application imports the version, and store the results. When your application runs, it then only uses the stored XSLT, Schematron or script.
- Reusing translations
-
If you translate while your application runs, store each result, by SDK version and file, and reuse it. Reuse the translators as well. The toolkit creates the symbol resolver once for each SDK version.
- Versions
-
Store the translated files of each SDK version separately, and select them by the SDK version of the notice. Always display and validate a notice with the files of its own SDK version.
See also: