Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parsing inmemory text using Xtext outside of Eclipse
Parsing inmemory text using Xtext outside of Eclipse [message #1765725] Tue, 13 June 2017 21:14 Go to next message
Santhosh Kumar is currently offline Santhosh KumarFriend
Messages: 44
Registered: March 2011
Member
Thank you for this example which shows how to load a full file [0].

However, we have a requirement to parse a inmemory text not stored in the file (for ex., use enters DSL in a webform, which we need to parse and validate). How do I go about parsing arbitrary text?

[0] https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F
Re: Parsing inmemory text using Xtext outside of Eclipse [message #1765727 is a reply to message #1765725] Tue, 13 June 2017 21:51 Go to previous messageGo to next message
Santhosh Kumar is currently offline Santhosh KumarFriend
Messages: 44
Registered: March 2011
Member
After some research, got this below snippet working fine. Is this correct?

	public static MyDslModel createModel(String dslText) {
		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("test.mydsl")); 
		try {
			resource.load(new ByteArrayInputStream(dslText.getBytes("UTF-8")), new HashMap<>());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		MyDslModel model = (MyDslModel) resource.getContents().get(0);
		
		return model;
	}
Re: Parsing inmemory text using Xtext outside of Eclipse [message #1765870 is a reply to message #1765725] Wed, 14 June 2017 06:28 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 748
Registered: July 2009
Location: Dortmund, Germany
Senior Member

I think you should better use resourceSet.getResource() instead of create/load.
Besides that it looks OK.
Re: Parsing inmemory text using Xtext outside of Eclipse [message #1766226 is a reply to message #1765870] Sun, 18 June 2017 22:18 Go to previous messageGo to next message
Santhosh Kumar is currently offline Santhosh KumarFriend
Messages: 44
Registered: March 2011
Member
@karsten, I tried to do
Resource resource = resourceSet.getResource(URI.createURI("test.mydsl"), true);
but errors out saying file not found. It is resolving the test.mydsl against workspace. Since that file not really exists and we are trying to load inmemory text, not sure if that will work out with that call.
Re: Parsing inmemory text using Xtext outside of Eclipse [message #1766228 is a reply to message #1766226] Sun, 18 June 2017 23:57 Go to previous messageGo to next message
Santhosh Kumar is currently offline Santhosh KumarFriend
Messages: 44
Registered: March 2011
Member
resourceSet.createResource started failing for second time we create same time. Not having complete knowledge about how EMF resource works, I adopted this version of utils which deletes the resource after parsing. Hope this is fine. Any feedback is appreciated.

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;

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

import com.MyDsl.MyDslStandaloneSetup;
import com.MyDsl.MyDsl.MyDslModel;
import com.google.inject.Injector;

public class MyDslUtils {
	private static final XtextResourceSet resourceSet;

	static {
		Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
	}

	public static MyDslVisitor parse(String dslText) {

		URI uri = URI.createURI("temp-" + Math.abs(Math.random()) + ".mydsl");
		Resource resource = resourceSet.getResources().stream().filter(res -> res.getURI().toFileString().equals(uri.toFileString())).findFirst()
				.orElseGet(() -> resourceSet.createResource(uri));

		try {
			resource.unload();
			resource.load(new ByteArrayInputStream(dslText.getBytes("UTF-8")), new HashMap<>());
			EList<EObject> contents = resource.getContents();
			if (contents.isEmpty()) {
				return null;
			}

			MyDslModel model = (MyDslModel) contents.get(0);

			return new MyDslVisitor(model);

		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				resource.delete(new HashMap<>());
			} catch (IOException e) {
			}
		}

	}

	public static void main(String[] args) {
		MyDslVisitor visitor = MyDslUtils.parse("load into account");
	}
}
Re: Parsing inmemory text using Xtext outside of Eclipse [message #1766240 is a reply to message #1766228] Mon, 19 June 2017 08:03 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14067
Registered: July 2009
Senior Member
where do you call this code from?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext UI project vs Xtext Ide project
Next Topic:XBase: Overloading '.' operations
Goto Forum:
  


Current Time: Sat Sep 25 09:19:07 GMT 2021

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

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

Back to the top