Search in sources :

Example 6 with EPackageRegistryImpl

use of org.eclipse.emf.ecore.impl.EPackageRegistryImpl in project sirius-web by eclipse-sirius.

the class DocumentService method getBytes.

/**
 * Returns the byte array of the serialization of the given document. The document can be serialized with a
 * {@link JsonResource} or an {@link XMIResource}.
 *
 * @param document
 *            The document to serialize
 * @param resourceKind
 *            The resource kind used to determine which {@link Resource} will be used to serialize the document
 * @return The byte array to the serialized document
 */
@Override
public Optional<byte[]> getBytes(Document document, String resourceKind) {
    Optional<byte[]> optionalBytes = Optional.empty();
    Resource outputResource = null;
    Map<String, Object> options = new HashMap<>();
    if (RESOURCE_KIND_JSON.equals(resourceKind)) {
        optionalBytes = Optional.of(document.getContent().getBytes());
    } else if (RESOURCE_KIND_XMI.equals(resourceKind)) {
        outputResource = new XMIResourceImpl(URI.createURI(document.getName()));
        options.put(XMIResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
        options.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
        options.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE);
    }
    if (outputResource == null) {
        return optionalBytes;
    }
    EPackageRegistryImpl ePackageRegistryImpl = new EPackageRegistryImpl();
    List<EPackage> ePackages = this.editingContextEPackageService.getEPackages(document.getProject().getId().toString());
    ePackages.forEach(ePackage -> ePackageRegistryImpl.put(ePackage.getNsURI(), ePackage));
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.setPackageRegistry(ePackageRegistryImpl);
    URI uri = URI.createURI(document.getName());
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
    resourceSet.getResources().add(resource);
    resourceSet.getResources().add(outputResource);
    try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
        resource.load(inputStream, new HashMap<>());
        outputResource.getContents().addAll(resource.getContents());
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            outputResource.save(outputStream, options);
            optionalBytes = Optional.of(outputStream.toByteArray());
        }
    } catch (IOException exception) {
        this.logger.warn(exception.getMessage(), exception);
    }
    return optionalBytes;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) HashMap(java.util.HashMap) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Resource(org.eclipse.emf.ecore.resource.Resource) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) EPackage(org.eclipse.emf.ecore.EPackage) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)

Example 7 with EPackageRegistryImpl

use of org.eclipse.emf.ecore.impl.EPackageRegistryImpl in project sirius-web by eclipse-sirius.

the class EditingDomainFactory method create.

public AdapterFactoryEditingDomain create() {
    ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory();
    composedAdapterFactory.addAdapterFactory(new EcoreAdapterFactory());
    composedAdapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());
    EPackage.Registry ePackageRegistry = new EPackageRegistryImpl();
    ePackageRegistry.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE);
    AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(composedAdapterFactory, new BasicCommandStack());
    ResourceSet resourceSet = editingDomain.getResourceSet();
    resourceSet.setPackageRegistry(ePackageRegistry);
    resourceSet.eAdapters().add(new ECrossReferenceAdapter());
    return editingDomain;
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ECrossReferenceAdapter(org.eclipse.emf.ecore.util.ECrossReferenceAdapter) ReflectiveItemProviderAdapterFactory(org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ComposedAdapterFactory(org.eclipse.emf.edit.provider.ComposedAdapterFactory) BasicCommandStack(org.eclipse.emf.common.command.BasicCommandStack) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EcoreAdapterFactory(org.eclipse.emf.ecore.util.EcoreAdapterFactory) EPackage(org.eclipse.emf.ecore.EPackage)

Example 8 with EPackageRegistryImpl

use of org.eclipse.emf.ecore.impl.EPackageRegistryImpl in project sirius-web by eclipse-sirius.

the class ProjectExportService method loadAllDocuments.

/**
 * Load all documents of the given project in a {@link ResourceSet}.
 *
 * @param projectId
 *            the ID of the project to export.
 * @return a {@link ResourceSet} containing all project documents.
 */
private ResourceSet loadAllDocuments(String projectId) {
    List<Document> documents = this.documentService.getDocuments(projectId);
    EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
    this.editingContextEPackageService.getEPackages(projectId).forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));
    ResourceSet resourceSet = new ResourceSetImpl();
    for (Document document : documents) {
        ResourceSet loadingResourceSet = new ResourceSetImpl();
        loadingResourceSet.setPackageRegistry(ePackageRegistry);
        URI uri = URI.createURI(document.getId().toString());
        JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
        loadingResourceSet.getResources().add(resource);
        Optional<byte[]> optionalBytes = this.documentService.getBytes(document, IDocumentService.RESOURCE_KIND_JSON);
        if (optionalBytes.isPresent()) {
            try (var inputStream = new ByteArrayInputStream(optionalBytes.get())) {
                resource.load(inputStream, null);
                resourceSet.getResources().add(resource);
            } catch (IOException exception) {
                this.logger.warn(exception.getMessage(), exception);
            }
        }
    }
    return resourceSet;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) Document(org.eclipse.sirius.web.services.api.document.Document) URI(org.eclipse.emf.common.util.URI)

Example 9 with EPackageRegistryImpl

use of org.eclipse.emf.ecore.impl.EPackageRegistryImpl in project sirius-components by eclipse-sirius.

the class EditingDomainFactory method create.

public AdapterFactoryEditingDomain create(Resource... resources) {
    ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory();
    composedAdapterFactory.addAdapterFactory(new EcoreAdapterFactory());
    composedAdapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory());
    EPackage.Registry ePackageRegistry = new EPackageRegistryImpl();
    ePackageRegistry.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE);
    AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(composedAdapterFactory, new BasicCommandStack());
    ResourceSet resourceSet = editingDomain.getResourceSet();
    for (Resource resource : resources) {
        resourceSet.getResources().add(resource);
    }
    resourceSet.setPackageRegistry(ePackageRegistry);
    resourceSet.eAdapters().add(new ECrossReferenceAdapter());
    return editingDomain;
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ECrossReferenceAdapter(org.eclipse.emf.ecore.util.ECrossReferenceAdapter) ReflectiveItemProviderAdapterFactory(org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ComposedAdapterFactory(org.eclipse.emf.edit.provider.ComposedAdapterFactory) BasicCommandStack(org.eclipse.emf.common.command.BasicCommandStack) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EcoreAdapterFactory(org.eclipse.emf.ecore.util.EcoreAdapterFactory) EPackage(org.eclipse.emf.ecore.EPackage)

Example 10 with EPackageRegistryImpl

use of org.eclipse.emf.ecore.impl.EPackageRegistryImpl in project sirius-components by eclipse-sirius.

the class ViewValidatorTests method testNodeStyleDescriptionValidDomainInPackageRegistry.

@Test
public void testNodeStyleDescriptionValidDomainInPackageRegistry() {
    Map<Object, Object> defaultContext = Diagnostician.INSTANCE.createDefaultContext();
    NodeDescription nodeDescription = ViewFactory.eINSTANCE.createNodeDescription();
    nodeDescription.setDomainType(SAMPLE_ENTITY_NAME);
    ResourceSetImpl resourceSet = new ResourceSetImpl();
    XMIResourceImpl viewResource = new XMIResourceImpl();
    viewResource.getContents().add(nodeDescription);
    resourceSet.getResources().add(viewResource);
    EPackageRegistryImpl packageRegistryImpl = new EPackageRegistryImpl();
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName(SAMPLE_DOMAIN_NAME);
    ePackage.setNsPrefix(SAMPLE_DOMAIN_NAME);
    // $NON-NLS-1$
    ePackage.setNsURI("domain://sample");
    EClass sampleClass = EcoreFactory.eINSTANCE.createEClass();
    sampleClass.setName(SAMPLE_ENTITY_NAME);
    ePackage.getEClassifiers().add(sampleClass);
    packageRegistryImpl.put(ePackage.getNsURI(), ePackage);
    resourceSet.setPackageRegistry(packageRegistryImpl);
    BasicDiagnostic diagnosticChain = new BasicDiagnostic(Diagnostic.OK, null, 0, null, null);
    boolean validationResult = new DiagramDescriptionValidator().validate(nodeDescription.eClass(), nodeDescription, diagnosticChain, defaultContext);
    assertThat(validationResult).isTrue();
    assertThat(diagnosticChain).isEqualTo(new BasicDiagnostic(Diagnostic.OK, null, 0, null, null));
}
Also used : NodeDescription(org.eclipse.sirius.components.view.NodeDescription) EClass(org.eclipse.emf.ecore.EClass) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) EPackage(org.eclipse.emf.ecore.EPackage) DiagramDescriptionValidator(org.eclipse.sirius.components.emf.view.diagram.DiagramDescriptionValidator) Test(org.junit.jupiter.api.Test)

Aggregations

EPackageRegistryImpl (org.eclipse.emf.ecore.impl.EPackageRegistryImpl)12 EPackage (org.eclipse.emf.ecore.EPackage)9 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)6 URI (org.eclipse.emf.common.util.URI)5 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)5 ECrossReferenceAdapter (org.eclipse.emf.ecore.util.ECrossReferenceAdapter)5 Resource (org.eclipse.emf.ecore.resource.Resource)4 ComposedAdapterFactory (org.eclipse.emf.edit.provider.ComposedAdapterFactory)4 DocumentEntity (org.eclipse.sirius.web.persistence.entities.DocumentEntity)4 Test (org.junit.jupiter.api.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 BasicCommandStack (org.eclipse.emf.common.command.BasicCommandStack)3 XMIResourceImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)3 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)3 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)3 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)3 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)3 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)3