Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Create a value converter
Create a value converter [message #1797485] Wed, 31 October 2018 10:29 Go to next message
Eclipse UserFriend
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 15:18 Go to previous messageGo to next message
Eclipse UserFriend
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: '`' .* '`';
Re: Create a value converter [message #1797588 is a reply to message #1797566] Fri, 02 November 2018 07:39 Go to previous messageGo to next message
Eclipse UserFriend
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 09:15 Go to previous message
Eclipse UserFriend
You would do the same as with ecore

Import „nsuriofyourmetamodel" as somename
Previous Topic:Change PLUGIN_ID field in generated UI activator
Next Topic:Xtext upgrade
Goto Forum:
  


Current Time: Wed Jun 18 18:07:03 EDT 2025

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

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

Back to the top