Skip to content

Commit

Permalink
Added ProtectedArea prefixes to the migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 22, 2023
1 parent daf0bc1 commit 3b83642
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -66,17 +67,20 @@
import org.eclipse.acceleo.model.mtl.TemplateInvocation;
import org.eclipse.acceleo.model.mtl.TypedModel;
import org.eclipse.acceleo.query.ast.AstFactory;
import org.eclipse.acceleo.query.ast.AstPackage;
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.StringLiteral;
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.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
Expand Down Expand Up @@ -142,7 +146,8 @@ public ModuleConverter(IModuleResolver moduleResolver, Path targetFolderPath) {
*/
@Override
public Object convert(EObject input) {
Object res = null;
final Object res;

switch (input.eClass().getClassifierID()) {
case MtlPackage.MODULE:
res = caseModule((org.eclipse.acceleo.model.mtl.Module)input);
Expand Down Expand Up @@ -193,6 +198,131 @@ public Object convert(EObject input) {
}
break;
}

if (res instanceof Module) {
final Iterator<EObject> it = ((Module)res).eAllContents();
while (it.hasNext()) {
final EObject current = it.next();
if (current instanceof ProtectedArea) {
setTagPrefixes((ProtectedArea)current);
}
}
}

return res;
}

/**
* Sets the {@link ProtectedArea#getStartTagPrefix() start tag} and {@link ProtectedArea#getEndTagPrefix()
* end tag}.
*
* @param protectedArea
* the {@link ProtectedArea}
*/
private void setTagPrefixes(ProtectedArea protectedArea) {
final TextStatement previousText = getPreviousTextStatement(protectedArea);
if (previousText != null && !previousText.isNewLineNeeded()) {
protectedArea.setStartTagPrefix(extractPrefix(previousText));
}
final TextStatement lastText = getLastText(protectedArea);
if (lastText != null && !lastText.isNewLineNeeded()) {
protectedArea.setEndTagPrefix(extractPrefix(lastText));
}
}

/**
* Gets the previous {@link Statement} before the given {@link ProtectedArea} if it's a
* {@link TextStatement}.
*
* @param protectedArea
* the {@link ProtectedArea}
* @return the previous {@link Statement} before the given {@link ProtectedArea} if it's a
* {@link TextStatement}, <code>null</code> otherwise
*/
private TextStatement getPreviousTextStatement(ProtectedArea protectedArea) {
final TextStatement res;

final EStructuralFeature containingFeature = protectedArea.eContainingFeature();
final EObject container = protectedArea.eContainer();
if (containingFeature != null && container != null) {
final Object values = container.eGet(containingFeature);
if (values instanceof Collection<?>) {
EObject previous = null;
for (Object value : (Collection<?>)values) {
if (value != protectedArea && value instanceof EObject) {
previous = (EObject)value;
} else {
break;
}
}
if (previous instanceof TextStatement) {
res = (TextStatement)previous;
} else {
res = null;
}
} else {
res = null;
}
} else {
res = null;
}

return res;
}

/**
* Gets the last statement of the given Pro {@link ProtectedArea} if it's a {@link TextStatement}.
*
* @param protectedArea
* the {@link ProtectedArea}
* @return the last statement of the given Pro {@link ProtectedArea} if it's a {@link TextStatement},
* <code>null</code> otherwise
*/
private TextStatement getLastText(ProtectedArea protectedArea) {
final TextStatement res;

if (!protectedArea.getBody().getStatements().isEmpty()) {
final Statement lastStatement = protectedArea.getBody().getStatements().get(protectedArea
.getBody().getStatements().size() - 1);
if (lastStatement instanceof TextStatement) {
res = (TextStatement)lastStatement;
} else {
res = null;
}
} else {
res = null;
}

return res;
}

/**
* Extracts the {@link ProtectedArea} prefix from the given {@link TextStatement}.
*
* @param textStatement
* the {@link TextStatement}
* @return the {@link ProtectedArea} prefix from the given {@link TextStatement} if any, <code>null</code>
* otherwise
*/
private org.eclipse.acceleo.Expression extractPrefix(final TextStatement textStatement) {
final org.eclipse.acceleo.Expression res;

final String text = textStatement.getValue();
int index = Math.max(text.lastIndexOf(NEW_LINE), 0);
while (index < text.length() && Character.isWhitespace(text.charAt(index))) {
index++;
}
if (index < text.length()) {
final String startPrefix = text.substring(index);
textStatement.setValue(text.substring(0, index));
final StringLiteral stringLiteral = AstPackage.eINSTANCE.getAstFactory().createStringLiteral();
stringLiteral.setValue(startPrefix);
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createExpression();
res.setAst(new AstResult(stringLiteral, null, null, Diagnostic.OK_INSTANCE));
} else {
res = null;
}

return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[module withPrefixes('http://www.eclipse.org/emf/2002/Ecore')/]

[template public myTemplate(myParam : ecore::EPackage)]
// [protected (myParam.name)]
some static text.
// [/protected]
[/template]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[module withPrefixes('http://www.eclipse.org/emf/2002/Ecore')/]

[template public myTemplate(myParam : ecore::EPackage)]
[protected (myParam.name) startTagPrefix('// ') endTagPrefix('// ')]
some static text.
[/protected]
[/template]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[module withPrefixes('http://www.eclipse.org/emf/2002/Ecore')/]

[template public myTemplate(myParam : ecore::EPackage)]
// [protected (myParam.name)]
some static text.
// [/protected]
[/template]
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:mtl="http://www.eclipse.org/acceleo/mtl/3.0" xmlns:ocl.ecore="http://www.eclipse.org/ocl/1.1.0/Ecore">
<mtl:Module name="withPrefixes" nsURI="resources::acceleoAqlTests::language::protectedArea::withPrefixes::withPrefixes" endHeaderPosition="61">
<input>
<takesTypesFrom href="http://www.eclipse.org/emf/2002/Ecore#/"/>
</input>
<ownedModuleElement xsi:type="mtl:Template" name="myTemplate" visibility="Public">
<body xsi:type="ocl.ecore:StringLiteralExp" stringSymbol=" // "/>
<body xsi:type="mtl:ProtectedAreaBlock">
<body xsi:type="ocl.ecore:StringLiteralExp" stringSymbol="&#xA; some static text.&#xA; // "/>
<marker xsi:type="ocl.ecore:PropertyCallExp">
<eType xsi:type="ocl.ecore:PrimitiveType" href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/String"/>
<source xsi:type="ocl.ecore:VariableExp" name="myParam" referredVariable="/0/myTemplate/myParam">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</source>
<referredProperty xsi:type="ecore:EAttribute" href="http://www.eclipse.org/emf/2002/Ecore#//ENamedElement/name"/>
</marker>
</body>
<parameter name="myParam">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</parameter>
</ownedModuleElement>
</mtl:Module>
<ecore:EPackage name="additions">
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_String_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/String"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_Integer_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/Integer"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_Real_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/Real"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="ecore_EObject_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/emf/2002/Ecore#//EObject"/>
</eAnnotations>
<eOperations name="myTemplate">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<contents xsi:type="ocl.ecore:Constraint"/>
</eAnnotations>
<eAnnotations source="MTL" references="/0/myTemplate"/>
<eType xsi:type="ocl.ecore:PrimitiveType" href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/String"/>
<eParameters name="myParam">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</eParameters>
</eOperations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_OclAny_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/OclAny"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_Collection(T)_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/Collection(T)"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_Sequence(T)_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/Sequence(T)"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="oclstdlib_OrderedSet(T)_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/OrderedSet(T)"/>
</eAnnotations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="ecore_EPackage_Class">
<eAnnotations source="http://www.eclipse.org/ocl/1.1.0/OCL">
<references href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</eAnnotations>
</eClassifiers>
</ecore:EPackage>
<ocl.ecore:Variable name="self">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EObject"/>
</ocl.ecore:Variable>
<ocl.ecore:Variable name="self">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</ocl.ecore:Variable>
<ocl.ecore:Variable name="self">
<eType xsi:type="ocl.ecore:PrimitiveType" href="http://www.eclipse.org/ocl/1.1.0/oclstdlib.ecore#/0/String"/>
</ocl.ecore:Variable>
<ocl.ecore:Variable name="self">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</ocl.ecore:Variable>
<ocl.ecore:Variable name="self">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EPackage"/>
</ocl.ecore:Variable>
<ocl.ecore:Variable name="self">
<eType xsi:type="ecore:EClass" href="http://www.eclipse.org/emf/2002/Ecore#//EObject"/>
</ocl.ecore:Variable>
<ecore:EAnnotation source="positions">
<eAnnotations source="positions.0" references="/0/myTemplate">
<details key="start" value="65"/>
<details key="end" value="213"/>
<details key="line" value="3"/>
</eAnnotations>
<eAnnotations source="positions.1" references="/0/myTemplate/%">
<details key="start" value="121"/>
<details key="end" value="128"/>
<details key="line" value="4"/>
</eAnnotations>
<eAnnotations source="positions.2" references="/0/myTemplate/%.1">
<details key="start" value="128"/>
<details key="end" value="201"/>
<details key="line" value="4"/>
</eAnnotations>
<eAnnotations source="positions.3" references="/0/myTemplate/%.1/%">
<details key="start" value="154"/>
<details key="end" value="189"/>
<details key="line" value="5"/>
</eAnnotations>
<eAnnotations source="positions.4" references="/0/myTemplate/%.1/%.1">
<details key="start" value="140"/>
<details key="end" value="152"/>
<details key="line" value="0"/>
</eAnnotations>
<eAnnotations source="positions.5" references="/0/myTemplate/%.1/%.1/myParam">
<details key="start" value="140"/>
<details key="end" value="147"/>
<details key="line" value="0"/>
</eAnnotations>
<eAnnotations source="positions.6" references="/0/myTemplate/myParam">
<details key="start" value="93"/>
<details key="end" value="118"/>
<details key="line" value="3"/>
</eAnnotations>
</ecore:EAnnotation>
</xmi:XMI>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[module withPrefixes('http://www.eclipse.org/emf/2002/Ecore')/]

[template public myTemplate(myParam : ecore::EPackage)]
[protected (myParam.name) startTagPrefix('// ') endTagPrefix('// ')]
some static text.
[/protected]
[/template]

0 comments on commit 3b83642

Please sign in to comment.