XML model to DSL syntax transformation [message #1739493] |
Tue, 02 August 2016 08:37  |
Eclipse User |
|
|
|
Hello,
I am working on a project that is related to an existing Xtext DSL (referred to as "ONE") but uses a separate meta model ("TWO"), so I have two Ecore meta model descriptions.
What I am trying to achieve is a model-to model transformation from ONE to TWO. This is nothing spectacular and there are tools such as QVT to create a model compliant to TWO's meta model which is saved to an XML file. But I am particularly interested in reaching the defined syntax of the target DSL!
Is there a tool I have not found yet which can convert the XML model representation to the ONE syntax defined in Xtext?
Best regards,
Christoph
PS: Obviously I could write a model-to-text generator from a TWO model to the ONE syntax but from a type safety perspective this seems more error-prone than a model instance conforming to ONE's meta model and just trying to create the proper representation.
|
|
|
|
|
|
|
|
|
Re: XML model to DSL syntax transformation [message #1739632 is a reply to message #1739591] |
Wed, 03 August 2016 08:04  |
Eclipse User |
|
|
|
In case someone is interested in the programmatic transformation between a generated Xtext language and XMI:
public static void init() {
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
// Register respective language(s)
if (!EPackage.Registry.INSTANCE.containsKey("<languageUri>")) {
EPackage.Registry.INSTANCE.put("<languageUri>", <DSLname>Package.eINSTANCE);
}
}
public static void DslToXmi(String inputUri, String outputUri) {
init();
// Source
Injector injector = new <DSLname>StandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSetXtext = injector.getInstance(XtextResourceSet.class);
resourceSetXtext.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resourceInput = resourceSetXtext.getResource(URI.createURI(inputUri), true);
// Create new target file
ResourceSet resourceSet = new ResourceSetImpl();
Resource resourceXmi = resourceSet.createResource(URI.createURI(outputUri));
// Copy content
resourceXmi.getContents().addAll(resourceInput.getContents());
try {
Map<String, String> options = new HashMap<String, String>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
resourceXmi.save(options);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void XmiToDsl(String inputUri, String outputUri) {
init();
// Source
ResourceSet resourceSet = new ResourceSetImpl();
Resource resourceXmi = resourceSet.getResource(URI.createURI(inputUri), true);
// Create new target file
Injector injector = new <DSLname>StandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSetXtext = injector.getInstance(XtextResourceSet.class);
resourceSetXtext.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resourceDsl = resourceSetXtext.createResource(URI.createURI(outputUri));
// Copy content
resourceDsl.getContents().addAll(resourceXmi.getContents());
try {
resourceDsl.save(Collections.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03031 seconds