/** * Set the current JSON-LD context to the given map. * * @param map a map of strings for the new JSON-LD context * @throws IOException on issue parsing JSON */ public void setContext(Map<String, Object> map) throws IOException { try { this.context = new Context().parse(map); } catch (JsonLdError e) { throw new IOException(jsonldContextParseError, e); } }
/** * Get a copy of the current prefix map. * * @return a copy of the current prefix map */ public Map<String, String> getPrefixes() { return this.context.getPrefixes(false); }
/** * Get a copy of the current context. * * @return a copy of the current context */ public Context getContext() { return this.context.clone(); }
/** * Add a prefix mapping to the current JSON-LD context, as a prefix string and target string. * Rebuilds the context. * * @param prefix the short prefix to add; should not include ":" * @param target the IRI string that is the target of the prefix * @throws IOException if prefix cannot be parsed */ public void addPrefix(String prefix, String target) throws IOException { try { context.put(prefix.trim(), target.trim()); context.remove("@base"); setContext((Map<String, Object>) context); } catch (Exception e) { throw new IOException(String.format(prefixLoadError, prefix, target), e); } }
Context result = this.clone(); // TODO: clone? result = new Context(this.options); continue; } else if (context instanceof Context) { result = ((Context) context).clone(); String uri = (String) result.get("@base"); uri = JsonLdUrl.resolve(uri, (String) context); result = result.parse(context, remoteContexts); final Object value = ((Map<String, Object>) context).get("@base"); if (value == null) { result.remove("@base"); } else if (value instanceof String) { if (JsonLdUtils.isAbsoluteIri((String) value)) { result.put("@base", value); } else { final String baseUri = (String) result.get("@base"); if (!JsonLdUtils.isAbsoluteIri(baseUri)) { throw new JsonLdError(Error.INVALID_BASE_IRI, baseUri); result.put("@base", JsonLdUrl.resolve(baseUri, (String) value)); final Object value = ((Map<String, Object>) context).get("@vocab"); if (value == null) { result.remove("@vocab"); } else if (value instanceof String) {
&& JsonLdConsts.INDEX.equals(this.getContainer(activeProperty))) { numberMembers--; final String typeMapping = getTypeMapping(activeProperty); final String languageMapping = getLanguageMapping(activeProperty); if (value.containsKey(JsonLdConsts.ID)) { return compactIri((String) value.get(JsonLdConsts.ID)); return compactIri((String) value.get(JsonLdConsts.ID), true); .equals(value.get(JsonLdConsts.LANGUAGE), this.get(JsonLdConsts.LANGUAGE))) { return valueValue; || !this.containsKey(JsonLdConsts.LANGUAGE) || (termDefinitions.containsKey(activeProperty) && getTermDefinition(activeProperty).containsKey(JsonLdConsts.LANGUAGE) && languageMapping == null))) { return valueValue;
Context activeCtx = new Context(opts); activeCtx = activeCtx.parse(context); compacted = tmp; final String alias = activeCtx.compactIri(JsonLdConsts.GRAPH); final Map<String, Object> rval = activeCtx.serialize(); rval.put(alias, compacted); return rval;
.getContainer(activeProperty))) && (v instanceof List || (v instanceof Map && ((Map<String, Object>) v) .containsKey("@list")))) { activeCtx = activeCtx.parse(elem.get("@context")); final String expandedProperty = activeCtx.expandIri(key, false, true, null, null); Object expandedValue = null; .expandIri((String) value, true, false, null, null); ((List<String>) expandedValue).add(activeCtx.expandIri((String) v, true, true, null, null)); expandedValue = activeCtx.expandIri((String) value, true, true, null, null); else if ("@language".equals(activeCtx.getContainer(key)) && value instanceof Map) { else if ("@index".equals(activeCtx.getContainer(key)) && value instanceof Map) { if ("@list".equals(activeCtx.getContainer(key))) { if (!(expandedValue instanceof Map) || !((Map<String, Object>) expandedValue).containsKey("@list")) { if (activeCtx.isReverseProperty(key)) { return activeCtx.expandValue(activeProperty, element);
Context context; if (api != null) { context = new Context(api.opts); } else { context = new Context(); context = context.parse(contextLike); final Map<String, String> prefixes = context.getPrefixes(true);
if (relativeToVocab && getInverse().containsKey(iri)) { String defaultLanguage = (String) this.get(JsonLdConsts.LANGUAGE); if (defaultLanguage == null) { defaultLanguage = JsonLdConsts.NONE; && ((Map<String, Object>) value).containsKey(JsonLdConsts.ID)) { final String result = this.compactIri( (String) ((Map<String, Object>) value).get(JsonLdConsts.ID), null, true, true); final String term = selectTerm(iri, containers, typeLanguage, preferredValues); if (relativeToVocab && this.containsKey(JsonLdConsts.VOCAB)) { final String vocab = (String) this.get(JsonLdConsts.VOCAB); + iri.substring(((String) termDefinition.get(JsonLdConsts.ID)).length()); compactIRI = _iriCompactionStep5point4(iri, value, compactIRI, candidate, termDefinitions); return JsonLdUrl.removeBase(this.get(JsonLdConsts.BASE), iri);
context = ((Map<String, Object>) context).get("@context"); Context activeCtx = new Context(opts); activeCtx = activeCtx.parse(context); final Map<String, Object> tmp = newMap(); tmp.put(activeCtx.compactIri("@graph", true), compacted); compacted = tmp;
final Context activeCtx = api.context.parse(((Map<String, Object>) frame).get("@context")); compacted = tmp; final String alias = activeCtx.compactIri("@graph"); final Map<String, Object> rval = activeCtx.serialize(); rval.put(alias, compacted); JsonLdUtils.removePreserve(activeCtx, rval, opts);
&& activeCtx.getContainer(activeProperty) == null) { return result.get(0); final Object compactedValue = activeCtx.compactValue(activeProperty, elem); if (!(compactedValue instanceof Map || compactedValue instanceof List)) { return compactedValue; compactedValue = activeCtx.compactIri((String) expandedValue, "@type".equals(expandedProperty)); types.add(activeCtx.compactIri(expandedType, true)); final String alias = activeCtx.compactIri(expandedProperty, true); if (activeCtx.isReverseProperty(property)) { if (("@set".equals(activeCtx.getContainer(property)) || !compactArrays) && !(value instanceof List)) { final List<Object> tmp = new ArrayList<>(); final String alias = activeCtx.compactIri("@reverse", true); && "@index".equals(activeCtx.getContainer(activeProperty))) { continue; final String alias = activeCtx.compactIri(expandedProperty, true); final String itemActiveProperty = activeCtx.compactIri(expandedProperty, expandedValue, true, insideReverse);
/** * Context Processing Algorithm * * http://json-ld.org/spec/latest/json-ld-api/#context-processing-algorithms * * @param localContext * The Local Context object. * @param remoteContexts * The list of Strings denoting the remote Context URLs. * @return The parsed and merged Context. * @throws JsonLdError * If there is an error parsing the contexts. */ @SuppressWarnings("unchecked") public Context parse(Object localContext, List<String> remoteContexts) throws JsonLdError { return parse(localContext, remoteContexts, false); }
/** * Returns the replacement value for a {@literal null} value in a map. */ private Object computeFieldValueIfPresent(String key, Object oldValue) { String term = term(key); String container = context.getContainer(term); if (container == null || container.isEmpty() || container.equals(JsonLdConsts.NONE)) { return null; } else { return oldValue; } }
/** * Returns the term used by this context for the supplied IRI. */ public Optional<String> lookupTerm(String iri) { // Special case for keywords if (isKeyword(iri)) { return Optional.of(iri); } Object definition = context.getInverse().get(iri); if (definition instanceof Map<?, ?>) { definition = ((Map<?, ?>) definition).values().iterator().next(); if (definition instanceof Map<?, ?>) { definition = ((Map<?, ?>) definition).get(JsonLdConsts.TYPE); if (definition instanceof Map<?, ?>) { return Optional.of(((Map<?, ?>) definition).values().iterator().next().toString()); } } } return Optional.empty(); }
/** Set an empty context. */ public void setContext() { this.context = new Context(); }
private void init(JsonLdOptions options) { this.options = options; if (options.getBase() != null) { this.put("@base", options.getBase()); } this.termDefinitions = newMap(); }
String getTypeMapping(String property) { return context.getTypeMapping(property); }
Map<String, Object> serialize() { return context.serialize(); }