Search in sources :

Example 6 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-core by eclipse.

the class AbstractResourceDescription method getNormalizedURI.

protected URI getNormalizedURI(Resource resource) {
    URI uri = resource.getURI();
    URIConverter uriConverter = resource.getResourceSet() != null ? resource.getResourceSet().getURIConverter() : null;
    if (uri != null && uriConverter != null) {
        if (!uri.isPlatform()) {
            return uriConverter.normalize(uri);
        }
        // EMF's GenModelResourceDescriptionStrategy does so as it needs to call GenModel.reconcile() eagerly.
        if (uri.isPlatformPlugin()) {
            URI resourceURI = uri.replacePrefix(URI.createURI("platform:/plugin/"), URI.createURI("platform:/resource/"));
            if (uriConverter.normalize(uri).equals(uriConverter.normalize(resourceURI)))
                return resourceURI;
        }
    }
    return uri;
}
Also used : URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 7 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-core by eclipse.

the class EcoreUtil2 method isValidUri.

/**
 * checks whether the given URI can be loaded given the context. I.e. there's a resource set with a corresponding
 * resource factory and the physical resource exists.
 */
public static boolean isValidUri(Resource resource, URI uri) {
    if (uri == null || uri.isEmpty()) {
        return false;
    }
    URI newURI = getResolvedImportUri(resource, uri);
    try {
        ResourceSet resourceSet = resource.getResourceSet();
        if (resourceSet.getResource(uri, false) != null)
            return true;
        URIConverter uriConverter = resourceSet.getURIConverter();
        URI normalized = uriConverter.normalize(newURI);
        if (normalized != null)
            // fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=326760
            if ("platform".equals(normalized.scheme()) && !normalized.isPlatform())
                return false;
        return uriConverter.exists(normalized, Collections.emptyMap());
    } catch (RuntimeException e) {
        // thrown by org.eclipse.emf.ecore.resource.ResourceSet#getResource(URI, boolean)
        log.trace("Cannot load resource: " + newURI, e);
    }
    return false;
}
Also used : ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 8 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project n4js by eclipse.

the class BuiltInSchemeAwareResource method createNewURIConverter.

private URIConverter createNewURIConverter() {
    URIConverter result = new ExtensibleURIConverterImpl();
    registrar.registerScheme(result);
    return result;
}
Also used : ExtensibleURIConverterImpl(org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl) URIConverter(org.eclipse.emf.ecore.resource.URIConverter)

Example 9 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project tdq-studio-se by Talend.

the class TDQFileItemImpl method eResolveProxy.

// similar with FileItemImpl's same method, because this class can not extend that class.so copy the method from
// FileItemImpl.
@Override
public EObject eResolveProxy(InternalEObject proxy) {
    URI proxyUri = proxy.eProxyURI();
    URI resourceUri = proxyUri.trimFragment();
    // bug 0020095
    if (eResource() == null) {
        return super.eResolveProxy(proxy);
    }
    ResourceSet resourceSet = eResource().getResourceSet();
    ByteArrayResource byteArrayResource = null;
    URIConverter theURIConverter = resourceSet.getURIConverter();
    URI normalizedURI = theURIConverter.normalize(resourceUri);
    // TUP-1814:because the routine proxyUri is File type,need handle with it.
    if ((proxyUri.isPlatform() && proxyUri.segmentCount() > 1 && "resource".equals(proxyUri.segment(0))) || proxyUri.isFile()) {
        List<Resource> resources = resourceSet.getResources();
        synchronized (resources) {
            for (Resource resource : resources) {
                if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI) && resource instanceof ByteArrayResource) {
                    byteArrayResource = (ByteArrayResource) resource;
                    break;
                }
            }
        }
        if (byteArrayResource == null) {
            byteArrayResource = new ByteArrayResource(resourceUri);
            resourceSet.getResources().add(byteArrayResource);
        }
        try {
            byteArrayResource.load(null);
        } catch (IOException e) {
        }
    } else {
        List<Resource> resources = resourceSet.getResources();
        synchronized (resources) {
            for (Resource resource : resources) {
                if (theURIConverter.normalize(resource.getURI()).equals(normalizedURI)) {
                    byteArrayResource = (ByteArrayResource) resource;
                    break;
                }
            }
        }
    }
    if (byteArrayResource != null && byteArrayResource.isLoaded()) {
        EObject object = byteArrayResource.getEObject(proxyUri.fragment().toString());
        if (object != null) {
            return object;
        }
    }
    throw new UnsupportedOperationException();
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ByteArrayResource(org.talend.core.model.properties.helper.ByteArrayResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ByteArrayResource(org.talend.core.model.properties.helper.ByteArrayResource) IOException(java.io.IOException) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 10 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project dsl-devkit by dsldevkit.

the class CheckGenModelUtil method findGenModelInContainers.

/**
 * Given an array of {@link IContainers}, searches through all *.genmodel files in all {@link IFolders}, ignoring nested folders, trying to find a
 * {@link GenModel} for the given {@link EPackage}.
 *
 * @param containers
 *          To search, may be empty, but must not be {@code null}
 * @param baseURI
 *          of these containers; must not be {@code null}
 * @param ePackage
 *          to find a genmodel for; must not be {@code null}
 * @param resourceSet
 *          to use for resource loading; must not be {@code null}
 * @return the genmodel, if found, or {@code null} if not.
 * @throws CoreException
 *           if enumerating folder contents does so
 */
private static GenModel findGenModelInContainers(final IContainer[] containers, final URI baseURI, final EPackage ePackage, final ResourceSet resourceSet) throws CoreException {
    Preconditions.checkNotNull(containers);
    Preconditions.checkNotNull(baseURI);
    Preconditions.checkNotNull(ePackage);
    Preconditions.checkNotNull(resourceSet);
    final URIConverter uriConverter = resourceSet.getURIConverter();
    for (IContainer container : containers) {
        if (!(container instanceof IFolder)) {
            continue;
        }
        IResource[] resources = container.members();
        for (IResource r : resources) {
            if (r.exists() && r instanceof IFile && GENMODEL_EXTENSION.equals(r.getFileExtension())) {
                URI uriToTry = uriConverter.normalize(baseURI.appendSegment(URI.encodeSegment(r.getName(), false)));
                GenModel result = loadGenModel(uriToTry, ePackage, resourceSet);
                if (result != null) {
                    return result;
                }
            }
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) IContainer(org.eclipse.core.resources.IContainer) URI(org.eclipse.emf.common.util.URI) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

URIConverter (org.eclipse.emf.ecore.resource.URIConverter)22 URI (org.eclipse.emf.common.util.URI)18 Resource (org.eclipse.emf.ecore.resource.Resource)9 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)6 InputStream (java.io.InputStream)4 ExtensibleURIConverterImpl (org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl)4 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)4 IOException (java.io.IOException)3 IContainer (org.eclipse.core.resources.IContainer)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 GenModel (org.eclipse.emf.codegen.ecore.genmodel.GenModel)2 EPackage (org.eclipse.emf.ecore.EPackage)2 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)2 Grammar (org.eclipse.xtext.Grammar)2 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)2 ResourceDescriptionsData (org.eclipse.xtext.resource.impl.ResourceDescriptionsData)2 XtextHyperlink (org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink)2 Test (org.junit.Test)2 ByteArrayResource (org.talend.core.model.properties.helper.ByteArrayResource)2