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