Search in sources :

Example 1 with GeneratedMetamodelImpl

use of org.eclipse.xtext.impl.GeneratedMetamodelImpl in project xtext-core by eclipse.

the class XtextLinkingService method createPackage.

private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) {
    final String nsURI = getMetamodelNsURI(text);
    final URI uri = URI.createURI(nsURI);
    if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI))
        return Collections.emptyList();
    EPackage pack = ((GeneratedMetamodelImpl) generatedMetamodel).basicGetEPackage();
    if (pack != null && !pack.eIsProxy())
        return singletonList((EObject) pack);
    final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage();
    generatedEPackage.setName(generatedMetamodel.getName());
    generatedEPackage.setNsPrefix(generatedMetamodel.getName());
    generatedEPackage.setNsURI(nsURI);
    final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri);
    XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet();
    if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) {
        generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
    } else {
        generatedPackageResource.setURI(URI.createURI(nsURI + "_" + generatedMetamodel.hashCode()));
        generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
    }
    generatedPackageResource.getContents().add(generatedEPackage);
    return Collections.<EObject>singletonList(generatedEPackage);
}
Also used : EcoreResourceFactoryImpl(org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) GeneratedMetamodelImpl(org.eclipse.xtext.impl.GeneratedMetamodelImpl) URI(org.eclipse.emf.common.util.URI) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 EPackage (org.eclipse.emf.ecore.EPackage)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 EcoreResourceFactoryImpl (org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl)1 GeneratedMetamodelImpl (org.eclipse.xtext.impl.GeneratedMetamodelImpl)1 DerivedStateAwareResource (org.eclipse.xtext.resource.DerivedStateAwareResource)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1