Java Code Examples for org.eclipse.xtext.validation.Issue#getSeverity()

The following examples show how to use org.eclipse.xtext.validation.Issue#getSeverity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuildRequest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean afterValidate(URI validated, Iterable<Issue> issues) {
	for (Issue issue : issues) {
		Severity severity = issue.getSeverity();
		if (severity != null) {
			switch (severity) {
			case ERROR:
				LOG.error(issue.toString());
				return false;
			case WARNING:
				LOG.warn(issue.toString());
				break;
			case INFO:
				LOG.info(issue.toString());
				break;
			case IGNORE:
				LOG.debug(issue.toString());
				break;
			}
		}
	}
	return true;
}
 
Example 2
Source File: ComparisonExpressionValidator.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
public IStatus validateXtextResource(final Injector injector, Resource resource, ResourceSet resourceSet) throws OperationCanceledError {
      final IResourceValidator xtextResourceChecker = injector.getInstance(IResourceValidator.class);
final MultiStatus status = new MultiStatus(ExpressionEditorPlugin.PLUGIN_ID, 0, "", null);
      final ConditionModelJavaValidator validator = injector.getInstance(ConditionModelJavaValidator.class);
      validator.setCurrentResourceSet(resourceSet);
final List<Issue> issues = xtextResourceChecker.validate(resource, CheckMode.FAST_ONLY, null);
if(issues.isEmpty()){
	updateDependencies(resource);
}
for(final Issue issue : issues){
	int severity = IStatus.ERROR;
	final Severity issueSeverity = issue.getSeverity();
	if(issueSeverity == Severity.WARNING){
		severity = IStatus.WARNING;
	}
	status.add(new Status(severity, ExpressionEditorPlugin.PLUGIN_ID, issue.getMessage()));
}

return status;
  }
 
Example 3
Source File: TestAssertions.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Assert that the given issue is inside the list of issues.
 *
 * @param issues the list of issues.
 * @param severity the expected severity.
 * @param model the issued model.
 * @param objectType the type of the object.
 * @param code the issue code.
 * @param messageParts the parts of the issue message that are expected.
 */
public static void assertIssue(List<Issue> issues, Severity severity, EObject model,
		EClass objectType, String code, String... messageParts) {
	Iterator<Issue> iterator = issues.iterator();
	while (iterator.hasNext()) {
		Issue issue = iterator.next();
		if (Objects.equal(issue.getCode(), code) && issue.getSeverity() == severity) {
			EObject object = model.eResource().getResourceSet().getEObject(issue.getUriToProblem(), true);
			if (objectType.isInstance(object)) {
				if (TestIssues.isIssueMessage(issue, messageParts)) {
					iterator.remove();
					return;
				}
			}
		}
	}
	StringBuilder message = new StringBuilder("Expected ");
	message.append(severity);
	message.append(" '");
	message.append(code);
	message.append("' on ");
	message.append(objectType.getName());
	message.append(" but got\n");
	TestIssues.getIssuesAsString(model, issues, message);
	fail(message.toString());
}
 
Example 4
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Output the given issues that result from the compilation of the SARL code.
 *
 * @param issues the issues to report.
 * @return {@code true} if at least one error was reported, {@code false} if
 *      no error was reported.
 */
protected boolean reportCompilationIssues(Iterable<Issue> issues) {
	boolean hasError = false;
	for (final Issue issue : issues) {
		final String issueMessage = createIssueMessage(issue);
		switch (issue.getSeverity()) {
		case ERROR:
			hasError = true;
			getLogger().severe(issueMessage);
			break;
		case WARNING:
			getLogger().warning(issueMessage);
			break;
		case INFO:
			getLogger().info(issueMessage);
			break;
		case IGNORE:
		default:
			break;
		}
		notifiesIssueMessageListeners(issue, issue.getUriToProblem(), issueMessage);
	}
	return hasError;
}
 
Example 5
Source File: ModelValidator.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Log issue.
 *
 * @param resource
 *          the resource
 * @param issue
 *          the issue
 * @param logger
 *          the logger
 */
private void logIssue(final Resource resource, final Issue issue, final Logger logger) {
  final String message = NLS.bind(MESSAGE_TEMPLATE, new Object[] {resource.getURI().lastSegment(), issue.getLineNumber(), issue.getMessage()});
  final Severity severity = issue.getSeverity();
  switch (severity) {
  case ERROR:
    logger.error(message);
    break;
  case WARNING:
    logger.warn(message);
    break;
  case INFO:
    if (logger.isInfoEnabled()) {
      logger.info(message);
    }
    break;

  default:
    break;
  }
}
 
Example 6
Source File: XpectN4JSES5GeneratorHelper.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private boolean registerErrors(Resource dep, StringBuilder errorResult) {
	boolean hasErrors = false;
	List<Issue> issues = resourceValidator.validate(dep, CheckMode.ALL, CancelIndicator.NullImpl);
	List<Issue> errorIssues = new ArrayList<>();
	for (Issue issue : issues) {
		if (Severity.ERROR == issue.getSeverity()) {
			errorIssues.add(issue);
		}
	}
	hasErrors = !errorIssues.isEmpty();
	if (hasErrors) {
		errorResult.append("Couldn't compile resource " + dep.getURI() + " because it contains errors: ");
		for (Issue errorIssue : errorIssues) {
			errorResult
					.append(NL + errorIssue.getMessage() + " at line " + errorIssue.getLineNumber());
		}
	}
	return hasErrors;
}
 
Example 7
Source File: IIssueHandler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param issues
 *            Validation issues to handle
 * @return <code>true</code> if an {@link Issue} with
 *         {@link Severity#ERROR} was found, <code>false</code>
 *         otherwise
 */
@Override
public boolean handleIssue(Iterable<Issue> issues) {
	boolean errorFree = true;
	for (Issue issue : issues) {
		switch (issue.getSeverity()) {
		case ERROR:
			LOG.error(issue.toString());
			errorFree = false;
			break;
		case WARNING:
			LOG.warn(issue.toString());
			break;
		case INFO:
			LOG.info(issue.toString());
			break;
		default:
			break;
		}
	}
	return errorFree;
}
 
Example 8
Source File: LSPIssue.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Copy constructor
 */
public LSPIssue(Issue copyFrom) {
	this();
	if (copyFrom.getOffset() != null)
		this.setOffset(copyFrom.getOffset());
	if (copyFrom.getLength() != null)
		this.setLength(copyFrom.getLength());
	if (copyFrom.getColumn() != null)
		this.setColumn(copyFrom.getColumn());
	if (copyFrom.getLineNumber() != null)
		this.setLineNumber(copyFrom.getLineNumber());
	if (copyFrom.getCode() != null)
		this.setCode(copyFrom.getCode());
	if (copyFrom.getMessage() != null)
		this.setMessage(copyFrom.getMessage());
	if (copyFrom.getUriToProblem() != null)
		this.setUriToProblem(copyFrom.getUriToProblem());
	if (copyFrom.getSeverity() != null)
		this.setSeverity(copyFrom.getSeverity());
	if (copyFrom.getType() != null)
		this.setType(copyFrom.getType());
	if (copyFrom.getData() != null)
		this.setData(copyFrom.getData());
}
 
Example 9
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void reportIssues(Iterable<Issue> issues) {
	for (Issue issue : issues) {
		StringBuilder issueBuilder = createIssueMessage(issue);
		if (Severity.ERROR == issue.getSeverity()) {
			log.error(issueBuilder.toString());
		} else if (Severity.WARNING == issue.getSeverity()) {
			log.warn(issueBuilder.toString());
		}
	}
}
 
Example 10
Source File: Validator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void validate(ResourceSet resourceSet, IResourceServiceProvider.Registry registry, Issues issues) {
	List<Resource> resources = Lists.newArrayList(resourceSet.getResources());
	for (Resource resource : resources) {
		try {
			resource.load(null);
			IResourceServiceProvider provider = registry.getResourceServiceProvider(resource.getURI());
			if (provider != null) {
				List<Issue> result = provider.getResourceValidator().validate(resource, CheckMode.ALL, null);
				for (Issue issue : result) {
					switch (issue.getSeverity()) {
						case ERROR:
							issues.addError(issue.getMessage(), issue);
							break;
						case WARNING:
							issues.addWarning(issue.getMessage(), issue);
							break;
						case INFO:
							issues.addInfo(issue.getMessage(), issue);
							break;
						case IGNORE:
							break;
					}
				}
			}
		} catch (IOException e) {
			throw new WorkflowInterruptedException("Couldn't load resource (" + resource.getURI() + ")", e);
		}
	}
	if (isStopOnError() && issues.hasErrors()) {
		String errorMessage = toString(issues);
		throw new WorkflowInterruptedException("Validation problems: \n" + errorMessage);
	}
}
 
Example 11
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert the given issues to diagnostics. Does not return any issue with severity {@link Severity#IGNORE ignore}
 * by default.
 */
protected List<Diagnostic> toDiagnostics(Iterable<? extends Issue> issues) {
	List<Diagnostic> result = new ArrayList<>();
	for (Issue issue : issues) {
		if (issue.getSeverity() != Severity.IGNORE) {
			result.add(toDiagnostic(issue));
		}
	}
	return result;
}
 
Example 12
Source File: FormatFragmentUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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);
      }
      throw new FileNotFoundException(uri.toString()); // NOPMD
    }
  }
  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);
}
 
Example 13
Source File: ValidValidatorFragment.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the valid model.
 *
 * @param grammar
 *          the grammar
 * @return the valid model
 */
private ValidModel getValidModel(final Grammar grammar) {
  if (model != null) {
    return model;
  }

  Resource resource = null;
  final String name = GrammarUtil.getSimpleName(grammar) + '.' + XTEXT_EXTENSION;
  URI uri;
  for (final Resource res : grammar.eResource().getResourceSet().getResources()) {
    if (res.getURI() != null && name.equals(EmfResourceUtil.getFileName(res.getURI()))) {
      resource = res;
      break;
    }
  }
  if (getValidURI() == null) {
    Assert.isNotNull(resource, NLS.bind(Messages.RESOURCE_NOT_FOUND, name));
    uri = resource.getURI().trimFileExtension().appendFileExtension(VALID_EXTENSION);
  } else {
    uri = URI.createURI(getValidURI());
  }

  resource = resource.getResourceSet().getResource(uri, true);

  final List<Issue> issues = VALIDATOR.validate(resource, LOGGER);
  for (final Issue issue : issues) {
    if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
      throw new WorkflowInterruptedException(NLS.bind(Messages.ERROR_FOUND, uri.toString()));
    }
  }
  model = (ValidModel) resource.getContents().get(0);
  return model;
}
 
Example 14
Source File: XtextEditorErrorTickUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Severity getSeverity(XtextEditor xtextEditor) {
	if (xtextEditor == null || xtextEditor.getInternalSourceViewer() == null)
		return null;
	IAnnotationModel model = xtextEditor.getInternalSourceViewer().getAnnotationModel();
	if (model != null) {
		Iterator<Annotation> iterator = model.getAnnotationIterator();
		boolean hasWarnings = false;
		boolean hasInfos = false;
		while (iterator.hasNext()) {
			Annotation annotation = iterator.next();
			if (!annotation.isMarkedDeleted()) {
				Issue issue = issueUtil.getIssueFromAnnotation(annotation);
				if (issue != null) {
					if (issue.getSeverity() == Severity.ERROR) {
						return Severity.ERROR;
					} else if (issue.getSeverity() == Severity.WARNING) {
						hasWarnings = true;
					} else if (issue.getSeverity() == Severity.INFO) {
						hasInfos = true;
					}
				}
			}
		}
		if (hasWarnings)
			return Severity.WARNING;
		if (hasInfos)
			return Severity.INFO;
	}
	return null;
}
 
Example 15
Source File: MarkerCreator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private Object getSeverity(Issue issue) {
	switch (issue.getSeverity()) {
		case ERROR : 
			return IMarker.SEVERITY_ERROR;
		case WARNING : 
			return IMarker.SEVERITY_WARNING;
		case INFO : 
			return IMarker.SEVERITY_INFO;
		default:
			throw new IllegalArgumentException(String.valueOf(issue.getSeverity()));
	}
}
 
Example 16
Source File: XtendBatchCompiler.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean apply(Issue issue) {
	return this.severity == issue.getSeverity();
}
 
Example 17
Source File: ExportFragment.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Get the export model that we have to process.
 *
 * @param grammar
 *          The grammar
 * @return The model
 */
private synchronized ExportModel getModel(final Grammar grammar) { // NOPMD NPathComplexity by wth on 24.11.10 08:22
  if (modelLoaded) {
    return model;
  }
  modelLoaded = true;
  Resource resource = grammar.eResource();
  if (resource == null) {
    return null;
  }

  final ResourceSet resourceSet = resource.getResourceSet();
  URI uri = null;
  if (getExportFileURI() != null) {
    uri = URI.createURI(getExportFileURI());
  } else {
    uri = grammar.eResource().getURI().trimFileExtension().appendFileExtension(EXPORT_FILE_EXTENSION);
  }
  try {
    resource = resourceSet.getResource(uri, true);
    final List<Issue> issues = VALIDATOR.validate(resource, LOGGER);

    for (final Issue issue : issues) {
      if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
        throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_EXPORT_ERRORS, uri));
      }
    }
    if (resource.getContents().size() == 0) {
      return null;
    }
    model = (ExportModel) resource.getContents().get(0);
    return model;
  } catch (final ClasspathUriResolutionException e) {
    // Resource does not exist.
    if (getExportFileURI() != null) {
      // Explicit file specified, but not found: stop the workflow.
      throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_NO_EXPORT_FILE, uri)); // NOPMD PreserveStackTrace by wth on 24.11.10 08:27
    }
    // No file found at implicit location: work with a null model, generating code that implements the default behavior.
    return null;
  }
}