Search in sources :

Example 6 with JsonResource

use of org.eclipse.sirius.emfjson.resource.JsonResource 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 JsonResource

use of org.eclipse.sirius.emfjson.resource.JsonResource in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
    this.counter.increment();
    IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    if (input instanceof UploadDocumentInput) {
        UploadDocumentInput uploadDocumentInput = (UploadDocumentInput) input;
        String projectId = uploadDocumentInput.getEditingContextId();
        UploadFile file = uploadDocumentInput.getFile();
        // @formatter:off
        Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
        // @formatter:on
        String name = file.getName().trim();
        if (optionalEditingDomain.isPresent()) {
            AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
            String content = this.getContent(adapterFactoryEditingDomain.getResourceSet().getPackageRegistry(), file);
            var optionalDocument = this.documentService.createDocument(projectId, name, content);
            if (optionalDocument.isPresent()) {
                Document document = optionalDocument.get();
                ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
                URI uri = URI.createURI(document.getId().toString());
                if (resourceSet.getResource(uri, false) == null) {
                    ResourceSet loadingResourceSet = new ResourceSetImpl();
                    loadingResourceSet.setPackageRegistry(resourceSet.getPackageRegistry());
                    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
                    loadingResourceSet.getResources().add(resource);
                    try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
                        resource.load(inputStream, null);
                    } catch (IOException exception) {
                        this.logger.warn(exception.getMessage(), exception);
                    }
                    resource.eAdapters().add(new DocumentMetadataAdapter(name));
                    resourceSet.getResources().add(resource);
                    payload = new UploadDocumentSuccessPayload(input.getId(), document);
                    changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
                }
            }
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) 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) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UploadDocumentSuccessPayload(org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Example 8 with JsonResource

use of org.eclipse.sirius.emfjson.resource.JsonResource in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandler method getContent.

private String getContent(EPackage.Registry registry, UploadFile file) {
    String uri = file.getName();
    // $NON-NLS-1$
    String content = "";
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.setPackageRegistry(registry);
    try (var inputStream = file.getInputStream()) {
        URI resourceURI = URI.createURI(uri);
        Optional<Resource> optionalInputResource = this.getResource(inputStream, resourceURI, resourceSet);
        if (optionalInputResource.isPresent()) {
            Resource inputResource = optionalInputResource.get();
            JsonResource ouputResource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(uri));
            resourceSet.getResources().add(ouputResource);
            ouputResource.getContents().addAll(inputResource.getContents());
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                Map<String, Object> saveOptions = new HashMap<>();
                saveOptions.put(JsonResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
                saveOptions.put(JsonResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
                saveOptions.put(JsonResource.OPTION_ID_MANAGER, new EObjectRandomIDManager());
                ouputResource.save(outputStream, saveOptions);
                content = outputStream.toString();
            }
        }
    } catch (IOException exception) {
        this.logger.warn(exception.getMessage(), exception);
    }
    return content;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) HashMap(java.util.HashMap) 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) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Example 9 with JsonResource

use of org.eclipse.sirius.emfjson.resource.JsonResource 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 10 with JsonResource

use of org.eclipse.sirius.emfjson.resource.JsonResource in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandlerTests method getEObjectUriToId.

/**
 * Loads a json resource with the {@link #JSON_CONTENT} to map the uriFragment of eObjects to the id supplied by
 * {@link EObjectIDManager}, then returns the map.
 */
private Map<String, String> getEObjectUriToId(EditingDomain editingDomain, byte[] resourceBytes) {
    Map<String, String> eObjectUriToId = new HashMap<>();
    // $NON-NLS-1$
    JsonResource jsonResource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("json.flow"));
    editingDomain.getResourceSet().getResources().add(jsonResource);
    Map<String, Object> options = new HashMap<>();
    options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
    try {
        // Load the resource to attach the contained element to the resource.
        ByteArrayInputStream inputStream = new ByteArrayInputStream(resourceBytes);
        jsonResource.load(inputStream, options);
        /*
             * Once the resource is loaded we cannot load it anymore, so we use the save to associate, in a map, the uri
             * fragment of eObject to theirs IDs that have been computed by the IDSupplier during the previous load.
             */
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        jsonResource.save(outputStream, Collections.singletonMap(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager() {

            @Override
            public String getOrCreateId(EObject eObject) {
                String id = super.getOrCreateId(eObject);
                String uriFragment = jsonResource.getURIFragment(eObject);
                eObjectUriToId.put(uriFragment, id);
                return id;
            }
        }));
    } catch (IOException exception) {
        fail(exception.getMessage());
    }
    return eObjectUriToId;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) EObject(org.eclipse.emf.ecore.EObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) EObjectIDManager(org.eclipse.sirius.components.emf.services.EObjectIDManager)

Aggregations

SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)12 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)12 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 URI (org.eclipse.emf.common.util.URI)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)5 HashMap (java.util.HashMap)4 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)4 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)4 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)4 EObject (org.eclipse.emf.ecore.EObject)3 EPackageRegistryImpl (org.eclipse.emf.ecore.impl.EPackageRegistryImpl)3 Document (org.eclipse.sirius.web.services.api.document.Document)3 EPackage (org.eclipse.emf.ecore.EPackage)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)2 IPayload (org.eclipse.sirius.components.core.api.IPayload)2