Search in sources :

Example 6 with WorkflowInterruptedException

use of org.eclipse.emf.mwe.core.WorkflowInterruptedException in project dsl-devkit by dsldevkit.

the class FormatFragmentUtil method getFormatModel.

/**
 * Retrieve the format model associated with a given grammar.
 * <p>
 * <em>Note</em>: Expected to either be in same folder with the same name (except for the extension) or in the SRC outlet.
 * </p>
 *
 * @param grammar
 *          the grammar, must not be {@code null}
 * @param context
 *          xpand execution context, must not be {@code null}
 * @return the format model, or {@code null} if the resource could not be loaded
 * @throws FileNotFoundException
 *           thrown if the format file could not be found
 */
@SuppressWarnings("PMD.NPathComplexity")
public static FormatConfiguration getFormatModel(final Grammar grammar, final XpandExecutionContext context) throws FileNotFoundException {
    Variable resourceUriVariable = context.getVariable("resourceUri");
    if (resourceUriVariable == null) {
        return null;
    }
    URI uri = (URI) resourceUriVariable.getValue();
    final Resource grammarResource = grammar.eResource();
    final ResourceSet resourceSet = grammarResource.getResourceSet();
    Resource formatResource = null;
    try {
        formatResource = resourceSet.getResource(uri, true);
    } catch (final ClasspathUriResolutionException e) {
        // make another attempt
        uri = getDefaultFormatLocation(grammar, context);
        try {
            formatResource = resourceSet.getResource(uri, true);
        } catch (WrappedException e1) {
            formatResource = resourceSet.getResource(uri, false);
            if (formatResource != null) {
                resourceSet.getResources().remove(formatResource);
            }
            // NOPMD
            throw new FileNotFoundException(uri.toString());
        }
    }
    if (formatResource == null) {
        throw new FileNotFoundException(uri.toString());
    }
    final List<Issue> issues = getModelValidator().validate(formatResource, LOG);
    for (final Issue issue : issues) {
        if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
            throw new WorkflowInterruptedException("Errors found in " + uri.toString() + ": " + issue.getMessage());
        }
    }
    return formatResource.getContents().size() == 0 ? null : (FormatConfiguration) formatResource.getContents().get(0);
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) Variable(org.eclipse.xtend.expression.Variable) Issue(org.eclipse.xtext.validation.Issue) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) WorkflowInterruptedException(org.eclipse.emf.mwe.core.WorkflowInterruptedException) Resource(org.eclipse.emf.ecore.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Aggregations

WorkflowInterruptedException (org.eclipse.emf.mwe.core.WorkflowInterruptedException)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 Issue (org.eclipse.xtext.validation.Issue)4 URI (org.eclipse.emf.common.util.URI)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1 EClass (org.eclipse.emf.ecore.EClass)1 EClassifier (org.eclipse.emf.ecore.EClassifier)1 EPackage (org.eclipse.emf.ecore.EPackage)1 WorkflowContext (org.eclipse.emf.mwe.core.WorkflowContext)1 Variable (org.eclipse.xtend.expression.Variable)1 IndexTestLanguageStandaloneSetup (org.eclipse.xtext.index.IndexTestLanguageStandaloneSetup)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 Test (org.junit.Test)1