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
Eclipse UserFriend
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 #1866567 is a reply to message #1866564] Sun, 02 June 2024 08:45 Go to previous messageGo to next message
Eclipse UserFriend
This is the default formatter Xtext ships with (one whitespace)
So you need to make sure you also implement the formatter
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
Eclipse UserFriend
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] by Moderator

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
Eclipse UserFriend
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
Eclipse UserFriend
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: Fri Feb 14 03:16:01 GMT 2025

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

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

Back to the top