Search in sources :

Example 1 with Registry

use of org.eclipse.emf.ecore.EPackage.Registry in project dsl-devkit by dsldevkit.

the class TransformUtil method lookupEPackage.

/**
 * Looks up an EPackage visible in the context of a grammar.
 *
 * @param grammar
 *          context grammar
 * @param packageName
 *          package name
 * @return EPackage or null
 */
public static EPackage lookupEPackage(final Grammar grammar, final String packageName) {
    try {
        for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
            if (decl.getEPackage() != null && packageName.equals(decl.getEPackage().getName())) {
                return decl.getEPackage();
            }
        }
        ResourceSet resourceSet = grammar.eResource().getResourceSet();
        loadImplicitMetamodels(resourceSet);
        // With DerivedStateAwareResources, calling getContents() may cause linking, which may load additional resources into the resource set.
        // If that happens, we get a ConcurrentModificationException in this loop if we just do
        // 
        // for (Resource res : resourceSet.getResources()) { ... }
        // 
        // Try to guard against that by explicitly copying the list. Since there may be other resource types that may do equally crazy things
        // upon getContents(), we do *not* try to guard by only calling getContents() if (!(res instanceof DerivedStateAwareResource)).
        List<Resource> resources = new ArrayList<Resource>(resourceSet.getResources());
        for (Resource res : resources) {
            // language resources. GrammarResources invoke linking here!
            if (!(res instanceof DerivedStateAwareResource) && !res.getContents().isEmpty() && res.getContents().get(0) instanceof EPackage) {
                EPackage epkg = (EPackage) res.getContents().get(0);
                if (packageName.equals(epkg.getName())) {
                    return epkg;
                }
            }
        }
        Registry reg = resourceSet.getPackageRegistry();
        for (String uri : reg.keySet()) {
            EPackage epkg = reg.getEPackage(uri);
            if (packageName.equals(epkg.getName())) {
                return epkg;
            }
        }
    // CHECKSTYLE:OFF
    } catch (Exception e) {
        // CHECKSTYLE:ON
        LOG.error("error looking up package " + packageName, e);
    }
    LOG.info("no package found with name " + packageName);
    return null;
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Registry(org.eclipse.emf.ecore.EPackage.Registry) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with Registry

use of org.eclipse.emf.ecore.EPackage.Registry 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)

Example 3 with Registry

use of org.eclipse.emf.ecore.EPackage.Registry in project xtext-core by eclipse.

the class EcoreUtil2Test method testExternalFormOfEReference.

@Test
public void testExternalFormOfEReference() throws Exception {
    Registry registry = EPackage.Registry.INSTANCE;
    Set<String> uris = Sets.newHashSet(registry.keySet());
    for (String string : uris) {
        EPackage ePackage = registry.getEPackage(string);
        TreeIterator<Object> iterator = EcoreUtil.getAllProperContents(ePackage, true);
        while (iterator.hasNext()) {
            Object next = iterator.next();
            if (next instanceof EReference) {
                EReference ref = (EReference) next;
                String externalForm = EcoreUtil2.toExternalForm(ref);
                assertEquals(ref.toString() + " - " + externalForm, ref, EcoreUtil2.getEReferenceFromExternalForm(registry, externalForm));
            }
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Registry(org.eclipse.emf.ecore.EPackage.Registry) EReference(org.eclipse.emf.ecore.EReference) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Aggregations

EPackage (org.eclipse.emf.ecore.EPackage)3 Registry (org.eclipse.emf.ecore.EPackage.Registry)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 ArrayList (java.util.ArrayList)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 EReference (org.eclipse.emf.ecore.EReference)1 URIConverter (org.eclipse.emf.ecore.resource.URIConverter)1 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)1 Grammar (org.eclipse.xtext.Grammar)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 DerivedStateAwareResource (org.eclipse.xtext.resource.DerivedStateAwareResource)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)1 IScope (org.eclipse.xtext.scoping.IScope)1 AbstractScope (org.eclipse.xtext.scoping.impl.AbstractScope)1 ScopeBasedSelectable (org.eclipse.xtext.scoping.impl.ScopeBasedSelectable)1 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)1 Test (org.junit.Test)1