Skip to content

Commit 0eae577

Browse files
committed
TEDEFO-4559-4561: Enhance EFX template translation with profiling options and reporting.
- Updated EfxTranslator and EfxTemplateTranslator interfaces to accept TranslatorOptions for profiling. - Introduced EfxProfilerReportGenerator for generating HTML profiling reports. - Added TranslatorTimings class to hold timing measurements for processing phases. - Enhanced EfxTemplateTranslatorV2 to support profiling and logging of translation times. - Updated EfxTranslatorOptions to include profiling settings.
1 parent 62d0153 commit 0eae577

File tree

8 files changed

+423
-29
lines changed

8 files changed

+423
-29
lines changed

src/main/java/eu/europa/ted/efx/EfxTranslator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
8484
final Path pathname, TranslatorOptions options)
8585
throws IOException, InstantiationException {
8686
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, dependencyFactory, options)
87-
.renderTemplate(pathname);
87+
.renderTemplate(pathname, options);
8888
}
8989

9090
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
@@ -98,7 +98,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
9898
final Path pathname, TranslatorOptions options)
9999
throws IOException, InstantiationException {
100100
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
101-
.renderTemplate(pathname);
101+
.renderTemplate(pathname, options);
102102
}
103103

104104
/**
@@ -130,7 +130,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
130130
final String qualifier, final String template, TranslatorOptions options)
131131
throws InstantiationException {
132132
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
133-
.renderTemplate(template);
133+
.renderTemplate(template, options);
134134
}
135135

136136
/**
@@ -164,7 +164,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
164164
final String qualifier, final InputStream stream, TranslatorOptions options)
165165
throws IOException, InstantiationException {
166166
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
167-
.renderTemplate(stream);
167+
.renderTemplate(stream, options);
168168
}
169169

170170
//#endregion Translate EFX templates ----------------------------------------

src/main/java/eu/europa/ted/efx/EfxTranslatorOptions.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.europa.ted.efx;
22

3+
import java.nio.file.Path;
34
import java.util.ArrayList;
45
import java.util.Arrays;
56
import java.util.List;
@@ -19,39 +20,59 @@ public class EfxTranslatorOptions implements TranslatorOptions {
1920
*/
2021
public static final String DEFAULT_UDF_NAMESPACE = "efx-udf";
2122

23+
/**
24+
* Default value for EFX profiling enablement.
25+
* By default, profiling is disabled for performance reasons.
26+
*/
27+
public static final boolean DEFAULT_PROFILER_ENABLED = false;
28+
29+
/**
30+
* Default value for EFX profiling output path.
31+
* By default, no profiling output file is generated.
32+
*/
33+
public static final Path DEFAULT_PROFILER_OUTPUT_PATH = null;
34+
2235
// Change to EfxDecimalFormatSymbols.EFX_DEFAULT to use the decimal format
2336
// preferred by OP (space as thousands separator and comma as decimal separator).
24-
public static final EfxTranslatorOptions DEFAULT = new EfxTranslatorOptions(DEFAULT_UDF_NAMESPACE, DecimalFormat.XSL_DEFAULT, Locale.ENGLISH);
37+
public static final EfxTranslatorOptions DEFAULT = new EfxTranslatorOptions(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, DecimalFormat.XSL_DEFAULT, Locale.ENGLISH);
2538

2639
private final DecimalFormat symbols;
2740
private final Locale primaryLocale;
2841
private final ArrayList<Locale> otherLocales;
2942
private final String userDefinedFunctionNamespace;
43+
private final boolean profilerEnabled;
44+
private final Path profilerOutputPath;
3045

3146
public EfxTranslatorOptions(DecimalFormat symbols) {
32-
this(DEFAULT_UDF_NAMESPACE, symbols);
47+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, symbols, Locale.ENGLISH);
3348
}
3449

3550
public EfxTranslatorOptions(String udfNamespace, DecimalFormat symbols) {
36-
this(udfNamespace, symbols, Locale.ENGLISH);
51+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, udfNamespace, symbols, Locale.ENGLISH);
3752
}
3853

3954
public EfxTranslatorOptions(DecimalFormat symbols, String primaryLanguage, String... otherLanguages) {
40-
this(symbols, Locale.forLanguageTag(primaryLanguage), Arrays.stream(otherLanguages).map(Locale::forLanguageTag).toArray(Locale[]::new));
55+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, symbols, Locale.forLanguageTag(primaryLanguage), Arrays.stream(otherLanguages).map(Locale::forLanguageTag).toArray(Locale[]::new));
4156
}
4257

4358
public EfxTranslatorOptions(String udfNamespace, DecimalFormat symbols, String primaryLanguage, String... otherLanguages) {
44-
this(udfNamespace, symbols, Locale.forLanguageTag(primaryLanguage), Arrays.stream(otherLanguages).map(Locale::forLanguageTag).toArray(Locale[]::new));
59+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, udfNamespace, symbols, Locale.forLanguageTag(primaryLanguage), Arrays.stream(otherLanguages).map(Locale::forLanguageTag).toArray(Locale[]::new));
4560
}
4661

4762
public EfxTranslatorOptions(DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
48-
this(DEFAULT_UDF_NAMESPACE, symbols, primaryLocale, otherLocales);
63+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, symbols, primaryLocale, otherLocales);
4964
}
5065

5166
public EfxTranslatorOptions(String udfNamespace, DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
67+
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, udfNamespace, symbols, primaryLocale, otherLocales);
68+
}
69+
70+
public EfxTranslatorOptions(boolean profilerEnabled, Path profilerOutputPath, String udfNamespace, DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
5271
this.userDefinedFunctionNamespace = udfNamespace;
5372
this.symbols = symbols;
5473
this.primaryLocale = primaryLocale;
74+
this.profilerEnabled = profilerEnabled;
75+
this.profilerOutputPath = profilerOutputPath;
5576
this.otherLocales = new ArrayList<>(Arrays.asList(otherLocales));
5677
}
5778

@@ -99,4 +120,14 @@ public String[] getAllLanguage3LetterCodes() {
99120
public String getUserDefinedFunctionNamespace() {
100121
return this.userDefinedFunctionNamespace;
101122
}
123+
124+
@Override
125+
public boolean isProfilerEnabled() {
126+
return this.profilerEnabled;
127+
}
128+
129+
@Override
130+
public Path getProfilerOutputPath() {
131+
return this.profilerOutputPath;
132+
}
102133
}

src/main/java/eu/europa/ted/efx/interfaces/EfxTemplateTranslator.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.InputStream;
1818
import java.nio.file.Path;
1919

20+
import eu.europa.ted.efx.EfxTranslatorOptions;
21+
2022
/**
2123
* Defines the API of an EFX template translator.
2224
*
@@ -25,22 +27,55 @@
2527
*/
2628
public interface EfxTemplateTranslator extends EfxExpressionTranslator {
2729

30+
/**
31+
* Translate the EFX template stored in a file, given the pathname of the file.
32+
*
33+
* @param pathname The path and filename of the EFX template file to translate.
34+
* @param options The options to be used by the EFX template translator.
35+
* @return A string containing the translated template.
36+
* @throws IOException If the file cannot be read.
37+
*/
38+
String renderTemplate(Path pathname, TranslatorOptions options) throws IOException;
39+
2840
/**
2941
* Translate the EFX template stored in a file, given the pathname of the file.
3042
*
3143
* @param pathname The path and filename of the EFX template file to translate.
3244
* @return A string containing the translated template.
3345
* @throws IOException If the file cannot be read.
3446
*/
35-
String renderTemplate(Path pathname) throws IOException;
47+
default String renderTemplate(Path pathname) throws IOException {
48+
return renderTemplate(pathname, EfxTranslatorOptions.DEFAULT);
49+
}
50+
51+
/**
52+
* Translate the EFX template stored in the given string.
53+
*
54+
* @param template A string containing an EFX template to be translated.
55+
* @param options The options to be used by the EFX template translator.
56+
* @return A string containing the translated template.
57+
*/
58+
String renderTemplate(String template, TranslatorOptions options);
3659

3760
/**
3861
* Translate the EFX template stored in the given string.
3962
*
4063
* @param template A string containing an EFX template to be translated.
4164
* @return A string containing the translated template.
4265
*/
43-
String renderTemplate(String template);
66+
default String renderTemplate(String template) {
67+
return renderTemplate(template, EfxTranslatorOptions.DEFAULT);
68+
}
69+
70+
/**
71+
* Translate the EFX template given as an InputStream.
72+
*
73+
* @param stream An InputStream with the EFX template to be translated.
74+
* @param options The options to be used by the EFX template translator.
75+
* @return A string containing the translated template.
76+
* @throws IOException If the InputStream cannot be read.
77+
*/
78+
String renderTemplate(InputStream stream, TranslatorOptions options) throws IOException;
4479

4580
/**
4681
* Translate the EFX template given as an InputStream.
@@ -49,5 +84,7 @@ public interface EfxTemplateTranslator extends EfxExpressionTranslator {
4984
* @return A string containing the translated template.
5085
* @throws IOException If the InputStream cannot be read.
5186
*/
52-
String renderTemplate(InputStream stream) throws IOException;
87+
default String renderTemplate(InputStream stream) throws IOException {
88+
return renderTemplate(stream, EfxTranslatorOptions.DEFAULT);
89+
}
5390
}

src/main/java/eu/europa/ted/efx/interfaces/TranslatorOptions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.europa.ted.efx.interfaces;
22

3+
import java.nio.file.Path;
4+
35
import eu.europa.ted.efx.model.DecimalFormat;
46

57
public interface TranslatorOptions {
@@ -14,4 +16,18 @@ public interface TranslatorOptions {
1416
public String[] getAllLanguage3LetterCodes();
1517

1618
public String getUserDefinedFunctionNamespace();
19+
20+
/**
21+
* Returns whether EFX profiling is enabled for performance analysis.
22+
*
23+
* @return true if EFX profiling should be enabled, false otherwise
24+
*/
25+
public boolean isProfilerEnabled();
26+
27+
/**
28+
* Returns the output path for EFX profiling results.
29+
*
30+
* @return Path where profiling results should be written, or null if no file output is desired
31+
*/
32+
public Path getProfilerOutputPath();
1733
}

src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import eu.europa.ted.efx.interfaces.ScriptGenerator;
2424
import eu.europa.ted.efx.interfaces.SymbolResolver;
2525
import eu.europa.ted.efx.interfaces.TranslatorContext;
26+
import eu.europa.ted.efx.interfaces.TranslatorOptions;
2627
import eu.europa.ted.efx.model.Context;
2728
import eu.europa.ted.efx.model.Context.FieldContext;
2829
import eu.europa.ted.efx.model.Context.NodeContext;
@@ -125,27 +126,30 @@ public EfxTemplateTranslatorV1(final MarkupGenerator markupGenerator,
125126
* Opens the indicated EFX file and translates the EFX template it contains.
126127
*/
127128
@Override
128-
public String renderTemplate(final Path pathname) throws IOException {
129-
130-
return renderTemplate(CharStreams.fromPath(pathname));
129+
public String renderTemplate(final Path pathname, TranslatorOptions options) throws IOException {
130+
return renderTemplate(CharStreams.fromPath(pathname), options);
131131
}
132132

133133
/**
134134
* Translates the template contained in the string passed as a parameter.
135135
*/
136136
@Override
137-
public String renderTemplate(final String template) {
138-
return renderTemplate(CharStreams.fromString(template));
137+
public String renderTemplate(final String template, TranslatorOptions options) {
138+
return renderTemplate(CharStreams.fromString(template), options);
139139
}
140140

141141
@Override
142-
public String renderTemplate(final InputStream stream) throws IOException {
143-
return renderTemplate(CharStreams.fromStream(stream));
142+
public String renderTemplate(final InputStream stream, TranslatorOptions options) throws IOException {
143+
return renderTemplate(CharStreams.fromStream(stream), options);
144144
}
145145

146-
private String renderTemplate(final CharStream charStream) {
146+
private String renderTemplate(final CharStream charStream, TranslatorOptions options) {
147147
logger.debug("Rendering template");
148148

149+
if (options != null && options.isProfilerEnabled()) {
150+
logger.warn("EFX profiling is not available for EFX-1 templates. No profiler output will be generated.");
151+
}
152+
149153
final EfxLexer lexer = new EfxLexer(charStream);
150154
final CommonTokenStream tokens = new CommonTokenStream(lexer);
151155
final EfxParser parser = new EfxParser(tokens);

0 commit comments

Comments
 (0)