Skip to content

Package: JsonLdTreeBuilder

JsonLdTreeBuilder

nameinstructionbranchcomplexitylinemethod
JsonLdTreeBuilder()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addTypes(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%
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%
createLiteralAttribute(String, Object)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
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$createLiteralAttribute$0(CollectionNode, Object)
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: 21
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%
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: 28
88%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 9
100%
M: 0 C: 1
100%
visitKnownInstance(Object)
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 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.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: private Field visitedField;
38:
39: @Override
40: public void openInstance(Object instance) {
41:• final CompositeNode newCurrent = visitedField != null ? JsonNodeFactory.createObjectNode(attId(visitedField)) :
42: JsonNodeFactory.createObjectNode();
43: openNewNode(newCurrent);
44: addTypes(instance);
45: this.visitedField = null;
46: }
47:
48: private void openNewNode(CompositeNode newNode) {
49:• if (currentNode != null) {
50:• if (currentNode.isOpen()) {
51: nodeStack.push(currentNode);
52: }
53: currentNode.addItem(newNode);
54: }
55: this.currentNode = newNode;
56: }
57:
58: private String attId(Field field) {
59: return BeanAnnotationProcessor.getAttributeIdentifier(field);
60: }
61:
62: private void addTypes(Object instance) {
63: final Set<String> types = BeanAnnotationProcessor.getOwlClasses(instance);
64: final CollectionNode typesNode = JsonNodeFactory.createCollectionNode(JsonLd.TYPE, types);
65:• for (String type : types) {
66: typesNode.addItem(JsonNodeFactory.createLiteralNode(type));
67: }
68: currentNode.addItem(typesNode);
69: }
70:
71: @Override
72: public void closeInstance(Object instance) {
73: currentNode.close();
74:• if (!nodeStack.empty()) {
75: this.currentNode = nodeStack.pop();
76: }
77: }
78:
79: @Override
80: public void visitKnownInstance(Object instance) {
81:• if (visitedField != null) {
82: currentNode.addItem(JsonNodeFactory
83: .createObjectIdNode(attId(visitedField), BeanAnnotationProcessor.getInstanceIdentifier(instance)));
84: } else {
85: currentNode.addItem(
86: JsonNodeFactory.createObjectIdNode(BeanAnnotationProcessor.getInstanceIdentifier(instance)));
87: }
88: this.visitedField = null;
89: }
90:
91: @Override
92: public void visitField(Field field, Object value) {
93:• if (value == null) {
94: return;
95: }
96:• if (BeanAnnotationProcessor.isObjectProperty(field)) {
97: this.visitedField = field;
98: } else {
99:• assert currentNode != null;
100: final String attName = attId(field);
101: final JsonNode attNode = createLiteralAttribute(attName, value);
102: currentNode.addItem(attNode);
103: }
104: }
105:
106: private JsonNode createLiteralAttribute(String attName, Object value) {
107: final JsonNode result;
108:• if (value instanceof Collection) {
109: final Collection<?> col = (Collection<?>) value;
110: final CollectionNode node = JsonNodeFactory.createCollectionNode(attName, col);
111: col.forEach(obj -> node.addItem(JsonNodeFactory.createLiteralNode(obj)));
112: result = node;
113: } else {
114: result = JsonNodeFactory.createLiteralNode(attName, value);
115: }
116: return result;
117: }
118:
119: @Override
120: public void openCollection(Collection<?> collection) {
121:• final CollectionNode newCurrent =
122: visitedField != null ? JsonNodeFactory.createCollectionNode(attId(visitedField), collection) :
123: JsonNodeFactory.createCollectionNode(collection);
124: openNewNode(newCurrent);
125: this.visitedField = null;
126: }
127:
128: @Override
129: public void closeCollection(Collection<?> collection) {
130:• assert currentNode instanceof CollectionNode;
131: closeInstance(collection);
132: }
133:
134: public CompositeNode getTreeRoot() {
135: return currentNode;
136: }
137: }