Search in sources :

Example 1 with EMFResourceUtils

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;
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Resource(org.eclipse.emf.ecore.resource.Resource) EObject(org.eclipse.emf.ecore.EObject) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) EMFResourceUtils(org.eclipse.sirius.components.emf.utils.EMFResourceUtils)

Example 2 with EMFResourceUtils

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

Example 3 with EMFResourceUtils

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Resource(org.eclipse.emf.ecore.resource.Resource) IOException(java.io.IOException) EMFResourceUtils(org.eclipse.sirius.components.emf.utils.EMFResourceUtils) BufferedInputStream(java.io.BufferedInputStream) BufferedReader(java.io.BufferedReader) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)

Aggregations

EMFResourceUtils (org.eclipse.sirius.components.emf.utils.EMFResourceUtils)3 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)3 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 XMIResourceImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)2 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1