public void writeGraph(String uri, Graph graph) { WriterGraphRIOT writer = RDFDataMgr.createGraphWriter(Lang.NTRIPLES); OutputStreamRIOTSender sender = new OutputStreamRIOTSender(writer); sender.setGraph(graph); OutputStreamHandle handle = new OutputStreamHandle(sender); this.graphManager.write(uri, handle, currentTransaction); } /**
public static void writeSPARQLDataFromString(String content, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { // Create Grapgh manager GraphManager sparqlGmgr = writeclient.newGraphManager(); StringHandle handle = new StringHandle(); handle.set(content.toString()); // write the triples in the doc into named graph sparqlGmgr.write(graphName, handle.withMimetype(RDFMimeTypes.TURTLE)); // Wait for index. Need a better way to handle this. try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.print("Exception from writeSPARQLDataFromString " + e.getMessage()); e.printStackTrace(); } }
@Test // GitIssue #319 public void testWrite_rdfxml_WrongTriplesSchema() throws Exception { Exception exp = null; File file = new File(datasource + "relative4.rdf"); FileHandle filehandle = new FileHandle(); filehandle.set(file); try { gmWriter.write("htp://test.sem.graph/rdf", filehandle.withMimetype(RDFMimeTypes.RDFXML)); } catch (Exception e) { exp = e; } assertTrue("Did not throw expected Exception, Expecting ::XDMP-BASEURI (err:FOER0000): Undeclared base URI " + "\n Received::" + exp, exp.toString().contains("XDMP-BASEURI (err:FOER0000): Undeclared base URI")); }
public static void writeNTriplesFromFile(String filename, String graphName) throws KeyManagementException, NoSuchAlgorithmException, Exception { File file = new File(datasource + filename); FileHandle filehandle = new FileHandle(); GraphManager sparqlGmgr = writeclient.newGraphManager(); filehandle.set(file); sparqlGmgr.write(graphName, filehandle.withMimetype(RDFMimeTypes.TURTLE)); }
@Test public void testThings_file() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/file"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); TriplesReadHandle things = gmWriter.things(new StringHandle(), "about"); assertTrue("Things did not return expected Uri's", things.toString().contains("<about> <http://purl.org/dc/elements/1.1/title> \"Anna's Homepage\" .")); gmWriter.delete(tripleGraphUri); }
@Test public void testWrite_rdfjson_TripleReadHandle() throws Exception { File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); TriplesReadHandle handle1 = gmWriter.read("htp://test.sem.graph/rdfjson", new JacksonHandle()); assertTrue("Did not insert document or inserted empty doc", handle1.toString().contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testThingsAs() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.NTRIPLES); File file = new File(datasource + "relative1.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("http://test.things.com/", filehandle); String things = gmWriter.thingsAs(String.class, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"); assertTrue( "Did not return Expected graph Uri's", things.equals("<#electricVehicle2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://people.aifb.kit.edu/awa/2011/smartgrid/schema/smartgrid#ElectricVehicle> .")); }
@Test public void testWrite_rdfjson_FileHandle() throws Exception { ObjectMapper mapper = new ObjectMapper(); new ObjectMapper(); File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); FileHandle handle = new FileHandle(); handle.setMimetype(RDFMimeTypes.RDFJSON); handle = gmWriter.read("htp://test.sem.graph/rdfjson", handle); File readFile = handle.get(); JsonNode readContent = mapper.readTree(readFile); // Verify read content with inserted content assertTrue("Did not insert document or inserted empty doc", readContent.toString() .contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testWrite_rdfjson_JacksonHandle() throws Exception { File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); JacksonHandle handle1 = gmWriter.read("htp://test.sem.graph/rdfjson", new JacksonHandle()); JsonNode readFile = handle1.get(); assertTrue("Did not insert document or inserted empty doc", readFile.toString().contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testWrite_trig_FileHandle() throws Exception { File file = new File(datasource + "semantics.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); FileHandle handle = gmWriter.read("htp://test.sem.graph/trig", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica")); }
@Test public void testRead_nonirirdfxml_FileHandle() throws Exception { StringHandle handle = new StringHandle(); File file = new File(datasource + "non-iri.xml"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/tripxmlnoniri", filehandle.withMimetype(RDFMimeTypes.TRIPLEXML)); try { handle = gmWriter.read("htp://test.sem.graph/tripxmlnoniri", new StringHandle()); } catch (Exception e) { assertTrue("Did not receive expected Error message, Expecting ::SEM-NOTRDF \n Received::" + e, e.toString().contains("SEM-NOTRDF") && e != null); } assertTrue("Read content has un-expected content ", handle.get().toString().contains("5.11")); }
@Test public void testRead_nonirin3_FileHandle() throws Exception { StringHandle handle = new StringHandle(); File file = new File(datasource + "non-iri.n3"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/n3noniri", filehandle.withMimetype(RDFMimeTypes.N3)); try { handle = gmWriter.read("htp://test.sem.graph/n3noniri", new StringHandle().withMimetype(RDFMimeTypes.N3)); } catch (Exception e) { assertTrue("Did not receive expected Error message, Expecting ::SEM-NOTRDF \n Received::" + e, e.toString().contains("SEM-NOTRDF") && e != null); } assertTrue("Read content has un-expected content ", handle.get().toString().contains("p0:named_graph")); }
@Test public void testThings_fileNomatch() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/file"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); Exception exp = null; try { TriplesReadHandle things = gmWriter.things(new StringHandle(), "noMatch"); assertTrue("Things did not return expected Uri's", things == null); } catch (Exception e) { exp = e; } assertTrue(exp.toString().contains("ResourceNotFoundException")); gmWriter.delete(tripleGraphUri); }
@Test public void testWrite_rdfxml_FileHandle() throws Exception { BufferedReader buffreader = new BufferedReader(new FileReader(datasource + "semantics.rdf")); ReaderHandle handle = new ReaderHandle(); handle.set(buffreader); gmWriter.write("http://test.reader.handle/bufferreadtrig", handle.withMimetype(RDFMimeTypes.RDFXML)); buffreader.close(); ReaderHandle read = gmWriter.read("http://test.reader.handle/bufferreadtrig", new ReaderHandle().withMimetype(RDFMimeTypes.RDFXML)); Reader readFile = read.get(); String readContent = convertReaderToString(readFile); assertTrue("Did not get receive expected string content, Received:: " + readContent, readContent.contains("http://www.daml.org/2001/12/factbook/vi#A113932")); handle.close(); read.close(); }
@Test public void testWrite_nquads_FileHandle() throws Exception { File file = new File(datasource + "relative2.nq"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/nquads", filehandle.withMimetype("application/n-quads")); StringHandle handle = gmWriter.read("htp://test.sem.graph/nquads", new StringHandle().withMimetype(RDFMimeTypes.NQUADS)); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("<#electricVehicle2>")); }
@Test public void testWrite_nTriples_FileHandle() throws Exception { File file = new File(datasource + "5triples.nt"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/n", filehandle.withMimetype("application/n-triples")); StringHandle handle = gmWriter.read("htp://test.sem.graph/n", new StringHandle().withMimetype(RDFMimeTypes.NTRIPLES)); assertTrue("Did not insert document or inserted empty doc", handle.toString().contains("<http://jibbering.com/foaf/santa.rdf#bod>")); }
@Test public void testThings_Differentmimetypes() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.TRIPLEXML); String tripleGraphUri = "http://test.things.com/multiple"; File file = new File(datasource + "relative5.xml"); gmWriter.write(tripleGraphUri, new FileHandle(file)); file = new File(datasource + "semantics.ttl"); gmWriter.merge(tripleGraphUri, new FileHandle(file).withMimetype(RDFMimeTypes.TURTLE)); StringHandle things = gmWriter.things(new StringHandle(), "http://dbpedia.org/resource/Hadoop"); assertTrue("Things did not return expected Uri's", things.get().contains("Apache Hadoop")); gmWriter.delete(tripleGraphUri); }
@BeforeClass public static void beforeClass() { Common.connect(); gmgr = Common.client.newGraphManager(); String nTriples = triple1 + "\n" + triple2; gmgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES); gmgr.write(graphUri, new StringHandle(nTriples)); smgr = Common.client.newSPARQLQueryManager(); }
@Test public void testMergeSameDoc_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trigMSameDoc", filehandle); gmWriter.merge("htp://test.sem.graph/trigMSameDoc", filehandle); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMSameDoc", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent .equals("{\"http://example.com/ns/directory#m\":{\"http://example.com/ns/person#firstName\":[{\"value\":\"Michelle\", \"" + "type\":\"literal\", \"datatype\":\"http://www.w3.org/2001/XMLSchema#string\"}]}}")); }
@BeforeClass public static void beforeClass() { Common.connect(); gmgr = Common.client.newGraphManager(); String triple = "<s> <p> <o>."; GraphPermissions perms = gmgr.permission("read-privileged", Capability.READ) .permission("write-privileged", Capability.UPDATE); gmgr.write(graphUri, new StringHandle(triple).withMimetype(RDFMimeTypes.NTRIPLES), perms); }