use of org.eclipse.sirius.components.emf.utils.EMFResourceUtils in project sirius-web by eclipse-sirius.
the class StereotypeBuilder method loadFromXMI.
private Resource loadFromXMI(URI uri, InputStream inputStream) throws IOException {
Resource inputResource = new XMIResourceImpl(uri);
Map<String, Object> xmiLoadOptions = new EMFResourceUtils().getXMILoadOptions(parserPool);
inputResource.load(inputStream, xmiLoadOptions);
return inputResource;
}
use of org.eclipse.sirius.components.emf.utils.EMFResourceUtils 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;
}
use of org.eclipse.sirius.components.emf.utils.EMFResourceUtils in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandler method getResource.
/**
* Returns the {@link Resource} with the given {@link URI} or {@link Optional#empty()} regarding to the content of
* the first line of the given {@link InputStream}.
*
* <p>
* Returns a {@link JsonResourceImpl} if the first line contains a '{', a {@link XMIResourceImpl} if the first line
* contains '<', {@link Optional#empty()} otherwise.
* </p>
*
* @param inputStream
* The {@link InputStream} used to determine which {@link Resource} to create
* @param resourceURI
* The {@link URI} to use to create the {@link Resource}
* @param resourceSet
* The {@link ResourceSet} used to store the loaded resource
* @return a {@link JsonResourceImpl}, a {@link XMIResourceImpl} or {@link Optional#empty()}
*/
private Optional<Resource> getResource(InputStream inputStream, URI resourceURI, ResourceSet resourceSet) {
Resource resource = null;
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
bufferedInputStream.mark(Integer.MAX_VALUE);
try (var reader = new BufferedReader(new InputStreamReader(bufferedInputStream, StandardCharsets.UTF_8))) {
String line = reader.readLine();
Map<String, Object> options = new HashMap<>();
if (line != null) {
if (line.contains("{")) {
// $NON-NLS-1$
resource = new SiriusWebJSONResourceFactoryImpl().createResource(resourceURI);
} else if (line.contains("<")) {
// $NON-NLS-1$
resource = new XMIResourceImpl(resourceURI);
options = new EMFResourceUtils().getXMILoadOptions();
}
}
bufferedInputStream.reset();
if (resource != null) {
resourceSet.getResources().add(resource);
resource.load(bufferedInputStream, options);
}
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
return Optional.ofNullable(resource);
}
Aggregations