|
15 | 15 | import static com.github.jsonldjava.core.JsonLdUtils.isString; |
16 | 16 | import static com.github.jsonldjava.core.JsonLdUtils.isValue; |
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URL; |
18 | 20 | import java.text.DecimalFormat; |
19 | 21 | import java.util.ArrayList; |
20 | 22 | import java.util.Collections; |
|
25 | 27 | import java.util.Set; |
26 | 28 | import java.util.regex.Pattern; |
27 | 29 |
|
| 30 | +import com.fasterxml.jackson.core.JsonParseException; |
| 31 | + |
28 | 32 | /** |
29 | 33 | * Starting to migrate away from using plain java Maps as the internal RDF |
30 | 34 | * 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) { |
359 | 363 | context.put(ns, prefix); |
360 | 364 | } |
361 | 365 |
|
362 | | - public void getNamespace(String ns) { |
363 | | - context.get(ns); |
| 366 | + public String getNamespace(String ns) { |
| 367 | + return context.get(ns); |
364 | 368 | } |
365 | 369 |
|
366 | 370 | /** |
@@ -394,45 +398,32 @@ public Map<String, Object> getContext() { |
394 | 398 | * |
395 | 399 | * @param context |
396 | 400 | * The context to parse |
| 401 | + * @throws JsonLdError If the context can't be parsed |
397 | 402 | */ |
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); |
411 | 407 | } else { |
412 | | - throw new RuntimeException("Can't handle context of type " + contextLike.getClass()); |
| 408 | + context = new Context(); |
413 | 409 | } |
| 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); |
414 | 414 |
|
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); |
417 | 417 | if ("@vocab".equals(key)) { |
418 | 418 | if (val == null || isString(val)) { |
419 | 419 | setNamespace("", (String) val); |
420 | 420 | } else { |
421 | | - // TODO: the context is actually invalid, should we throw an |
422 | | - // exception? |
423 | 421 | } |
424 | | - } else if ("@context".equals(key)) { |
425 | | - // go deeper! |
426 | | - parseContext(context.get("@context")); |
427 | 422 | } else if (!isKeyword(key)) { |
| 423 | + setNamespace(key, val); |
428 | 424 | // TODO: should we make sure val is a valid URI prefix (i.e. it |
429 | 425 | // ends with /# or ?) |
430 | 426 | // 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 | | - } |
436 | 427 | } |
437 | 428 | } |
438 | 429 | } |
|
0 commit comments