Skip to content

Package: JsonLdTreeBuilder

JsonLdTreeBuilder

nameinstructionbranchcomplexitylinemethod
JsonLdTreeBuilder(ValueSerializers, JsonLdContextFactory)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
closeCollection(SerializationContext)
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
closeCurrentNode()
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%
closeObject(SerializationContext)
M: 0 C: 11
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: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
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: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
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%
visitIndividual(SerializationContext)
M: 4 C: 27
87%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
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.context.JsonLdContextFactory;
16: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
17: import cz.cvut.kbss.jsonld.serialization.model.CompositeNode;
18: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
19: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
20: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializer;
21: import cz.cvut.kbss.jsonld.serialization.serializer.ValueSerializers;
22: import cz.cvut.kbss.jsonld.serialization.traversal.InstanceVisitor;
23: import cz.cvut.kbss.jsonld.serialization.traversal.SerializationContext;
24:
25: import java.util.Collection;
26: import java.util.Set;
27: import java.util.Stack;
28:
29: /**
30: * Builds an abstract representation of a JSON-LD tree, which is a result of object graph traversal by {@link
31: * cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser}.
32: */
33: public class JsonLdTreeBuilder implements InstanceVisitor {
34:
35: private final Stack<CompositeNode<?>> nodeStack = new Stack<>();
36: private CompositeNode<?> currentNode;
37:
38: private final ValueSerializers serializers;
39: private final JsonLdContextFactory jsonLdContextFactory;
40:
41: public JsonLdTreeBuilder(ValueSerializers serializers, JsonLdContextFactory jsonLdContextFactory) {
42: this.serializers = serializers;
43: this.jsonLdContextFactory = jsonLdContextFactory;
44: }
45:
46: @Override
47: public void visitIndividual(SerializationContext<?> ctx) {
48: final ValueSerializer s = serializers.getIndividualSerializer();
49: final JsonNode node = s.serialize(ctx.getValue(), ctx);
50:• if (currentNode != null) {
51: currentNode.addItem(node);
52: } else {
53:• assert node instanceof CompositeNode;
54: currentNode = (CompositeNode<?>) node;
55: }
56: }
57:
58: @Override
59: public boolean visitObject(SerializationContext<?> ctx) {
60:• if (serializers.hasCustomSerializer(ctx.getValue().getClass())) {
61: final ValueSerializer serializer = serializers.getSerializer(ctx).get();
62: final JsonNode node = serializer.serialize(ctx.getValue(), ctx);
63:• if (node != null) {
64:• if (currentNode != null) {
65: currentNode.addItem(node);
66: } else {
67:• assert node instanceof CompositeNode;
68: currentNode = (CompositeNode<?>) node;
69: }
70: }
71: return false;
72: }
73: return true;
74: }
75:
76: @Override
77: public void openObject(SerializationContext<?> ctx) {
78: final ObjectNode newCurrent = JsonNodeFactory.createObjectNode(ctx.getTerm());
79: openNewNode(newCurrent);
80: // Prepare to create new JSON-LD context when an object is open
81: ctx.setJsonLdContext(jsonLdContextFactory.createJsonLdContext(ctx.getJsonLdContext()));
82: }
83:
84: private void openNewNode(CompositeNode<?> newNode) {
85:• if (currentNode != null) {
86:• if (currentNode.isOpen()) {
87: nodeStack.push(currentNode);
88: }
89: currentNode.addItem(newNode);
90: }
91: this.currentNode = newNode;
92: }
93:
94: @Override
95: public void closeObject(SerializationContext<?> ctx) {
96:• if (!ctx.isCurrentEmpty()) {
97: currentNode.prependItem(ctx.getContextNode());
98: }
99: closeCurrentNode();
100: }
101:
102: private void closeCurrentNode() {
103: currentNode.close();
104:• if (!nodeStack.empty()) {
105: this.currentNode = nodeStack.pop();
106: }
107: }
108:
109: @Override
110: public void visitIdentifier(SerializationContext<String> idCtx) {
111:• assert currentNode.isOpen();
112: currentNode.addItem(serializers.getIdentifierSerializer().serialize(idCtx.getValue(), idCtx));
113: }
114:
115: @Override
116: public void visitTypes(SerializationContext<Set<String>> typesCtx) {
117: currentNode.addItem(serializers.getTypesSerializer().serialize(typesCtx.getValue(), typesCtx));
118: }
119:
120: @Override
121: public void visitAttribute(SerializationContext<?> ctx) {
122:• if (ctx.getValue() != null) {
123:• assert currentNode != null;
124: final ValueSerializer serializer = serializers.getOrDefault(ctx);
125: final JsonNode node = serializer.serialize(ctx.getValue(), ctx);
126:• if (node != null) {
127: currentNode.addItem(node);
128: }
129: }
130: }
131:
132: @Override
133: public void openCollection(SerializationContext<? extends Collection<?>> ctx) {
134: final CollectionNode<?> newCurrent =JsonNodeFactory.createCollectionNode(ctx.getTerm(), ctx.getValue());
135: openNewNode(newCurrent);
136: }
137:
138: @Override
139: public void closeCollection(SerializationContext<?> ctx) {
140:• assert currentNode instanceof CollectionNode;
141: closeCurrentNode();
142: }
143:
144: public CompositeNode<?> getTreeRoot() {
145: return currentNode;
146: }
147: }