Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XMI to DSL
XMI to DSL [message #1866564] Sun, 02 June 2024 08:43 Go to next message
Alex Oli is currently offline Alex OliFriend
Messages: 19
Registered: May 2024
Junior Member
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]

Report message to a moderator

Re: XMI to DSL [message #1866567 is a reply to message #1866564] Sun, 02 June 2024 08:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14698
Registered: July 2009
Senior Member
This is the default formatter Xtext ships with (one whitespace)
So you need to make sure you also implement the formatter


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XMI to DSL [message #1866570 is a reply to message #1866567] Sun, 02 June 2024 08:50 Go to previous messageGo to next message
Alex Oli is currently offline Alex OliFriend
Messages: 19
Registered: May 2024
Junior Member
Programmatically in this plugin class or an Xtext class? I'm new to Xtext so I don't understand it yet. I cannot find any Formatter class in Xtext

[Updated on: Sun, 02 June 2024 08:59]

Report message to a moderator

Re: XMI to DSL [message #1866573 is a reply to message #1866570] Sun, 02 June 2024 09:40 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 160
Registered: February 2016
Senior Member
Hello Alex!

Just take a look at the Formatting section of the Xtext documentation:
https://eclipse.dev/Xtext/documentation/303_runtime_concepts.html#formatting

Hope that helps!
Tamás
Re: XMI to DSL [message #1866591 is a reply to message #1866573] Mon, 03 June 2024 07:21 Go to previous message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 160
Registered: February 2016
Senior Member
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.
Previous Topic:Xtext 2.35.0 is released
Next Topic:Embedding Comments in Custom DSL Files
Goto Forum:
  


Current Time: Sat Jul 20 02:06:10 GMT 2024

Powered by FUDForum. Page generated in 0.04918 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top