Implementing MarkupGenerator
The interface MarkupGenerator writes the output of a template file in your target markup language. Only the
EfxTemplateTranslator needs it: the translation of expressions and rules files does not use it.
Overview
A template file describes what to display for a notice (see
Template file). The template translator reads it, checks it,
and calls the methods of your MarkupGenerator to write the result in your target language. This can be an XSLT
stylesheet, a FreeMarker or Thymeleaf template, a JavaScript function, or another format in which an application can
display a notice.
Your markup generator works together with your ScriptGenerator. The script generator translates the expressions of
the template file, such as BT-00-Number + 1. The markup generator receives these expressions already translated,
and writes the markup that displays them.
The interface has fewer methods than ScriptGenerator, and they fall into two groups:
-
Render methods turn one part of EFX into markup: a piece of text, the value of an expression, a label, a hyperlink, a declaration, or the call of a fragment.
-
Compose methods combine markup that other methods have already produced: the definition of a fragment from its content and its children, and the whole output file from its sections.
The render and compose methods return a Markup, an object that holds a text in your target language. The method
escapeSpecialCharacters, which escapes the characters that have a special meaning in your markup language, returns
a String. You create a Markup with new Markup(String), and read its text through its script field. Markup.join(Markup) joins two of them, and
Markup.empty() gives an empty one.
Getting started
Your class implements MarkupGenerator directly. Like the script generator, it receives the TranslatorOptions in
its constructor, and the toolkit finds it through the annotation @SdkComponent:
@SdkComponent(versions = {"2"}, componentType = SdkComponentType.MARKUP_GENERATOR, qualifier = "html")
public class HtmlMarkupGenerator implements MarkupGenerator {
private final TranslatorOptions translatorOptions;
public HtmlMarkupGenerator(TranslatorOptions translatorOptions) {
this.translatorOptions = translatorOptions;
}
// ... implement all methods
}
The toolkit has no markup generator of its own, but other libraries on the classpath can register one, as the Notice Viewer does. A qualifier of your own avoids a clash (see Component registration). For how to put the translator together, see Assembling and running a translator.
Two methods are helpers that the translator calls whenever it needs them:
getEfxDataTypeEquivalent(Class<? extends EfxDataType>)-
Gives, as a
Markup, the name of the type in your target language that matches an EFX data type, such asxs:decimalforEfxDataType.Numberin XSLT. The translator uses it for the types of variables, parameters and arguments. escapeSpecialCharacters(String)-
Escapes the characters of a fixed text that have a special meaning in your target language, such as
<and&in XML, so that they appear as written. The translator calls it on every fixed text of a template line before it passes the text torenderFreeText.
How the translator builds the output
The translator turns each template line into a fragment: a piece of markup with a name, which other markup can
call. It is the equivalent of an xsl:template in XSLT, or of a function in a programming language. A fragment
displays the content of its line, and then calls the fragments of the lines nested under it, once for each
occurrence that their context matches in the notice. A callable template (a --- TEMPLATE --- section) is also a
fragment, named after the template.
The translator names the fragments after their section and their place in it:
-
body01,body02… for the lines at the top of the main section, andbody0101for the first line nested underbody01; -
summary01,nav01,header01… in the summary, navigation and header sections.
A callable template t is the fragment t. Suppose that t has one line at the top, with no context declaration,
no outline marker and no bullet. Then t is the fragment of that line, and the lines nested under it are t01 …
Otherwise, the fragments of its lines at the top are t01, t02 …, and t only calls them. Each fragment has a name
of its own in the file, so you can use the names as they are.
The following template file shows the order of the calls:
LET text:$title = 'Notice';
--- TEMPLATE t ---
LET text:$label;
DISPLAY ${$label};
#tag WITH BT-00-Text DISPLAY Text;
--- MAIN ---
1 DISPLAY ${$title};
WITH BT-00-Text WHEN BT-00-Text == 'a' DISPLAY A OTHERWISE DISPLAY #{name}: $value;
WITH ND-SubNode INVOKE t('x');
The translator calls your methods in this order:
-
renderVariableDeclarationfor$title, and the other declaration methods for the global declarations of the file. -
For each callable template,
composeFragmentDefinitionfor the template and for each of its lines. While it builds the content of a line, it calls the render methods for its text, expressions and labels. -
The translator then processes the main, summary, navigation and header sections, in this order, whatever their order in the file. It starts each section with its first line at the top. For a line at the top, it calls
renderFragmentInvocationand thenrenderContextLoop, and the call that results becomes part of the section. For that line and for each line nested under it, the translator:-
renders the content of the line;
-
renders the calls of the nested lines with
renderFragmentInvocationfollowed byrenderContextLoop, which become the children; -
calls
composeFragmentDefinition.
-
-
composeNoticeHeaderputs together the header of the notice from the calls of the lines of the header section. -
composeOutputFileputs together the declarations, the fragments, the sections and the header.
With the markup generator used in the tests of the toolkit, which writes each call as text, the result is:
GLOBALS:
string:title='Notice'
TEMPLATES:
let t(string:label) -> {
call(t01(string:label=$label))
for-each(/*/PathNode/TextField).call(t02(string:label=$label)) }
let t01(string:label) -> { eval($label) }
let t02(string:label) -> { text('Text') }
let body01() -> { #1: eval($title)
for-each(PathNode/TextField).call(body0101())
for-each(SubNode).call(body0102()) }
let body0101() -> { choose { when ./normalize-space(text()) = 'a': text('A'),
otherwise: caption(label(concat('field', '|', 'name', '|', 'BT-00-Text')))eval(./normalize-space(text())) } }
let body0102() -> { call(t(string:label='x') with nestingLevelOffset 1, headingLevelOffset 1 and navIdPrefix '1') }
MAIN:
for-each(/*).call(body01())
The test generator writes a caption as caption(…), before its content. The expressions, such as
./normalize-space(text()), come from the script generator, in this case the XPath script generator. The following
sections describe each method.
Declarations
The global declarations of a template file (see
Declarations) are rendered once, before the fragments. The
translator passes the markup that they produce to composeOutputFile as globals.
renderVariableDeclaration(String name, TypedExpression initialiser)-
Declares a global variable. The data type of the variable, and whether it is a sequence, are those of the initialiser: use
initialiser.getDataType(). renderFunctionDeclaration(String name, Map<String, Class<? extends EfxDataType>> parameters, TypedExpression expression)-
Declares a user-defined function. The parameters come in the order of their declaration, each with its EFX data type. The type that the function gives is the data type of the expression.
renderDictionaryDeclaration(String name, PathExpression match, StringExpression key)-
Declares a dictionary: an index of the occurrences that
matchselects, by the key thatkeycomputes from each of them.
Content
These methods render what a template line displays (see Template content). The translator joins their results, in the order of the content, into the content of the line.
renderFreeText(String freeText, TranslatorContext translatorContext)-
Renders a fixed text.
escapeSpecialCharactershas already escaped the text. renderVariableExpression(Expression expression, FieldTypes fieldType, TranslatorContext translatorContext)-
Renders the value of an expression block, such as
${BT-00-Number}or$value. The expression can give a single value or a sequence.fieldType, from the packageeu.europa.ted.efx.model.types, is the eForms type of the value. A reference to a field, such as${BT-505-Organization}or$value, has the type of its field, such asurl. A computed value has the type named like its EFX type:text,number,indicator,date,timeorduration. With it, your markup generator can, for example, display an internet address as a link. renderLabelFromKey(StringExpression key, TranslatorContext translatorContext)renderLabelFromKey(StringExpression key, NumericExpression quantity, TranslatorContext translatorContext)-
Renders a label whose key the translator has worked out, such as
#{name}or#{field|name|BT-00-Text}. The key is an expression, because part of it can be computed only when the notice is displayed. With a quantity, the label has a singular and a plural form: choose the form that fits the quantity. renderLabelFromExpression(Expression expression, TranslatorContext translatorContext)renderLabelFromExpression(Expression expression, NumericExpression quantity, TranslatorContext translatorContext)-
Renders a label whose key is the value of an expression, such as
#{$value}for a code. The expression can give several keys. renderHyperlink(Markup label, StringExpression url, TranslatorContext translatorContext)-
Renders a hyperlink:
labelis the markup already rendered for what the link displays, andurlthe expression that gives its target. renderLineBreak(TranslatorContext translatorContext)-
Renders a line break, such as
<br/>in HTML.
The TranslatorContext shows which section the translator is translating: getCurrentSection() gives SUMMARY,
NAVIGATION or HEADER in those sections, and DEFAULT in the main section and in the callable templates. The
translator translates a callable template once, and the template is displayed wherever it is invoked. So the current
section does not show in which sections its lines will be displayed.
Fragments
Three methods produce the fragments and the calls between them.
composeFragmentDefinition
Markup composeFragmentDefinition(String name, FragmentOutline outline,
List<Conditional> conditionals, Markup caption, Markup content, Markup children,
Set<Parameter> parameters, TranslatorContext translatorContext);
Defines the fragment of a template line, or of a callable template.
name-
The name of the fragment, used by the calls that invoke it.
outline-
Where the fragment is in the outline of its own section or callable template: what it is, its outline number, its levels and the body of its navigation id. See The outline of a fragment.
conditionals-
The
WHENalternatives of the line (see Conditional display), in the order in which they are written. EachConditionalhas a condition,getCondition(), the caption of its template,getCaption(), and the markup to display when the condition is true,getMarkup(). The first alternative whose condition is true is displayed. The list is empty for a line withoutWHEN. caption-
The caption of
content: what its template displays before the colon that separates it from the content (see Caption). It is an empty markup when the template has no caption, and always for a heading and in EFX-1. The translator does not pass the colon. Your markup generator decides how to display the caption and the content after it, for example in two columns, or with a separator that depends on the language of the display. content-
For a line without alternatives, what the line displays, after its caption if it has one. For a line with alternatives, what it displays when no condition is true: the
OTHERWISEalternative, or an empty markup when there is none. children-
The calls of the fragments of the lines nested under this one, already rendered, one after the other. Display them after the content.
parameters-
The parameters that the fragment receives: the parameters of a callable template, and the variables that the line and the lines above it declare in their context declarations. Each
Parameterhas a name,getName(), and a type already translated withgetEfxDataTypeEquivalent,getType().
The translator passes the definitions of all fragments to composeOutputFile, which puts them in the output
file.
renderContextLoop
Markup renderContextLoop(PathExpression context, Markup content, Set<Argument> arguments);
Repeats content, the call of a fragment, once for each occurrence that context selects in the notice, with that
occurrence as the context of the expressions of the fragment. In XSLT, this is an xsl:for-each.
arguments are the variables that the fragment receives: those of the lines above, passed on, then those that the
line declares in its context declaration (see
Context declaration). Each
Argument is a Parameter with a value, getValue(): the translated expression that computes it. Compute the
values inside the loop, because they can depend on the occurrence.
A line without a context declaration at the top of a callable template is displayed in the context in which the template is invoked, so the translator does not put its call in a loop.
renderFragmentInvocation
Markup renderFragmentInvocation(String name, OutlineOffset offset,
Set<Argument> arguments, TranslatorContext translatorContext);
Calls the fragment name with arguments. The translator calls it in two cases:
-
For each line nested under another, and for each line at the top of a section, to call the fragment of the line. The arguments pass on the variables that the line receives, and
offsetisOutlineOffset.NONE. -
For a line that invokes a callable template (see Template invocation), to call the template. The arguments give the values of the parameters of the template, in the order of the parameters, and
offsetshows what the invoking line puts above the lines of the template. See The outline of a fragment.
The outline of a fragment
A template file has an outline: its headings, their outline numbers, and how the lines are nested (see
Heading). The translator passes the outline
of each fragment in a FragmentOutline, and what each call of a callable template adds to it in an OutlineOffset.
Both classes, and the enumeration FragmentKind, are in the package eu.europa.ted.efx.model.templates. They only
hold information: your markup generator decides how to use it.
FragmentKind
outline.getKind() shows what the fragment is:
| Kind | Fragment |
|---|---|
|
A template line that starts with an outline marker: a number, |
|
A template line without an outline marker or a bullet that displays content. |
|
A template line that starts with a bullet, |
|
A template line that invokes a callable template in each of its alternatives. It displays nothing of its own: the
templates it invokes take its place. Its fragment holds only the choice between its alternatives, if it has any, and
the calls of the templates. Do not write a block of your own for it, such as an HTML |
|
A callable template whose lines at the top are fragments of their own: it has several lines at the top, or one line with a context declaration, an outline marker or a bullet. It is not displayed itself: its fragment holds only the calls of these lines, which take the place of the line that invokes the template. Do not write a block of your own for it either. A callable template with one line at the top, without context declaration, outline marker or bullet, is the fragment of that line, with the kind of that line. |
FragmentOutline
getNumber()-
The outline number of the line as it is displayed, such as
4.2. It is an empty text when the line has no number, and also when it has no nested lines, because only a line with nested lines displays its number. getNestingLevel()-
1 for a line at the top of its section or template, one more for each line it is nested in. 0 for a fragment of kind
TEMPLATE. getHeadingLevel()-
For a heading, the number of headings it is nested in, plus 1. 0 for any other fragment.
getNavIdBody()-
For a heading with an outline number or a tag, its navigation address in its own section or template: the numbers and tags of the headings it is nested in, the outermost first, then its own, joined by
_(OutlineAddress.SEPARATOR), such as5_1_10or1_notice-info. An empty text for any other fragment.
All these values are those of the line in its own section or callable template.
OutlineOffset
The translator translates a callable template once, and the template is displayed wherever it is invoked. So the levels and the navigation id of its lines depend on where they are displayed. The call of a template passes what the invoking line puts above the lines of the template, which take its place:
getNestingLevelOffset()-
The nesting level of the invoking line, minus 1.
getHeadingLevelOffset()-
The number of headings that the invoking line is nested in.
getNavIdPrefix()-
The navigation address of the nearest heading above the invoking line that has an outline number or a tag, or an empty text when there is none.
The call of any other fragment passes OutlineOffset.NONE: 0, 0 and an empty text. The outline of a line already
counts the lines above it in its own section or template.
Where a line is displayed
Where a line is displayed, its values are those of its outline, combined with the offsets of all the template calls that led to it:
-
its nesting level is the sum of the nesting level offsets of these calls, plus its own nesting level;
-
for a heading, its heading level is the sum of the heading level offsets of these calls, plus its own heading level; for any other line it stays 0;
-
its navigation id is the navigation id prefixes of these calls, then its own navigation id body, joined by
_, followed by a suffix that your markup generator adds (see Navigation ids).
A template can be invoked from several places, so these sums are known only when the notice is displayed. Your markup generator must carry them from call to call. Each fragment receives the offsets of the calls that led to it, and passes them on to the fragments that it calls. When it calls a template, it adds the offset of the call. The Notice Viewer does this with three hidden parameters, one for each value (see Reference: the Notice Viewer).
For example:
--- TEMPLATE t ---
DISPLAY T1;
#tag DISPLAY T2;
DISPLAY T3;
DISPLAY T4;
--- MAIN ---
DISPLAY A;
1 DISPLAY B;
INVOKE t();
DISPLAY D;
The line INVOKE t(); has nesting level 3 and is nested in one heading, 1. It calls t with the nesting level
offset 2, the heading level offset 1 and the navigation id prefix 1. In the template, the line #tag has nesting
level 2, heading level 1 and navigation id body tag. So it is displayed at nesting level 4, as a heading of level 2,
with the navigation id 1_tag followed by the suffix.
Where the lines are displayed, their nesting levels are: A 1, B 2, T1 3, T2 4, T3 5, T4 6, D 3. Their heading levels are: B 1, T2 2, and 0 for all other lines.
Navigation ids
A navigation id identifies a heading where it is displayed, so that a navigation entry can link to it. It has three parts:
-
The prefix, from the calls that led to the line: its place in the whole notice.
-
The body, from
getNavIdBody(): its place in its own section or template. -
The suffix, which your markup generator adds when the notice is displayed. It tells apart the occurrences for which the heading is displayed. For example, a heading displayed for each lot needs a different id for each lot. In XSLT,
generate-id()of the context element does this.
A link must be able to reach a heading displayed in the main section by its navigation id. A heading displayed in the navigation section is a navigation entry. It links to the heading of the main section that has the same navigation id, and what it displays becomes the text of the link (see Navigation section). A heading displayed in the summary section is neither.
The suffix comes from the occurrence for which the line is displayed. So a navigation entry finds its heading only when both are displayed for the same occurrence. The translator checks that a heading of the main section has the address of each navigation entry, but it cannot check that the occurrence is the same.
composeNoticeHeader
Markup composeNoticeHeader(Map<HeaderPart, Markup> parts, List<Markup> lines,
TranslatorContext translatorContext);
Puts the header of the notice together, from the lines of the header section (see Header section):
parts-
For each part of the header that the template displays, the call of the line that displays it, already wrapped in its context loop. The keys are the values of the enumeration
HeaderPart, in the packageeu.europa.ted.efx.model.templates:HEADING,SUBHEADING,TITLE,COUNTRY,MAIN_CLASSIFICATION,MAIN_NATURE,FORM_TYPE,NOTICE_TYPE,PUBLICATION_NUMBER,OJ_ISSUEandPUBLICATION_DATE. The entries come in the order ofHeaderPart. A part that the template does not display has no entry. lines-
The calls of the normal lines at the top of the header section, after the parts, in their order.
The translator calls this method once for each EFX-2 template file, also when the file has no header section: then
both are empty. It passes the result to composeOutputFile.
Each line of the header section is a fragment, like the lines of the other sections: the translator defines it with
composeFragmentDefinition. A line that displays a part is a normal line at the top: FragmentKind.NORMAL, without
an outline number, at nesting level 1 and heading level 0, without nested lines. For HEADING and SUBHEADING, the
caption is empty. Nothing in the definition shows which part the line displays: only the key of parts does.
The names of the parts tell your markup generator what each part shows, so it can place each one. For example, the Notice Viewer writes the title in the title of the HTML page, and the other parts above the display of the notice.
composeOutputFile
Markup composeOutputFile(List<Markup> globals, List<Markup> mainSection,
List<Markup> summarySection, List<Markup> navigationSection, Markup header,
List<Markup> fragments);
Puts the output file together:
globals-
The global declarations, in the order of the template file.
mainSection,summarySection,navigationSection-
The calls of the lines at the top of each section, already wrapped in their context loops. A section that the template file does not have is an empty list.
header-
The header of the notice, as
composeNoticeHeaderput it together. fragments-
The definitions of all fragments: those of the callable templates first, then those of the sections.
The header, the summary and the navigation are separate views of the same notice. Your markup generator decides where and how to display each of them.
Deprecated methods
The interface keeps older forms of several methods, marked @Deprecated(forRemoval = true). They have default
implementations, and the translator does not call them. Do not override them: they will disappear before version
2.0.0. They include the forms of composeFragmentDefinition without a FragmentOutline, the form of
renderFragmentInvocation without an OutlineOffset, the forms of the render methods without a
TranslatorContext, the forms of composeOutputFile without a header, and the forms of renderVariableExpression
without a fieldType.
Reference: the Notice Viewer
The eForms Notice Viewer is a sample application that translates view templates into XSLT and displays notices in HTML. Its markup generator is an example of a complete implementation:
-
eu.europa.ted.eforms.viewer.generator.sdk1.XslMarkupGeneratorimplements all methods. Each method fills a FreeMarker template, insrc/main/resources/templates/xsl-markup/html/:fragment_definition.ftlwrites anxsl:template,fragment_invocation.ftlanxsl:call-template,context_loop.ftlanxsl:for-each, andoutput_file.ftlthe stylesheet. -
eu.europa.ted.eforms.viewer.generator.sdk2.XslMarkupGeneratorextends it for EFX-2 and carries the outline from call to call. Every fragment receives three parameters,navIdPrefix,nestingLevelOffsetandheadingLevelOffset, and passes them on in its calls, extended with the offset of each template call. A heading receives the idconcat($navIdPrefix, '<body>', '-', generate-id()). You cannot declare names that start with__in EFX, so these parameters cannot have the same name as a parameter of a template.
For example, the line WITH ND-SubNode INVOKE t('x'); of the template file above, nested under the heading 1,
becomes this fragment (apart from differences in white space):
<xsl:template name="body0102">
<xsl:param name="__navIdPrefix"/>
<xsl:param name="__nestingLevelOffset"/>
<xsl:param name="__headingLevelOffset"/>
<xsl:call-template name="t">
<xsl:with-param name="label" select="'x'"/>
<xsl:with-param name="__navIdPrefix" select="concat($__navIdPrefix, '1_')"/>
<xsl:with-param name="__nestingLevelOffset" select="$__nestingLevelOffset + 1"/>
<xsl:with-param name="__headingLevelOffset" select="$__headingLevelOffset + 1"/>
</xsl:call-template>
</xsl:template>
The fragment has the kind INVOCATION, so it writes no section of its own.
The Notice Viewer is a sample: it shows how to use the toolkit, but it is not meant for use in production without changes.
See also: