Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to Integrate Xtext into other projects(How to integrate a language written in Xtext into another project.)
How to Integrate Xtext into other projects [message #1771522] Sat, 26 August 2017 16:11 Go to next message
Robert Klinger is currently offline Robert KlingerFriend
Messages: 10
Registered: July 2017
Junior Member
Hello,

is there a recommended way to integrate a language written in Xtext into another project?

For example: I want to write a JavaFX application an within this application the user should be able to write his programm into the UI, the UI should then be read by the JavaFX application and the text transfered to Xtext so that Xtext generates code.

A possible solution would be to "manually" (JavaFX-Application writes and saves) write into the eclipse IDE and save to invoke the generator of the Xtext-framework. This would obviously be very bad.

Basically: Is it possible to manually call the scanner, parser etc. of a certain Xtext project with another Java-app?

Greetings,
Robert
Re: How to Integrate Xtext into other projects [message #1771523 is a reply to message #1771522] Sat, 26 August 2017 16:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
Yes see https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1771524 is a reply to message #1771523] Sat, 26 August 2017 16:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
Have a look at https://www.eclipse.org/forums/index.php/t/1075480/ too

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1771539 is a reply to message #1771522] Sun, 27 August 2017 10:09 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 550
Registered: July 2009
Location: Dortmund, Germany
Senior Member

You might want to consider embedding an Xtext editor e.g. via a Web editor embedded in a Browser widget. This way you get rich support of your language without caring too much with the integration.
Re: How to Integrate Xtext into other projects [message #1772049 is a reply to message #1771539] Mon, 04 September 2017 19:59 Go to previous messageGo to next message
Robert Klinger is currently offline Robert KlingerFriend
Messages: 10
Registered: July 2017
Junior Member
Thank you.

I ve used following code in a Java-Application. This code does work if I run it inside the xtext-project in a java file. I ve imported manually "org.eclipse.xtext_....jar", "org.eclipse.emf.common_....jar", "org.eclipse.emf.ecore_....jar", "com.google.inject_....jar" and "org.eclipse.emf.mwe.utils_....jar".

ToolLanguagePackage.eINSTANCE.eClass();
Injector injector = new ToolLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();		
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		
Resource resource = resourceSet.getResource(URI.createURI("platform:/resource/org.xtext.example.mydsl/src/example.mydsl"), true);
		
Model model = (Model) resource.getContents().get(0);		
System.out.println(model.getGreetings().get(0).getName());


I get this exception:
java.lang.reflect.InvocationTargetException
	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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
	at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
	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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.xml.sax.SAXParseExceptionpublicId: platform:/resource/org.xtext.example.mydsl/src/example.mydsl; systemId: platform:/resource/org.xtext.example.mydsl/src/example.mydsl; lineNumber: 1; columnNumber: 1; Content ist nicht zulässig in Prolog.
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
	at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:265)
	at org.eclipse.xtext.resource.SynchronizedXtextResourceSet.getResource(SynchronizedXtextResourceSet.java:25)
	at application.Main.main(Main.java:60)
	... 11 more
Caused by: org.xml.sax.SAXParseExceptionpublicId: platform:/resource/org.xtext.example.mydsl/src/example.mydsl; systemId: platform:/resource/org.xtext.example.mydsl/src/example.mydsl; lineNumber: 1; columnNumber: 1; Content ist nicht zulässig in Prolog.
	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)
	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 org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
	... 14 more
Exception running application application.Main


Especially the message: "platform:/resource/org.xtext.example.mydsl/src/example.mydsl; systemId: platform:/resource/org.xtext.example.mydsl/src/example.mydsl; lineNumber: 1; columnNumber: 1; Content ist nicht zulässig in Prolog."

I am not quiet getting what this message means


____________________________________________________________
The idea with web integration sounds great though. How can I integrate my xtext grammar into the web and use it in a java-Application?


Greeting and thank you for your help,
Robert Klinger
Re: How to Integrate Xtext into other projects [message #1772056 is a reply to message #1772049] Tue, 05 September 2017 04:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
Platform resource is a eclipse Uri and not a standalone Uri.
Are you sure the file extension is correct?


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1772057 is a reply to message #1772056] Tue, 05 September 2017 04:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
And how you you export this? Using the export runnable jar wizard ?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1772117 is a reply to message #1772057] Tue, 05 September 2017 17:03 Go to previous messageGo to next message
Robert Klinger is currently offline Robert KlingerFriend
Messages: 10
Registered: July 2017
Junior Member
The file extension should be correct.

I am not exporting anything manually yet. Should I?

I am not sure If I really understood on how to import the language the right way.
I ve used Properties - Java Build Path and added under project the org.xtext.toolLanguage and under libraries the 5 .jars mentioned in the other post. Am I missing something?

The platform resource: I am working in one Eclipse workspace with the Xtext-Project and another Java-Project. Should this work or am I missing here something? :)

___________________________________________________
I have another question though and don´t want to make a new topic out of it: Is the xtext-File written Xtext? So is there a grammar for the file extension "xtext" and if so, is it public?

___________________________________________________
EDIT: I somehow fixed it, I am not sure how, but it works. Maybe the file could not get parsed properly with the grammar, I am not sure though. Sadly I was not really able to understand the exceptions I got :)

The code now:
//imports

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;

import org.xtext.ToolLanguageStandaloneSetup;
import org.xtext.toolLanguage.Model;
import org.xtext.toolLanguage.ToolLanguagePackage;

//these would be the imports for the default dsl
//import org.xtext.example.mydsl.MyDslStandaloneSetup; 
//import org.xtext.example.mydsl.myDsl.Model;
//import org.xtext.toolLanguage.Model;


import com.google.inject.Injector;
//main-Code
Injector injector = new ToolLanguageStandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		
		Resource resource = resourceSet.getResource(URI.createFileURI("./example.toolLanguage"), true);
		Model model = (Model) resource.getContents().get(0);

		System.out.println(model.getRules());		


I manually added under properties - Java Build Path:
Projects: The xtext Project needed
Libraries:
com.google.inject_3.0.0...
org.eclipse.emf.common_2.12.0...
org.eclipse.emf.ecore_2.12.0...
org.eclipse.xtext_2.11.0...

You should find some of these jars referenced in the Xtext-Project too :)
Though you might want to consider using some build-tool or something :D

___________________________________
Again thank you very much for your help, specially thank you Christian Dietrich, you helped me a lot so far :)

[Updated on: Tue, 05 September 2017 17:30]

Report message to a moderator

Re: How to Integrate Xtext into other projects [message #1772118 is a reply to message #1772117] Tue, 05 September 2017 17:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
can you please share an example project via git or zip

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1772120 is a reply to message #1772118] Tue, 05 September 2017 17:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
btw do you know you can create a xtext project with maven or gradle build in the wizard wich might make it easier to do the packaging and dependency mgmt right?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: How to Integrate Xtext into other projects [message #1772122 is a reply to message #1772120] Tue, 05 September 2017 18:55 Go to previous messageGo to next message
Robert Klinger is currently offline Robert KlingerFriend
Messages: 10
Registered: July 2017
Junior Member
I don t know maven nor do I know gradle and I only want to test a simple implementation :) In future projects I would use them but thank you for the tip :)

Is the language for the xtext-files written as a .xtext-grammar?
Re: How to Integrate Xtext into other projects [message #1772124 is a reply to message #1772122] Tue, 05 September 2017 19:09 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11367
Registered: July 2009
Senior Member
Yes Xtext is written with Xtext

https://github.com/eclipse/xtext-core/blob/master/org.eclipse.xtext/src/org/eclipse/xtext/Xtext.xtext


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com

[Updated on: Tue, 05 September 2017 19:11]

Report message to a moderator

Previous Topic:Error 404 with CORS enabled
Next Topic:Xtext and e4 Application
Goto Forum:
  


Current Time: Tue Nov 21 12:16:14 GMT 2017

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

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