-
Notifications
You must be signed in to change notification settings - Fork 14
/
ObjectConverter.java
65 lines (56 loc) · 2.27 KB
/
ObjectConverter.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
package cz.cvut.kbss.jopa.oom.converter;
import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.NamedResource;
/**
* Allows to convert values between arbitrary types.
* <p>
* The main intended use is for annotation property mapping to attributes of type {@link Object}, which can hold both
* literal values and references to other individuals. In that case, the loaded instance is a {@link NamedResource} and
* needs to be transformed to a {@link java.net.URI} to prevent the internal OntoDriver API from leaking into the
* application.
* <p>
* Similarly, OntoDriver API's {@link LangString} is transformed to {@link MultilingualString}.
* <p>
* In all other cases, the values will be just returned without any conversion.
*/
public class ObjectConverter implements ConverterWrapper<Object, Object> {
private final boolean preferMultilingualString;
public ObjectConverter() {
this.preferMultilingualString = false;
}
public ObjectConverter(boolean preferMultilingualString) {
this.preferMultilingualString = preferMultilingualString;
}
@Override
public Object convertToAxiomValue(Object value) {
if (IdentifierTransformer.isValidIdentifierType(value.getClass()) && !(value instanceof String)) {
return NamedResource.create(IdentifierTransformer.valueAsUri(value));
}
return value;
}
@Override
public Object convertToAttribute(Object value) {
if (value instanceof NamedResource) {
return ((NamedResource) value).getIdentifier();
} else if (value instanceof LangString) {
final LangString ls = (LangString) value;
if (preferMultilingualString) {
final MultilingualString ms = new MultilingualString();
ms.set(ls.getLanguage().orElse(null), ls.getValue());
return ms;
} else {
return ls.getValue();
}
}
return value;
}
@Override
public boolean supportsAxiomValueType(Class<?> type) {
return true;
}
public boolean doesPreferMultilingualString() {
return preferMultilingualString;
}
}