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