Search in sources :

Example 1 with Variable

use of org.eclipse.xtend.expression.Variable 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

FileNotFoundException (java.io.FileNotFoundException)1 URI (org.eclipse.emf.common.util.URI)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 WorkflowInterruptedException (org.eclipse.emf.mwe.core.WorkflowInterruptedException)1 Variable (org.eclipse.xtend.expression.Variable)1 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)1 Issue (org.eclipse.xtext.validation.Issue)1