15
15
*/
16
16
package com .apicatalog .jsonld .deseralization ;
17
17
18
- import java .math .BigDecimal ;
19
- import java .text .DecimalFormat ;
20
- import java .text .DecimalFormatSymbols ;
21
- import java .util .HashSet ;
22
- import java .util .List ;
23
- import java .util .Locale ;
24
- import java .util .Optional ;
25
- import java .util .Set ;
26
- import java .util .logging .Level ;
27
- import java .util .logging .Logger ;
28
-
29
18
import com .apicatalog .jsonld .JsonLdError ;
30
19
import com .apicatalog .jsonld .JsonLdOptions ;
31
20
import com .apicatalog .jsonld .JsonLdOptions .RdfDirection ;
46
35
import com .apicatalog .rdf .RdfValue ;
47
36
import com .apicatalog .rdf .lang .RdfConstants ;
48
37
import com .apicatalog .rdf .lang .XsdConstants ;
49
-
50
38
import jakarta .json .JsonNumber ;
51
39
import jakarta .json .JsonObject ;
52
40
import jakarta .json .JsonString ;
53
41
import jakarta .json .JsonValue ;
54
42
43
+ import java .math .BigDecimal ;
44
+ import java .text .DecimalFormat ;
45
+ import java .text .DecimalFormatSymbols ;
46
+ import java .util .List ;
47
+ import java .util .Locale ;
48
+ import java .util .logging .Level ;
49
+ import java .util .logging .Logger ;
50
+
55
51
/**
56
- *
57
52
* @see <a href="https://w3c.github.io/json-ld-api/#deserialize-json-ld-to-rdf-algorithm">Object to RDF Conversion</a>
58
- *
59
53
*/
60
54
final class ObjectToRdf {
61
55
@@ -64,7 +58,9 @@ final class ObjectToRdf {
64
58
private static final DecimalFormat xsdNumberFormat =
65
59
new DecimalFormat ("0.0##############E0" , new DecimalFormatSymbols (Locale .ENGLISH ));
66
60
67
- static { xsdNumberFormat .setMinimumFractionDigits (1 ); }
61
+ static {
62
+ xsdNumberFormat .setMinimumFractionDigits (1 );
63
+ }
68
64
69
65
// required
70
66
private JsonObject item ;
@@ -75,8 +71,6 @@ final class ObjectToRdf {
75
71
private RdfDirection rdfDirection ;
76
72
private boolean uriValidation ;
77
73
78
- private final Set <String > absoluteIris = new HashSet <>();
79
-
80
74
private ObjectToRdf (JsonObject item , List <RdfTriple > triples , NodeMap nodeMap ) {
81
75
this .item = item ;
82
76
this .triples = triples ;
@@ -88,7 +82,7 @@ private ObjectToRdf(JsonObject item, List<RdfTriple> triples, NodeMap nodeMap) {
88
82
}
89
83
90
84
public static final ObjectToRdf with (JsonObject item , List <RdfTriple > triples , NodeMap nodeMap ) {
91
- return new ObjectToRdf (item , triples , nodeMap );
85
+ return new ObjectToRdf (item , triples , nodeMap );
92
86
}
93
87
94
88
public ObjectToRdf rdfDirection (RdfDirection rdfDirection ) {
@@ -104,67 +98,58 @@ public RdfValue build() throws JsonLdError {
104
98
JsonValue id = item .get (Keywords .ID );
105
99
106
100
if (JsonUtils .isNotString (id ) || JsonUtils .isNull (id )) {
107
- return null ;
101
+ return null ;
108
102
}
109
103
110
- String idString = ((JsonString )id ).getString ();
104
+ String idString = ((JsonString ) id ).getString ();
111
105
112
106
if (BlankNode .isWellFormed (idString )) {
113
107
return Rdf .createBlankNode (idString );
114
108
115
- } else if (absoluteIris .contains (idString )) {
116
- return Rdf .createIRI (idString );
117
- }else if ( UriUtils .isAbsoluteUri (idString , uriValidation )) {
118
- absoluteIris .add (idString );
109
+ } else if (UriUtils .isAbsoluteUri (idString , uriValidation )) {
119
110
return Rdf .createIRI (idString );
120
111
}
121
112
122
- return null ;
113
+ return null ;
123
114
}
124
115
125
116
// 3.
126
117
if (ListObject .isListObject (item )) {
127
118
return ListToRdf
128
- .with (item .get (Keywords .LIST ).asJsonArray (), triples , nodeMap )
129
- .rdfDirection (rdfDirection )
130
- .uriValidation (uriValidation )
131
- .build ();
119
+ .with (item .get (Keywords .LIST ).asJsonArray (), triples , nodeMap )
120
+ .rdfDirection (rdfDirection )
121
+ .uriValidation (uriValidation )
122
+ .build ();
132
123
}
133
124
134
125
// 4.
135
126
if (!ValueObject .isValueObject (item )) {
136
- return null ;
127
+ return null ;
137
128
}
138
129
139
130
final JsonValue value = item .get (Keywords .VALUE );
140
131
141
132
// 5.
142
133
String datatype = item .containsKey (Keywords .TYPE ) && JsonUtils .isString (item .get (Keywords .TYPE ))
143
- ? item .getString (Keywords .TYPE )
144
- : null ;
134
+ ? item .getString (Keywords .TYPE )
135
+ : null ;
145
136
146
137
// 6.
147
138
if (datatype != null && !Keywords .JSON .equals (datatype )) {
148
-
149
- if (!absoluteIris .contains (datatype )) {
150
- boolean absoluteUri = UriUtils .isAbsoluteUri (datatype , uriValidation );
151
- if (absoluteUri ) {
152
- absoluteIris .add (datatype );
153
- }else {
154
- LOGGER .log (Level .WARNING , "Datatype [{0}] is not an absolute IRI nor @json and value is skipped." , datatype );
155
- return null ;
156
- }
139
+ boolean absoluteUri = UriUtils .isAbsoluteUri (datatype , uriValidation );
140
+ if (!absoluteUri ) {
141
+ LOGGER .log (Level .WARNING , "Datatype [{0}] is not an absolute IRI nor @json and value is skipped." , datatype );
142
+ return null ;
157
143
}
158
-
159
144
}
160
145
161
146
// 7.
162
147
if (item .containsKey (Keywords .LANGUAGE )
163
148
&& (JsonUtils .isNotString (item .get (Keywords .LANGUAGE ))
164
- || !LanguageTag .isWellFormed (item .getString (Keywords .LANGUAGE )))
165
- ) {
149
+ || !LanguageTag .isWellFormed (item .getString (Keywords .LANGUAGE )))
150
+ ) {
166
151
LOGGER .log (Level .WARNING , "Language tag [{0}] is not well formed string and value is skipped." , item .get (Keywords .LANGUAGE ));
167
- return null ;
152
+ return null ;
168
153
}
169
154
170
155
String valueString = null ;
@@ -174,7 +159,7 @@ public RdfValue build() throws JsonLdError {
174
159
valueString = JsonCanonicalizer .canonicalize (value );
175
160
datatype = RdfConstants .JSON ;
176
161
177
- // 9.
162
+ // 9.
178
163
} else if (JsonUtils .isTrue (value )) {
179
164
180
165
valueString = "true" ;
@@ -192,25 +177,25 @@ public RdfValue build() throws JsonLdError {
192
177
}
193
178
194
179
195
- // 10. - 11.
180
+ // 10. - 11.
196
181
} else if (JsonUtils .isNumber (value )) {
197
182
198
- JsonNumber number = ((JsonNumber )value );
183
+ JsonNumber number = ((JsonNumber ) value );
199
184
200
185
201
186
// 11.
202
- if ((!number .isIntegral () && number .doubleValue () % -1 != 0 )
187
+ if ((!number .isIntegral () && number .doubleValue () % -1 != 0 )
203
188
|| XsdConstants .DOUBLE .equals (datatype )
204
189
|| number .bigDecimalValue ().compareTo (BigDecimal .ONE .movePointRight (21 )) >= 0
205
- ) {
190
+ ) {
206
191
207
192
valueString = toXsdDouble (number .bigDecimalValue ());
208
193
209
194
if (datatype == null ) {
210
195
datatype = XsdConstants .DOUBLE ;
211
196
}
212
197
213
- // 10.
198
+ // 10.
214
199
} else {
215
200
216
201
valueString = number .bigIntegerValue ().toString ();
@@ -221,22 +206,22 @@ public RdfValue build() throws JsonLdError {
221
206
222
207
}
223
208
224
- // 12.
209
+ // 12.
225
210
} else if (datatype == null ) {
226
211
227
212
datatype = item .containsKey (Keywords .LANGUAGE )
228
- ? RdfConstants .LANG_STRING
229
- : XsdConstants .STRING
230
- ;
213
+ ? RdfConstants .LANG_STRING
214
+ : XsdConstants .STRING
215
+ ;
231
216
}
232
217
233
218
if (valueString == null ) {
234
219
235
220
if (JsonUtils .isNotString (value )) {
236
- return null ;
221
+ return null ;
237
222
}
238
223
239
- valueString = ((JsonString )value ).getString ();
224
+ valueString = ((JsonString ) value ).getString ();
240
225
}
241
226
242
227
RdfLiteral rdfLiteral = null ;
@@ -246,18 +231,18 @@ public RdfValue build() throws JsonLdError {
246
231
247
232
// 13.1.
248
233
final String language = item .containsKey (Keywords .LANGUAGE )
249
- ? item .getString (Keywords .LANGUAGE ).toLowerCase ()
250
- : "" ;
234
+ ? item .getString (Keywords .LANGUAGE ).toLowerCase ()
235
+ : "" ;
251
236
// 13.2.
252
237
if (RdfDirection .I18N_DATATYPE == rdfDirection ) {
253
238
datatype = "https://www.w3.org/ns/i18n#"
254
- .concat (language )
255
- .concat ("_" )
256
- .concat (item .getString (Keywords .DIRECTION ));
239
+ .concat (language )
240
+ .concat ("_" )
241
+ .concat (item .getString (Keywords .DIRECTION ));
257
242
258
243
rdfLiteral = Rdf .createTypedString (valueString , datatype );
259
244
260
- // 13.3.
245
+ // 13.3.
261
246
} else if (RdfDirection .COMPOUND_LITERAL == rdfDirection ) {
262
247
263
248
final String blankNodeId = nodeMap .createIdentifier ();
@@ -267,31 +252,31 @@ public RdfValue build() throws JsonLdError {
267
252
268
253
// 13.3.2.
269
254
triples .add (Rdf .createTriple (
270
- subject ,
271
- Rdf .createIRI (RdfConstants .VALUE ),
272
- Rdf .createString (valueString ))
273
- );
255
+ subject ,
256
+ Rdf .createIRI (RdfConstants .VALUE ),
257
+ Rdf .createString (valueString ))
258
+ );
274
259
275
260
// 13.3.3.
276
261
if (item .containsKey (Keywords .LANGUAGE ) && JsonUtils .isString (item .get (Keywords .LANGUAGE ))) {
277
262
triples .add (Rdf .createTriple (
278
- subject ,
279
- Rdf .createIRI (RdfConstants .LANGUAGE ),
280
- Rdf .createString (item .getString (Keywords .LANGUAGE ).toLowerCase ()))
281
- );
263
+ subject ,
264
+ Rdf .createIRI (RdfConstants .LANGUAGE ),
265
+ Rdf .createString (item .getString (Keywords .LANGUAGE ).toLowerCase ()))
266
+ );
282
267
}
283
268
284
269
// 13.3.4.
285
270
triples .add (Rdf .createTriple (
286
- subject ,
287
- Rdf .createIRI (RdfConstants .DIRECTION ),
288
- Rdf .createString (item .getString (Keywords .DIRECTION )))
289
- );
271
+ subject ,
272
+ Rdf .createIRI (RdfConstants .DIRECTION ),
273
+ Rdf .createString (item .getString (Keywords .DIRECTION )))
274
+ );
290
275
291
276
return Rdf .createBlankNode (blankNodeId );
292
277
}
293
278
294
- // 14.
279
+ // 14.
295
280
} else {
296
281
if (item .containsKey (Keywords .LANGUAGE ) && JsonUtils .isString (item .get (Keywords .LANGUAGE ))) {
297
282
0 commit comments