Skip to content

Package: JsonLdTreeBuilder

JsonLdTreeBuilder

nameinstructionbranchcomplexitylinemethod
JsonLdTreeBuilder(ValueSerializers)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
closeCollection(SerializationContext)
M: 4 C: 9
69%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
closeObject(SerializationContext)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getTreeRoot()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
openCollection(SerializationContext)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
openNewNode(CompositeNode)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
openObject(SerializationContext)
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
visitAttribute(SerializationContext)
M: 4 C: 25
86%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 7
100%
M: 0 C: 1
100%
visitIdentifier(SerializationContext)
M: 4 C: 17
81%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
visitObject(SerializationContext)
M: 12 C: 34
74%
M: 4 C: 4
50%
M: 3 C: 2
40%
M: 2 C: 8
80%
M: 0 C: 1
100%
visitTypes(SerializationContext)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jsonld.serialization;
14:
15: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
16: import cz.cvut.kbss.jsonld.serialization.model.CompositeNode;
17: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
18: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
19: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
20: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializers;
21: import cz.cvut.kbss.jsonld.serialization.traversal.InstanceVisitor;
22: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
23:
24: import java.util.Collection;
25: import java.util.Set;
26: import java.util.Stack;
27:
28: /**
29: * Builds an abstract representation of a JSON-LD tree, which is a result of object graph traversal by {@link
30: * cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser}.
31: */
32: public class JsonLdTreeBuilder implements InstanceVisitor {
33:
34: private final Stack<CompositeNode<?>> nodeStack = new Stack<>();
35: private CompositeNode<?> currentNode;
36:
37: private final ValueSerializers serializers;
38:
39: public JsonLdTreeBuilder(ValueSerializers serializers) {
40: this.serializers = serializers;
41: }
42:
43: @Override
44: public boolean visitObject(SerializationContext<?> ctx) {
45:• if (serializers.hasCustomSerializer(ctx.getValue().getClass())) {
46: final ValueSerializer serializer = serializers.getSerializer(ctx).get();
47: final JsonNode node = serializer.serialize(ctx.getValue(), ctx);
48:• if (node != null) {
49:• if (currentNode != null) {
50: currentNode.addItem(node);
51: } else {
52:• assert node instanceof CompositeNode;
53: currentNode = (CompositeNode<?>) node;
54: }
55: }
56: return false;
57: }
58: return true;
59: }
60:
61: @Override
62: public void openObject(SerializationContext<?> ctx) {
63:• final ObjectNode newCurrent = ctx.getTerm() != null ? JsonNodeFactory.createObjectNode(ctx.getTerm()) :
64: JsonNodeFactory.createObjectNode();
65: openNewNode(newCurrent);
66: }
67:
68: private void openNewNode(CompositeNode<?> newNode) {
69:• if (currentNode != null) {
70:• if (currentNode.isOpen()) {
71: nodeStack.push(currentNode);
72: }
73: currentNode.addItem(newNode);
74: }
75: this.currentNode = newNode;
76: }
77:
78: @Override
79: public void closeObject(SerializationContext<?> ctx) {
80: currentNode.close();
81:• if (!nodeStack.empty()) {
82: this.currentNode = nodeStack.pop();
83: }
84: }
85:
86: @Override
87: public void visitIdentifier(SerializationContext<String> idCtx) {
88:• assert currentNode.isOpen();
89: currentNode.addItem(serializers.getIdentifierSerializer().serialize(idCtx.getValue(), idCtx));
90: }
91:
92: @Override
93: public void visitTypes(SerializationContext<Set<String>> typesCtx) {
94: currentNode.addItem(serializers.getTypesSerializer().serialize(typesCtx.getValue(), typesCtx));
95: }
96:
97: @Override
98: public void visitAttribute(SerializationContext<?> ctx) {
99:• if (ctx.getValue() != null) {
100:• assert currentNode != null;
101: final ValueSerializer serializer = serializers.getOrDefault(ctx);
102: final JsonNode node = serializer.serialize(ctx.getValue(), ctx);
103:• if (node != null) {
104: currentNode.addItem(node);
105: }
106: }
107: }
108:
109: @Override
110: public void openCollection(SerializationContext<? extends Collection<?>> ctx) {
111: final CollectionNode<?> newCurrent =
112:• ctx.getTerm() != null ? JsonNodeFactory.createCollectionNode(ctx.getTerm(),
113: ctx.getValue()) :
114: JsonNodeFactory.createCollectionNode(ctx.getValue());
115: openNewNode(newCurrent);
116: }
117:
118: @Override
119: public void closeCollection(SerializationContext<?> ctx) {
120:• assert currentNode instanceof CollectionNode;
121: closeObject(ctx);
122: }
123:
124: public CompositeNode<?> getTreeRoot() {
125: return currentNode;
126: }
127: }