-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultInstanceBuilder.java
205 lines (184 loc) · 8.04 KB
/
DefaultInstanceBuilder.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Copyright (C) 2017 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.deserialization;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
import cz.cvut.kbss.jsonld.common.CollectionType;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import java.lang.reflect.Field;
import java.util.*;
/**
* Default implementation of the JSON-LD deserializer, which takes values parsed from a JSON-LD document and builds Java
* instances from them.
*/
public class DefaultInstanceBuilder implements InstanceBuilder {
// Identifiers to instances
private final Map<String, Object> knownInstances = new HashMap<>();
private final Stack<InstanceContext> openInstances = new Stack<>();
private InstanceContext currentInstance;
// TODO Add support for polymorphism
@Override
public void openObject(String property) {
Objects.requireNonNull(property);
final Field targetField = currentInstance.getFieldForProperty(property);
assert targetField != null;
final Class<?> type = targetField.getType();
final InstanceContext<?> ctx;
if (BeanClassProcessor.isIdentifierType(type)) {
ctx = new NodeReferenceContext<>(currentInstance, targetField, knownInstances);
} else {
assert BeanAnnotationProcessor.isOwlClassEntity(type);
final Object instance = BeanClassProcessor.createInstance(type);
ctx = new SingularObjectContext<>(instance,
BeanAnnotationProcessor.mapSerializableFields(type), knownInstances);
currentInstance.setFieldValue(targetField, instance);
}
openInstances.push(currentInstance);
this.currentInstance = ctx;
}
@Override
public <T> void openObject(Class<T> cls) {
if (BeanClassProcessor.isIdentifierType(cls)) {
final InstanceContext<T> context = new NodeReferenceContext<>(currentInstance, knownInstances);
assert currentInstance != null;
openInstances.push(currentInstance);
this.currentInstance = context;
} else {
final T instance = BeanClassProcessor.createInstance(cls);
final InstanceContext<T> context = new SingularObjectContext<>(instance,
BeanAnnotationProcessor.mapSerializableFields(cls), knownInstances);
replaceCurrentContext(instance, context);
}
}
private <T> void replaceCurrentContext(T instance, InstanceContext<?> ctx) {
if (currentInstance != null) {
currentInstance.addItem(instance);
openInstances.push(currentInstance);
}
this.currentInstance = ctx;
}
@Override
public void closeObject() {
currentInstance.close();
if (!openInstances.isEmpty()) {
this.currentInstance = openInstances.pop();
}
}
@Override
public void openCollection(String property) {
Objects.requireNonNull(property);
final Field targetField = currentInstance.getFieldForProperty(property);
final InstanceContext<?> ctx;
if (targetField == null) {
if (BeanAnnotationProcessor.hasPropertiesField(currentInstance.getInstanceType()) &&
!JsonLd.TYPE.equals(property)) {
ctx = buildPropertiesContext(property);
} else {
ctx = new DummyCollectionInstanceContext(knownInstances);
}
} else {
verifyPluralAttribute(property, targetField);
final Collection<?> instance = BeanClassProcessor.createCollection(targetField);
if (JsonLd.TYPE.equals(property)) {
ctx = new TypesContext(instance, knownInstances,
BeanClassProcessor.getCollectionItemType(targetField), currentInstance.getInstanceType());
} else {
ctx = new CollectionInstanceContext<>(instance, BeanClassProcessor.getCollectionItemType(targetField),
knownInstances);
}
currentInstance.setFieldValue(targetField, instance);
}
openInstances.push(currentInstance);
this.currentInstance = ctx;
}
private InstanceContext<?> buildPropertiesContext(String property) {
final Field propsField = BeanAnnotationProcessor.getPropertiesField(currentInstance.getInstanceType());
BeanClassProcessor.verifyPropertiesFieldType(propsField);
Map<?, ?> propertiesMap =
(Map<?, ?>) BeanClassProcessor.getFieldValue(propsField, currentInstance.getInstance());
if (propertiesMap == null) {
propertiesMap = new HashMap<>();
currentInstance.setFieldValue(propsField, propertiesMap);
}
return new PropertiesInstanceContext(propertiesMap, property, propsField);
}
private void verifyPluralAttribute(String property, Field field) {
if (!isPlural(property)) {
throw JsonLdDeserializationException.singularAttributeCardinalityViolated(property, field);
}
}
@Override
public void openCollection(CollectionType collectionType) {
final Collection<?> collection = BeanClassProcessor.createCollection(collectionType);
final InstanceContext<?> context = new CollectionInstanceContext<>(collection, knownInstances);
replaceCurrentContext(collection, context);
}
@Override
public void closeCollection() {
if (!openInstances.isEmpty()) {
currentInstance = openInstances.pop();
}
}
@Override
public void addValue(String property, Object value) {
assert currentInstance != null;
if (JsonLd.ID.equals(property)) {
currentInstance.setIdentifierValue(value);
return;
}
final Field targetField = currentInstance.getFieldForProperty(property);
assert targetField != null;
// This is in case there is only one value in the JSON-LD array, because then it might be treated as single valued attribute
if (BeanClassProcessor.isCollection(targetField)) {
openCollection(property);
addValue(value);
closeCollection();
} else {
currentInstance.setFieldValue(targetField, value);
}
}
@Override
public void addValue(Object value) {
assert currentInstance != null;
currentInstance.addItem(value);
}
@Override
public Object getCurrentRoot() {
return currentInstance != null ? currentInstance.getInstance() : null;
}
@Override
public Class<?> getCurrentCollectionElementType() {
try {
return currentInstance.getItemType();
} catch (UnsupportedOperationException e) {
throw new JsonLdDeserializationException("The current instance is not a collection.", e);
}
}
@Override
public boolean isPlural(String property) {
assert isPropertyMapped(property);
final Field mappedField = currentInstance.getFieldForProperty(property);
return mappedField == null || BeanClassProcessor.isCollection(mappedField);
}
@Override
public boolean isPropertyMapped(String property) {
return currentInstance.isPropertyMapped(property) || JsonLd.TYPE.equals(property);
}
@Override
public Class<?> getCurrentContextType() {
return currentInstance.getInstanceType();
}
}