Search in sources :

Example 16 with URIConverter

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

the class FlatResourceSetBasedAllContainersState method containsURI.

@Override
public boolean containsURI(String containerHandle, URI candidateURI) {
    if (!HANDLE.equals(containerHandle))
        return false;
    if (resourceSet instanceof XtextResourceSet) {
        ResourceDescriptionsData descriptionsData = findResourceDescriptionsData(resourceSet);
        if (descriptionsData != null) {
            return descriptionsData.getResourceDescription(candidateURI) != null;
        }
        Collection<URI> allUris = ((XtextResourceSet) resourceSet).getNormalizationMap().values();
        for (URI uri : allUris) {
            if (uri.equals(candidateURI)) {
                return true;
            }
        }
        return false;
    }
    URIConverter uriConverter = resourceSet.getURIConverter();
    for (Resource r : resourceSet.getResources()) {
        URI normalized = uriConverter.normalize(r.getURI());
        if (normalized.equals(candidateURI)) {
            return true;
        }
    }
    return false;
}
Also used : ResourceDescriptionsData(org.eclipse.xtext.resource.impl.ResourceDescriptionsData) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Resource(org.eclipse.emf.ecore.resource.Resource) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 17 with URIConverter

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

the class BuiltInSchemeRegistrar method registerScheme.

/**
 * Configure the resourceSet such that it understands the n4js scheme. Use the given classLoader to lookup the
 * resources.
 *
 * @param classLoader
 *            the classLoader to use.
 */
public void registerScheme(ResourceSet resourceSet, @SuppressWarnings("hiding") ClassLoader classLoader) {
    // tell EMF to resolve a classpath URI which actually has not been a classpath URI (but a SCHEME/n4js
    // URI):
    URIConverter converter = resourceSet.getURIConverter();
    if (registerScheme(converter, classLoader)) {
        ExecutionEnvironmentDescriptor descriptor = new ExecutionEnvironmentDescriptor(resourceSet, classLoader);
        register(resourceSet, descriptor);
    }
}
Also used : URIConverter(org.eclipse.emf.ecore.resource.URIConverter)

Example 18 with URIConverter

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

the class N4JSResource 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 19 with URIConverter

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

the class GenModelUtil2 method getGenModelResource.

/**
 * Returns the genmodel resource for the given model element.
 *
 * @param eModelElement
 *          the model element
 * @return the genmodel resource
 */
private static Resource getGenModelResource(final EModelElement eModelElement) {
    final Resource res = eModelElement.eResource();
    ResourceSet resourceSet = res.getResourceSet();
    if (resourceSet == null) {
        resourceSet = new ResourceSetImpl();
        resourceSet.getResources().add(res);
    }
    URI uri = res.getURI();
    final URIConverter uriConverter = URIConverter.INSTANCE;
    uri = uriConverter.normalize(uri);
    // $NON-NLS-1$
    uri = uri.trimFileExtension().appendFileExtension("genmodel");
    uri = uriConverter.normalize(uri);
    if (uri.scheme().equals("http")) /* toString().equals(EcorePackage.eNS_URI) */
    {
        // optimization, because we are not interested in the extension for the Ecore model.
        return null;
    // otherwise getResource will go on the internet to load the model and we loose 20 seconds on each call!
    }
    if (uriConverter.exists(uri, null)) {
        Resource genModelResource = null;
        try {
            genModelResource = resourceSet.getResource(uri, true);
        } catch (final WrappedException we) {
            throw new IllegalStateException(// $NON-NLS-1$
            "could not retrieve resource for URI " + uri + " please add URI maps for all relevant Ecore models to the workflow.", // $NON-NLS-1$
            we);
        }
        return genModelResource;
    } else {
        return null;
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 20 with URIConverter

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

the class EPackageScopeProvider method scope_EPackage.

/**
 * Scope for {@link EPackage}. These are read from the registry as well as from the {@link Grammar Xtext grammar} corresponding
 * to the scope model (if any).
 *
 * @param context
 *          context scope DSL model (usually the root element, but any object will do)
 * @param reference
 *          context reference (unused for the time being)
 * @return scope with all available {@link EPackage EPackages} with {@link EPackage#getNsURI()} as name
 */
// CHECKSTYLE:OFF
public IScope scope_EPackage(final EObject context, final EReference reference) {
    // CHECKSTYLE:ON
    Resource rsc = context.eResource();
    IScope result = IScope.NULLSCOPE;
    // Add packages from the registry first.
    final Registry packageRegistry = EPackage.Registry.INSTANCE;
    result = new AbstractScope(result, false) {

        @Override
        protected IEObjectDescription getSingleLocalElementByName(final QualifiedName name) {
            return getEPackage(nameConverter.toString(name));
        }

        @Override
        protected Iterable<IEObjectDescription> getAllLocalElements() {
            return Iterables.filter(Iterables.transform(Sets.newHashSet(packageRegistry.keySet()), this::getEPackage), Predicates.notNull());
        }

        private IEObjectDescription getEPackage(final String nsURI) {
            try {
                EPackage ePackage = packageRegistry.getEPackage(nsURI);
                return ePackage != null ? EObjectDescription.create(QualifiedName.create(nsURI), ePackage) : null;
            // CHECKSTYLE:OFF
            } catch (Exception e) {
                // CHECKSTYLE:ON
                // $NON-NLS-1$
                LOG.warn("could not load package " + nsURI, e);
                return null;
            }
        }
    };
    // Add the index
    IResourceDescriptions descriptions = descriptionsProvider.getResourceDescriptions(context.eResource());
    if (descriptions != null) {
        result = SelectableBasedScope.createScope(result, descriptions, EcorePackage.Literals.EPACKAGE, false);
    }
    // Add the global scope
    result = SelectableBasedScope.createScope(result, new ScopeBasedSelectable(globalScopeProvider.getScope(rsc, reference, null)), EcorePackage.Literals.EPACKAGE, false);
    // Now add all packages from the grammar
    final URI grammarUri = rsc.getURI().trimFileExtension().appendFileExtension(XTEXT_EXTENSION);
    final ResourceSet resourceSet = rsc.getResourceSet();
    final URIConverter uriConverter = resourceSet.getURIConverter();
    if (uriConverter.exists(grammarUri, null)) {
        final Resource grammarResource = resourceSet.getResource(grammarUri, true);
        if (grammarResource != null && !grammarResource.getContents().isEmpty()) {
            final Grammar grammar = (Grammar) grammarResource.getContents().get(0);
            final IScope parent = result;
            result = new SimpleScope(parent, Iterables.transform(Iterables.filter(getGrammarEPackages(grammar), Predicates.notNull()), new Function<EPackage, IEObjectDescription>() {

                @Override
                public IEObjectDescription apply(final EPackage param) {
                    return EObjectDescription.create(param.getNsURI(), param);
                }
            }));
        }
    }
    return result;
}
Also used : AbstractScope(org.eclipse.xtext.scoping.impl.AbstractScope) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Resource(org.eclipse.emf.ecore.resource.Resource) Registry(org.eclipse.emf.ecore.EPackage.Registry) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) EPackage(org.eclipse.emf.ecore.EPackage) ScopeBasedSelectable(org.eclipse.xtext.scoping.impl.ScopeBasedSelectable) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) IScope(org.eclipse.xtext.scoping.IScope) URIConverter(org.eclipse.emf.ecore.resource.URIConverter)

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