Search in sources :

Example 1 with IStorage2UriMapper

use of org.eclipse.xtext.ui.resource.IStorage2UriMapper in project xtext-eclipse by eclipse.

the class EPackageChooser method createResourceSet.

private ResourceSet createResourceSet(URI genModelUri) {
    ResourceSetImpl resourceSet;
    if (genModelUri.fileExtension().equals("xcore")) {
        IResourceServiceProvider resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE.getResourceServiceProvider(genModelUri);
        IStorage2UriMapper storage2UriMapper = resourceServiceProvider.get(IStorage2UriMapper.class);
        IProject project = storage2UriMapper.getStorages(genModelUri).iterator().next().getSecond();
        resourceSet = (ResourceSetImpl) resourceServiceProvider.get(IResourceSetProvider.class).get(project);
    } else {
        resourceSet = new ResourceSetImpl();
    }
    Resource ecorePackageResource = EcorePackage.eINSTANCE.eResource();
    Map<URI, Resource> uriResourceMap = Maps.newHashMap();
    uriResourceMap.put(URI.createPlatformResourceURI(PATH_TO_ECORE_ECORE, true), ecorePackageResource);
    uriResourceMap.put(URI.createPlatformPluginURI(PATH_TO_ECORE_ECORE, true), ecorePackageResource);
    resourceSet.setURIResourceMap(uriResourceMap);
    return resourceSet;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) IStorage2UriMapper(org.eclipse.xtext.ui.resource.IStorage2UriMapper) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) IResourceSetProvider(org.eclipse.xtext.ui.resource.IResourceSetProvider)

Example 2 with IStorage2UriMapper

use of org.eclipse.xtext.ui.resource.IStorage2UriMapper in project xtext-eclipse by eclipse.

the class TraceEditor method getLocalStorage.

protected IStorage getLocalStorage(IResourceServiceProvider serviceProvider, URI traceURI) {
    TraceFileNameProvider fileNameProvider = serviceProvider.get(TraceFileNameProvider.class);
    IStorage2UriMapper storage2UriMapper = serviceProvider.get(IStorage2UriMapper.class);
    String generatedName = fileNameProvider.getJavaFromTrace(traceURI.lastSegment());
    URI generatedURI = traceURI.trimSegments(1).appendSegment(generatedName);
    for (Pair<IStorage, IProject> x : storage2UriMapper.getStorages(generatedURI)) return x.getFirst();
    throw new IllegalStateException("Could not find IStorage for " + generatedURI);
}
Also used : TraceFileNameProvider(org.eclipse.xtext.generator.trace.TraceFileNameProvider) IStorage2UriMapper(org.eclipse.xtext.ui.resource.IStorage2UriMapper) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IProject(org.eclipse.core.resources.IProject)

Example 3 with IStorage2UriMapper

use of org.eclipse.xtext.ui.resource.IStorage2UriMapper 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

URI (org.eclipse.emf.common.util.URI)3 IStorage2UriMapper (org.eclipse.xtext.ui.resource.IStorage2UriMapper)3 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 IFile (org.eclipse.core.resources.IFile)1 IStorage (org.eclipse.core.resources.IStorage)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)1 TraceFileNameProvider (org.eclipse.xtext.generator.trace.TraceFileNameProvider)1 MarkerCreator (org.eclipse.xtext.ui.editor.validation.MarkerCreator)1 IResourceSetProvider (org.eclipse.xtext.ui.resource.IResourceSetProvider)1 IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)1