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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations