-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
JsonLdToRdf.java
187 lines (146 loc) · 6.65 KB
/
JsonLdToRdf.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.apicatalog.jsonld.deseralization;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.apicatalog.jsonld.JsonLdError;
import com.apicatalog.jsonld.JsonLdOptions.RdfDirection;
import com.apicatalog.jsonld.flattening.NodeMap;
import com.apicatalog.jsonld.json.JsonUtils;
import com.apicatalog.jsonld.lang.BlankNode;
import com.apicatalog.jsonld.lang.Keywords;
import com.apicatalog.jsonld.lang.Utils;
import com.apicatalog.jsonld.uri.UriUtils;
import com.apicatalog.rdf.Rdf;
import com.apicatalog.rdf.RdfDataset;
import com.apicatalog.rdf.RdfResource;
import com.apicatalog.rdf.RdfTriple;
import com.apicatalog.rdf.RdfValue;
import com.apicatalog.rdf.lang.RdfConstants;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
public final class JsonLdToRdf {
private static final Logger LOGGER = Logger.getLogger(JsonLdToRdf.class.getName());
// required
private final NodeMap nodeMap;
private final RdfDataset dataset;
// optional
private boolean produceGeneralizedRdf;
private RdfDirection rdfDirection;
private JsonLdToRdf(NodeMap nodeMap, RdfDataset dataset) {
this.nodeMap = nodeMap;
this.dataset = dataset;
this.produceGeneralizedRdf = false;
this.rdfDirection = null;
}
public static final JsonLdToRdf with(NodeMap nodeMap, RdfDataset dataset) {
return new JsonLdToRdf(nodeMap, dataset);
}
public JsonLdToRdf produceGeneralizedRdf(boolean enable) {
this.produceGeneralizedRdf = enable;
return this;
}
public JsonLdToRdf rdfDirection(RdfDirection rdfDirection) {
this.rdfDirection = rdfDirection;
return this;
}
public RdfDataset build() throws JsonLdError {
// 1.
for (final String graphName : Utils.index(nodeMap.graphs(), true)) {
// 1.2.
final RdfResource rdfGraphName;
if (Keywords.DEFAULT.equals(graphName)) {
rdfGraphName = null;
} else {
// 1.1.
if (BlankNode.isWellFormed(graphName)) {
rdfGraphName = Rdf.createBlankNode(graphName);
} else if (UriUtils.isAbsoluteUri(graphName)) {
rdfGraphName = Rdf.createIRI(graphName);
} else {
continue;
}
}
// 1.3.
for (final String subject : Utils.index(nodeMap.subjects(graphName), true)) {
final RdfResource rdfSubject;
// 1.3.1.
if (BlankNode.isWellFormed(subject)) {
rdfSubject = Rdf.createBlankNode(subject);
} else if (UriUtils.isAbsoluteUri(subject)) {
rdfSubject = Rdf.createIRI(subject);
} else {
LOGGER.log(Level.WARNING, "Non well-formed subject [{0}] has been skipped.", subject);
continue;
}
// 1.3.2.
for (final String property : Utils.index(nodeMap.properties(graphName, subject), true)) {
// 1.3.2.1.
if (Keywords.TYPE.equals(property)) {
for (JsonValue type : nodeMap.get(graphName, subject, property).asJsonArray()) {
if (JsonUtils.isNotString(type)) {
continue;
}
final String typeString = ((JsonString)type).getString();
final RdfValue rdfObject;
if (BlankNode.isWellFormed(typeString)) {
rdfObject = Rdf.createBlankNode(typeString);
} else if (UriUtils.isAbsoluteUri(typeString)) {
rdfObject = Rdf.createIRI(typeString);
} else {
continue;
}
dataset.add(Rdf.createNQuad(
rdfSubject,
Rdf.createIRI(RdfConstants.TYPE),
rdfObject,
rdfGraphName
));
}
// 1.3.2.2.
} else if (!Keywords.contains(property)
&& ((BlankNode.isWellFormed(property) && !produceGeneralizedRdf)
|| UriUtils.isAbsoluteUri(property))) {
// 1.3.2.5.
for (JsonValue item : nodeMap.get(graphName, subject, property).asJsonArray()) {
// 1.3.2.5.1.
final List<RdfTriple> listTriples = new ArrayList<>();
// 1.3.2.5.2.
ObjectToRdf
.with(item.asJsonObject(), listTriples, nodeMap)
.rdfDirection(rdfDirection)
.build()
.ifPresent(rdfObject ->
dataset.add(Rdf.createNQuad(
rdfSubject,
Rdf.createResource(property),
rdfObject,
rdfGraphName
)));
// 1.3.2.5.3.
listTriples.stream()
.map(triple -> Rdf.createNQuad(triple, rdfGraphName))
.forEach(dataset::add);
}
}
}
}
}
return dataset;
}
}