-
Notifications
You must be signed in to change notification settings - Fork 3
/
ModuleConverter.java
679 lines (607 loc) · 24.3 KB
/
ModuleConverter.java
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*******************************************************************************
* Copyright (c) 2017, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.migration.converters;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.acceleo.AcceleoASTNode;
import org.eclipse.acceleo.AcceleoFactory;
import org.eclipse.acceleo.Binding;
import org.eclipse.acceleo.Block;
import org.eclipse.acceleo.Comment;
import org.eclipse.acceleo.CommentBody;
import org.eclipse.acceleo.Documentation;
import org.eclipse.acceleo.FileStatement;
import org.eclipse.acceleo.ForStatement;
import org.eclipse.acceleo.IfStatement;
import org.eclipse.acceleo.Import;
import org.eclipse.acceleo.LetStatement;
import org.eclipse.acceleo.Metamodel;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.ModuleElement;
import org.eclipse.acceleo.ModuleReference;
import org.eclipse.acceleo.OpenModeKind;
import org.eclipse.acceleo.ProtectedArea;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Statement;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.TextStatement;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.VisibilityKind;
import org.eclipse.acceleo.aql.migration.IModuleResolver;
import org.eclipse.acceleo.aql.migration.MigrationException;
import org.eclipse.acceleo.aql.migration.converters.utils.TypeUtils;
import org.eclipse.acceleo.aql.parser.AcceleoParser;
import org.eclipse.acceleo.model.mtl.FileBlock;
import org.eclipse.acceleo.model.mtl.ForBlock;
import org.eclipse.acceleo.model.mtl.IfBlock;
import org.eclipse.acceleo.model.mtl.LetBlock;
import org.eclipse.acceleo.model.mtl.MtlPackage;
import org.eclipse.acceleo.model.mtl.ProtectedAreaBlock;
import org.eclipse.acceleo.model.mtl.TypedModel;
import org.eclipse.acceleo.query.ast.AstFactory;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ast.CallType;
import org.eclipse.acceleo.query.ast.Expression;
import org.eclipse.acceleo.query.ast.Lambda;
import org.eclipse.acceleo.query.ast.VariableDeclaration;
import org.eclipse.acceleo.query.parser.AstResult;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ocl.ecore.EcorePackage;
import org.eclipse.ocl.ecore.OCLExpression;
import org.eclipse.ocl.ecore.OperationCallExp;
import org.eclipse.ocl.ecore.StringLiteralExp;
/**
* A converter dedicated to MTL elements.
*
* @author <a href="mailto:william.piers@obeo.fr">William Piers</a>
*/
public final class ModuleConverter extends AbstractConverter {
/**
* The new line string.
*/
private static final String NEW_LINE = "\n";
/**
* A converter dedicated to expressions.
*/
private ExpressionConverter expressionConverter;
/**
* The module resolver, for imports/extends.
*/
private IModuleResolver moduleResolver;
/**
* The {@link List} of service class to copy.
*/
private final List<String> serviceClassToCopy = new ArrayList<>();
/**
* The target folder {@link Path}.
*/
private final Path targetFolderPath;
/**
* Creates the converter using the given module resolver.
*
* @param moduleResolver
* the module resolver
* @param targetFolderPath
* the target folder {@link Path}
*/
public ModuleConverter(IModuleResolver moduleResolver, Path targetFolderPath) {
this.moduleResolver = moduleResolver;
this.targetFolderPath = targetFolderPath;
expressionConverter = new ExpressionConverter(targetFolderPath);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.aql.migration.converters.AbstractConverter#convert(org.eclipse.emf.ecore.EObject)
*/
@Override
public Object convert(EObject input) {
Object res = null;
switch (input.eClass().getClassifierID()) {
case MtlPackage.MODULE:
res = caseModule((org.eclipse.acceleo.model.mtl.Module)input);
break;
case MtlPackage.TEMPLATE:
res = caseTemplate((org.eclipse.acceleo.model.mtl.Template)input);
break;
case MtlPackage.QUERY:
res = caseQuery((org.eclipse.acceleo.model.mtl.Query)input);
break;
case MtlPackage.DOCUMENTATION:
case MtlPackage.MODULE_DOCUMENTATION:
case MtlPackage.MODULE_ELEMENT_DOCUMENTATION:
res = caseDocumentation((org.eclipse.acceleo.model.mtl.Documentation)input);
break;
case MtlPackage.COMMENT:
res = caseComment((org.eclipse.acceleo.model.mtl.Comment)input);
break;
case MtlPackage.FILE_BLOCK:
res = caseFileBlock((FileBlock)input);
break;
case MtlPackage.FOR_BLOCK:
res = caseForBlock((ForBlock)input);
break;
case MtlPackage.IF_BLOCK:
res = caseIfBlock((IfBlock)input);
break;
case MtlPackage.LET_BLOCK:
res = caseLetBlock((LetBlock)input);
break;
case MtlPackage.PROTECTED_AREA_BLOCK:
res = caseProtectedAreaBlock((ProtectedAreaBlock)input);
break;
case EcorePackage.VARIABLE:
res = caseVariable((org.eclipse.ocl.ecore.Variable)input);
break;
case EcorePackage.STRING_LITERAL_EXP:
res = caseText((StringLiteralExp)input);
break;
case MtlPackage.TYPED_MODEL:
res = caseTypedModel((TypedModel)input);
break;
default:
if (input instanceof OCLExpression) {
res = expressionConverter.convertToStatement((OCLExpression)input);
} else {
throw new MigrationException(input);
}
break;
}
return res;
}
private Object caseModule(org.eclipse.acceleo.model.mtl.Module inputModule) {
final Module outputModule = AcceleoFactory.eINSTANCE.createModule();
outputModule.setName(getModuleName(inputModule.getName()));
map(inputModule.getInput(), outputModule.getMetamodels());
map(inputModule.getOwnedModuleElement(), outputModule.getModuleElements());
// extends
if (!inputModule.getExtends().isEmpty()) {
ModuleReference moduleReference = AcceleoFactory.eINSTANCE.createModuleReference();
// only one module can be extended
org.eclipse.acceleo.model.mtl.Module extendedModule = inputModule.getExtends().get(0);
if (extendedModule.getNsURI() != null) {
moduleReference.setQualifiedName(getModuleName(extendedModule.getNsURI()));
} else {
moduleReference.setQualifiedName(getModuleName(moduleResolver.getQualifiedName(inputModule,
extendedModule)));
}
outputModule.setExtends(moduleReference);
}
// imports
for (org.eclipse.acceleo.model.mtl.Module importedModule : inputModule.getImports()) {
Import outputImport = AcceleoFactory.eINSTANCE.createImport();
ModuleReference moduleReference = AcceleoFactory.eINSTANCE.createModuleReference();
if (importedModule.getNsURI() != null) {
moduleReference.setQualifiedName(getModuleName(importedModule.getNsURI()));
} else {
moduleReference.setQualifiedName(getModuleName(moduleResolver.getQualifiedName(inputModule,
importedModule)));
}
outputImport.setModule(moduleReference);
outputModule.getImports().add(outputImport);
}
// add imports for invoke()
addInvokeImports(inputModule, outputModule);
for (Entry<Call, String> entry : expressionConverter.getJavaServiceCalls().entrySet()) {
if (isAmbiguousJavaServiceCall(outputModule, entry.getKey())) {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
final File javaFile = new File(targetFolderPath + FileSystems.getDefault().getSeparator()
+ entry.getValue().replace(".", FileSystems.getDefault().getSeparator()) + ".java");
if (javaFile.exists()) {
try {
final IDocument document = new Document(new String(Files.readAllBytes(javaFile
.toPath())));
parser.setSource(document.get().toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit)parser.createAST(null);
cu.accept(new AmbiguousServiceMethodRefactorVisitor(document, entry.getKey()));
Files.write(javaFile.toPath(), document.get().getBytes(), StandardOpenOption.CREATE);
entry.getKey().setServiceName(entry.getKey().getServiceName()
+ ExpressionConverter.JAVA_SERVICE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return outputModule;
}
/**
* Gets the Acceleo 4 identifier for the given name or qualified name of a module.
*
* @param name
* the name or qualified name of a module
* @return the Acceleo 4 identifier for the given name or qualified name of a module
*/
private String getModuleName(String name) {
final StringBuilder res = new StringBuilder();
final int lastColonIndex = name.lastIndexOf("::");
final int start;
if (lastColonIndex >= 0) {
start = lastColonIndex + 2;
res.append(name.substring(0, start));
} else {
start = 0;
}
char charAt = name.charAt(start);
if (Character.isJavaIdentifierStart(charAt)) {
res.append(charAt);
} else {
res.append('_');
}
for (int i = start + 1; i < name.length(); i++) {
charAt = name.charAt(i);
if (Character.isJavaIdentifierPart(charAt)) {
res.append(charAt);
} else {
res.append('_');
}
}
return res.toString();
}
private boolean isAmbiguousJavaServiceCall(Module module, Call call) {
boolean res = false;
for (ModuleElement element : module.getModuleElements()) {
if (element instanceof Template) {
res = call.getServiceName().equals(((Template)element).getName()) && parameterMatch(call
.getArguments(), ((Template)element).getParameters());
} else if (element instanceof Query) {
res = call.getServiceName().equals(((Query)element).getName()) && parameterMatch(call
.getArguments(), ((Query)element).getParameters());
}
if (res) {
break;
}
}
return res;
}
private boolean parameterMatch(EList<Expression> arguments, EList<Variable> parameters) {
boolean res = true;
if (arguments.size() == parameters.size()) {
// TODO we should validate each expression and Variable type to see if they match
res = true;
} else {
res = false;
}
return res;
}
private void addInvokeImports(org.eclipse.acceleo.model.mtl.Module inputModule,
final Module outputModule) {
// TODO this is sub optimal, we should have access to the output Module when migrating
// OperationCallExp
final Set<String> knownImports = new HashSet<String>();
for (Import imp : outputModule.getImports()) {
knownImports.add(imp.getModule().getQualifiedName());
}
final List<String> imports = new ArrayList<String>();
final Iterator<EObject> it = inputModule.eAllContents();
while (it.hasNext()) {
final EObject eObj = it.next();
if (eObj instanceof OperationCallExp && expressionConverter.isInvokeCall(
(OperationCallExp)eObj)) {
final OperationCallExp call = (OperationCallExp)eObj;
if (call.getArgument().get(0) instanceof StringLiteralExp) {
final String serviceClassName = ((org.eclipse.ocl.expressions.StringLiteralExp<EClassifier>)call
.getArgument().get(0)).getStringSymbol();
final String toImport = serviceClassName.replace(".", AcceleoParser.QUALIFIER_SEPARATOR);
if (!knownImports.contains(toImport)) {
knownImports.add(toImport);
serviceClassToCopy.add(serviceClassName);
imports.add(toImport);
}
}
}
}
Collections.sort(imports);
for (String toImport : imports) {
final Import imp = AcceleoFactory.eINSTANCE.createImport();
final ModuleReference moduleRef = AcceleoFactory.eINSTANCE.createModuleReference();
imp.setModule(moduleRef);
moduleRef.setQualifiedName(toImport);
outputModule.getImports().add(imp);
}
}
private Object caseTemplate(org.eclipse.acceleo.model.mtl.Template inputTemplate) {
List<EObject> res = new ArrayList<>();
Template outputTemplate = AcceleoFactory.eINSTANCE.createTemplate();
if (inputTemplate.getDocumentation() != null) {
outputTemplate.setDocumentation((Documentation)convert(inputTemplate.getDocumentation()));
}
outputTemplate.setName(inputTemplate.getName());
outputTemplate.setMain(inputTemplate.isMain());
outputTemplate.setVisibility(VisibilityKind.getByName(inputTemplate.getVisibility().getName()
.toLowerCase()));
// parameters
map(inputTemplate.getParameter(), outputTemplate.getParameters());
// post
if (inputTemplate.getPost() != null) {
outputTemplate.setPost(expressionConverter.convertToExpression(inputTemplate.getPost(), true));
}
// guard
if (inputTemplate.getGuard() != null) {
outputTemplate.setGuard(expressionConverter.convertToExpression(inputTemplate.getGuard(), true));
}
// main comment
if (inputTemplate.isMain()) {
Comment mainComment = AcceleoFactory.eINSTANCE.createComment();
CommentBody commentBody = AcceleoFactory.eINSTANCE.createCommentBody();
commentBody.setValue("@main");
mainComment.setBody(commentBody);
res.add(mainComment);
}
res.add(outputTemplate);
// statements
final Block body = createBlock(outputTemplate, inputTemplate.getBody());
outputTemplate.setBody(body);
return res;
}
private Object caseQuery(org.eclipse.acceleo.model.mtl.Query inputQuery) {
Query outputQuery = AcceleoFactory.eINSTANCE.createQuery();
if (inputQuery.getDocumentation() != null) {
outputQuery.setDocumentation((Documentation)convert(inputQuery.getDocumentation()));
}
outputQuery.setName(inputQuery.getName());
outputQuery.setVisibility(VisibilityKind.getByName(inputQuery.getVisibility().getName()
.toLowerCase()));
map(inputQuery.getParameter(), outputQuery.getParameters());
outputQuery.setBody(expressionConverter.convertToExpression(inputQuery.getExpression(), false));
outputQuery.setType(createAstResult(TypeUtils.createTypeLiteral(inputQuery.getType())));
return outputQuery;
}
private Object caseFileBlock(FileBlock input) {
FileStatement output = AcceleoFactory.eINSTANCE.createFileStatement();
if (input.getCharset() != null) {
output.setCharset(expressionConverter.convertToExpression(input.getCharset(), false));
}
output.setUrl(expressionConverter.convertToExpression(input.getFileUrl(), false));
output.setMode(OpenModeKind.getByName(input.getOpenMode().getName().toLowerCase()));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private Object caseLetBlock(LetBlock input) {
if (!input.getElseLet().isEmpty()) {
throw new MigrationException(input.getElseLet().get(0));
}
LetStatement output = AcceleoFactory.eINSTANCE.createLetStatement();
Binding binding = AcceleoFactory.eINSTANCE.createBinding();
output.getVariables().add(binding);
binding.setName(input.getLetVariable().getName());
if (!TypeUtils.containsOclAny(input.getLetVariable().getType())) {
binding.setType(createAstResult(TypeUtils.createTypeLiteral(input.getLetVariable().getType())));
}
binding.setInitExpression(expressionConverter.convertToExpression((OCLExpression)input
.getLetVariable().getInitExpression(), false));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private TextStatement newLineAfterEndBlock() {
final TextStatement res = AcceleoFactory.eINSTANCE.createTextStatement();
res.setValue("");
res.setNewLineNeeded(true);
return res;
}
private Object caseForBlock(ForBlock input) {
ForStatement output = AcceleoFactory.eINSTANCE.createForStatement();
Binding binding = AcceleoFactory.eINSTANCE.createBinding();
output.setBinding(binding);
org.eclipse.ocl.ecore.Variable loopVariable = input.getLoopVariable();
if (loopVariable != null) {
binding.setName(loopVariable.getName());
binding.setType(createAstResult(TypeUtils.createTypeLiteral(loopVariable.getType())));
} else {
// TODO manage implicit for block
throw new MigrationException(input);
}
binding.setInitExpression(getInitExpression(input));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
OCLExpression each = input.getEach();
if (each != null) {
output.setSeparator(expressionConverter.convertToExpression(each, false));
}
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private org.eclipse.acceleo.Expression getInitExpression(ForBlock input) {
final org.eclipse.acceleo.Expression res;
final org.eclipse.acceleo.Expression initExpression = expressionConverter.convertToExpression(input
.getIterSet(), false);
if (input.getGuard() != null) {
final Call selectCall = AstFactory.eINSTANCE.createCall();
selectCall.setType(CallType.COLLECTIONCALL);
selectCall.setServiceName("select");
selectCall.getArguments().add(initExpression.getAst().getAst());
final Lambda lambda = AstFactory.eINSTANCE.createLambda();
final VariableDeclaration varDeclaration = AstFactory.eINSTANCE.createVariableDeclaration();
varDeclaration.setName(input.getLoopVariable().getName());
lambda.getParameters().add(varDeclaration);
final org.eclipse.acceleo.Expression guardExpression = expressionConverter.convertToExpression(
input.getGuard(), false);
lambda.setExpression(guardExpression.getAst().getAst());
selectCall.getArguments().add(lambda);
res = AcceleoFactory.eINSTANCE.createExpression();
res.setAst(new AstResult(selectCall, null, null, Diagnostic.OK_INSTANCE));
} else {
res = initExpression;
}
return res;
}
private List<Statement> caseIfBlock(IfBlock input) {
IfStatement output = AcceleoFactory.eINSTANCE.createIfStatement();
output.setCondition(expressionConverter.convertToExpression(input.getIfExpr(), false));
// then statements
final Block thenBlock = createBlock(output, input.getBody());
output.setThen(thenBlock);
List<org.eclipse.acceleo.model.mtl.Block> inputElseBlocks = new ArrayList<>();
inputElseBlocks.addAll(input.getElseIf());
if (input.getElse() != null) {
inputElseBlocks.add(input.getElse());
}
if (!inputElseBlocks.isEmpty()) {
IfStatement current = output;
for (org.eclipse.acceleo.model.mtl.Block inputElseBlock : inputElseBlocks) {
// else statements
if (inputElseBlock instanceof IfBlock) {
Block elseBlock = AcceleoFactory.eINSTANCE.createBlock();
current.setElse(elseBlock);
current = (IfStatement)caseIfBlock((IfBlock)inputElseBlock).get(0);
elseBlock.getStatements().add(current);
} else {
final Block elseBlock = createBlock(current, inputElseBlock.getBody());
current.setElse(elseBlock);
}
}
}
return Arrays.asList(new Statement[] {output, newLineAfterEndBlock() });
}
private Object caseText(StringLiteralExp input) {
final List<TextStatement> outputs = new ArrayList<TextStatement>();
final String text = input.getStringSymbol().replace("[", "['['/]");
int startOfText = 0;
int endOfText;
do {
endOfText = text.indexOf(NEW_LINE, startOfText);
if (endOfText < 0) {
endOfText = text.length();
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setNewLineNeeded(false);
outputs.add(output);
} else {
final TextStatement output = AcceleoFactory.eINSTANCE.createTextStatement();
output.setValue(text.substring(startOfText, endOfText));
output.setNewLineNeeded(true);
outputs.add(output);
}
startOfText = endOfText + NEW_LINE.length();
} while (endOfText < text.length() && startOfText < text.length());
return outputs;
}
private Object caseTypedModel(TypedModel input) {
List<EObject> res = new ArrayList<>();
for (EPackage ePackage : input.getTakesTypesFrom()) {
Metamodel metamodel = AcceleoFactory.eINSTANCE.createMetamodel();
if (ePackage.eIsProxy()) {
// we create a dummy EPackage
EPackage dummy = org.eclipse.emf.ecore.EcoreFactory.eINSTANCE.createEPackage();
dummy.setNsURI(EcoreUtil.getURI(ePackage).toString().split("#")[0]);
metamodel.setReferencedPackage(dummy);
} else {
metamodel.setReferencedPackage(ePackage);
}
res.add(metamodel);
}
return res;
}
private Object caseVariable(org.eclipse.ocl.ecore.Variable inputVariable) {
Variable outputVariable = AcceleoFactory.eINSTANCE.createVariable();
outputVariable.setName(inputVariable.getName());
outputVariable.setType(createAstResult(TypeUtils.createTypeLiteral(inputVariable.getType())));
return outputVariable;
}
private Object caseDocumentation(org.eclipse.acceleo.model.mtl.Documentation input) {
Documentation output = AcceleoFactory.eINSTANCE.createModuleElementDocumentation();
CommentBody body = AcceleoFactory.eINSTANCE.createCommentBody();
StringBuilder formatValue = new StringBuilder();
formatValue.append(NEW_LINE);
for (String line : input.getBody().getValue().split(NEW_LINE)) {
if (!line.trim().isEmpty()) {
formatValue.append(" * " + line + NEW_LINE);
}
}
formatValue.append("*");
body.setValue(formatValue.toString());
output.setBody(body);
return output;
}
private Object caseComment(org.eclipse.acceleo.model.mtl.Comment inputComment) {
final String commentValue;
final Comment outputComment;
if (inputComment.getBody().getValue().contains("/]")) {
outputComment = AcceleoFactory.eINSTANCE.createBlockComment();
commentValue = inputComment.getBody().getValue();
} else {
outputComment = AcceleoFactory.eINSTANCE.createComment();
commentValue = inputComment.getBody().getValue().substring(1);
}
final CommentBody commentBody = AcceleoFactory.eINSTANCE.createCommentBody();
commentBody.setValue(commentValue);
outputComment.setBody(commentBody);
return outputComment;
}
private Object caseProtectedAreaBlock(ProtectedAreaBlock input) {
ProtectedArea output = AcceleoFactory.eINSTANCE.createProtectedArea();
output.setId(expressionConverter.convertToExpression(input.getMarker(), false));
// statements
final Block body = createBlock(output, input.getBody());
output.setBody(body);
return Arrays.asList(new Object[] {output, newLineAfterEndBlock() });
}
private Block createBlock(AcceleoASTNode node, List<OCLExpression> inputStatements) {
Block res = AcceleoFactory.eINSTANCE.createBlock();
map(inputStatements, res.getStatements());
if (node instanceof Template || node instanceof File) {
res.setInlined(false);
} else if (node instanceof ProtectedArea) {
res.setInlined(false);
// remove the first new line
res.getStatements().remove(0);
} else {
boolean inlined = true;
for (Statement statement : res.getStatements()) {
if (statement instanceof TextStatement && ((TextStatement)statement).isNewLineNeeded()) {
inlined = false;
break;
}
}
res.setInlined(inlined);
}
if (res.getStatements().isEmpty()) {
res.setInlined(false);
}
return res;
}
/**
* Gets the {@link List} of service class to copy.
*
* @return the {@link List} of service class to copy
*/
public List<String> getServiceClassToCopy() {
return serviceClassToCopy;
}
}