Skip to contentMethod: isTraversable(Object)
1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization.traversal;
16:
17: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
18: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
19: import cz.cvut.kbss.jsonld.serialization.context.JsonLdContext;
20:
21: import java.util.Collection;
22: import java.util.Map;
23: import java.util.Objects;
24:
25: /**
26: * Serializes a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
27: * <p>
28: * Note that at the moment, when the map also contains a property which is already mapped by another field, a conflict in
29: * the resulting JSON-LD will arise.
30: */
31: class PropertiesTraverser {
32:
33: private final ObjectGraphTraverser parent;
34:
35: PropertiesTraverser(ObjectGraphTraverser parent) {
36: this.parent = parent;
37: }
38:
39: public void traverseProperties(SerializationContext<Map<?, ?>> ctx) {
40: for (Map.Entry<?, ?> e : ctx.getValue().entrySet()) {
41: final String property = e.getKey().toString();
42: if (e.getValue() == null) {
43: continue;
44: }
45: if (e.getValue() instanceof Collection) {
46: final Collection<?> propertyValues = (Collection<?>) e.getValue();
47: serializePropertyValues(property, propertyValues, ctx.getJsonLdContext());
48: } else {
49: visitSingleValue(property, e.getValue(), ctx.getJsonLdContext());
50: }
51: }
52: }
53:
54: private void visitSingleValue(String property, Object value, JsonLdContext jsonLdContext) {
55: assert value != null;
56: if (isTraversable(value)) {
57: parent.traverseSingular(new SerializationContext<>(property, value, jsonLdContext));
58: } else {
59: parent.visitAttribute(new SerializationContext<>(property, value, jsonLdContext));
60: }
61: }
62:
63: private static boolean isTraversable(Object value) {
64: final Class<?> cls = value.getClass();
65:• return (BeanClassProcessor.isIdentifierType(value.getClass()) && !String.class.equals(cls)) ||
66:• BeanAnnotationProcessor.isOwlClassEntity(value.getClass()) ||
67:• BeanAnnotationProcessor.hasTypesField(cls);
68: }
69:
70: private void serializePropertyValues(String property, Collection<?> values, JsonLdContext jsonLdContext) {
71: if (values.isEmpty()) {
72: return;
73: }
74: if (values.size() == 1) {
75: final Object val = values.iterator().next();
76: if (val != null) {
77: visitSingleValue(property, val, jsonLdContext);
78: }
79: } else {
80: final SerializationContext<Collection<?>> colContext = new SerializationContext<>(property, values, jsonLdContext);
81: parent.openCollection(colContext);
82: values.stream().filter(Objects::nonNull).forEach(v -> visitSingleValue(null, v, jsonLdContext));
83: parent.closeCollection(colContext);
84: }
85: }
86: }