Skip to content

Commit

Permalink
Merge pull request #37 from OP-TED/feature/TEDEFO-2520-support-efx-2
Browse files Browse the repository at this point in the history
Add support for validating EFX-2 expressions and templates (TEDEFO-2520)
  • Loading branch information
bertrand-lorentz authored Jul 28, 2023
2 parents a333463 + dabf212 commit 9f9ec48
Show file tree
Hide file tree
Showing 37 changed files with 10,231 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<maven.compiler.target>${java.version}</maven.compiler.target>

<version.eforms-core-java>1.0.5</version.eforms-core-java>
<version.efx-toolkit>1.2.0</version.efx-toolkit>
<version.efx-toolkit>2.0.0-SNAPSHOT</version.efx-toolkit>

<!-- Version - Third-party libraries -->
<version.commons-collections4>4.4</version.commons-collections4>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package eu.europa.ted.eforms.sdk.analysis.efx.mock;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import eu.europa.ted.efx.interfaces.MarkupGenerator;
import eu.europa.ted.efx.model.Expression;
import eu.europa.ted.efx.model.Expression.PathExpression;
import eu.europa.ted.efx.model.Expression.StringExpression;
import eu.europa.ted.efx.model.Markup;
import eu.europa.ted.efx.model.expressions.Expression;
import eu.europa.ted.efx.model.expressions.path.PathExpression;
import eu.europa.ted.efx.model.expressions.scalar.NumericExpression;
import eu.europa.ted.efx.model.expressions.scalar.StringExpression;
import eu.europa.ted.efx.model.templates.Markup;

public class MarkupGeneratorMock implements MarkupGenerator {
@Override
public Markup renderVariableExpression(Expression valueReference) {
return new Markup(String.format("eval(%s)", valueReference.script));
return new Markup(String.format("eval(%s)", valueReference.getScript()));
}

@Override
public Markup renderLabelFromKey(StringExpression key) {
return new Markup(String.format("label(%s)", key.script));
return new Markup(String.format("label(%s)", key.getScript()));
}

@Override
public Markup renderLabelFromExpression(Expression expression) {
return new Markup(String.format("label(%s)", expression.script));
return new Markup(String.format("label(%s)", expression.getScript()));
}

@Override
Expand All @@ -34,10 +34,6 @@ public Markup renderFreeText(String freeText) {
}

@Override
public Markup composeFragmentDefinition(String name, String number, Markup content) {
return this.composeFragmentDefinition(name, number, content, new LinkedHashSet<>());
}

public Markup composeFragmentDefinition(String name, String number, Markup content,
Set<String> parameters) {
if (StringUtils.isBlank(number)) {
Expand All @@ -49,13 +45,9 @@ public Markup composeFragmentDefinition(String name, String number, Markup conte
}

@Override
public Markup renderFragmentInvocation(String name, PathExpression context) {
return this.renderFragmentInvocation(name, context, new LinkedHashSet<>());
}

public Markup renderFragmentInvocation(String name, PathExpression context,
Set<Pair<String, String>> variables) {
return new Markup(String.format("for-each(%s).call(%s(%s))", context.script, name,
return new Markup(String.format("for-each(%s).call(%s(%s))", context.getScript(), name,
variables.stream()
.map(v -> String.format("%s:%s", v.getLeft(), v.getRight()))
.collect(Collectors.joining(", "))));
Expand All @@ -67,4 +59,25 @@ public Markup composeOutputFile(List<Markup> body, List<Markup> templates) {
templates.stream().map(t -> t.script).collect(Collectors.joining("\n")),
body.stream().map(t -> t.script).collect(Collectors.joining("\n"))));
}

@Override
public Markup renderLabelFromKey(StringExpression key, NumericExpression quantity) {
if (quantity.isEmpty()) {
return new Markup(String.format("label(%s)", key.getScript()));
}
return new Markup(String.format("label(%s, %s)", key.getScript(), quantity.getScript()));
}

@Override
public Markup renderLabelFromExpression(Expression expression, NumericExpression quantity) {
if (quantity.isEmpty()) {
return new Markup(String.format("label(%s)", expression.getScript()));
}
return new Markup(String.format("label(%s, %s)", expression.getScript(), quantity.getScript()));
}

@Override
public Markup renderLineBreak() {
return new Markup("<line-break>");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import eu.europa.ted.efx.xpath.XPath20BaseListener;
import eu.europa.ted.efx.xpath.XPath20Lexer;
import eu.europa.ted.efx.xpath.XPath20Parser;
import eu.europa.ted.efx.xpath.XPath20Parser.AxisstepContext;
import eu.europa.ted.efx.xpath.XPath20Parser.FilterexprContext;
import eu.europa.ted.efx.xpath.XPath20Parser.PredicateContext;
import eu.europa.ted.efx.xpath.XPath20Parser.StepexprContext;;

public class XPathSplitter extends XPath20BaseListener {

Expand Down Expand Up @@ -81,9 +82,44 @@ private Boolean inPredicateMode() {
}

@Override
public void exitStepexpr(StepexprContext ctx) {
if (!inPredicateMode()) {
this.steps.offer(new StepInfo(ctx, this::getInputText));
public void exitAxisstep(AxisstepContext ctx) {
if (inPredicateMode()) {
return;
}

// When we recognize a step, we add it to the queue if is is empty.
// If the queue is not empty, and the depth of the new step is not smaller than
// the depth of the last step in the queue, then this step needs to be added to
// the queue too.
// Otherwise, the last step in the queue is a sub-expression of the new step,
// and we need to
// replace it in the queue with the new step.
if (this.steps.isEmpty() || !this.steps.getLast().isPartOf(ctx.getSourceInterval())) {
this.steps.offer(new AxisStepInfo(ctx, this::getInputText));
} else {
Interval removedInterval = ctx.getSourceInterval();
while(!this.steps.isEmpty() && this.steps.getLast().isPartOf(removedInterval)) {
this.steps.removeLast();
}
this.steps.offer(new AxisStepInfo(ctx, this::getInputText));
}
}

@Override
public void exitFilterexpr(FilterexprContext ctx) {
if (inPredicateMode()) {
return;
}

// Same logic as for axis steps here (sse exitAxisstep).
if (this.steps.isEmpty() || !this.steps.getLast().isPartOf(ctx.getSourceInterval())) {
this.steps.offer(new FilterStepInfo(ctx, this::getInputText));
} else {
Interval removedInterval = ctx.getSourceInterval();
while(!this.steps.isEmpty() && this.steps.getLast().isPartOf(removedInterval)) {
this.steps.removeLast();
}
this.steps.offer(new FilterStepInfo(ctx, this::getInputText));
}
}

Expand All @@ -97,18 +133,28 @@ public void exitPredicate(PredicateContext ctx) {
this.predicateMode--;
}

public class AxisStepInfo extends StepInfo {

public AxisStepInfo(AxisstepContext ctx, Function<ParserRuleContext, String> getInputText) {
super(ctx.reversestep() != null? getInputText.apply(ctx.reversestep()) : getInputText.apply(ctx.forwardstep()),
ctx.predicatelist().predicate().stream().map(getInputText).collect(Collectors.toList()), ctx.getSourceInterval());
}
}

public class FilterStepInfo extends StepInfo {

public FilterStepInfo(FilterexprContext ctx, Function<ParserRuleContext, String> getInputText) {
super(getInputText.apply(ctx.primaryexpr()),
ctx.predicatelist().predicate().stream().map(getInputText).collect(Collectors.toList()), ctx.getSourceInterval());
}
}

public class StepInfo {
String stepText;
List<String> predicates;
int a;
int b;

public StepInfo(StepexprContext ctx, Function<ParserRuleContext, String> getInputText) {
this.stepText = getInputText.apply(ctx.step());
this.predicates =
ctx.predicatelist().predicate().stream().map(getInputText).collect(Collectors.toList());
}

protected StepInfo(String stepText, List<String> predicates, Interval interval) {
this.stepText = stepText;
this.predicates = predicates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import eu.europa.ted.efx.interfaces.ScriptGenerator;
import eu.europa.ted.efx.interfaces.SymbolResolver;
import eu.europa.ted.efx.interfaces.TranslatorDependencyFactory;
import eu.europa.ted.efx.interfaces.TranslatorOptions;

/**
* Validates EFX expressions and templates
Expand Down Expand Up @@ -144,16 +145,18 @@ public SymbolResolver createSymbolResolver(final String sdkVersion) {
}

@Override
public ScriptGenerator createScriptGenerator(final String sdkVersion) {
public ScriptGenerator createScriptGenerator(final String sdkVersion,
final TranslatorOptions options) {
try {
return ComponentFactory.getScriptGenerator(sdkVersion);
return ComponentFactory.getScriptGenerator(sdkVersion, options);
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Override
public MarkupGenerator createMarkupGenerator(final String sdkVersion) {
public MarkupGenerator createMarkupGenerator(final String sdkVersion,
final TranslatorOptions options) {
return new MarkupGeneratorMock();
}

Expand Down
169 changes: 169 additions & 0 deletions src/test/resources/eforms-sdk-tests/efx-2/codelists/codelists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"ublVersion" : "2.3",
"sdkVersion" : "1.6.0",
"metadataDatabase" : {
"version" : "1.6.0",
"createdOn" : "2023-02-17T12:00:00"
},
"codelists" : [ {
"id" : "bri",
"parentId" : "notice-type",
"filename" : "notice-type_bri.gc",
"description" : "List of allowed Notice Type codes for a Business Register Information document",
"_label" : "codelist|name|bri"
}, {
"id" : "brin-ecs",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_brin-ecs.gc",
"description" : "List of allowed Notice Subtype codes for a notice concerning a European Company or a European Cooperative Society registration processing.",
"_label" : "codelist|name|brin-ecs"
}, {
"id" : "brin-eeig",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_brin-eeig.gc",
"description" : "List of allowed Notice Subtype codes for a notice concerning a European Economic Interest Grouping registration processing.",
"_label" : "codelist|name|brin-eeig"
}, {
"id" : "can-desg",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_can-desg.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CAN Design Notice Type.",
"_label" : "codelist|name|can-desg"
}, {
"id" : "can-modif",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_can-modif.gc",
"description" : "List of allowed Notice Subtype codes for a notice of Contract Modification Notice Type .",
"_label" : "codelist|name|can-modif"
}, {
"id" : "can-social",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_can-social.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CAN Social Notice Type .",
"_label" : "codelist|name|can-social"
}, {
"id" : "can-standard",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_can-standard.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CAN Standard Notice Type .",
"_label" : "codelist|name|can-standard"
}, {
"id" : "can-tran",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_can-tran.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CAN Public Transport Service Notice Type .",
"_label" : "codelist|name|can-tran"
}, {
"id" : "cn-desg",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_cn-desg.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CN Design Notice Type.",
"_label" : "codelist|name|cn-desg"
}, {
"id" : "cn-social",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_cn-social.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CN Social Notice Type.",
"_label" : "codelist|name|cn-social"
}, {
"id" : "cn-standard",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_cn-standard.gc",
"description" : "List of allowed Notice Subtype codes for a notice of CN Standard Design Notice Type.",
"_label" : "codelist|name|cn-standard"
}, {
"id" : "competition",
"parentId" : "notice-type",
"filename" : "notice-type_competition.gc",
"description" : "List of allowed Notice Type codes for a notice of Competition Form Type",
"_label" : "codelist|name|competition"
}, {
"id" : "cont-modif",
"parentId" : "notice-type",
"filename" : "notice-type_cont-modif.gc",
"description" : "List of allowed Notice Type codes for a notice of Contract Modification Form Type",
"_label" : "codelist|name|cont-modif"
}, {
"id" : "dir-awa-pre",
"parentId" : "notice-type",
"filename" : "notice-type_dir-awa-pre.gc",
"description" : "List of allowed Notice Type codes for a Direct Award Preannouncement Form Type",
"_label" : "codelist|name|dir-awa-pre"
}, {
"id" : "notice-subtype",
"filename" : "notice-subtype.gc",
"description" : "List of possible Notice Subtype codes for a Notice of type Business Registration Information Notice.",
"_label" : "codelist|name|notice-subtype"
}, {
"id" : "notice-type",
"filename" : "notice-type.gc",
"description" : "This table provides a list of public procurement notices according to procurement legislation published once a project is approved.",
"_label" : "codelist|name|notice-type"
}, {
"id" : "pin-buyer",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-buyer.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN Buyer Notice Type.",
"_label" : "codelist|name|pin-buyer"
}, {
"id" : "pin-cfc-social",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-cfc-social.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN Call for Competition Social Notice Type.",
"_label" : "codelist|name|pin-cfc-social"
}, {
"id" : "pin-cfc-standard",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-cfc-standard.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN Call for Competition Standard Notice Type.",
"_label" : "codelist|name|pin-cfc-standard"
}, {
"id" : "pin-only",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-only.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN Only Notice Type.",
"_label" : "codelist|name|pin-only"
}, {
"id" : "pin-rtl",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-rtl.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN Reduced Time Limit Notice Type.",
"_label" : "codelist|name|pin-rtl"
}, {
"id" : "pin-tran",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_pin-tran.gc",
"description" : "List of allowed Notice Subtype codes for a notice of PIN for Public Transportation Service Contract Notice Type.",
"_label" : "codelist|name|pin-tran"
}, {
"id" : "planning",
"parentId" : "notice-type",
"filename" : "notice-type_planning.gc",
"description" : "List of allowed Notice Type codes for a Planning Form Type",
"_label" : "codelist|name|planning"
}, {
"id" : "qu-sy",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_qu-sy.gc",
"description" : "List of allowed Notice Subtype codes for a notice of Qualification System Notice Type.",
"_label" : "codelist|name|qu-sy"
}, {
"id" : "result",
"parentId" : "notice-type",
"filename" : "notice-type_result.gc",
"description" : "List of allowed Notice Type codes for a Result Form Type",
"_label" : "codelist|name|result"
}, {
"id" : "subco",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_subco.gc",
"description" : "List of allowed Notice Subtype codes for a notice of Subcontracting Notice Type.",
"_label" : "codelist|name|subco"
}, {
"id" : "veat",
"parentId" : "notice-subtype",
"filename" : "notice-subtype_veat.gc",
"description" : "List of allowed Notice Subtype codes for a notice of Voluntary ex-ante Transparency Notice Type.",
"_label" : "codelist|name|veat"
} ]
}
Loading

0 comments on commit 9f9ec48

Please sign in to comment.