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);
}
Aggregations