Search in sources :

Example 6 with IResourceValidator

use of org.eclipse.xtext.validation.IResourceValidator in project snow-owl by b2ihealthcare.

the class SnomedEclLabelerRequestTest method setup.

@Before
public void setup() {
    final IParser parser = ECL_INJECTOR.getInstance(IParser.class);
    final IResourceValidator resourceValidator = ECL_INJECTOR.getInstance(IResourceValidator.class);
    final ISerializer serializer = ECL_INJECTOR.getInstance(ISerializer.class);
    Builder contextBuilder = TestBranchContext.on(MAIN).with(EclParser.class, new DefaultEclParser(parser, resourceValidator)).with(EclSerializer.class, new DefaultEclSerializer(serializer)).with(Index.class, rawIndex()).with(RevisionIndex.class, index()).with(ObjectMapper.class, getMapper());
    CodeSystemResource.configureCodeSystem(contextBuilder);
    context = contextBuilder.build();
}
Also used : IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) RevisionIndex(com.b2international.index.revision.RevisionIndex) Builder(com.b2international.snowowl.test.commons.snomed.TestBranchContext.Builder) IParser(org.eclipse.xtext.parser.IParser) ISerializer(org.eclipse.xtext.serializer.ISerializer) Before(org.junit.Before)

Example 7 with IResourceValidator

use of org.eclipse.xtext.validation.IResourceValidator in project dsl-devkit by dsldevkit.

the class ValidMarkerUpdateJob method run.

/**
 * {@inheritDoc}
 */
@Override
protected IStatus run(final IProgressMonitor monitor) {
    final IResourceValidator resourceValidator = resourceServiceProvider.getResourceValidator();
    // Object the list of resources
    final List<URI> uris = getResourceDescriptionURIs();
    // Let's start (number of task = number of resource * 2 (loading + validating))
    // $NON-NLS-1$
    monitor.beginTask("", uris.size() * 2);
    for (final URI uri : uris) {
        // Last chance to cancel before next validation
        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }
        // Get the file; only local files will be re-validated, derived files are ignored
        final IFile iFile = getFileFromStorageMapper(uri);
        if (iFile != null && !iFile.isDerived(IFile.CHECK_ANCESTORS)) {
            // $NON-NLS-1$
            monitor.subTask("loading " + iFile.getName());
            // Load the corresponding resource
            boolean loaded = false;
            Resource eResource = null;
            try {
                eResource = resourceSet.getResource(uri, false);
                if ((eResource == null) || (eResource != null && !eResource.isLoaded())) {
                    // if the resource does not exist in the resource set, or is not loaded yet
                    // load it.
                    eResource = resourceSet.getResource(uri, true);
                    loaded = true;
                }
                monitor.worked(1);
            // CHECKSTYLE:OFF
            } catch (final RuntimeException e) {
                // CHECKSTYLE:ON
                // $NON-NLS-1$
                LOGGER.error(MessageFormat.format("{0} could not be validated.", iFile.getName()), e);
            } finally {
                if (eResource != null) {
                    validate(resourceValidator, iFile, eResource, monitor);
                    if (loaded) {
                        // NOPMD
                        // unload any resource that was previously loaded as part of this loop.
                        eResource.unload();
                    }
                }
            }
        }
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI)

Example 8 with IResourceValidator

use of org.eclipse.xtext.validation.IResourceValidator in project n4js by eclipse.

the class OwnResourceValidatorAwareValidatingEditorCallback method newValidationJob.

private ValidationJob newValidationJob(final XtextEditor editor) {
    final IXtextDocument document = editor.getDocument();
    final IAnnotationModel annotationModel = editor.getInternalSourceViewer().getAnnotationModel();
    final IssueResolutionProvider issueResolutionProvider = getService(editor, IssueResolutionProvider.class);
    final MarkerTypeProvider markerTypeProvider = getService(editor, MarkerTypeProvider.class);
    final MarkerCreator markerCreator = getService(editor, MarkerCreator.class);
    final IValidationIssueProcessor issueProcessor = new CompositeValidationIssueProcessor(new AnnotationIssueProcessor(document, annotationModel, issueResolutionProvider), new MarkerIssueProcessor(editor.getResource(), markerCreator, markerTypeProvider));
    return editor.getDocument().modify(resource -> {
        final IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider();
        final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
        return new ValidationJob(resourceValidator, editor.getDocument(), issueProcessor, ALL);
    });
}
Also used : IValidationIssueProcessor(org.eclipse.xtext.ui.editor.validation.IValidationIssueProcessor) MarkerTypeProvider(org.eclipse.xtext.ui.validation.MarkerTypeProvider) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IssueResolutionProvider(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) MarkerCreator(org.eclipse.xtext.ui.editor.validation.MarkerCreator) MarkerIssueProcessor(org.eclipse.xtext.ui.editor.validation.MarkerIssueProcessor) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AnnotationIssueProcessor(org.eclipse.xtext.ui.editor.validation.AnnotationIssueProcessor) ValidationJob(org.eclipse.xtext.ui.editor.validation.ValidationJob)

Example 9 with IResourceValidator

use of org.eclipse.xtext.validation.IResourceValidator in project n4js by eclipse.

the class N4HeadlessCompiler method validateProject.

/*
	 * ===============================================================================================================
	 *
	 * PROJECT VALIDATION
	 *
	 * ===============================================================================================================
	 */
/**
 * Validates all non-external Xtext resources of the given project. Prints issues and adds them to the given issue
 * acceptor.
 *
 * @param markedProject
 *            the project to validate
 * @param recorder
 *            the progress recorder
 * @param issueAcceptor
 *            the issue acceptor
 * @throws N4JSCompileErrorException
 *             if an error occurs during validation
 */
private void validateProject(MarkedProject markedProject, N4ProgressStateRecorder recorder, IssueAcceptor issueAcceptor) throws N4JSCompileErrorException {
    if (logger.isVerbose())
        logger.info(" Validating project " + markedProject);
    IssueCollector issueCollector = new IssueCollector();
    IssueFilter issueFilter = new IssueFilter(issueCollector, issue -> issue.getSeverity() == Severity.ERROR);
    issueAcceptor = new IssueAcceptorTee(issueAcceptor, issueFilter);
    // validation TODO see IDE-1426 redesign validation calls with generators
    for (Resource resource : markedProject.resources) {
        if (// is Xtext resource
        resource instanceof XtextResource && // is validating
        (!n4jsCore.isNoValidate(resource.getURI())) && // not in external folder
        (!markedProject.externalResources.contains(resource))) {
            if (logger.isCreateDebugOutput())
                logger.debug("   Validating resource " + resource.getURI());
            XtextResource xtextResource = (XtextResource) resource;
            IResourceValidator validator = xtextResource.getResourceServiceProvider().getResourceValidator();
            List<Issue> issues = validator.validate(xtextResource, CheckMode.ALL, CancelIndicator.NullImpl);
            if (!issues.isEmpty()) {
                recorder.markResourceIssues(resource, issues);
                issueAcceptor.acceptAll(issues);
                issues.stream().forEach(logger::issue);
            }
        }
    }
    // Projects should not compile if there are severe errors:
    if (!isKeepOnCompiling()) {
        failOnErrors(issueCollector.getCollectedIssues(), markedProject.project.getProjectId());
    }
}
Also used : Issue(org.eclipse.xtext.validation.Issue) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 10 with IResourceValidator

use of org.eclipse.xtext.validation.IResourceValidator in project dsl-devkit by dsldevkit.

the class CheckMarkerUpdateJob method run.

/**
 * {@inheritDoc}
 */
@Override
protected IStatus run(final IProgressMonitor monitor) {
    // Let's start (number of task = number of resource * 2 (loading + validating))
    // $NON-NLS-1$
    monitor.beginTask("", 2 * this.uris.size());
    for (final URI uri : this.uris) {
        // Last chance to cancel before next validation
        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }
        final IResourceServiceProvider serviceProvider = serviceProviderRegistry.getResourceServiceProvider(uri);
        if (serviceProvider == null) {
            // This may happen for non-Xtext resources in ice entities
            if (LOGGER.isDebugEnabled()) {
                // $NON-NLS-1$
                LOGGER.debug(MessageFormat.format("Could not validate {0}: no resource service provider found", uri.toString()));
            }
            // Skip to next URI
            continue;
        }
        final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
        final IStorage2UriMapper uriMapper = serviceProvider.get(IStorage2UriMapper.class);
        final MarkerCreator markerCreator = serviceProvider.get(MarkerCreator.class);
        // Get the file; only local files will be re-validated, derived files are ignored
        final IFile iFile = getFileFromStorageMapper(uriMapper, uri);
        if (iFile == null) {
            // no storage mapping found for this URI
            continue;
        }
        if (resourceValidator == null) {
            // $NON-NLS-1$
            LOGGER.error(MessageFormat.format("Could not validate {0}: no resource validator found", iFile.getName()));
        } else if (iFile != null) {
            // $NON-NLS-1$
            monitor.subTask("loading " + iFile.getName());
            // Don't try to evaluate resource set before it has been checked that the storage provider contains a mapping
            // for current uri
            final ResourceSet resourceSet = getResourceSet(uriMapper, uri);
            // Load the corresponding resource
            boolean loaded = false;
            Resource eResource = null;
            try {
                eResource = resourceSet.getResource(uri, false);
                if ((eResource == null) || (eResource != null && !eResource.isLoaded())) {
                    // if the resource does not exist in the resource set, or is not loaded yet
                    // load it.
                    eResource = resourceSet.getResource(uri, true);
                    loaded = true;
                }
                monitor.worked(1);
            // CHECKSTYLE:OFF
            } catch (final RuntimeException e) {
                // CHECKSTYLE:ON
                // $NON-NLS-1$
                LOGGER.error(MessageFormat.format("{0} could not be validated.", iFile.getName()), e);
            } finally {
                if (eResource != null) {
                    validateAndCreateMarkers(resourceValidator, markerCreator, iFile, eResource, monitor);
                    // $NON-NLS-1$
                    LOGGER.debug("Validated " + uri);
                    if (loaded) {
                        // NOPMD
                        // unload any resource that was previously loaded as part of this loop.
                        eResource.unload();
                    }
                }
            }
        }
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IFile(org.eclipse.core.resources.IFile) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) IStorage2UriMapper(org.eclipse.xtext.ui.resource.IStorage2UriMapper) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) MarkerCreator(org.eclipse.xtext.ui.editor.validation.MarkerCreator)

Aggregations

IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)23 Issue (org.eclipse.xtext.validation.Issue)18 XtextResource (org.eclipse.xtext.resource.XtextResource)16 Test (org.junit.Test)14 Resource (org.eclipse.emf.ecore.resource.Resource)7 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)4 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 URI (org.eclipse.emf.common.util.URI)2 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)2 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)2 MarkerCreator (org.eclipse.xtext.ui.editor.validation.MarkerCreator)2 RevisionIndex (com.b2international.index.revision.RevisionIndex)1 Builder (com.b2international.snowowl.test.commons.snomed.TestBranchContext.Builder)1 Injector (com.google.inject.Injector)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 Model (org.eclipse.vorto.core.api.model.model.Model)1