-
Notifications
You must be signed in to change notification settings - Fork 45
/
JSONResourceFactory.java
75 lines (65 loc) · 2.54 KB
/
JSONResourceFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*******************************************************************************
* Copyright (c) 2019, 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.emf.services;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResourceImpl;
/**
* Special factory for {@link JsonResource}.</br>
* This factory ensures that objects in that resource are identified by their uuid.
*
* @author lfasani
*/
public class JSONResourceFactory extends ResourceFactoryImpl {
@Override
public JsonResource createResource(URI uri) {
// @formatter:off
Optional.ofNullable(uri)
.map(URI::scheme)
.filter(Objects::nonNull)
.filter(Predicate.not(String::isEmpty))
.orElseThrow(() -> new IllegalArgumentException(String.format("Missing scheme for URI %s", uri)));
// @formatter:on
Map<String, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
options.put(JsonResource.OPTION_DISPLAY_DYNAMIC_INSTANCES, true);
return new JsonResourceImpl(uri, options);
}
/**
* Create a resource which URI is made of a correct scheme and the given id as path.
*
* @param path
* the path of the resource
*/
public JsonResource createResourceFromPath(String path) {
URI uri = this.createResourceURI(path);
return this.createResource(uri);
}
/**
* Create an URI with a correct scheme and the given id as path.
*
* @param resourceId
* the id of the resource
*/
public URI createResourceURI(String resourceId) {
// There are three slashes because the URI authority is empty
return URI.createURI(IEMFEditingContext.RESOURCE_SCHEME + ":///" + resourceId);
}
}