Skip to content

Commit

Permalink
Added a workaround for formatting dates and times in EFX 1 templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rousso committed Nov 15, 2023
1 parent e96d7a9 commit a177d31
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import eu.europa.ted.efx.model.Context.FieldContext;
import eu.europa.ted.efx.model.Context.NodeContext;
import eu.europa.ted.efx.model.expressions.Expression;
import eu.europa.ted.efx.model.expressions.TypedExpression;
import eu.europa.ted.efx.model.expressions.path.PathExpression;
import eu.europa.ted.efx.model.expressions.path.StringPathExpression;
import eu.europa.ted.efx.model.expressions.scalar.StringExpression;
import eu.europa.ted.efx.model.expressions.sequence.StringSequenceExpression;
import eu.europa.ted.efx.model.templates.ContentBlock;
import eu.europa.ted.efx.model.templates.ContentBlockStack;
import eu.europa.ted.efx.model.templates.Markup;
import eu.europa.ted.efx.model.types.EfxDataType;
import eu.europa.ted.efx.model.variables.Variable;
import eu.europa.ted.efx.model.variables.VariableList;
import eu.europa.ted.efx.sdk1.EfxParser.AssetIdContext;
Expand Down Expand Up @@ -426,7 +428,20 @@ public void exitAssetId(AssetIdContext ctx) {
*/
@Override
public void exitStandardExpressionBlock(StandardExpressionBlockContext ctx) {
this.stack.push(this.stack.pop(Expression.class));
var expression = this.stack.pop(Expression.class);

// This is a hack to make sure that the date and time expressions are rendered in the correct
// format. We had to do this because EFX 1 does not support the format-date() and format-time()
// functions.
if (TypedExpression.class.isAssignableFrom(expression.getClass())) {
if (EfxDataType.Date.class.isAssignableFrom(((TypedExpression) expression).getDataType())) {
expression = new StringExpression("format-date(" + expression.getScript() + ", '[D01]/[M01]/[Y0001]')");
} else if (EfxDataType.Time.class.isAssignableFrom(((TypedExpression) expression).getDataType())) {
expression = new StringExpression("format-time(" + expression.getScript() + ", '[H01]:[m01] [Z]')");
}
}

this.stack.push(expression);
}

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,14 @@ void testEndOfLineComments() {
"let block01() -> { label(concat('field', '|', 'name', '|', 'BT-00-Text'))text(' ')text('blah blah') }\nfor-each(/*).call(block01())",
translateTemplate("{ND-Root} #{name|BT-00-Text} blah blah // comment blah blah"));
}

@Test
void testImplicitFormatting_Dates() {
assertEquals("let block01() -> { eval(format-date(PathNode/StartDateField/xs:date(text()), '[D01]/[M01]/[Y0001]')) }\nfor-each(/*).call(block01())", translateTemplate("{ND-Root} ${BT-00-StartDate}"));
}

@Test
void testImplicitFormatting_Times() {
assertEquals("let block01() -> { eval(format-time(PathNode/StartTimeField/xs:time(text()), '[H01]:[m01] [Z]')) }\nfor-each(/*).call(block01())", translateTemplate("{ND-Root} ${BT-00-StartTime}"));
}
}

0 comments on commit a177d31

Please sign in to comment.