-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJsonRepresentationReader.java
More file actions
143 lines (120 loc) · 5.97 KB
/
JsonRepresentationReader.java
File metadata and controls
143 lines (120 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.theoryinpractise.halbuilder.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationException;
import com.theoryinpractise.halbuilder.api.RepresentationFactory;
import com.theoryinpractise.halbuilder.api.RepresentationReader;
import com.theoryinpractise.halbuilder.impl.api.Support;
import com.theoryinpractise.halbuilder.impl.representations.MutableRepresentation;
import java.io.Reader;
import java.util.Iterator;
import java.util.Map;
import static com.theoryinpractise.halbuilder.impl.api.Support.CURIES;
import static com.theoryinpractise.halbuilder.impl.api.Support.EMBEDDED;
import static com.theoryinpractise.halbuilder.impl.api.Support.HREF;
import static com.theoryinpractise.halbuilder.impl.api.Support.HREFLANG;
import static com.theoryinpractise.halbuilder.impl.api.Support.LINKS;
import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
import static com.theoryinpractise.halbuilder.impl.api.Support.PROFILE;
import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
public class JsonRepresentationReader implements RepresentationReader {
private RepresentationFactory representationFactory;
public JsonRepresentationReader(RepresentationFactory representationFactory) {
this.representationFactory = representationFactory;
}
public ReadableRepresentation read(Reader reader) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(reader, JsonNode.class);
MutableRepresentation resource = readResource(rootNode);
return resource.toImmutableResource();
} catch (Exception e) {
throw new RepresentationException(e);
}
}
private MutableRepresentation readResource(JsonNode rootNode) {
MutableRepresentation resource = new MutableRepresentation(representationFactory);
readNamespaces(resource, rootNode);
readLinks(resource, rootNode);
readProperties(resource, rootNode);
readResources(resource, rootNode);
return resource;
}
private void readNamespaces(MutableRepresentation resource, JsonNode rootNode) {
if (rootNode.has(LINKS)) {
JsonNode linksNode = rootNode.get(LINKS);
if (linksNode.has(CURIES)) {
JsonNode curieNode = linksNode.get(CURIES);
if (curieNode.isArray()) {
Iterator<JsonNode> values = curieNode.elements();
while (values.hasNext()) {
JsonNode valueNode = values.next();
resource.withNamespace(valueNode.get(NAME).asText(), valueNode.get(HREF).asText());
}
} else {
resource.withNamespace(curieNode.get(NAME).asText(), curieNode.get(HREF).asText());
}
}
}
}
private void readLinks(MutableRepresentation resource, JsonNode rootNode) {
if (rootNode.has(LINKS)) {
Iterator<Map.Entry<String, JsonNode>> fields = rootNode.get(LINKS).fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> keyNode = fields.next();
if (!CURIES.equals((keyNode.getKey()))) {
if (keyNode.getValue().isArray()) {
Iterator<JsonNode> values = keyNode.getValue().elements();
while (values.hasNext()) {
JsonNode valueNode = values.next();
withJsonLink(resource, keyNode, valueNode);
}
} else {
withJsonLink(resource, keyNode, keyNode.getValue());
}
}
}
}
}
private void withJsonLink(MutableRepresentation resource, Map.Entry<String, JsonNode> keyNode, JsonNode valueNode) {
String rel = keyNode.getKey();
String href = valueNode.get(HREF).asText();
String name = optionalNodeValueAsText(valueNode, NAME);
String title = optionalNodeValueAsText(valueNode, TITLE);
String hreflang = optionalNodeValueAsText(valueNode, HREFLANG);
String profile = optionalNodeValueAsText(valueNode, PROFILE);
resource.withLink(rel, href, name, title, hreflang, profile);
}
String optionalNodeValueAsText(JsonNode node, String key) {
JsonNode value = node.get(key);
return value != null ? value.asText() : null;
}
private void readProperties(MutableRepresentation resource, JsonNode rootNode) {
Iterator<String> fieldNames = rootNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
if (!Support.RESERVED_JSON_PROPERTIES.contains(fieldName)) {
JsonNode field = rootNode.get(fieldName);
resource.withProperty(fieldName, field.isNull() ? null : ( !field.isContainerNode() ? field.asText() : (ReadableRepresentation) readResource( field)));
}
}
}
private void readResources(MutableRepresentation resource, JsonNode rootNode) {
if (rootNode.has(EMBEDDED)) {
Iterator<Map.Entry<String, JsonNode>> fields = rootNode.get(EMBEDDED).fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> keyNode = fields.next();
if (keyNode.getValue().isArray()) {
Iterator<JsonNode> values = keyNode.getValue().elements();
while (values.hasNext()) {
JsonNode valueNode = values.next();
resource.withRepresentation(keyNode.getKey(), readResource(valueNode));
}
} else {
resource.withRepresentation(keyNode.getKey(), readResource(keyNode.getValue()));
}
}
}
}
}