/** * Converts RDF statements into JSON-LD. * * @param dataset * the RDF statements. * @return A list of JSON-LD objects found in the given dataset. * @throws JsonLdError * If there was an error during conversion from RDF to JSON-LD. */ public List<Object> fromRDF(final RDFDataset dataset) throws JsonLdError { return fromRDF(dataset, false); }
private class EmbedNode { public Object parent = null; public String property = null; public EmbedNode(Object parent, String property) { this.parent = parent; this.property = property; } }
/** * Performs JSON-LD <a * href="http://json-ld.org/spec/latest/json-ld-framing/">framing</a>. * * @param input * the expanded JSON-LD to frame. * @param frame * the expanded JSON-LD frame to use. * @return the framed output. * @throws JsonLdError * If the framing was not successful. */ public List<Object> frame(Object input, List<Object> frame) throws JsonLdError { // create framing state final FramingContext state = new FramingContext(this.opts); // use tree map so keys are sotred by default final Map<String, Object> nodes = new TreeMap<>(); generateNodeMap(input, nodes); this.nodeMap = (Map<String, Object>) nodes.get("@default"); final List<Object> framed = new ArrayList<>(); // NOTE: frame validation is done by the function not allowing anything // other than list to me passed frame(state, this.nodeMap, (frame != null && !frame.isEmpty() ? (Map<String, Object>) frame.get(0) : newMap()), framed, null); return framed; }
final List<Object> expandedFrame = expand(frame, opts); final JsonLdApi api = new JsonLdApi(expandedInput, opts); final List<Object> framed = api.frame(expandedInput, expandedFrame); final Context activeCtx = api.context.parse(((Map<String, Object>) frame).get("@context")); Object compacted = api.compact(activeCtx, null, framed); if (!(compacted instanceof List)) { final List<Object> tmp = new ArrayList<>();
nodeMap.put("@default", newMap()); new JsonLdApi(opts).generateNodeMap(expanded, nodeMap); activeCtx = activeCtx.parse(context); Object compacted = new JsonLdApi(opts).compact(activeCtx, null, flattened, opts.getCompactArrays()); if (!(compacted instanceof List)) {
final Map<String, Object> matches = filterNodes(state, nodes, frame); Boolean embedOn = getFrameFlag(frame, "@embed", state.embed); final Boolean explicicOn = getFrameFlag(frame, "@explicit", state.explicit); removeEmbed(state, id); addFrameOutput(state, parent, property, output); } else { embedValues(state, element, prop, output); addFrameOutput(state, output, prop, list); frame(state, tmp, (Map<String, Object>) ((List<Object>) frame.get(prop)) .get(0), list, "@list"); addFrameOutput(state, list, "@list", listitem); frame(state, tmp, (Map<String, Object>) ((List<Object>) frame.get(prop)).get(0), output, prop); addFrameOutput(state, output, prop, item); final boolean omitDefaultOn = getFrameFlag(propertyFrame, "@omitDefault", state.omitDefault); if (!omitDefaultOn && !output.containsKey(prop)) {
/** * Performs RDF dataset normalization on the given JSON-LD input. The output * is an RDF dataset unless the 'format' option is used. * * @param input * the JSON-LD input to normalize. * @param options * the options to use: [base] the base IRI to use. [format] the * format if output is a string: 'application/nquads' for * N-Quads. [loadContext(url, callback(err, url, result))] the * context loader. * @return The JSON-LD object * @throws JsonLdError * If there is an error normalizing the dataset. */ public static Object normalize(Object input, JsonLdOptions options) throws JsonLdError { final JsonLdOptions opts = new JsonLdOptions(options.getBase()); opts.format = null; final RDFDataset dataset = (RDFDataset) toRDF(input, opts); return new JsonLdApi(options).normalize(dataset); }
/** * Expansion Algorithm * * http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm * * @param activeCtx * The Active Context * @param element * The current element * @return The expanded JSON-LD object. * @throws JsonLdError * If there was an error during expansion. */ public Object expand(Context activeCtx, Object element) throws JsonLdError { return expand(activeCtx, null, element); }
generateNodeMap(item, nodeMap, activeGraph, activeSubject, activeProperty, list); if (activeSubject != null && activeSubject.toString().startsWith("_:")) { String old = blankNodeMapping.get(activeSubject.toString()); blankNodeMapping.put(activeSubject.toString(), old + generateBlankNodeId(activeProperty, element)); newTypes.add(generateBlankNodeIdentifier(item)); } else { newTypes.add(item); generateNodeMap(elem.get("@list"), nodeMap, activeGraph, activeSubject, activeProperty, result); if (id != null) { if (id.startsWith("_:")) { id = generateBlankNodeIdentifier(id); id = generateBlankNodeIdentifier(null); generateNodeMap(value, nodeMap, activeGraph, referencedNode, stringObjectEntry.getKey(), null); generateNodeMap(elem.remove("@graph"), nodeMap, id, null, null, null); property = generateBlankNodeIdentifier(property); generateNodeMap(value, nodeMap, activeGraph, id, property, null);
generateNodeMap(item, nodeMap, activeGraph, activeSubject, activeProperty, list); newTypes.add(generateBlankNodeIdentifier(item)); } else { newTypes.add(item); generateNodeMap(elem.get(JsonLdConsts.LIST), nodeMap, activeGraph, activeSubject, activeProperty, result); if (id != null) { if (id.startsWith("_:")) { id = generateBlankNodeIdentifier(id); id = generateBlankNodeIdentifier(null); generateNodeMap(value, nodeMap, activeGraph, referencedNode, property, null); generateNodeMap(elem.remove(JsonLdConsts.GRAPH), nodeMap, id, null, null, null); property = generateBlankNodeIdentifier(property); generateNodeMap(value, nodeMap, activeGraph, id, property, null);
/** * Generates a fresh, unused, blank node identifier using the algorithm * specified in: * * http://www.w3.org/TR/json-ld-api/#generate-blank-node-identifier * * @return A fresh, unused, blank node identifier. */ String generateBlankNodeIdentifier() { return generateBlankNodeIdentifier(null); }
/** * Compaction Algorithm * * http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm * * @param activeCtx * The Active Context * @param activeProperty * The Active Property * @param element * The current element * @return The compacted JSON-LD object. * @throws JsonLdError * If there was an error during compaction. */ public Object compact(Context activeCtx, String activeProperty, Object element) throws JsonLdError { return compact(activeCtx, activeProperty, element, JsonLdOptions.DEFAULT_COMPACT_ARRAYS); }
continue; embedValues(state, s, stringObjectEntry.getKey(), o); addFrameOutput(state, output, property, o); addFrameOutput(state, output, property, JsonLdUtils.clone(o));
private Map<String, Object> filterNodes(FramingContext state, Map<String, Object> nodes, Map<String, Object> frame, boolean requireAll) throws JsonLdError { final Map<String, Object> rval = newMap(); for (final String id : nodes.keySet()) { final Map<String, Object> element = (Map<String, Object>) nodes.get(id); if (element != null && filterNode(state, element, frame, requireAll)) { rval.put(id, element); } } return rval; }
final JsonLdApi api = new JsonLdApi(expandedInput, opts); final Context activeCtx = api.context .parse(((Map<String, Object>) frame).get(JsonLdConsts.CONTEXT)); final List<Object> framed = api.frame(expandedInput, expandedFrame); if (opts.getPruneBlankNodeIdentifiers()) { JsonLdUtils.pruneBlankNodes(framed); Object compacted = api.compact(activeCtx, null, framed, opts.getCompactArrays()); final Map<String, Object> rval = activeCtx.serialize(); final boolean addGraph = ((!(compacted instanceof List)) && !opts.getOmitGraph());