-
Notifications
You must be signed in to change notification settings - Fork 15
/
ResourceDeserializer.java
executable file
·136 lines (107 loc) · 4.33 KB
/
ResourceDeserializer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
* Copyright (c) 2019-2021 Guillaume Hillairet and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the MIT License which is
* available at https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: EPL-2.0 OR MIT
*******************************************************************************/
package org.eclipse.emfcloud.jackson.databind.deser;
import static org.eclipse.emfcloud.jackson.databind.EMFContext.Attributes.RESOURCE_SET;
import java.io.IOException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emfcloud.jackson.databind.EMFContext;
import org.eclipse.emfcloud.jackson.handlers.URIHandler;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
public class ResourceDeserializer extends JsonDeserializer<Resource> {
private final URIHandler uriHandler;
public ResourceDeserializer(final URIHandler uriHandler) {
this.uriHandler = uriHandler;
}
@Override
public boolean isCachable() { return true; }
@Override
public Resource deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
return deserialize(jp, ctxt, null);
}
@Override
@SuppressWarnings("checkstyle:cyclomaticComplexity")
public Resource deserialize(final JsonParser jp, final DeserializationContext ctxt, final Resource intoValue)
throws IOException {
final Resource resource = getResource(ctxt, intoValue);
if (resource == null) {
throw new IllegalArgumentException("Invalid resource");
}
EMFContext.init(resource, ctxt);
if (!jp.hasCurrentToken()) {
jp.nextToken();
}
JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(ctxt.constructType(EObject.class));
if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
while (jp.nextToken() != JsonToken.END_ARRAY) {
EObject value = (EObject) deserializer.deserialize(jp, ctxt);
if (value != null) {
resource.getContents().add(value);
}
EMFContext.setParent(ctxt, null);
}
} else if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
EObject value = (EObject) deserializer.deserialize(jp, ctxt);
if (value != null) {
resource.getContents().add(value);
}
}
EMFContext.resolve(ctxt, uriHandler);
return resource;
}
private Resource getResource(final DeserializationContext context, Resource resource) {
if (resource == null) {
resource = EMFContext.getResource(context);
if (resource == null) {
ResourceSet resourceSet = getResourceSet(context);
URI uri = getURI(context);
resource = resourceSet.createResource(uri);
// no factory found for uri
if (resource == null) {
throw new RuntimeException("Cannot create resource for uri " + uri);
}
}
} else {
ResourceSet resourceSet = resource.getResourceSet();
if (resourceSet == null) {
resourceSet = getResourceSet(context);
resourceSet.getResources().add(resource);
}
return resource;
}
return resource;
}
protected ResourceSet getResourceSet(final DeserializationContext context) {
ResourceSet resourceSet = EMFContext.getResourceSet(context);
if (resourceSet == null) {
resourceSet = new ResourceSetImpl();
context.setAttribute(RESOURCE_SET, resourceSet);
}
return resourceSet;
}
private URI getURI(final DeserializationContext ctxt) {
URI uri = EMFContext.getURI(ctxt);
if (uri == null) {
uri = URI.createURI("default");
}
return uri;
}
@Override
public Class<Resource> handledType() {
return Resource.class;
}
}