Skip to content

Package: JsonLdTreeBuilder

JsonLdTreeBuilder

nameinstructionbranchcomplexitylinemethod
JsonLdTreeBuilder()
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
attId(Field)
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%
closeCollection(Collection)
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%
closeInstance(Object)
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$visitField$1(JsonNode)
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%
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(Collection)
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
openInstance(Object)
M: 0 C: 18
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%
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%
visitField(Field, Object)
M: 4 C: 38
90%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 10
100%
M: 0 C: 1
100%
visitIdentifier(String, Object)
M: 4 C: 12
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
visitKnownInstance(String, Object)
M: 0 C: 27
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
visitTypes(Collection, Object)
M: 0 C: 13
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) 2017 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.JsonLd;
18: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
19: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
20: import cz.cvut.kbss.jsonld.serialization.model.CompositeNode;
21: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
22: import cz.cvut.kbss.jsonld.serialization.traversal.InstanceVisitor;
23:
24: import java.lang.reflect.Field;
25: import java.util.Collection;
26: import java.util.List;
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: private Field visitedField;
38:
39: private final FieldSerializer literalSerializer = new LiteralFieldSerializer();
40: private final FieldSerializer propertiesSerializer = new PropertiesFieldSerializer();
41:
42: @Override
43: public void openInstance(Object instance) {
44:• final CompositeNode newCurrent = visitedField != null ? JsonNodeFactory.createObjectNode(attId(visitedField)) :
45: JsonNodeFactory.createObjectNode();
46: openNewNode(newCurrent);
47: this.visitedField = null;
48: }
49:
50: private String attId(Field field) {
51: return BeanAnnotationProcessor.getAttributeIdentifier(field);
52: }
53:
54: private void openNewNode(CompositeNode newNode) {
55:• if (currentNode != null) {
56:• if (currentNode.isOpen()) {
57: nodeStack.push(currentNode);
58: }
59: currentNode.addItem(newNode);
60: }
61: this.currentNode = newNode;
62: }
63:
64: @Override
65: public void closeInstance(Object instance) {
66: currentNode.close();
67:• if (!nodeStack.empty()) {
68: this.currentNode = nodeStack.pop();
69: }
70: }
71:
72: @Override
73: public void visitIdentifier(String identifier, Object instance) {
74:• assert currentNode.isOpen();
75: currentNode.addItem(JsonNodeFactory.createObjectIdNode(JsonLd.ID, identifier));
76: }
77:
78: @Override
79: public void visitTypes(Collection<String> types, Object instance) {
80: final CollectionNode typesNode = JsonNodeFactory.createCollectionNode(JsonLd.TYPE, types);
81: types.forEach(type -> typesNode.addItem(JsonNodeFactory.createLiteralNode(type)));
82: currentNode.addItem(typesNode);
83: }
84:
85: @Override
86: public void visitKnownInstance(String id, Object instance) {
87:• if (visitedField != null) {
88: openNewNode(JsonNodeFactory.createObjectNode(attId(visitedField)));
89: } else {
90: openNewNode(JsonNodeFactory.createObjectNode());
91: }
92: currentNode.addItem(JsonNodeFactory.createLiteralNode(JsonLd.ID, id));
93: closeInstance(instance);
94: this.visitedField = null;
95: }
96:
97: @Override
98: public void visitField(Field field, Object value) {
99:• if (value == null || BeanAnnotationProcessor.isTypesField(field)) {
100: return;
101: }
102:• if (BeanAnnotationProcessor.isObjectProperty(field)) {
103: this.visitedField = field;
104: } else {
105:• assert currentNode != null;
106: final List<JsonNode> nodes;
107:• if (BeanAnnotationProcessor.isPropertiesField(field)) {
108: // A problem could be when the properties contain a property mapped by the model as well
109: nodes = propertiesSerializer.serializeField(field, value);
110: } else {
111: nodes = literalSerializer.serializeField(field, value);
112: }
113: nodes.forEach(node -> currentNode.addItem(node));
114: }
115: }
116:
117: @Override
118: public void openCollection(Collection<?> collection) {
119:• final CollectionNode newCurrent =
120: visitedField != null ? JsonNodeFactory.createCollectionNode(attId(visitedField), collection) :
121: JsonNodeFactory.createCollectionNode(collection);
122: openNewNode(newCurrent);
123: this.visitedField = null;
124: }
125:
126: @Override
127: public void closeCollection(Collection<?> collection) {
128:• assert currentNode instanceof CollectionNode;
129: closeInstance(collection);
130: }
131:
132: public CompositeNode getTreeRoot() {
133: return currentNode;
134: }
135: }