Skip to content
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
8 changes: 4 additions & 4 deletions src/main/java/eu/europa/ted/efx/EfxTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, dependencyFactory, options)
.renderTemplate(pathname);
.renderTemplate(pathname, options);
}

public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
Expand All @@ -98,7 +98,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(pathname);
.renderTemplate(pathname, options);
}

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
final String qualifier, final String template, TranslatorOptions options)
throws InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(template);
.renderTemplate(template, options);
}

/**
Expand Down Expand Up @@ -164,7 +164,7 @@ public static String translateTemplate(final TranslatorDependencyFactory depende
final String qualifier, final InputStream stream, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(stream);
.renderTemplate(stream, options);
}

//#endregion Translate EFX templates ----------------------------------------
Expand Down
43 changes: 37 additions & 6 deletions src/main/java/eu/europa/ted/efx/EfxTranslatorOptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.europa.ted.efx;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -19,39 +20,59 @@ public class EfxTranslatorOptions implements TranslatorOptions {
*/
public static final String DEFAULT_UDF_NAMESPACE = "efx-udf";

/**
* Default value for EFX profiling enablement.
* By default, profiling is disabled for performance reasons.
*/
public static final boolean DEFAULT_PROFILER_ENABLED = false;

/**
* Default value for EFX profiling output path.
* By default, no profiling output file is generated.
*/
public static final Path DEFAULT_PROFILER_OUTPUT_PATH = null;

// Change to EfxDecimalFormatSymbols.EFX_DEFAULT to use the decimal format
// preferred by OP (space as thousands separator and comma as decimal separator).
public static final EfxTranslatorOptions DEFAULT = new EfxTranslatorOptions(DEFAULT_UDF_NAMESPACE, DecimalFormat.XSL_DEFAULT, Locale.ENGLISH);
public static final EfxTranslatorOptions DEFAULT = new EfxTranslatorOptions(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, DecimalFormat.XSL_DEFAULT, Locale.ENGLISH);

private final DecimalFormat symbols;
private final Locale primaryLocale;
private final ArrayList<Locale> otherLocales;
private final String userDefinedFunctionNamespace;
private final boolean profilerEnabled;
private final Path profilerOutputPath;

public EfxTranslatorOptions(DecimalFormat symbols) {
this(DEFAULT_UDF_NAMESPACE, symbols);
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, symbols, Locale.ENGLISH);
}

public EfxTranslatorOptions(String udfNamespace, DecimalFormat symbols) {
this(udfNamespace, symbols, Locale.ENGLISH);
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, udfNamespace, symbols, Locale.ENGLISH);
}

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

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

public EfxTranslatorOptions(DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
this(DEFAULT_UDF_NAMESPACE, symbols, primaryLocale, otherLocales);
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, DEFAULT_UDF_NAMESPACE, symbols, primaryLocale, otherLocales);
}

public EfxTranslatorOptions(String udfNamespace, DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
this(DEFAULT_PROFILER_ENABLED, DEFAULT_PROFILER_OUTPUT_PATH, udfNamespace, symbols, primaryLocale, otherLocales);
}

public EfxTranslatorOptions(boolean profilerEnabled, Path profilerOutputPath, String udfNamespace, DecimalFormat symbols, Locale primaryLocale, Locale... otherLocales) {
this.userDefinedFunctionNamespace = udfNamespace;
this.symbols = symbols;
this.primaryLocale = primaryLocale;
this.profilerEnabled = profilerEnabled;
this.profilerOutputPath = profilerOutputPath;
this.otherLocales = new ArrayList<>(Arrays.asList(otherLocales));
}

Expand Down Expand Up @@ -99,4 +120,14 @@ public String[] getAllLanguage3LetterCodes() {
public String getUserDefinedFunctionNamespace() {
return this.userDefinedFunctionNamespace;
}

@Override
public boolean isProfilerEnabled() {
return this.profilerEnabled;
}

@Override
public Path getProfilerOutputPath() {
return this.profilerOutputPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.InputStream;
import java.nio.file.Path;

import eu.europa.ted.efx.EfxTranslatorOptions;

/**
* Defines the API of an EFX template translator.
*
Expand All @@ -25,22 +27,55 @@
*/
public interface EfxTemplateTranslator extends EfxExpressionTranslator {

/**
* Translate the EFX template stored in a file, given the pathname of the file.
*
* @param pathname The path and filename of the EFX template file to translate.
* @param options The options to be used by the EFX template translator.
* @return A string containing the translated template.
* @throws IOException If the file cannot be read.
*/
String renderTemplate(Path pathname, TranslatorOptions options) throws IOException;

/**
* Translate the EFX template stored in a file, given the pathname of the file.
*
* @param pathname The path and filename of the EFX template file to translate.
* @return A string containing the translated template.
* @throws IOException If the file cannot be read.
*/
String renderTemplate(Path pathname) throws IOException;
default String renderTemplate(Path pathname) throws IOException {
return renderTemplate(pathname, EfxTranslatorOptions.DEFAULT);
}

/**
* Translate the EFX template stored in the given string.
*
* @param template A string containing an EFX template to be translated.
* @param options The options to be used by the EFX template translator.
* @return A string containing the translated template.
*/
String renderTemplate(String template, TranslatorOptions options);

/**
* Translate the EFX template stored in the given string.
*
* @param template A string containing an EFX template to be translated.
* @return A string containing the translated template.
*/
String renderTemplate(String template);
default String renderTemplate(String template) {
return renderTemplate(template, EfxTranslatorOptions.DEFAULT);
}

/**
* Translate the EFX template given as an InputStream.
*
* @param stream An InputStream with the EFX template to be translated.
* @param options The options to be used by the EFX template translator.
* @return A string containing the translated template.
* @throws IOException If the InputStream cannot be read.
*/
String renderTemplate(InputStream stream, TranslatorOptions options) throws IOException;

/**
* Translate the EFX template given as an InputStream.
Expand All @@ -49,5 +84,7 @@ public interface EfxTemplateTranslator extends EfxExpressionTranslator {
* @return A string containing the translated template.
* @throws IOException If the InputStream cannot be read.
*/
String renderTemplate(InputStream stream) throws IOException;
default String renderTemplate(InputStream stream) throws IOException {
return renderTemplate(stream, EfxTranslatorOptions.DEFAULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ default public Markup renderFragmentInvocation(final String name, final Set<Argu
* Given a fragment name (identifier), and an evaluation context, this method
* returns the code that invokes the fragment.
* @deprecated This method is deprecated and will be removed in future versions.
* Use {@link #renderFragmentInvocation(String, PathExpression, Set)} instead.
* Use {@link #renderFragmentInvocation(String, Set, TranslatorContext)} instead.
* We are keeping the method temporarily to prevent build errors.
* This method is being deprecated as of version 2.0.0-alpha.6 and
* will be removed before version 2.0.0 is released.
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/eu/europa/ted/efx/interfaces/TranslatorOptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.europa.ted.efx.interfaces;

import java.nio.file.Path;

import eu.europa.ted.efx.model.DecimalFormat;

public interface TranslatorOptions {
Expand All @@ -14,4 +16,18 @@ public interface TranslatorOptions {
public String[] getAllLanguage3LetterCodes();

public String getUserDefinedFunctionNamespace();

/**
* Returns whether EFX profiling is enabled for performance analysis.
*
* @return true if EFX profiling should be enabled, false otherwise
*/
public boolean isProfilerEnabled();

/**
* Returns the output path for EFX profiling results.
*
* @return Path where profiling results should be written, or null if no file output is desired
*/
public Path getProfilerOutputPath();
}
20 changes: 12 additions & 8 deletions src/main/java/eu/europa/ted/efx/sdk1/EfxTemplateTranslatorV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import eu.europa.ted.efx.interfaces.ScriptGenerator;
import eu.europa.ted.efx.interfaces.SymbolResolver;
import eu.europa.ted.efx.interfaces.TranslatorContext;
import eu.europa.ted.efx.interfaces.TranslatorOptions;
import eu.europa.ted.efx.model.Context;
import eu.europa.ted.efx.model.Context.FieldContext;
import eu.europa.ted.efx.model.Context.NodeContext;
Expand Down Expand Up @@ -125,27 +126,30 @@ public EfxTemplateTranslatorV1(final MarkupGenerator markupGenerator,
* Opens the indicated EFX file and translates the EFX template it contains.
*/
@Override
public String renderTemplate(final Path pathname) throws IOException {

return renderTemplate(CharStreams.fromPath(pathname));
public String renderTemplate(final Path pathname, TranslatorOptions options) throws IOException {
return renderTemplate(CharStreams.fromPath(pathname), options);
}

/**
* Translates the template contained in the string passed as a parameter.
*/
@Override
public String renderTemplate(final String template) {
return renderTemplate(CharStreams.fromString(template));
public String renderTemplate(final String template, TranslatorOptions options) {
return renderTemplate(CharStreams.fromString(template), options);
}

@Override
public String renderTemplate(final InputStream stream) throws IOException {
return renderTemplate(CharStreams.fromStream(stream));
public String renderTemplate(final InputStream stream, TranslatorOptions options) throws IOException {
return renderTemplate(CharStreams.fromStream(stream), options);
}

private String renderTemplate(final CharStream charStream) {
private String renderTemplate(final CharStream charStream, TranslatorOptions options) {
logger.debug("Rendering template");

if (options != null && options.isProfilerEnabled()) {
logger.warn("EFX profiling is not available for EFX-1 templates. No profiler output will be generated.");
}

final EfxLexer lexer = new EfxLexer(charStream);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final EfxParser parser = new EfxParser(tokens);
Expand Down
Loading