Skip to content

Commit 40ed16e

Browse files
committed
custom URI validator
1 parent 68add68 commit 40ed16e

File tree

8 files changed

+1071
-164
lines changed

8 files changed

+1071
-164
lines changed

src/main/java/com/apicatalog/jsonld/context/ActiveContext.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public Optional<TermDefinition> getTerm(final String value) {
120120
}
121121

122122
public TermDefinition getTermNullable(final String value) {
123-
if(value == null) return null;
123+
if (value == null) {
124+
return null;
125+
}
124126
return terms.get(value);
125127
}
126128

@@ -237,13 +239,16 @@ public String toString() {
237239
List<CachedPrefix> prefixCache = new ArrayList<>(10);
238240

239241
public TermDefinition getPrefix(String prefix) {
242+
if (prefix == null) {
243+
return null;
244+
}
240245
if (prefixCache.size() <= 10) {
241246
for (CachedPrefix cachedPrefix : prefixCache) {
242247
if (cachedPrefix.prefix.equals(prefix)) {
243248
return cachedPrefix.termDefinition;
244249
}
245250
}
246-
}else {
251+
} else {
247252
prefixCache.clear();
248253
}
249254

src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java

+2-17
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public final class JsonLdToRdf {
5555
private RdfDirection rdfDirection;
5656
private boolean uriValidation;
5757

58-
private final Set<String> absoluteIris = new HashSet<>();
59-
6058
private JsonLdToRdf(NodeMap nodeMap, RdfDataset dataset) {
6159
this.nodeMap = nodeMap;
6260
this.dataset = dataset;
@@ -99,10 +97,7 @@ public RdfDataset build() throws JsonLdError {
9997

10098
rdfGraphName = Rdf.createBlankNode(graphName);
10199

102-
} else if (absoluteIris.contains(graphName)) {
103-
rdfGraphName = Rdf.createIRI(graphName);
104-
} else if (UriUtils.isAbsoluteUri(graphName, uriValidation)) {
105-
absoluteIris.add(graphName);
100+
} else if (UriUtils.isAbsoluteUri(graphName, uriValidation)) {
106101
rdfGraphName = Rdf.createIRI(graphName);
107102
} else {
108103
continue;
@@ -117,11 +112,7 @@ public RdfDataset build() throws JsonLdError {
117112
// 1.3.1.
118113
if (BlankNode.isWellFormed(subject)) {
119114
rdfSubject = Rdf.createBlankNode(subject);
120-
121-
} else if (absoluteIris.contains(subject)) {
122-
rdfSubject = Rdf.createIRI(subject);
123115
} else if (UriUtils.isAbsoluteUri(subject, uriValidation)) {
124-
absoluteIris.add(subject);
125116
rdfSubject = Rdf.createIRI(subject);
126117
} else {
127118
LOGGER.log(Level.WARNING, "Non well-formed subject [{0}] has been skipped.", subject);
@@ -147,10 +138,7 @@ public RdfDataset build() throws JsonLdError {
147138
if (BlankNode.isWellFormed(typeString)) {
148139
rdfObject = Rdf.createBlankNode(typeString);
149140

150-
} else if (absoluteIris.contains(typeString)) {
151-
rdfObject = Rdf.createIRI(typeString);
152141
} else if (UriUtils.isAbsoluteUri(typeString, uriValidation)) {
153-
absoluteIris.add(typeString);
154142
rdfObject = Rdf.createIRI(typeString);
155143
} else {
156144
continue;
@@ -172,10 +160,7 @@ public RdfDataset build() throws JsonLdError {
172160
if (BlankNode.isWellFormed(property)) {
173161
rdfProperty = !produceGeneralizedRdf ? Rdf.createBlankNode(property) : null;
174162

175-
} else if (absoluteIris.contains(property)) {
176-
rdfProperty = Rdf.createIRI(property);
177-
} else if (UriUtils.isAbsoluteUri(property, uriValidation)) {
178-
absoluteIris.add(property);
163+
} else if (UriUtils.isAbsoluteUri(property, uriValidation)) {
179164
rdfProperty = Rdf.createIRI(property);
180165
} else {
181166
rdfProperty = null;

src/main/java/com/apicatalog/jsonld/deseralization/ObjectToRdf.java

+61-76
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@
1515
*/
1616
package com.apicatalog.jsonld.deseralization;
1717

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-
2918
import com.apicatalog.jsonld.JsonLdError;
3019
import com.apicatalog.jsonld.JsonLdOptions;
3120
import com.apicatalog.jsonld.JsonLdOptions.RdfDirection;
@@ -46,16 +35,21 @@
4635
import com.apicatalog.rdf.RdfValue;
4736
import com.apicatalog.rdf.lang.RdfConstants;
4837
import com.apicatalog.rdf.lang.XsdConstants;
49-
5038
import jakarta.json.JsonNumber;
5139
import jakarta.json.JsonObject;
5240
import jakarta.json.JsonString;
5341
import jakarta.json.JsonValue;
5442

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+
5551
/**
56-
*
5752
* @see <a href="https://w3c.github.io/json-ld-api/#deserialize-json-ld-to-rdf-algorithm">Object to RDF Conversion</a>
58-
*
5953
*/
6054
final class ObjectToRdf {
6155

@@ -64,7 +58,9 @@ final class ObjectToRdf {
6458
private static final DecimalFormat xsdNumberFormat =
6559
new DecimalFormat("0.0##############E0", new DecimalFormatSymbols(Locale.ENGLISH));
6660

67-
static { xsdNumberFormat.setMinimumFractionDigits(1); }
61+
static {
62+
xsdNumberFormat.setMinimumFractionDigits(1);
63+
}
6864

6965
// required
7066
private JsonObject item;
@@ -75,8 +71,6 @@ final class ObjectToRdf {
7571
private RdfDirection rdfDirection;
7672
private boolean uriValidation;
7773

78-
private final Set<String> absoluteIris = new HashSet<>();
79-
8074
private ObjectToRdf(JsonObject item, List<RdfTriple> triples, NodeMap nodeMap) {
8175
this.item = item;
8276
this.triples = triples;
@@ -88,7 +82,7 @@ private ObjectToRdf(JsonObject item, List<RdfTriple> triples, NodeMap nodeMap) {
8882
}
8983

9084
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);
9286
}
9387

9488
public ObjectToRdf rdfDirection(RdfDirection rdfDirection) {
@@ -104,67 +98,58 @@ public RdfValue build() throws JsonLdError {
10498
JsonValue id = item.get(Keywords.ID);
10599

106100
if (JsonUtils.isNotString(id) || JsonUtils.isNull(id)) {
107-
return null;
101+
return null;
108102
}
109103

110-
String idString = ((JsonString)id).getString();
104+
String idString = ((JsonString) id).getString();
111105

112106
if (BlankNode.isWellFormed(idString)) {
113107
return Rdf.createBlankNode(idString);
114108

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)) {
119110
return Rdf.createIRI(idString);
120111
}
121112

122-
return null;
113+
return null;
123114
}
124115

125116
// 3.
126117
if (ListObject.isListObject(item)) {
127118
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();
132123
}
133124

134125
// 4.
135126
if (!ValueObject.isValueObject(item)) {
136-
return null;
127+
return null;
137128
}
138129

139130
final JsonValue value = item.get(Keywords.VALUE);
140131

141132
// 5.
142133
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;
145136

146137
// 6.
147138
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;
157143
}
158-
159144
}
160145

161146
// 7.
162147
if (item.containsKey(Keywords.LANGUAGE)
163148
&& (JsonUtils.isNotString(item.get(Keywords.LANGUAGE))
164-
|| !LanguageTag.isWellFormed(item.getString(Keywords.LANGUAGE)))
165-
) {
149+
|| !LanguageTag.isWellFormed(item.getString(Keywords.LANGUAGE)))
150+
) {
166151
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;
168153
}
169154

170155
String valueString = null;
@@ -174,7 +159,7 @@ public RdfValue build() throws JsonLdError {
174159
valueString = JsonCanonicalizer.canonicalize(value);
175160
datatype = RdfConstants.JSON;
176161

177-
// 9.
162+
// 9.
178163
} else if (JsonUtils.isTrue(value)) {
179164

180165
valueString = "true";
@@ -192,25 +177,25 @@ public RdfValue build() throws JsonLdError {
192177
}
193178

194179

195-
// 10. - 11.
180+
// 10. - 11.
196181
} else if (JsonUtils.isNumber(value)) {
197182

198-
JsonNumber number = ((JsonNumber)value);
183+
JsonNumber number = ((JsonNumber) value);
199184

200185

201186
// 11.
202-
if ((!number.isIntegral() && number.doubleValue() % -1 != 0)
187+
if ((!number.isIntegral() && number.doubleValue() % -1 != 0)
203188
|| XsdConstants.DOUBLE.equals(datatype)
204189
|| number.bigDecimalValue().compareTo(BigDecimal.ONE.movePointRight(21)) >= 0
205-
) {
190+
) {
206191

207192
valueString = toXsdDouble(number.bigDecimalValue());
208193

209194
if (datatype == null) {
210195
datatype = XsdConstants.DOUBLE;
211196
}
212197

213-
// 10.
198+
// 10.
214199
} else {
215200

216201
valueString = number.bigIntegerValue().toString();
@@ -221,22 +206,22 @@ public RdfValue build() throws JsonLdError {
221206

222207
}
223208

224-
// 12.
209+
// 12.
225210
} else if (datatype == null) {
226211

227212
datatype = item.containsKey(Keywords.LANGUAGE)
228-
? RdfConstants.LANG_STRING
229-
: XsdConstants.STRING
230-
;
213+
? RdfConstants.LANG_STRING
214+
: XsdConstants.STRING
215+
;
231216
}
232217

233218
if (valueString == null) {
234219

235220
if (JsonUtils.isNotString(value)) {
236-
return null;
221+
return null;
237222
}
238223

239-
valueString = ((JsonString)value).getString();
224+
valueString = ((JsonString) value).getString();
240225
}
241226

242227
RdfLiteral rdfLiteral = null;
@@ -246,18 +231,18 @@ public RdfValue build() throws JsonLdError {
246231

247232
// 13.1.
248233
final String language = item.containsKey(Keywords.LANGUAGE)
249-
? item.getString(Keywords.LANGUAGE).toLowerCase()
250-
: "";
234+
? item.getString(Keywords.LANGUAGE).toLowerCase()
235+
: "";
251236
// 13.2.
252237
if (RdfDirection.I18N_DATATYPE == rdfDirection) {
253238
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));
257242

258243
rdfLiteral = Rdf.createTypedString(valueString, datatype);
259244

260-
// 13.3.
245+
// 13.3.
261246
} else if (RdfDirection.COMPOUND_LITERAL == rdfDirection) {
262247

263248
final String blankNodeId = nodeMap.createIdentifier();
@@ -267,31 +252,31 @@ public RdfValue build() throws JsonLdError {
267252

268253
// 13.3.2.
269254
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+
);
274259

275260
// 13.3.3.
276261
if (item.containsKey(Keywords.LANGUAGE) && JsonUtils.isString(item.get(Keywords.LANGUAGE))) {
277262
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+
);
282267
}
283268

284269
// 13.3.4.
285270
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+
);
290275

291276
return Rdf.createBlankNode(blankNodeId);
292277
}
293278

294-
// 14.
279+
// 14.
295280
} else {
296281
if (item.containsKey(Keywords.LANGUAGE) && JsonUtils.isString(item.get(Keywords.LANGUAGE))) {
297282

0 commit comments

Comments
 (0)