AbstractJsonLdGraphLike(final String bnodePrefix) { this(new RDFDataset(), bnodePrefix); }
@Override public RDFDataset parse(Object input) throws JsonLdError { count = 0; final Map<BlankNode, String> blankNodeMap = new HashMap<BlankNode, String>(1024); final RDFDataset result = new RDFDataset(); if (input instanceof Graph) { for (final Triple t : ((Graph) input)) { handleStatement(result, t, blankNodeMap); } } blankNodeMap.clear(); // help gc return result; }
@Override public RDFDataset parse(Object input) throws JsonLdError { count = 0; Map<BlankNode,String> bNodeMap = new HashMap<BlankNode,String>(1024); final RDFDataset result = new RDFDataset(); if(input instanceof Graph){ for(Triple t : ((Graph)input)){ handleStatement(result,t, bNodeMap); } } bNodeMap.clear(); //help gc return result; }
@Override public RDFDataset parse(Object input) throws JsonLdError { count = 0; Map<BlankNode,String> bNodeMap = new HashMap<BlankNode,String>(1024); final RDFDataset result = new RDFDataset(); if(input instanceof Graph){ for(Triple t : ((Graph)input)){ handleStatement(result,t, bNodeMap); } } bNodeMap.clear(); //help gc return result; }
public RDFDataset parseXmlJson(JSONObject input) throws JSONException { RDFDataset dataset = new RDFDataset(); if(!input.has("rdf:RDF")){ }else{ JSONObject rdf = input.getJSONObject("rdf:RDF"); HashMap<String, String> namespaces = new HashMap<>(); Iterator<String> keys = rdf.keys(); while(keys.hasNext()){ String key = keys.next(); if(key.contains("xmlns:")){ namespaces.put(key.replace("xmlns:", ""), rdf.getString(key)); dataset.setNamespace(key.replace("xmlns:", ""), rdf.getString(key)); } } for(String ns : namespaces.keySet()){ rdf.remove("xmlns:"+ns); } //System.out.println(rdf.toString(2)); parseInner(rdf, dataset, namespaces); } return dataset; }
/** * Adds RDF triples for each graph in the current node map to an RDF * dataset. * * @return the RDF dataset. * @throws JsonLdError * If there was an error converting from JSON-LD to RDF. */ public RDFDataset toRDF() throws JsonLdError { // TODO: make the default generateNodeMap call (i.e. without a // graphName) create and return the nodeMap final Map<String, Object> nodeMap = newMap(); nodeMap.put("@default", newMap()); generateNodeMap(this.value, nodeMap); final RDFDataset dataset = new RDFDataset(this); for (final Map.Entry<String, Object> stringObjectEntry : nodeMap.entrySet()) { // 4.1) if (JsonLdUtils.isRelativeIri(stringObjectEntry.getKey())) { continue; } final Map<String, Object> graph = (Map<String, Object>) stringObjectEntry.getValue(); replaceBlankNode(graph); dataset.graphToRDF(stringObjectEntry.getKey(), graph); } return dataset; }
@Override public RDFDataset parse(Object input) throws JsonLdError { final RDFDataset result = new RDFDataset(); // empty dataset if no input given if (input == null) { return result; } if (input instanceof ModelSet) { importModelSet(result, (ModelSet) input); } else if (input instanceof Model) { importModel(result, (Model) input); } else { throw new JsonLdError(Error.INVALID_INPUT, "RDF2Go parser expects a Model or ModelSet object as input"); } return result; }
/** * Adds RDF triples for each graph in the current node map to an RDF * dataset. * * @return the RDF dataset. * @throws JsonLdError * If there was an error converting from JSON-LD to RDF. */ public RDFDataset toRDF() throws JsonLdError { // TODO: make the default generateNodeMap call (i.e. without a // graphName) create and return the nodeMap final Map<String, Object> nodeMap = newMap(); nodeMap.put(JsonLdConsts.DEFAULT, newMap()); generateNodeMap(this.value, nodeMap); final RDFDataset dataset = new RDFDataset(this); for (final String graphName : nodeMap.keySet()) { // 4.1) if (JsonLdUtils.isRelativeIri(graphName)) { continue; } final Map<String, Object> graph = (Map<String, Object>) nodeMap.get(graphName); dataset.graphToRDF(graphName, graph); } return dataset; }
/** * Adds RDF triples for each graph in the current node map to an RDF * dataset. * * @return the RDF dataset. * @throws JsonLdError * If there was an error converting from JSON-LD to RDF. */ public RDFDataset toRDF() throws JsonLdError { // TODO: make the default generateNodeMap call (i.e. without a // graphName) create and return the nodeMap final Map<String, Object> nodeMap = newMap(); nodeMap.put(JsonLdConsts.DEFAULT, newMap()); generateNodeMap(this.value, nodeMap); final RDFDataset dataset = new RDFDataset(this); for (final String graphName : nodeMap.keySet()) { // 4.1) if (JsonLdUtils.isRelativeIri(graphName)) { continue; } final Map<String, Object> graph = (Map<String, Object>) nodeMap.get(graphName); dataset.graphToRDF(graphName, graph); } return dataset; }
@Override public RDFDataset parse(Object input) throws JsonLdError { final RDFDataset result = new RDFDataset(); if (input instanceof Statement) { handleStatement(result, (Statement) input); } else if (input instanceof Graph) { if (input instanceof Model) { final Set<Namespace> namespaces = ((Model) input).getNamespaces(); for (final Namespace nextNs : namespaces) { result.setNamespace(nextNs.getName(), nextNs.getPrefix()); } } for (final Statement nextStatement : (Graph) input) { handleStatement(result, nextStatement); } } return result; }
@Override public RDFDataset parse(Object input) throws JsonLdError { final RDFDataset result = new RDFDataset(); if (input instanceof Statement) { handleStatement(result, (Statement)input); } else if (input instanceof Graph) { if (input instanceof NamespaceAware) { final Set<Namespace> namespaces = ((NamespaceAware)input).getNamespaces(); for (final Namespace nextNs : namespaces) { result.setNamespace(nextNs.getName(), nextNs.getPrefix()); } } for (final Statement nextStatement : (Graph)input) { handleStatement(result, nextStatement); } } return result; }
@Override public RDFDataset parse(Object input) throws JsonLdError { final RDFDataset result = new RDFDataset(); if (input instanceof Statement) { handleStatement(result, (Statement)input); } else if (input instanceof Graph) { if (input instanceof NamespaceAware) { final Set<Namespace> namespaces = ((NamespaceAware)input).getNamespaces(); for (final Namespace nextNs : namespaces) { result.setNamespace(nextNs.getName(), nextNs.getPrefix()); } } for (final Statement nextStatement : (Graph)input) { handleStatement(result, nextStatement); } } return result; }
@Test public void testFrame0010() throws IOException, JsonLdError { final Object frame = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-frame.jsonld")); //{ // "@id": "http://example.com/main/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": { // "@id": "http://example.com/rdf/id", // "http://www.w3.org/1999/02/22-rdf-syntax-ns#label": "someLabel" // } //} final RDFDataset ds = new RDFDataset(); ds.addTriple("http://example.com/main/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "http://example.com/rdf/id"); ds.addTriple("http://example.com/rdf/id", "http://www.w3.org/1999/02/22-rdf-syntax-ns#label", "someLabel", null, null); final JsonLdOptions opts = new JsonLdOptions(); opts.setProcessingMode(JsonLdOptions.JSON_LD_1_0); final Object in = new JsonLdApi(opts).fromRDF(ds, true); final Map<String, Object> frame2 = JsonLdProcessor.frame(in, frame, opts); final Object out = JsonUtils .fromInputStream(getClass().getResourceAsStream("/custom/frame-0010-out.jsonld")); assertEquals(out, frame2); } }
@Test public void literalsInDataset() throws Exception { final RDFDataset dataset = new RDFDataset(); dataset.addQuad("http://example.com/p", "http://example.com/p", "Same", null, null, "http://example.com/g1"); dataset.addQuad("http://example.com/p", "http://example.com/p", "Different", null, null, "http://example.com/g1"); final List<Quad> quads = dataset.getQuads("http://example.com/g1"); final Quad q1 = quads.get(0); final Quad q2 = quads.get(1); assertNotEquals(q1, q2); assertNotEquals(0, q1.compareTo(q2)); assertNotEquals(0, q1.getObject().compareTo(q2.getObject())); }
@Override public RDFDataset parse(Object object) throws JsonLdError { RDFDataset result = new RDFDataset() ; if ( object instanceof DatasetGraph )
@Test public void prefixUsedToShortenPredicate() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("ex", "http://www.a.com/foo/"); inputRdf.addTriple("http://www.a.com/foo/s", "http://www.a.com/foo/p", "http://www.a.com/foo/o"); assertEquals("http://www.a.com/foo/", inputRdf.getNamespace("ex")); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("http://www.a.com/foo/p")); }
@Test public void modelToJsonLD() throws Exception { JenaJSONLD.init(); // Only needed once final Model model = ModelFactory.createDefaultModel(); final Resource resource = model.createResource("http://example.com/test"); final Property property = model.createProperty("http://example.com/value"); model.add(resource, property, "Test"); final JsonLdOptions options = new JsonLdOptions(); options.format = "application/ld+json"; final JsonLdApi api = new JsonLdApi(options); final RDFDataset dataset = new RDFDataset(api); final Object json = api.fromRDF(dataset); final String jsonStr = JsonUtils.toPrettyString(json); System.out.println(jsonStr); // [ { // "@id" : "http://example.com/test", // "http://example.com/value" : [ { // "@value" : "Test" // } ] // } ] } }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aat_rev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aat_rev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertTrue("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }
@Test public void fromRdfWithNamespaceLexicographicallyShortestChosen2() throws Exception { final RDFDataset inputRdf = new RDFDataset(); inputRdf.setNamespace("aat", "http://vocab.getty.edu/aat/"); inputRdf.setNamespace("aatrev", "http://vocab.getty.edu/aat/rev/"); inputRdf.addTriple("http://vocab.getty.edu/aat/rev/5001065997", JsonLdConsts.RDF_TYPE, "http://vocab.getty.edu/aat/datatype"); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final Object fromRDF = JsonLdProcessor.compact(new JsonLdApi(options).fromRDF(inputRdf), inputRdf.getContext(), options); final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(fromRDF, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aatrev")); final String toJSONLD = JsonUtils.toPrettyString(fromRDF); // System.out.println(toJSONLD); assertFalse("The lexicographically shortest URI was not chosen", toJSONLD.contains("aat:rev/")); }
final RDFDataset inputRdf = new RDFDataset(); final String ns = "http://www.example.com/foo/"; inputRdf.setNamespace("ex", ns);