Search in sources :

Example 56 with ResourceSet

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

the class EMFGeneratorFragment2 method generate.

@Override
public void generate() {
    boolean _isEmpty = IterableExtensions.isEmpty(Iterables.<GeneratedMetamodel>filter(this.getGrammar().getMetamodelDeclarations(), GeneratedMetamodel.class));
    if (_isEmpty) {
        return;
    }
    try {
        final Grammar clonedGrammar = this.cloneGrammarIntoNewResourceSet(this.getGrammar());
        final ResourceSet workingResourceSet = clonedGrammar.eResource().getResourceSet();
        final Function1<GeneratedMetamodel, EPackage> _function = (GeneratedMetamodel it) -> {
            return it.getEPackage();
        };
        final List<EPackage> generatedPackages = IterableExtensions.<EPackage>toList(IterableExtensions.<GeneratedMetamodel, EPackage>map(Iterables.<GeneratedMetamodel>filter(clonedGrammar.getMetamodelDeclarations(), GeneratedMetamodel.class), _function));
        if ((this.genModel != null)) {
            this.registerUsedGenModel(workingResourceSet.getURIConverter(), clonedGrammar);
        }
        boolean _isEmpty_1 = generatedPackages.isEmpty();
        boolean _not = (!_isEmpty_1);
        if (_not) {
            final Map<String, EPackage> usedEPackages = this.findAllUsedEPackages(generatedPackages);
            final Map<String, EPackage> loadedEPackages = this.findEPackagesInGenPackages(usedEPackages.keySet(), workingResourceSet);
            final Map<EObject, EObject> eNamedElementMapping = this.createENamedElementMapping(usedEPackages, loadedEPackages);
            this.replaceReferencesInGeneratedPackages(generatedPackages, eNamedElementMapping);
            final Resource ePackageResource = this.createResourceForEPackages(clonedGrammar, generatedPackages, workingResourceSet);
            if ((!this.skipGenerate)) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("Generating EMF model");
                {
                    if (this.generateEdit) {
                        {
                            if (this.generateEditor) {
                                _builder.append(", edit");
                            } else {
                                _builder.append(" and edit");
                            }
                        }
                    }
                }
                {
                    if (this.generateEditor) {
                        _builder.append(" and editor");
                    }
                }
                _builder.append(" code");
                EMFGeneratorFragment2.LOG.info(_builder);
                final GenModel genModel = this.getSaveAndReconcileGenModel(clonedGrammar, generatedPackages, workingResourceSet);
                genModel.reconcile();
                this.doGenerate(genModel);
                this.addProjectContributions(clonedGrammar, generatedPackages, workingResourceSet);
                if (this.bindEPackageAndEFactory) {
                    for (final EPackage pkg : generatedPackages) {
                        {
                            final GenPackage genPkg = GenModelUtil2.getGenPackage(pkg, genModel.eResource().getResourceSet());
                            GuiceModuleAccess.BindingFactory _bindingFactory = new GuiceModuleAccess.BindingFactory();
                            TypeReference _typeRef = TypeReference.typeRef(genPkg.getQualifiedPackageInterfaceName());
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    String _packageInterfaceName = genPkg.getPackageInterfaceName();
                                    _builder.append(_packageInterfaceName);
                                    _builder.append(".eINSTANCE");
                                }
                            };
                            GuiceModuleAccess.BindingFactory _addTypeToInstance = _bindingFactory.addTypeToInstance(_typeRef, _client);
                            TypeReference _typeRef_1 = TypeReference.typeRef(genPkg.getQualifiedFactoryInterfaceName());
                            StringConcatenationClient _client_1 = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    String _factoryInterfaceName = genPkg.getFactoryInterfaceName();
                                    _builder.append(_factoryInterfaceName);
                                    _builder.append(".eINSTANCE");
                                }
                            };
                            _addTypeToInstance.addTypeToInstance(_typeRef_1, _client_1).contributeTo(this.getLanguage().getRuntimeGenModule());
                        }
                    }
                }
            }
            this.saveResource(ePackageResource);
        }
        this.updateBuildProperties();
    } catch (final Throwable _t) {
        if (_t instanceof Exception) {
            final Exception e = (Exception) _t;
            EMFGeneratorFragment2.LOG.error("Failed to execute EMF generator", e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : GuiceModuleAccess(org.eclipse.xtext.xtext.generator.model.GuiceModuleAccess) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Grammar(org.eclipse.xtext.Grammar) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) EPackage(org.eclipse.emf.ecore.EPackage) GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Example 57 with ResourceSet

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

the class GrammarAccessFragment2 method addAllGrammarsToResource.

protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) {
    boolean _add = visitedGrammars.add(grammar);
    boolean _not = (!_add);
    if (_not) {
        return;
    }
    resource.getContents().add(grammar);
    EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations();
    for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) {
        {
            final EPackage pack = metamodelDecl.getEPackage();
            final Resource packResource = pack.eResource();
            String _string = packResource.getURI().toString();
            String _nsURI = pack.getNsURI();
            boolean _notEquals = (!Objects.equal(_string, _nsURI));
            if (_notEquals) {
                final ResourceSet packResourceSet = packResource.getResourceSet();
                if ((packResourceSet != null)) {
                    EPackage topMost = pack;
                    while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) {
                        topMost = topMost.getESuperPackage();
                    }
                    if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) {
                        boolean _isEmpty = topMost.getEClassifiers().isEmpty();
                        boolean _not_1 = (!_isEmpty);
                        if (_not_1) {
                            packResource.setURI(URI.createURI(topMost.getNsURI()));
                        } else {
                            this.moveSubpackagesToNewResource(topMost, resource.getResourceSet());
                        }
                    }
                    boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI());
                    boolean _not_2 = (!_equals);
                    if (_not_2) {
                        this.movePackageToNewResource(topMost, resource.getResourceSet());
                    }
                }
            }
        }
    }
    EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
    for (final Grammar usedGrammar : _usedGrammars) {
        this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars);
    }
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) FragmentFakingEcoreResource(org.eclipse.xtext.xtext.generator.grammarAccess.FragmentFakingEcoreResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 58 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project tdi-studio-se by Talend.

the class ImportItemUtil method createResource.

private Resource createResource(ItemRecord itemRecord, IPath path, boolean byteArrayResource) throws FileNotFoundException {
    Resource resource;
    ResourceSet resourceSet = itemRecord.getResourceSet();
    if (byteArrayResource) {
        resource = new ByteArrayResource(getURI(path));
        resourceSet.getResources().add(resource);
    } else {
        if (FileConstants.ITEM_EXTENSION.equals(path.getFileExtension())) {
            String projectName = "";
            if (itemRecord.getItemProject() != null) {
                projectName = itemRecord.getItemProject().getTechnicalLabel();
                projectName = projectName.toLowerCase();
            }
            // note: do similar code as the CwmResourceFactory
            //$NON-NLS-1$
            String business = projectName + "/businessProcess/";
            //$NON-NLS-1$
            String context = projectName + "/context/";
            //$NON-NLS-1$
            String process = projectName + "/process/";
            //$NON-NLS-1$
            String joblet = projectName + "/joblets/";
            String pathString = path.toPortableString();
            pathString = pathString.toLowerCase();
            // PTODO, maybe will bring bugs, like mr job,route, maybe jobscript
            if (pathString.contains(process) || pathString.contains(context) || pathString.contains(business) || pathString.contains(joblet)) {
                resource = new TalendXMIResource(getURI(path));
            } else {
                resource = new CwmResource(getURI(path));
            }
            resourceSet.getResources().add(resource);
        } else {
            resource = resourceSet.createResource(getURI(path));
        }
    }
    return resource;
}
Also used : CwmResource(org.talend.model.emf.CwmResource) TalendXMIResource(org.talend.commons.runtime.model.emf.TalendXMIResource) CwmResource(org.talend.model.emf.CwmResource) TalendXMIResource(org.talend.commons.runtime.model.emf.TalendXMIResource) 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)

Example 59 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project benchmarx by eMoflon.

the class MediniQVTFamiliesToPersonsConfig method saveModels.

/**
	 * Allows to save the current state of the source and target models
	 * 
	 * @param name : Filename 
	 */
public void saveModels(String name) {
    ResourceSet set = new ResourceSetImpl();
    set.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
    URI srcURI = URI.createFileURI(RESULTPATH + "/" + name + "Family.xmi");
    URI trgURI = URI.createFileURI(RESULTPATH + "/" + name + "Person.xmi");
    Resource resSource = set.createResource(srcURI);
    Resource resTarget = set.createResource(trgURI);
    EObject colSource = EcoreUtil.copy(getSourceModel());
    EObject colTarget = EcoreUtil.copy(getTargetModel());
    resSource.getContents().add(colSource);
    resTarget.getContents().add(colTarget);
    try {
        resSource.save(null);
        resTarget.save(null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) EObject(org.eclipse.emf.ecore.EObject) XMIResourceFactoryImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Example 60 with ResourceSet

use of org.eclipse.emf.ecore.resource.ResourceSet in project xtext-xtend by eclipse.

the class EclipseFileSystemTest method testIssue383.

@Test
public void testIssue383() {
    if ((this.fs instanceof AbstractFileSystemSupport)) {
        final AbstractFileSystemSupport afs = ((AbstractFileSystemSupport) this.fs);
        final IProject px = this.createProject("px");
        this.createProject("py");
        final ResourceSet rs = this.resourceSetProvider.get(px);
        final Resource rx = rs.createResource(URI.createPlatformResourceURI("px/foo/xxxx", true));
        final Resource rxb = rs.createResource(URI.createPlatformResourceURI("px/bar/yyyy", true));
        final Resource ry = rs.createResource(URI.createPlatformResourceURI("py/bar/xxxx", true));
        Path pathx = afs.getPath(rx);
        Assert.assertEquals("/px/foo/xxxx", pathx.toString());
        Path pathxb = afs.getPath(rxb);
        Assert.assertEquals("/px/bar/yyyy", pathxb.toString());
        Path pathy = afs.getPath(ry);
        Assert.assertEquals("/py/bar/xxxx", pathy.toString());
    } else {
        Assert.fail("fs is no AbstractFileSystemSupport");
    }
}
Also used : Path(org.eclipse.xtend.lib.macro.file.Path) AbstractFileSystemSupport(org.eclipse.xtend.core.macro.AbstractFileSystemSupport) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test) JavaIoFileSystemTest(org.eclipse.xtend.ide.tests.macros.JavaIoFileSystemTest)

Aggregations

ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)123 Resource (org.eclipse.emf.ecore.resource.Resource)71 Test (org.junit.Test)51 EObject (org.eclipse.emf.ecore.EObject)34 URI (org.eclipse.emf.common.util.URI)32 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)32 IChangeSerializer (org.eclipse.xtext.ide.serializer.IChangeSerializer)21 InMemoryURIHandler (org.eclipse.xtext.testing.util.InMemoryURIHandler)21 IEmfResourceChange (org.eclipse.xtext.ide.serializer.IEmfResourceChange)20 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)17 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)17 XtextResource (org.eclipse.xtext.resource.XtextResource)16 EPackage (org.eclipse.emf.ecore.EPackage)12 IFile (org.eclipse.core.resources.IFile)10 Node (org.eclipse.xtext.ide.tests.testlanguage.partialSerializationTestLanguage.Node)10 ArrayList (java.util.ArrayList)9 EClass (org.eclipse.emf.ecore.EClass)9 ChangeSerializer (org.eclipse.xtext.ide.serializer.impl.ChangeSerializer)9 IOException (java.io.IOException)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)8