Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added workaround for generating labels from a sequence of asset-ids in EFX 1 #96

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having 2 "for" in for $item in for $t in ./normalize... looks a bit weird, but it's not a real problem, and I guess it might be unavoidable.

Copy link
Contributor Author

@rousso rousso Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks weird, but you will not necessarily have a for loop in the sequence expression. The sequence expression could be something like ${('a', 'b', 'c')} for example.

translateTemplate("{BT-00-Text} #{field|name|${for text:$t in BT-00-Text return $t}}"));
}

@Test
void testShorthandBtLabelReference() {
assertEquals(
Expand Down