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%
lambda$visitTypes$0(CollectionNode, String)
M: 0 C: 5
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: 6
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: 5
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: 13
76%
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: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

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