Search in sources :

Example 1 with JsonResource

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

the class EditingContextEPackageService method loadDomainDefinitions.

private void loadDomainDefinitions(ResourceSet resourceSet, DocumentEntity domainDocument) {
    URI uri = URI.createURI(domainDocument.getId().toString());
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
    try (var inputStream = new ByteArrayInputStream(domainDocument.getContent().getBytes())) {
        resourceSet.getResources().add(resource);
        resource.load(inputStream, null);
    } catch (IOException | IllegalArgumentException exception) {
        this.logger.warn(exception.getMessage(), exception);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Example 2 with JsonResource

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

the class EditingContextSearchService method findById.

@Override
public Optional<IEditingContext> findById(String editingContextId) {
    long start = System.currentTimeMillis();
    // $NON-NLS-1$
    this.logger.debug("Loading the editing context {}", editingContextId);
    AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(this.composedAdapterFactory, new BasicCommandStack());
    ResourceSet resourceSet = editingDomain.getResourceSet();
    resourceSet.eAdapters().add(new ECrossReferenceAdapter());
    EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
    this.globalEPackageRegistry.forEach(ePackageRegistry::put);
    List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
    additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));
    resourceSet.setPackageRegistry(ePackageRegistry);
    List<DocumentEntity> documentEntities = new IDParser().parse(editingContextId).map(this.documentRepository::findAllByProjectId).orElseGet(List::of);
    for (DocumentEntity documentEntity : documentEntities) {
        URI uri = URI.createURI(documentEntity.getId().toString());
        JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
        try (var inputStream = new ByteArrayInputStream(documentEntity.getContent().getBytes())) {
            resourceSet.getResources().add(resource);
            resource.load(inputStream, null);
            resource.eAdapters().add(new DocumentMetadataAdapter(documentEntity.getName()));
        } catch (IOException | IllegalArgumentException exception) {
            // $NON-NLS-1$
            this.logger.warn("An error occured while loading document {}: {}.", documentEntity.getId(), exception.getMessage());
            resourceSet.getResources().remove(resource);
        }
    }
    // $NON-NLS-1$
    this.logger.debug("{} documents loaded for the editing context {}", resourceSet.getResources().size(), editingContextId);
    long end = System.currentTimeMillis();
    this.timer.record(end - start, TimeUnit.MILLISECONDS);
    return Optional.of(new EditingContext(editingContextId, editingDomain));
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) BasicCommandStack(org.eclipse.emf.common.command.BasicCommandStack) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) EPackage(org.eclipse.emf.ecore.EPackage) ECrossReferenceAdapter(org.eclipse.emf.ecore.util.ECrossReferenceAdapter) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) List(java.util.List)

Example 3 with JsonResource

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

the class StereotypeBuilder method getStereotypeBody.

public String getStereotypeBody(EObject rootEObject) {
    // $NON-NLS-1$
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("inmemory"));
    if (rootEObject != null) {
        resource.getContents().add(rootEObject);
    }
    // $NON-NLS-1$
    String content = "";
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        Map<String, Object> options = new HashMap<>();
        options.put(JsonResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
        options.put(JsonResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
        resource.save(outputStream, options);
        content = outputStream.toString();
    } catch (IOException exception) {
        this.logger.error(exception.getMessage(), exception);
    }
    return content;
}
Also used : HashMap(java.util.HashMap) 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)

Example 4 with JsonResource

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

the class CreateDocumentEventHandler method createDocument.

private void createDocument(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IInput input, IEditingContext editingContext, String editingContextId, String name, StereotypeDescription stereotypeDescription) {
    IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    // @formatter:off
    Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    if (optionalEditingDomain.isPresent()) {
        AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
        ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
        var optionalDocument = this.documentService.createDocument(editingContextId, name, stereotypeDescription.getContent());
        if (optionalDocument.isPresent()) {
            Document document = optionalDocument.get();
            URI uri = URI.createURI(document.getId().toString());
            JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
            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 CreateDocumentSuccessPayload(input.getId());
            changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) CreateDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateDocumentSuccessPayload) 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) 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) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Example 5 with JsonResource

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

the class StereotypeBuilder method saveAsJSON.

private String saveAsJSON(URI uri, Resource inputResource) throws IOException {
    String content;
    JsonResource ouputResource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
    ouputResource.getContents().addAll(inputResource.getContents());
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        Map<String, Object> jsonSaveOptions = new EMFResourceUtils().getFastJSONSaveOptions();
        jsonSaveOptions.put(JsonResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
        jsonSaveOptions.put(JsonResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
        ouputResource.save(outputStream, jsonSaveOptions);
        content = outputStream.toString(StandardCharsets.UTF_8);
    }
    return content;
}
Also used : 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) EMFResourceUtils(org.eclipse.sirius.components.emf.utils.EMFResourceUtils)

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