Skip to content

Method: initializeTargetClassResolver()

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization;
19:
20: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
21: import cz.cvut.kbss.jsonld.ConfigParam;
22: import cz.cvut.kbss.jsonld.Configuration;
23: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
24: import cz.cvut.kbss.jsonld.common.Configured;
25: import cz.cvut.kbss.jsonld.deserialization.expanded.ExpandedJsonLdDeserializer;
26: import cz.cvut.kbss.jsonld.deserialization.util.ClasspathScanner;
27: import cz.cvut.kbss.jsonld.deserialization.util.TargetClassResolver;
28: import cz.cvut.kbss.jsonld.deserialization.util.TargetClassResolverConfig;
29: import cz.cvut.kbss.jsonld.deserialization.util.TypeMap;
30: import jakarta.json.JsonValue;
31:
32: import java.util.Objects;
33:
34: /**
35: * Takes a pre-processed JSON-LD structure and deserializes it.
36: */
37: public abstract class JsonLdDeserializer implements Configured {
38:
39: private static final TypeMap TYPE_MAP = new TypeMap();
40:
41: private final Configuration configuration;
42:
43: protected final TargetClassResolver classResolver;
44:
45: protected ValueDeserializers deserializers = new CommonValueDeserializers();
46:
47: protected JsonLdDeserializer() {
48: this.configuration = new Configuration();
49: this.classResolver = initializeTargetClassResolver();
50: }
51:
52: protected JsonLdDeserializer(Configuration configuration) {
53: this.configuration = Objects.requireNonNull(configuration);
54: this.classResolver = initializeTargetClassResolver();
55: }
56:
57: private TargetClassResolver initializeTargetClassResolver() {
58: final String scanPath = configuration.get(ConfigParam.SCAN_PACKAGE, "");
59: final TypeMap typeMap = discoverAvailableTypes(scanPath, configuration.is(ConfigParam.DISABLE_TYPE_MAP_CACHE));
60: return new TargetClassResolver(typeMap,
61: new TargetClassResolverConfig(
62: configuration.is(ConfigParam.ASSUME_TARGET_TYPE),
63: configuration().is(ConfigParam.ENABLE_OPTIMISTIC_TARGET_TYPE_RESOLUTION),
64: configuration().is(ConfigParam.PREFER_SUPERCLASS)));
65: }
66:
67: /**
68: * Finds potential deserialization target types on the classpath.
69: *
70: * @param scanPath Path to scan on classpath
71: * @return Map of types to Java classes
72: */
73: private static TypeMap discoverAvailableTypes(String scanPath, boolean disableCache) {
74: final TypeMap map = disableCache ? new TypeMap() : TYPE_MAP;
75: if (!map.isEmpty()) {
76: return map;
77: }
78: new ClasspathScanner(c -> {
79: final OWLClass ann = c.getDeclaredAnnotation(OWLClass.class);
80: if (ann != null) {
81: map.register(BeanAnnotationProcessor.expandIriIfNecessary(ann.iri(), c), c);
82: }
83: }).processClasses(scanPath);
84: return map;
85: }
86:
87: @Override
88: public Configuration configuration() {
89: return configuration;
90: }
91:
92: /**
93: * Registers a custom deserializer for the specified type.
94: * <p>
95: * If a deserializer already existed for the type, it is replaced by the new one.
96: *
97: * @param type Target type to register the deserializer for
98: * @param deserializer Deserializer to register
99: * @param <T> Target type
100: */
101: public <T> void registerDeserializer(Class<T> type, ValueDeserializer<T> deserializer) {
102: Objects.requireNonNull(type);
103: Objects.requireNonNull(deserializer);
104: deserializers.registerDeserializer(type, deserializer);
105: }
106:
107: /**
108: * Deserializes the specified JSON-LD data.
109: *
110: * @param <T> The type of the target object
111: * @param jsonLd JSON-LD structure
112: * @param resultClass Type of the result instance
113: * @return Deserialized Java instance
114: */
115: public abstract <T> T deserialize(JsonValue jsonLd, Class<T> resultClass);
116:
117: /**
118: * Creates deserializer for expanded JSON-LD, initialized with the specified configuration.
119: *
120: * @param configuration Configuration of the deserializer
121: * @return New deserializer
122: */
123: public static JsonLdDeserializer createExpandedDeserializer(Configuration configuration) {
124: return new ExpandedJsonLdDeserializer(configuration);
125: }
126:
127: /**
128: * Creates deserializer for expanded JSON-LD.
129: *
130: * @return New deserializer
131: */
132: public static JsonLdDeserializer createExpandedDeserializer() {
133: return new ExpandedJsonLdDeserializer();
134: }
135: }