Skip to content

Commit

Permalink
Merge pull request #96 from OP-TED/TEDEFO-2812-labels-from-sequence
Browse files Browse the repository at this point in the history
Added workaround for generating labels from a sequence of asset-ids in EFX 1
  • Loading branch information
rousso authored Nov 16, 2023
2 parents 99222af + 335b262 commit 8c8711f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,36 @@ public void exitExpressionTemplate(ExpressionTemplateContext ctx) {

@Override
public void exitStandardLabelReference(StandardLabelReferenceContext ctx) {
StringExpression assetId = ctx.assetId() != null ? this.stack.pop(StringExpression.class)
: this.script.getStringLiteralFromUnquotedString("");
if (!this.stack.empty() && StringSequenceExpression.class.isAssignableFrom(this.stack.peek().getClass()) && ctx.assetId() != null) {

// This is a workaround that allows EFX 1 to render a sequence of labels without a special
// syntax. When a standard label reference is processed, the template translator checks if
// the assetId is provided with a SequenceExpression. If this is the case, then the
// translator generates the appropriate code to render a sequence of labels.
//
// For example, this will render a sequence of labels for a label reference of the form
// #{assetType|labelType|${for text:$t in ('assetId1','assetId2') return $t}}:}
// The only restriction is that the assetType and labelType must be the same for all labels in the sequence.

StringSequenceExpression assetIdSequence = this.stack.pop(StringSequenceExpression.class);
this.exitStandardLabelReference(ctx, assetIdSequence);
} else {

// Standard implementation as originally intended by EFX 1

StringExpression assetId = ctx.assetId() != null ? this.stack.pop(StringExpression.class)
: this.script.getStringLiteralFromUnquotedString("");
this.exitStandardLabelReference(ctx, assetId);
}
}

/**
* Renders a single label from a standard label reference.
*
* @param ctx The ParserRuleContext of the standard label reference.
* @param assetId The assetId of the label to render.
*/
private void exitStandardLabelReference(StandardLabelReferenceContext ctx, StringExpression assetId) {
StringExpression labelType = ctx.labelType() != null ? this.stack.pop(StringExpression.class)
: this.script.getStringLiteralFromUnquotedString("");
StringExpression assetType = ctx.assetType() != null ? this.stack.pop(StringExpression.class)
Expand All @@ -258,6 +286,39 @@ public void exitStandardLabelReference(StandardLabelReferenceContext ctx) {
this.script.getStringLiteralFromUnquotedString("|"), assetId))));
}

/**
* Renders a sequence of labels from a standard label reference.
*
* @param ctx The ParserRuleContext of the standard label reference.
* @param assetIdSequence The sequence of assetIds for the labels to render.
*/
private void exitStandardLabelReference(StandardLabelReferenceContext ctx, StringSequenceExpression assetIdSequence) {
StringExpression labelType = ctx.labelType() != null ? this.stack.pop(StringExpression.class)
: this.script.getStringLiteralFromUnquotedString("");
StringExpression assetType = ctx.assetType() != null ? this.stack.pop(StringExpression.class)
: this.script.getStringLiteralFromUnquotedString("");

Variable loopVariable = new Variable("item",
this.script.composeVariableDeclaration("item", StringExpression.class), StringExpression.empty(),
this.script.composeVariableReference("item", StringExpression.class));

this.stack.push(this.markup.renderLabelFromExpression(
this.script.composeDistinctValuesFunction(
this.script.composeForExpression(
this.script.composeIteratorList(
List.of(
this.script.composeIteratorExpression(loopVariable.declarationExpression, assetIdSequence))),
this.script.composeStringConcatenation(List.of(
assetType,
this.script.getStringLiteralFromUnquotedString("|"),
labelType,
this.script.getStringLiteralFromUnquotedString("|"),
new StringExpression(loopVariable.referenceExpression.getScript()))),
StringSequenceExpression.class),
StringSequenceExpression.class)));
}


@Override
public void exitShorthandBtLabelReference(ShorthandBtLabelReferenceContext ctx) {
StringExpression assetId = this.script.getStringLiteralFromUnquotedString(ctx.BtId().getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ void testStandardLabelReference_UsingLabelTypeAsAssetId() {
translateTemplate("{BT-00-Text} #{auxiliary|text|value}"));
}

@Test
void testStandardLabelReference_WithAssetIdIterator() {
assertEquals(
"let block01() -> { label(distinct-values(for $item in for $t in ./normalize-space(text()) return $t return concat('field', '|', 'name', '|', $item))) }\nfor-each(/*/PathNode/TextField).call(block01())",
translateTemplate("{BT-00-Text} #{field|name|${for text:$t in BT-00-Text return $t}}"));
}

@Test
void testShorthandBtLabelReference() {
assertEquals(
Expand Down

0 comments on commit 8c8711f

Please sign in to comment.