Skip to content

Commit d3d0027

Browse files
committed
Use Context to parse contexts and find prefixes
1 parent 67cdb6f commit d3d0027

1 file changed

Lines changed: 19 additions & 28 deletions

File tree

core/src/main/java/com/github/jsonldjava/core/RDFDataset.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import static com.github.jsonldjava.core.JsonLdUtils.isString;
1616
import static com.github.jsonldjava.core.JsonLdUtils.isValue;
1717

18+
import java.io.IOException;
19+
import java.net.URL;
1820
import java.text.DecimalFormat;
1921
import java.util.ArrayList;
2022
import java.util.Collections;
@@ -25,6 +27,8 @@
2527
import java.util.Set;
2628
import java.util.regex.Pattern;
2729

30+
import com.fasterxml.jackson.core.JsonParseException;
31+
2832
/**
2933
* Starting to migrate away from using plain java Maps as the internal RDF
3034
* dataset store. Currently each item just wraps a Map based on the old format
@@ -359,8 +363,8 @@ public void setNamespace(String ns, String prefix) {
359363
context.put(ns, prefix);
360364
}
361365

362-
public void getNamespace(String ns) {
363-
context.get(ns);
366+
public String getNamespace(String ns) {
367+
return context.get(ns);
364368
}
365369

366370
/**
@@ -394,45 +398,32 @@ public Map<String, Object> getContext() {
394398
*
395399
* @param context
396400
* The context to parse
401+
* @throws JsonLdError If the context can't be parsed
397402
*/
398-
public void parseContext(Object contextLike) {
399-
Map<String, Object> context;
400-
401-
if (contextLike instanceof Map) {
402-
context = (Map<String, Object>) contextLike;
403-
} else if (contextLike instanceof List) {
404-
for (Object cntx : (List)contextLike) {
405-
parseContext(cntx);
406-
}
407-
return;
408-
} else if (contextLike instanceof String) {
409-
// FIXME: Ignore external contexts for now
410-
return;
403+
public void parseContext(Object contextLike) throws JsonLdError {
404+
Context context;
405+
if (api != null) {
406+
context = new Context(api.opts);
411407
} else {
412-
throw new RuntimeException("Can't handle context of type " + contextLike.getClass());
408+
context = new Context();
413409
}
410+
// Context will do our recursive parsing and initial IRI resolution
411+
context = context.parse(contextLike);
412+
// And then leak to us the potential 'prefixes'
413+
Map<String, String> prefixes = context.getPrefixes(false);
414414

415-
for (final String key : context.keySet()) {
416-
final Object val = context.get(key);
415+
for (final String key : prefixes.keySet()) {
416+
final String val = prefixes.get(key);
417417
if ("@vocab".equals(key)) {
418418
if (val == null || isString(val)) {
419419
setNamespace("", (String) val);
420420
} else {
421-
// TODO: the context is actually invalid, should we throw an
422-
// exception?
423421
}
424-
} else if ("@context".equals(key)) {
425-
// go deeper!
426-
parseContext(context.get("@context"));
427422
} else if (!isKeyword(key)) {
423+
setNamespace(key, val);
428424
// TODO: should we make sure val is a valid URI prefix (i.e. it
429425
// ends with /# or ?)
430426
// or is it ok that full URIs for terms are used?
431-
if (val instanceof String) {
432-
setNamespace(key, (String) context.get(key));
433-
} else if (isObject(val) && ((HashMap<String, Object>) val).containsKey("@id")) {
434-
setNamespace(key, (String) ((HashMap<String, Object>) val).get("@id"));
435-
}
436427
}
437428
}
438429
}

0 commit comments

Comments
 (0)