XMI to DSL [message #1866564] |
Sun, 02 June 2024 08:43  |
Eclipse User |
|
|
|
Hi everyone,
I've created a plugin to convert a DSL file into XMI because I use ATL to perform M2M transformations. Essentially, I go from a generic model to a more specific one based on that generic model.
The problem is that I now need to convert XMI into DSL to continue configuring that DSL, but now with the grammar of the more specific model available.
I followed the DSL to XMI plugin logic, but it copies the XMI content to a single line.
This is my grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!';
and this is my XMI to DSL plugin:
package popup.action;
import java.io.IOException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.xtext.resource.XtextResourceSet;
public class XMIReader {
public static void convertXMI2DSL(String filePath, Shell shell) {
// Create a new resource set
ResourceSet resourceSet = new XtextResourceSet();
// Create a URI from the XMI file path
URI xmiUri = URI.createFileURI(filePath);
// Load the XMI file into the resource set
Resource xmiResource = resourceSet.getResource(xmiUri, true);
// Resolve all references
EcoreUtil.resolveAll(resourceSet);
// Create a URI for the DSL file
URI dslUri = URI.createURI(xmiUri.trimFileExtension().toString() + ".mydsl");
// Create a new DSL resource
Resource dslResource = resourceSet.createResource(dslUri);
// Add the contents of the XMI resource to the DSL resource
dslResource.getContents().addAll(xmiResource.getContents());
try {
// Save the DSL resource
dslResource.save(null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In runtime I define a XMI like this:
<?xml version="1.0" encoding="ASCII"?>
<myDsl:Model xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:myDsl="http://www.xtext.org/example/mydsl/MyDsl">
<greetings name="Alex"/>
<greetings name="John"/>
</myDsl:Model>
the final result is this:
Hello Alex ! Hello John !
Why is this copying content to a single line?
[Updated on: Sun, 02 June 2024 08:43] by Moderator Report message to a moderator
|
|
|
|
|
|
Re: XMI to DSL [message #1866591 is a reply to message #1866573] |
Mon, 03 June 2024 07:21  |
Eclipse User |
|
|
|
Furthermore, if you need some help how to start implementing such a formatter, you can take some inspiration from the Xtext examples shipped with the Xtext framwork.
|
|
|
Powered by
FUDForum. Page generated in 0.05228 seconds