-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultInstanceBuilder.java
161 lines (141 loc) · 6.06 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
/**
* Copyright (C) 2016 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.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();
assert BeanAnnotationProcessor.isOwlClassEntity(type);
final Object instance = BeanClassProcessor.createInstance(type);
final InstanceContext<?> 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) {
final T instance = BeanClassProcessor.createInstance(cls);
final SingularObjectContext<?> ctx = new SingularObjectContext<>(instance,
BeanAnnotationProcessor.mapSerializableFields(cls), knownInstances);
replaceCurrentContext(instance, ctx);
}
private <T> void replaceCurrentContext(T instance, InstanceContext<?> ctx) {
if (currentInstance != null) {
currentInstance.addItem(instance);
openInstances.push(currentInstance);
}
this.currentInstance = ctx;
}
@Override
public void closeObject() {
if (!openInstances.isEmpty()) {
this.currentInstance = openInstances.pop();
}
}
@Override
public void openCollection(String property) {
Objects.requireNonNull(property);
final Field targetField = currentInstance.getFieldForProperty(property);
assert targetField != null;
final Collection<?> instance = BeanClassProcessor.createCollection(targetField);
final InstanceContext<Collection<?>> ctx = new CollectionInstanceContext<>(instance,
BeanClassProcessor.getCollectionItemType(targetField), knownInstances);
currentInstance.setFieldValue(targetField, instance);
openInstances.push(currentInstance);
this.currentInstance = ctx;
}
@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;
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);
if (BeanAnnotationProcessor.isInstanceIdentifier(targetField)) {
registerKnownInstance(targetField);
}
}
}
private void registerKnownInstance(Field targetField) {
final Object instance = currentInstance.getInstance();
knownInstances.put(BeanClassProcessor.getFieldValue(targetField, instance).toString(), instance);
}
@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);
return BeanClassProcessor.isCollection(currentInstance.getFieldForProperty(property));
}
@Override
public boolean isPropertyMapped(String property) {
return currentInstance.isPropertyMapped(property);
}
@Override
public Class<?> getCurrentContextType() {
return currentInstance.getInstanceType();
}
}