Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext parser programmatically for XMI generation
Xtext parser programmatically for XMI generation [message #1743247] Tue, 13 September 2016 14:17 Go to next message
Jean FAAR is currently offline Jean FAARFriend
Messages: 3
Registered: September 2016
Junior Member
Hi all,

I am using Xtext as a parser to transform an input file with the extension .mydsl to a .xmi file.
I implemented my grammar and I can use it by opening an Eclipse Application and creating a file with the extension .mydsl. The extension is recognized and I can paste my input file into this file.
Then, I open this file with Sample Reflective Ecore Model Editor and finally I realise a "save as" to transform it in .xmi file.

It is quite laborious then I would like to automate this transformation. I created a java application (IHM) and I tried this code from https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
 Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
 XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
 resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
 Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));
 InputStream in = new ByteArrayInputStream("type foo type bar".getBytes());
 resource.load(in, resourceSet.getLoadOptions());
 Model model = (Model) resource.getContents().get(0);


It is not working.
Do I have to save the resource as xmi?

Could you help me please?
Thanks,

Jean
Re: Xtext parser programmatically for XMI generation [message #1743361 is a reply to message #1743247] Wed, 14 September 2016 14:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what do you get as exception / error etc



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext parser programmatically for XMI generation [message #1743364 is a reply to message #1743361] Wed, 14 September 2016 14:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
And why do you want to the the xmi transformation at all?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext parser programmatically for XMI generation [message #1743367 is a reply to message #1743364] Wed, 14 September 2016 14:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the following reads the model for me as expected:

public class Main {
	
	public static void main(String[] args) throws IOException {
		 Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		 XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		 Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl"));
		 InputStream in = new ByteArrayInputStream("Hello A! Hello B!".getBytes());
		 resource.load(in, resourceSet.getLoadOptions());
		 Model model = (Model) resource.getContents().get(0);
		 System.out.println(model);
	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext parser programmatically for XMI generation [message #1743380 is a reply to message #1743367] Wed, 14 September 2016 17:57 Go to previous messageGo to next message
Jean FAAR is currently offline Jean FAARFriend
Messages: 3
Registered: September 2016
Junior Member
Hi Christian,

Thank you for your answers.

Firstly, I am going to give you more information about my project.
My objective is to convert a file written according to mydsl to a HTML file or Matlab file.
For the moment, I am doing it in two time:
- I already have the transformation from XMI file to HTML or Matlab. It is implemented with acceleo. I insert it on my HMI (java application), it is working.
- Then I have to implement a transformation from my DSL file to XMI. As I said previously, I did it with Xtext, it works manually but I have some difficulties to insert it in my HMI.

Please find below my HMI:
http://www.zimagez.com/miniature/dsltransformation.png

In the future, I will hide the step of the XMI generation.

Now about my issue:
I tried your code, I can't make it working.
Please find below the error log
org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Contenu non autorisé dans le prologue.
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:195)
	at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:261)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
	at fr.estaca.dbc.convertor.ui.View$3.run(View.java:225)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)
Caused by: org.xml.sax.SAXParseExceptionpublicId: dummy:/example.mydsl; systemId: dummy:/example.mydsl; lineNumber: 1; columnNumber: 1; Contenu non autorisé dans le prologue.
	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
	... 4 more
java.lang.reflect.InvocationTargetException
	at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:398)
	at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:481)
	at fr.estaca.dbc.convertor.ui.View.transform(View.java:272)
	at fr.estaca.dbc.convertor.ui.View.widgetSelected(View.java:168)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:249)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4410)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4228)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3816)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1121)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1022)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:150)
	at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:687)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:604)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
	at fr.estaca.dbc.convertor.ui.Application.start(Application.java:20)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:673)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:610)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1519)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1492)
Caused by: org.eclipse.emf.common.util.BasicEList$BasicIndexOutOfBoundsException: index=0, size=0
	at org.eclipse.emf.common.util.BasicEList.get(BasicEList.java:346)
	at fr.estaca.dbc.convertor.ui.View$3.run(View.java:230)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)


Thanks for your help.

Jean
Re: Xtext parser programmatically for XMI generation [message #1743383 is a reply to message #1743380] Wed, 14 September 2016 18:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the trace looks like you call this code from eclipse (osgi environment). that is a bad idea.
the thing is called standalonesetup for a reason. you may have a look at

http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html

btw why dont you simply load the xtext file in acceleo


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext parser programmatically for XMI generation [message #1743394 is a reply to message #1743383] Wed, 14 September 2016 21:42 Go to previous message
Jean FAAR is currently offline Jean FAARFriend
Messages: 3
Registered: September 2016
Junior Member
Ok, thanks Christian.

I will try to find a solution from the link that you send me.

And I am also looking at include Xtext into acceleo as you advise me, I found old post in the forum:
https://www.eclipse.org/forums/index.php/t/261318/
http://www.eclipse.org/forums/index.php/m/901947/#msg_901947
Previous Topic:xtext discouraged access warning
Next Topic:Maven plug-in for generate code in xText 2.10.0
Goto Forum:
  


Current Time: Wed Apr 24 15:11:27 GMT 2024

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

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

Back to the top