Skip to content

Package: JsonLdTreeBuilder

JsonLdTreeBuilder

nameinstructionbranchcomplexitylinemethod
JsonLdTreeBuilder()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
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: 48
92%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 0 C: 12
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 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.JsonLd;
16: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
17: import cz.cvut.kbss.jsonld.serialization.model.CollectionNode;
18: import cz.cvut.kbss.jsonld.serialization.model.CompositeNode;
19: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
20: import cz.cvut.kbss.jsonld.serialization.traversal.InstanceVisitor;
21:
22: import java.lang.reflect.Field;
23: import java.util.Collection;
24: import java.util.List;
25: import java.util.Stack;
26:
27: /**
28: * Builds an abstract representation of a JSON-LD tree, which is a result of object graph traversal by {@link
29: * cz.cvut.kbss.jsonld.serialization.traversal.ObjectGraphTraverser}.
30: */
31: public class JsonLdTreeBuilder implements InstanceVisitor {
32:
33: private final Stack<CompositeNode> nodeStack = new Stack<>();
34: private CompositeNode currentNode;
35: private Field visitedField;
36:
37: private final FieldSerializer literalSerializer = new LiteralFieldSerializer();
38: private final FieldSerializer annotationSerializer = new AnnotationFieldSerializer();
39: private final FieldSerializer propertiesSerializer = new PropertiesFieldSerializer();
40:
41: @Override
42: public void openInstance(Object instance) {
43:• final CompositeNode newCurrent = visitedField != null ? JsonNodeFactory.createObjectNode(attId(visitedField)) :
44: JsonNodeFactory.createObjectNode();
45: openNewNode(newCurrent);
46: this.visitedField = null;
47: }
48:
49: private String attId(Field field) {
50: return BeanAnnotationProcessor.getAttributeIdentifier(field);
51: }
52:
53: private void openNewNode(CompositeNode newNode) {
54:• if (currentNode != null) {
55:• if (currentNode.isOpen()) {
56: nodeStack.push(currentNode);
57: }
58: currentNode.addItem(newNode);
59: }
60: this.currentNode = newNode;
61: }
62:
63: @Override
64: public void closeInstance(Object instance) {
65: currentNode.close();
66:• if (!nodeStack.empty()) {
67: this.currentNode = nodeStack.pop();
68: }
69: }
70:
71: @Override
72: public void visitIdentifier(String identifier, Object instance) {
73:• assert currentNode.isOpen();
74: currentNode.addItem(JsonNodeFactory.createObjectIdNode(JsonLd.ID, identifier));
75: }
76:
77: @Override
78: public void visitTypes(Collection<String> types, Object instance) {
79: final CollectionNode typesNode = JsonNodeFactory.createCollectionNode(JsonLd.TYPE, types);
80: types.forEach(type -> typesNode.addItem(JsonNodeFactory.createLiteralNode(type)));
81: currentNode.addItem(typesNode);
82: }
83:
84: @Override
85: public void visitKnownInstance(String id, Object instance) {
86:• if (visitedField != null) {
87: openNewNode(JsonNodeFactory.createObjectNode(attId(visitedField)));
88: } else {
89: openNewNode(JsonNodeFactory.createObjectNode());
90: }
91: currentNode.addItem(JsonNodeFactory.createLiteralNode(JsonLd.ID, id));
92: closeInstance(instance);
93: this.visitedField = null;
94: }
95:
96: @Override
97: public void visitField(Field field, Object value) {
98:• if (value == null || BeanAnnotationProcessor.isTypesField(field)) {
99: return;
100: }
101:• if (BeanAnnotationProcessor.isObjectProperty(field)) {
102: this.visitedField = field;
103: } else {
104:• assert currentNode != null;
105: final List<JsonNode> nodes;
106:• if (BeanAnnotationProcessor.isAnnotationProperty(field)) {
107: nodes = annotationSerializer.serializeField(field, value);
108:• } else if (BeanAnnotationProcessor.isPropertiesField(field)) {
109: // A problem could be when the properties contain a property mapped by the model as well
110: nodes = propertiesSerializer.serializeField(field, value);
111: } else {
112: nodes = literalSerializer.serializeField(field, value);
113: }
114: nodes.forEach(node -> currentNode.addItem(node));
115: }
116: }
117:
118: @Override
119: public void openCollection(Collection<?> collection) {
120:• final CollectionNode newCurrent =
121: visitedField != null ? JsonNodeFactory.createCollectionNode(attId(visitedField), collection) :
122: JsonNodeFactory.createCollectionNode(collection);
123: openNewNode(newCurrent);
124: this.visitedField = null;
125: }
126:
127: @Override
128: public void closeCollection(Collection<?> collection) {
129:• assert currentNode instanceof CollectionNode;
130: closeInstance(collection);
131: }
132:
133: public CompositeNode getTreeRoot() {
134: return currentNode;
135: }
136: }