Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Standalone Testing Main function
Standalone Testing Main function [message #1726402] Sat, 12 March 2016 16:58 Go to next message
joao antonio is currently offline joao antonioFriend
Messages: 1
Registered: March 2016
Junior Member
Good afternoon,

I'm following an example from Lorenzo Bettini's book from chapter 7.
The idea is to create a main function and to compile a program to check if some input file provides errors.

Here is the code:

public static void main(String[] args) {
		Injector injector = new org.example.entities.EntitiesStandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		Resource resource = resourceSet.getResource(URI.createURI("a.entities"), true);
		System.out.println(resource.getErrors());
		//Model model = (Model) resource.getContents().get(0); //Trigger parsing and validation
	}


The problem I get is that resource.getErrors() does not return any errors provided by my code in the validator, for example. As a fact, I only get errors from XtextLinkingDiagnostic, among other default(?) error providers.

Thanks in advance.

Re: Standalone Testing Main function [message #1726414 is a reply to message #1726402] Sun, 13 March 2016 05:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14067
Registered: July 2009
Senior Member
From the docs

You can use the IResourceValidator to validate a given resource programmatically. Example:

@Inject IResourceValidator resourceValidator

def void checkResource(Resource resource) {
val issues = resourceValidator.validate(resource,
CheckMode.ALL, CancelIndicator.NullImpl)
for (issue: issues) {
switch issue.severity {
case ERROR:
println("ERROR: " + issue.message)
case WARNING:
println("WARNING: " + issue.message)
}
}
}


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
Re: Standalone Testing Main function [message #1726648 is a reply to message #1726402] Tue, 15 March 2016 10:51 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1784
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 12/03/2016 21:48, joao antonio wrote:
> Good afternoon,
>
> I'm following an example from Lorenzo Bettini's book from chapter 7.
> The idea is to create a main function and to compile a program to check
> if some input file provides errors.
>
> Here is the code:
>
>
> public static void main(String[] args) {
> Injector injector = new
> org.example.entities.EntitiesStandaloneSetup().createInjectorAndDoEMFRegistration();
>
> XtextResourceSet resourceSet =
> injector.getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
> Resource resource =
> resourceSet.getResource(URI.createURI("a.entities"), true);
> System.out.println(resource.getErrors());
> //Model model = (Model) resource.getContents().get(0); //Trigger
> parsing and validation
> }
>
>
> The problem I get is that resource.getErrors() does not return any
> errors provided by my code in the validator, for example. As a fact, I
> only get errors from XtextLinkingDiagnostic, among other default(?)
> error providers.
>
> Thanks in advance.
>
>

Hi there

if you followed the book from the beginning, you may have a look at the
Main.java in the entities example to get an idea of how to trigger
validation in the standalone main:

IResourceValidator validator =
injector.getInstance(IResourceValidator.class);

// validate the resource
List<Issue> list = validator.validate(resource, CheckMode.ALL,
CancelIndicator.NullImpl);
if (!list.isEmpty()) {
for (Issue issue : list) {
System.err.println(issue);
}
return;
}

hope this helps
cheers
Lorenzo

--
Prof. Lorenzo Bettini, Computer Science, DISIA, Univ. Firenze
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Previous Topic:Creating Cross-Reference between XExpression and XVariableDeclaration outside of XBlockExpression
Next Topic:Xtext non-LL(*) for Declaration and Assignment
Goto Forum:
  


Current Time: Fri Sep 24 18:58:21 GMT 2021

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

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

Back to the top