-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEfxTranslator.java
More file actions
334 lines (298 loc) · 17.3 KB
/
EfxTranslator.java
File metadata and controls
334 lines (298 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright 2022 European Union
*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European
* Commission – subsequent versions of the EUPL (the "Licence"); You may not use this work except in
* compliance with the Licence. You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence
* is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the Licence for the specific language governing permissions and limitations under
* the Licence.
*/
package eu.europa.ted.efx;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Map;
import java.util.Set;
import eu.europa.ted.efx.component.EfxTranslatorFactory;
import eu.europa.ted.efx.interfaces.TranslatorDependencyFactory;
import eu.europa.ted.efx.interfaces.TranslatorOptions;
import eu.europa.ted.efx.model.dependencies.DependencyGraph;
/**
* Provided for convenience, this class exposes static methods that allow you to quickly instantiate
* an EFX translator to translate EFX expressions and templates.
*/
public class EfxTranslator {
private EfxTranslator() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
private static TranslatorOptions defaultOptions = EfxTranslatorOptions.DEFAULT;
//#region Translate EFX expressions -----------------------------------------
/**
* Instantiates an EFX expression translator and translates a given expression.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX expression translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the
* expression to be translated.
* @param expression The EFX expression to translate.
* @param expressionParameters The values of any parameters that the EFX expression requires.
* @param options The options to be used by the EFX expression translator.
* @return The translated expression in the target script language supported by the given
* {@link TranslatorDependencyFactory}.
* @throws InstantiationException If the EFX expression translator cannot be instantiated.
*/
public static String translateExpression(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String expression, TranslatorOptions options, final String... expressionParameters)
throws InstantiationException {
return EfxTranslatorFactory.getEfxExpressionTranslator(sdkVersion, dependencyFactory, options)
.translateExpression(expression, expressionParameters);
}
public static String translateExpression(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String expression, final String... expressionParameters) throws InstantiationException {
return translateExpression(dependencyFactory, sdkVersion, expression, defaultOptions, expressionParameters);
}
//#endregion Translate EFX expressions --------------------------------------
//#region Translate EFX templates -------------------------------------------
/**
* Instantiates an EFX template translator and translates the EFX template contained in the given
* file.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX template translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* template to be translated.
* @param pathname The path to the file containing the EFX template to translate.
* @return The translated template in the target markup language supported by the given
* {@link TranslatorDependencyFactory}.
* @param options The options to be used by the EFX template translator.
* @throws IOException If the file cannot be read.
* @throws InstantiationException If the EFX template translator cannot be instantiated.
*/
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, dependencyFactory, options)
.renderTemplate(pathname, options);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final Path pathname)
throws IOException, InstantiationException {
return translateTemplate(dependencyFactory, sdkVersion, pathname, defaultOptions);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String qualifier,
final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(pathname, options);
}
/**
* Instantiates an EFX template translator and translates the given EFX template.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX template translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* template to be translated.
* @param template A string containing the EFX template to translate.
* @return The translated template in the target markup language supported by the given
* {@link TranslatorDependencyFactory}.
* @param options The options to be used by the EFX template translator.
* @throws InstantiationException If the EFX template translator cannot be instantiated.
*/
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String template, TranslatorOptions options)
throws InstantiationException {
return translateTemplate(dependencyFactory, sdkVersion, "", template, options);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String template)
throws InstantiationException {
return translateTemplate(dependencyFactory, sdkVersion, template, defaultOptions);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String qualifier, final String template, TranslatorOptions options)
throws InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(template, options);
}
/**
* Instantiates an EFX template translator and translates the EFX template contained in the given
* InputStream.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX template translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* template to be translated.
* @param stream An InputStream containing the EFX template to be translated.
* @return The translated template in the target markup language supported by the given
* {@link TranslatorDependencyFactory}.
* @param options The options to be used by the EFX template translator.
* @throws IOException If the InputStream cannot be read.
* @throws InstantiationException If the EFX template translator cannot be instantiated.
*/
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final InputStream stream, TranslatorOptions options)
throws IOException, InstantiationException {
return translateTemplate(dependencyFactory, sdkVersion, "", stream, options);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final InputStream stream)
throws IOException, InstantiationException {
return translateTemplate(dependencyFactory, sdkVersion, stream, defaultOptions);
}
public static String translateTemplate(final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String qualifier, final InputStream stream, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxTemplateTranslator(sdkVersion, qualifier, dependencyFactory, options)
.renderTemplate(stream, options);
}
//#endregion Translate EFX templates ----------------------------------------
//#region Translate EFX rules -----------------------------------------------
/**
* Instantiates an EFX rules translator and translates the EFX rules contained in the given file.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX rules translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* rules to be translated.
* @param pathname The path to the file containing the EFX rules to translate.
* @param options The options to be used by the EFX rules translator.
* @return A map where keys are output file paths (relative) and values are the generated Schematron content.
* @throws IOException If the file cannot be read.
* @throws InstantiationException If the EFX rules translator cannot be instantiated.
*/
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxRulesTranslator(sdkVersion, dependencyFactory, options)
.translateRules(pathname, options);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final Path pathname)
throws IOException, InstantiationException {
return translateRules(dependencyFactory, sdkVersion, pathname, defaultOptions);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String qualifier, final Path pathname, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxRulesTranslator(sdkVersion, qualifier, dependencyFactory, options)
.translateRules(pathname, options);
}
/**
* Instantiates an EFX rules translator and translates the given EFX rules.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX rules translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* rules to be translated.
* @param rules A string containing the EFX rules to translate.
* @param options The options to be used by the EFX rules translator.
* @return A map where keys are output file paths (relative) and values are the generated Schematron content.
* @throws InstantiationException If the EFX rules translator cannot be instantiated.
*/
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String rules, TranslatorOptions options)
throws InstantiationException {
return translateRules(dependencyFactory, sdkVersion, "", rules, options);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String rules)
throws InstantiationException {
return translateRules(dependencyFactory, sdkVersion, rules, defaultOptions);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String qualifier, final String rules, TranslatorOptions options)
throws InstantiationException {
return EfxTranslatorFactory.getEfxRulesTranslator(sdkVersion, qualifier, dependencyFactory, options)
.translateRules(rules, options);
}
/**
* Instantiates an EFX rules translator and translates the EFX rules contained in the given
* InputStream.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the EFX rules translator.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the EFX
* rules to be translated.
* @param stream An InputStream containing the EFX rules to be translated.
* @param options The options to be used by the EFX rules translator.
* @return A map where keys are output file paths (relative) and values are the generated Schematron content.
* @throws IOException If the InputStream cannot be read.
* @throws InstantiationException If the EFX rules translator cannot be instantiated.
*/
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final InputStream stream, TranslatorOptions options)
throws IOException, InstantiationException {
return translateRules(dependencyFactory, sdkVersion, "", stream, options);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final InputStream stream)
throws IOException, InstantiationException {
return translateRules(dependencyFactory, sdkVersion, stream, defaultOptions);
}
public static Map<String, String> translateRules(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String qualifier, final InputStream stream, TranslatorOptions options)
throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxRulesTranslator(sdkVersion, qualifier, dependencyFactory, options)
.translateRules(stream, options);
}
//#endregion Translate EFX rules --------------------------------------------
//#region Extract EFX dependencies -------------------------------------------
/**
* Instantiates an EFX compute dependency extractor and extracts all field and node identifiers
* referenced in the given expression.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the extractor.
* @param sdkVersion The version of the eForms SDK that defines the EFX grammar used by the
* expression to be analysed.
* @param expression The EFX expression to analyse.
* @return An unmodifiable set of field and node identifiers referenced in the expression.
* @throws InstantiationException If the dependency extractor cannot be instantiated.
*/
public static Set<String> extractComputeDependencies(final TranslatorDependencyFactory dependencyFactory,
final String sdkVersion, final String expression) throws InstantiationException {
return EfxTranslatorFactory.getEfxComputeDependencyExtractor(sdkVersion, dependencyFactory)
.extractDependencies(expression);
}
/**
* Instantiates an EFX validation dependency extractor and extracts the dependency graph
* from the given EFX rules string.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the extractor.
* @param sdkVersion The version of the eForms SDK.
* @param rules The EFX rules to analyse.
* @return A {@link DependencyGraph} with all dependencies and reverse dependencies.
* @throws InstantiationException If the dependency extractor cannot be instantiated.
*/
public static DependencyGraph extractValidationDependencies(
final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final String rules) throws InstantiationException {
return EfxTranslatorFactory.getEfxValidationDependencyExtractor(sdkVersion, dependencyFactory)
.extractDependencyGraph(rules);
}
/**
* Instantiates an EFX validation dependency extractor and extracts the dependency graph
* from the given EFX rules file.
*
* @param dependencyFactory A {@link TranslatorDependencyFactory} to be used for instantiating the
* dependencies of the extractor.
* @param sdkVersion The version of the eForms SDK.
* @param pathname The path to the EFX rules file.
* @return A {@link DependencyGraph} with all dependencies and reverse dependencies.
* @throws IOException If the file cannot be read.
* @throws InstantiationException If the dependency extractor cannot be instantiated.
*/
public static DependencyGraph extractValidationDependencies(
final TranslatorDependencyFactory dependencyFactory, final String sdkVersion,
final Path pathname) throws IOException, InstantiationException {
return EfxTranslatorFactory.getEfxValidationDependencyExtractor(sdkVersion, dependencyFactory)
.extractDependencyGraph(pathname);
}
//#endregion Extract EFX dependencies ----------------------------------------
}