-
Notifications
You must be signed in to change notification settings - Fork 3
/
standaloneMain.mtl
511 lines (460 loc) · 17.7 KB
/
standaloneMain.mtl
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
[comment encoding = UTF-8 /]
[**
* Generate the Java main class for a main module.
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
* @version 1.0.0
* @since 1.0.0
*/]
[module standaloneMain('http://www.eclipse.org/acceleo/4.0')/]
[import org::eclipse::acceleo::aql::ide::ui::module::requests::requests/]
[import org::eclipse::acceleo::aql::ide::ui::module::services::Services/]
[**
* Generate the Java main class for a main module.
* @param module the module containing a main template
*/]
[template public standaloneMainFile(module : acceleo::Module)]
[file (module.getClassName() + '.java', overwrite, 'UTF-8')]
[protected ('copyright') startTagPrefix('//') endTagPrefix('//')]
[/protected]
[let package = module.getJavaPackage()]
[if (package.size() <> 0)]
package [package/];
[/if]
[/let]
[protected ('imports') startTagPrefix('//') endTagPrefix('//')]
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.aql.evaluation.AcceleoEvaluator;
import org.eclipse.acceleo.aql.evaluation.GenerationResult;
import org.eclipse.acceleo.aql.evaluation.strategy.DefaultGenerationStrategy;
import org.eclipse.acceleo.aql.evaluation.strategy.DefaultWriterFactory;
import org.eclipse.acceleo.aql.evaluation.strategy.IAcceleoGenerationStrategy;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.aql.parser.ModuleLoader;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
import org.eclipse.acceleo.query.ast.TypeLiteral;
import org.eclipse.acceleo.query.runtime.impl.namespace.ClassLoaderQualifiedNameResolver;
import org.eclipse.acceleo.query.runtime.impl.namespace.JavaLoader;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameQueryEnvironment;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
[module.getAllEPackages().getJavaImport()->sep('\n')/]
[/protected]
/**
* Standalone launcher for [module.getQualifiedName()/].
*
* @author [''.getUserName()/]
* @generated
*/
public class [module.getClassName()/] {
/**
* The {@link List} of resources to load.
*
* @generated
*/
protected final List<String> resources;
/**
* The target folder for the generation.
*
* @generated
*/
protected final String target;
/**
* Constructor.
*
* @param resources
* the {@link List} of model resources to load
* @param target
* the target folder for the generation
* @generated
*/
public MainGenerator(List<String> resources, String target) {
this.resources = resources;
this.target = target;
}
/**
* Main entry point.
*
* @param args
* resources separated by a comma, target folder
*/
public static void main(String['['/]] args) {
if (args.length == 2) {
final List<String> resources = new ArrayList<>();
for (String resource : args['['/]0].split(",")) {
resources.add(resource.trim());
}
final String target = args['['/]1];
final [module.getClassName()/] generator = new [module.getClassName()/](resources, target);
generator.generate();
} else {
printUsage();
}
}
/**
* Print the usage.
*
* @generated
*/
private static void printUsage() {
System.out.println("Usage: <resources> <target>");
System.out.println("Example: model1.xmi,model2.xmi src-gen/");
}
/**
* Registers the given {@link EPackage} in the given {@link IQualifiedNameQueryEnvironment} recursively.
*
* @param environment
* the {@link IQualifiedNameQueryEnvironment}
* @param ePackage
* the {@link EPackage}
* @generated
*/
private static void registerEPackage(IQualifiedNameQueryEnvironment environment, EPackage ePackage) {
environment.registerEPackage(ePackage);
for (EPackage child : ePackage.getESubpackages()) {
registerEPackage(environment, child);
}
}
/**
* Generates.
*
* @generated
*/
public void generate() {
// inputs
final String moduleQualifiedName = getModuleQualifiedName();
final URI targetURI = getTargetURI(target);
final Map<String, String> options = getOptions();
// create the resource set used to load models
final Object generationKey = new Object();
final List<Exception> exceptions = new ArrayList<>();
final ResourceSet resourceSet = createDefaultResourceSet();
final ResourceSet resourceSetForModels = createResourceSetForModel(generationKey, options, exceptions,
resourceSet);
// load models
standaloneInitialization(resourceSetForModels);
loadResources(resourceSetForModels, resources);
// prepare Acceleo environment
final IQualifiedNameResolver resolver = createResolver();
final IQualifiedNameQueryEnvironment queryEnvironment = createAcceleoQueryEnvironment(options,
resolver, resourceSetForModels);
AcceleoEvaluator evaluator = createAcceleoEvaluator(resolver, queryEnvironment);
final IAcceleoGenerationStrategy strategy = createGenerationStrategy(resourceSetForModels);
final Module module = (Module)resolver.resolve(moduleQualifiedName);
final URI logURI = AcceleoUtil.getlogURI(targetURI, options.get(AcceleoUtil.LOG_URI_OPTION));
beforeGeneration(evaluator, queryEnvironment, module, resourceSetForModels, strategy, targetURI,
logURI);
try {
final Map<EClass, List<EObject>> valuesCache = new LinkedHashMap<>();
for (Template template : getTemplates(module)) {
final EClassifierTypeLiteral eClassifierTypeLiteral = (EClassifierTypeLiteral)template
.getParameters().get(0).getType().getAst();
final List<EObject> values = getValues(queryEnvironment, valuesCache, eClassifierTypeLiteral,
resourceSetForModels);
final String parameterName = template.getParameters().get(0).getName();
Map<String, Object> variables = new LinkedHashMap<>();
for (EObject value : values) {
variables.put(parameterName, value);
AcceleoUtil.generate(template, variables, evaluator, queryEnvironment, strategy,
targetURI, logURI);
}
}
} finally {
AQLUtils.cleanResourceSetForModels(generationKey, resourceSetForModels);
AcceleoUtil.cleanServices(queryEnvironment, resourceSetForModels);
printDiagnostics(evaluator.getGenerationResult());
afterGeneration(evaluator.getGenerationResult());
}
}
/**
* Gets the {@link List} of {@link Template} to generate for the given {@link Module}.
*
* @param module
* the {@link Module}
* @return the {@link List} of {@link Template} to generate for the given {@link Module}
*/
protected List<Template> getTemplates(Module module) {
return AcceleoUtil.getMainTemplate(module);
}
/**
* Gets the {@link List} of {@link EObject} values to use.
*
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param valuesCache
* the cache for any previous values
* @param type
* the {@link TypeLiteral}
* @param resourceSetForModels
* the {@link ResourceServices} for models
* @return the {@link List} of {@link EObject} values to use
*/
protected List<EObject> getValues(IQualifiedNameQueryEnvironment queryEnvironment,
final Map<EClass, List<EObject>> valuesCache, TypeLiteral type,
ResourceSet resourceSetForModels) {
final List<EObject> values = AcceleoUtil.getValues(type, queryEnvironment, resourceSetForModels
.getResources(), valuesCache);
return values;
}
/**
* Gets the module qualified name.
*
* @return the module qualified name
* @generated
*/
protected String getModuleQualifiedName() {
return "[module.getQualifiedName()/]";
}
/**
* Gets the target folder {@link URI}.
*
* @param target
* the target folder {@link String}.
* @return the target folder {@link URI}
* @generated
*/
protected URI getTargetURI(String target) {
return URI.createFileURI(new File(target).getAbsolutePath() + "/");
}
/**
* Gets the {@link Map} of options for the generation.
*
* @return the {@link Map} of options for the generation
* @generated
*/
protected Map<String, String> getOptions() {
Map<String, String> res = new LinkedHashMap<>();
res.put(AcceleoUtil.LOG_URI_OPTION, "acceleo.log");
res.put(AcceleoUtil.NEW_LINE_OPTION, System.lineSeparator());
return res;
}
/**
* Creates the default {@link ResourceSet}.
*
* @return the created default {@link ResourceSet}
* @generated
*/
protected ResourceSet createDefaultResourceSet() {
return new ResourceSetImpl();
}
/**
* Creates the {@link ResourceSet} for models.
*
* @param generationKey
* the generation key
* @param options
* the {@link Map} of options
* @param exceptions
* the {@link List} of exceptions
* @param resourceSet
* the default {@link ResourceSet}
* @return the created {@link ResourceSet} for models
* @generated
*/
protected ResourceSet createResourceSetForModel(Object generationKey, Map<String, String> options,
List<Exception> exceptions, ResourceSet resourceSet) {
final ResourceSet resourceSetForModels = AQLUtils.createResourceSetForModels(exceptions,
generationKey, resourceSet, options);
return resourceSetForModels;
}
/**
* Initializes the {@link ResourceSet} for models for standalone use.
*
* @param resourceSetForModels
* the {@link ResourceSet} for models
* @generated
*/
protected void standaloneInitialization(ResourceSet resourceSetForModels) {
// initialize EPackages
[module.getAllEPackages().getJavaInitialize()->sep('\n')/]
// register default XMI resource factory
resourceSetForModels.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
}
/**
* Loads {@link Resource} in the given {@link ResourceSet} for models.
*
* @param resourceSetForModels
* the {@link ResourceSet} for models
* @param resources
* the {@link List} of resource names to load
* @generated
*/
protected void loadResources(ResourceSet resourceSetForModels, List<String> resources) {
for (String resource : resources) {
resourceSetForModels.getResource(URI.createURI(resource, true), true);
}
}
/**
* Creates the {@link IQualifiedNameResolver}.
*
* @return the created {@link IQualifiedNameResolver}
* @generated
*/
protected IQualifiedNameResolver createResolver() {
final IQualifiedNameResolver resolver = new ClassLoaderQualifiedNameResolver(this.getClass()
.getClassLoader(), AcceleoParser.QUALIFIER_SEPARATOR);
return resolver;
}
/**
* Creates the {@link IQualifiedNameQueryEnvironment}.
*
* @param options
* the {@link Map} of options
* @param resolver
* the {@link IQualifiedNameResolver}
* @param resourceSetForModels
* the {@link ResourceSet} for models
* @return the created {@link IQualifiedNameQueryEnvironment}
* @generated
*/
protected IQualifiedNameQueryEnvironment createAcceleoQueryEnvironment(Map<String, String> options,
IQualifiedNameResolver resolver, ResourceSet resourceSetForModels) {
final IQualifiedNameQueryEnvironment queryEnvironment = AcceleoUtil.newAcceleoQueryEnvironment(
options, resolver, resourceSetForModels, false);
for (String nsURI : new ArrayList<String>(EPackage.Registry.INSTANCE.keySet())) {
registerEPackage(queryEnvironment, EPackage.Registry.INSTANCE.getEPackage(nsURI));
}
return queryEnvironment;
}
/**
* Creates the {@link AcceleoEvaluator}
*
* @param resolver
* the {@link IQualifiedNameResolver}
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @return the created {@link AcceleoEvaluator}
* @generated
*/
protected AcceleoEvaluator createAcceleoEvaluator(IQualifiedNameResolver resolver,
IQualifiedNameQueryEnvironment queryEnvironment) {
AcceleoEvaluator evaluator = new AcceleoEvaluator(queryEnvironment.getLookupEngine(), System
.lineSeparator());
resolver.addLoader(new ModuleLoader(new AcceleoParser(), evaluator));
resolver.addLoader(new JavaLoader(AcceleoParser.QUALIFIER_SEPARATOR, false));
return evaluator;
}
protected IAcceleoGenerationStrategy createGenerationStrategy(final ResourceSet resourceSetForModels) {
final IAcceleoGenerationStrategy strategy = new DefaultGenerationStrategy(resourceSetForModels
.getURIConverter(), new DefaultWriterFactory()) {
@Override
public IAcceleoWriter createWriterFor(URI uri, OpenModeKind openMode, Charset charset,
String lineDelimiter) throws IOException {
System.out.println(uri.toString());
return super.createWriterFor(uri, openMode, charset, lineDelimiter);
}
};
return strategy;
}
/**
* Before the generation starts.
*
* @param evaluator
* the {@link AcceleoEvaluator}
* @param queryEnvironment
* the {@link IQualifiedNameQueryEnvironment}
* @param module
* the {@link Module}
* @param resourceSetForModels
* the {@link ResourceSet} containing loaded models
* @param generationStrategy
* the {@link IAcceleoGenerationStrategy}
* @param destination
* destination {@link URI}
* @param logURI
* the {@link URI} for logging if nay, <code>null</code> otherwise
* @generated
*/
protected void beforeGeneration(AcceleoEvaluator evaluator,
IQualifiedNameQueryEnvironment queryEnvironment, Module module, ResourceSet resourceSetForModels,
IAcceleoGenerationStrategy strategy, URI destination, URI logURI) {
// this is called before the generation starts
}
/**
* Prints the diagnostics for the given {@link GenerationResult}.
*
* @param generationResult
* the {@link GenerationResult}
* @generated
*/
protected void printDiagnostics(GenerationResult generationResult) {
if (generationResult.getDiagnostic().getSeverity() > Diagnostic.INFO) {
PrintStream stream;
switch (generationResult.getDiagnostic().getSeverity()) {
case Diagnostic.WARNING:
stream = System.out;
stream.println("WARNING");
break;
case Diagnostic.ERROR:
// Fall-through
default:
// Shouldn't happen as we only show warnings and errors
stream = System.err;
stream.println("ERROR");
break;
}
printDiagnostic(stream, generationResult.getDiagnostic(), "");
}
}
/**
* Prints the given {@link Diagnostic} for the given {@link PrintStream}.
*
* @param stream
* the {@link PrintStream}
* @param diagnostic
* the {@link Diagnostic}
* @param indentation
* the current indentation
*/
protected void printDiagnostic(PrintStream stream, Diagnostic diagnostic, String indentation) {
String nextIndentation = indentation;
if (diagnostic.getMessage() != null) {
stream.print(indentation);
switch (diagnostic.getSeverity()) {
case Diagnostic.INFO:
stream.print("INFO: ");
break;
case Diagnostic.WARNING:
stream.print("WARNING: ");
break;
case Diagnostic.ERROR:
stream.print("ERROR: ");
break;
}
stream.println(diagnostic.getMessage());
nextIndentation += "\t";
}
for (Diagnostic child : diagnostic.getChildren()) {
printDiagnostic(stream, child, nextIndentation);
}
}
/**
* After the generation finished.
*
* @param generationResult
* the {@link GenerationResult}
* @generated
*/
protected void afterGeneration(GenerationResult generationResult) {
// this is called after the generation finished
}
}
[/file]
[/template]