Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Create a value converter
Create a value converter [message #1797485] Wed, 31 October 2018 14:29 Go to next message
Nuno Burnay is currently offline Nuno BurnayFriend
Messages: 5
Registered: October 2018
Junior Member
I'm trying to create a converter for a terminal rule of my grammar:

terminal URI_TEMPLATE: '`' .* '`';


I created a conversion class for that purpose:

public class HeadRESTTerminalConverters extends DefaultTerminalConverters {
	
	@Inject
	private PrettyPrintUriTemplate prettyPrint;
	
	@ValueConverter(rule = "URI_TEMPLATE")
	public IValueConverter<Optional<UriTemplate>> URI_TEMPLATE() {
		return new AbstractNullSafeConverter<Optional<UriTemplate>>() {
			@Override
			protected Optional<UriTemplate> internalToValue(String string, INode node) {
				return new UriTemplateParser().parse(string);
			}
			@Override
			protected String internalToString(Optional<UriTemplate> value) {
				return "`" + prettyPrint.print(value.get()) + "`";
			}
		};
	}
}


And I added the class to the runtime module:

class HeadRESTRuntimeModule extends AbstractHeadRESTRuntimeModule {
	
	override Class<? extends IValueConverterService> bindIValueConverterService() {
         HeadRESTTerminalConverters
    }
    
}


However, when generated again the artifacts, the terminal rule stays with the standard type String. Am I missing something?

Thank you.
Re: Create a value converter [message #1797566 is a reply to message #1797485] Thu, 01 November 2018 19:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you actually need a EDatatype (via imported metamodel) to map to a custom Java Class.
then you have to state that the rule produces that datatype in the grammar

import "......" as mymetamodel

terminal URI_TEMPLATE returns mymetamodel::MyDataType: '`' .* '`';


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Create a value converter [message #1797588 is a reply to message #1797566] Fri, 02 November 2018 11:39 Go to previous messageGo to next message
Nuno Burnay is currently offline Nuno BurnayFriend
Messages: 5
Registered: October 2018
Junior Member
Thank you for the answer.

I understood the general idea, however I cannot understand what to put in the "....." part of the import in this case (I'm suppose that must be similar to the 'ecore' case).

Now I have a solution that works, using the ecore::EJavaObject and doing casts in the necessary places, but it seems that your solution is more elegant.
Re: Create a value converter [message #1797594 is a reply to message #1797588] Fri, 02 November 2018 13:15 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
You would do the same as with ecore

Import „nsuriofyourmetamodel" as somename


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Change PLUGIN_ID field in generated UI activator
Next Topic:Xtext upgrade
Goto Forum:
  


Current Time: Fri Apr 26 08:40:49 GMT 2024

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

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

Back to the top