• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #378

29 Apr 2025 01:00PM UTC coverage: 86.311%. First build
#378

Pull #233

github

web-flow
Merge pull request #253 from kit-data-manager/fix-renaming-of-data-set-entities

Fix renaming of data set entities
Pull Request #233: Version 2.1.0

598 of 763 new or added lines in 29 files covered. (78.37%)

1879 of 2177 relevant lines covered (86.31%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/kit/datamanager/ro_crate/util/JsonLdExpander.java
1
package edu.kit.datamanager.ro_crate.util;
2

3
import com.fasterxml.jackson.databind.*;
4
import com.fasterxml.jackson.databind.node.*;
5

6
import java.util.*;
7

8
import java.io.File;
9

10
/**
11
 * Utility class for expanding and pruning JSON-LD documents.
12
 * <p>
13
 * This class provides functionality to resolve references in JSON-LD based on "@id" values,
14
 * expanding the data structure by replacing references with their full content.
15
 * It also handles circular references to prevent infinite recursion.
16
 *
17
 * @author jejkal
18
 */
NEW
19
public class JsonLdExpander {
×
20

21
    public static JsonNode expandAndPrune(File jsonLdFile) throws Exception {
NEW
22
        if (jsonLdFile == null) {
×
NEW
23
            throw new IllegalArgumentException("JSON-LD file must not be null.");
×
24
        }
NEW
25
        if (!jsonLdFile.exists() || !jsonLdFile.canRead()) {
×
NEW
26
            throw new IllegalArgumentException("JSON-LD file does not exist or cannot be read: " + jsonLdFile.getAbsolutePath());
×
27
        }
NEW
28
        ObjectMapper mapper = new ObjectMapper();
×
NEW
29
        JsonNode root = mapper.readTree(jsonLdFile);
×
NEW
30
        return expandAndPrune(root);
×
31
    }
32

33
    public static JsonNode expandAndPrune(JsonNode root) throws IllegalArgumentException {
NEW
34
        if (root == null) {
×
NEW
35
            throw new IllegalArgumentException("Root node must not be null.");
×
36
        }
NEW
37
        JsonNode graph = root.path("@graph");
×
NEW
38
        ObjectMapper mapper = new ObjectMapper();
×
39
        // Index all items by @id
NEW
40
        Map<String, JsonNode> idMap = new HashMap<>();
×
NEW
41
        for (JsonNode node : graph) {
×
NEW
42
            if (node.has("@id")) {
×
NEW
43
                idMap.put(node.path("@id").asText(), node);
×
44
            }
NEW
45
        }
×
46

47
        // Track which ids were directly referenced and expanded
NEW
48
        Set<String> expandedIds = new HashSet<>();
×
49

NEW
50
        ArrayNode expandedGraph = mapper.createArrayNode();
×
NEW
51
        for (JsonNode node : graph) {
×
52
            // Include only if it's NOT a referenced/expanded object
NEW
53
            if (node.has("@id") && expandedIds.contains(node.path("@id").asText())) {
×
NEW
54
                continue; // skip referenced/expanded nodes
×
55
            }
56

NEW
57
            JsonNode expandedNode = expandNode(node, idMap, expandedIds, mapper, new HashSet<>());
×
NEW
58
            expandedGraph.add(expandedNode);
×
NEW
59
        }
×
60

61
        // Rebuild root
NEW
62
        ObjectNode newRoot = mapper.createObjectNode();
×
NEW
63
        newRoot.set("@context", root.path("@context"));
×
NEW
64
        newRoot.set("@graph", expandedGraph);
×
NEW
65
        return newRoot;
×
66
    }
67

68
    private static JsonNode expandNode(JsonNode node, Map<String, JsonNode> idMap, Set<String> expandedIds, ObjectMapper mapper, Set<String> visited) {
NEW
69
        if (!node.isObject()) {
×
NEW
70
            return node;
×
71
        }
72

NEW
73
        ObjectNode result = mapper.createObjectNode();
×
NEW
74
        Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
×
75

NEW
76
        while (fields.hasNext()) {
×
NEW
77
            Map.Entry<String, JsonNode> entry = fields.next();
×
NEW
78
            String key = entry.getKey();
×
NEW
79
            JsonNode value = entry.getValue();
×
80

NEW
81
            if (value.isObject() && value.has("@id")) {
×
NEW
82
                String refId = value.path("@id").asText();
×
NEW
83
                if (!visited.contains(refId) && idMap.containsKey(refId)) {
×
NEW
84
                    visited.add(refId);
×
NEW
85
                    expandedIds.add(refId);
×
NEW
86
                    result.set(key, expandNode(idMap.getOrDefault(refId, NullNode.getInstance()), idMap, expandedIds, mapper, new HashSet<>(visited)));
×
87
                } else {
NEW
88
                    result.set(key, value);
×
89
                }
NEW
90
            } else if (value.isArray()) {
×
NEW
91
                ArrayNode newArray = mapper.createArrayNode();
×
NEW
92
                for (JsonNode element : value) {
×
NEW
93
                    if (element.isObject() && element.has("@id")) {
×
NEW
94
                        String refId = element.path("@id").asText();
×
NEW
95
                        if (!visited.contains(refId) && idMap.containsKey(refId)) {
×
NEW
96
                            visited.add(refId);
×
NEW
97
                            expandedIds.add(refId);
×
NEW
98
                            newArray.add(expandNode(idMap.getOrDefault(refId, NullNode.getInstance()), idMap, expandedIds, mapper, new HashSet<>(visited)));
×
99
                        } else {
NEW
100
                            newArray.add(element);
×
101
                        }
NEW
102
                    } else {
×
NEW
103
                        newArray.add(expandNode(element, idMap, expandedIds, mapper, visited));
×
104
                    }
NEW
105
                }
×
NEW
106
                result.set(key, newArray);
×
NEW
107
            } else if (value.isObject()) {
×
NEW
108
                result.set(key, expandNode(value, idMap, expandedIds, mapper, visited));
×
109
            } else {
NEW
110
                result.set(key, value);
×
111
            }
NEW
112
        }
×
113

NEW
114
        return result;
×
115
    }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc