Java Code Examples for org.apache.jena.riot.RDFDataMgr#read()

The following examples show how to use org.apache.jena.riot.RDFDataMgr#read() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
protected static Model jsonLdAsJenaModel(InputStream jsonIn, URI base)
		throws IOException, RiotException {
	Model model = ModelFactory.createDefaultModel();

	ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
	try {
		// TAVERNA-971: set context classloader so jarcache.json is consulted
		// even through OSGi
		Thread.currentThread().setContextClassLoader(RDFToManifest.class.getClassLoader());

		// Now we can parse the JSON-LD without network access
		RDFDataMgr.read(model, jsonIn, base.toASCIIString(), Lang.JSONLD);
	} finally {
		// Restore old context class loader (if any)
		Thread.currentThread().setContextClassLoader(oldCl);
	}
	return model;
}
 
Example 2
Source Project: xcurator   File: ExRIOT_1.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String...argv)
{
    Model m = ModelFactory.createDefaultModel() ;
    // read into the model.
    m.read("data.ttl") ;
    
    // Alternatively, use the RDFDataMgr, which reads from the web,
    // with content negotiation.  Plain names are assumed to be 
    // local files where file extension indicates the syntax.  
    
    Model m2 = RDFDataMgr.loadModel("data.ttl") ;
    
    // read in more data, the remote server serves up the data
    // with the right MIME type.
    RDFDataMgr.read(m2, "http://host/some-published-data") ;
    
    
    // Read some data but also give a hint for the synatx if it is not
    // discovered by inspectying the file or by HTTP content negotiation.  
    RDFDataMgr.read(m2, "some-more-data.out", RDFLanguages.TURTLE) ;
}
 
Example 3
Source Project: xcurator   File: ExRIOT_3.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String...argv)
{
    Dataset ds = null ;
    
    // Read a TriG file into quad storage in-memory.
    ds = RDFDataMgr.loadDataset("data.trig") ;
    
    // read some (more) data into a dataset graph.
    RDFDataMgr.read(ds, "data2.trig") ;
    
    // Create a dataset,
    Dataset ds2 = DatasetFactory.createTxnMem() ;
    // read in data, indicating the syntax in case the remote end does not
    // correctly provide the HTTP content type.
    RDFDataMgr.read(ds2, "http://host/data2.unknown", TRIG) ;
}
 
Example 4
private static void readTrigFromStream(Dataset ds, Dataset dsnew) {
    InputStream is = null;
    try {
        is = System.in;

        if (is==null){
            throw new IllegalArgumentException(
                    "No stream input!");
        }

        ByteArrayOutputStream b = cloneInputStream(is);
        InputStream is1 = new ByteArrayInputStream(b.toByteArray());
        InputStream is2 = new ByteArrayInputStream(b.toByteArray());

        RDFDataMgr.read(ds, is1, RDFLanguages.TRIG);
        RDFDataMgr.read(dsnew, is2, RDFLanguages.TRIG);

    }
    finally {
        // close the streams using close method

    }

}
 
Example 5
Source Project: RDFstarTools   File: QueryExecutionTest.java    License: Apache License 2.0 5 votes vote down vote up
protected Model loadModel( String filename )
{
	final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();

	final Graph g = new GraphWrapperStar( GraphFactory.createDefaultGraph() );
	final Model m = ModelFactory.createModelForGraph(g);
	RDFDataMgr.read(m, fullFilename);
	return m;
}
 
Example 6
Source Project: trellis   File: JenaIOServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void testNTriplesSerializer() {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service3.write(getTriples(), out, NTRIPLES, identifier);
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final org.apache.jena.graph.Graph graph = createDefaultGraph();
    RDFDataMgr.read(graph, in, Lang.NTRIPLES);
    assertTrue(validateGraph(fromJena(graph)), "Failed round-trip for N-Triples!");
}
 
Example 7
Source Project: trellis   File: JenaIOServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void testBufferedSerializer() {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service.write(getTriples(), out, RDFXML, identifier);
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final org.apache.jena.graph.Graph graph = createDefaultGraph();
    RDFDataMgr.read(graph, in, Lang.RDFXML);
    assertTrue(validateGraph(fromJena(graph)), "Failed round-trip for RDFXML!");
}
 
Example 8
Source Project: trellis   File: JenaIOServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void testTurtleSerializer() {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service.write(getTriples(), out, TURTLE, identifier);
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final org.apache.jena.graph.Graph graph = createDefaultGraph();
    RDFDataMgr.read(graph, in, Lang.TURTLE);
    assertTrue(validateGraph(fromJena(graph)), "Failed round-trip for Turtle!");
}
 
Example 9
Source Project: trellis   File: JenaIOServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void testNullHtmlSerializer() {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service.write(getTriples(), out, RDFA, identifier);
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final org.apache.jena.graph.Graph graph = createDefaultGraph();
    RDFDataMgr.read(graph, in, Lang.TURTLE);
    assertTrue(validateGraph(fromJena(graph)), "null HTML serialization didn't default to Turtle!");
}
 
Example 10
Source Project: RDFUnit   File: RdfStreamReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void readDataset(Dataset dataset) throws RdfReaderException {
    try {
        RDFDataMgr.read(dataset, inputStream, null, RDFLanguages.nameToLang(format));
    } catch (Exception e) {
        throw new RdfReaderException(e.getMessage(), e);
    }

}
 
Example 11
/**
 * Load an ontology into a DatasetGraph. The ontology is not changed.
 *
 * @deprecated use {@link #loadOntologyAsDataset(OWLOntology)} instead.
 * @param ontology The ontology to load into the graph
 * @return A new DatasetGraph with the ontology loaded into the default graph
 * @throws OWLOntologyStorageException rarely
 */
@Deprecated
public static DatasetGraph loadOntology(OWLOntology ontology) throws OWLOntologyStorageException {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  ontology.getOWLOntologyManager().saveOntology(ontology, new TurtleDocumentFormat(), os);
  DatasetGraph dsg = DatasetGraphFactory.create();
  RDFDataMgr.read(dsg.getDefaultGraph(), new ByteArrayInputStream(os.toByteArray()), Lang.TURTLE);
  return dsg;
}
 
Example 12
@Override
public void readFrom(InputStream in, Syntax syntax, String baseURI)
		throws IOException, ModelRuntimeException,
		SyntaxNotSupportedException {

	RDFDataMgr.read(this.dataset.asDatasetGraph(), in, baseURI, getJenaLang(syntax));
}
 
Example 13
/**
 * Builds a new {@link Validator} with the given rule for determining the sample size.
 */
public Validator() {
	schema = ModelFactory.createDefaultModel();
			
	RDFDataMgr.read(schema, getClass().getResourceAsStream("/aliada-ontology.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/efrbroo.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/current.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/wgs84_pos.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/time.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/skos-xl.owl"), Lang.RDFXML);
       RDFDataMgr.read(schema, getClass().getResourceAsStream("/0.1.owl"), Lang.RDFXML);		
       
       reasoner = ReasonerRegistry.getOWLMicroReasoner().bindSchema(schema);
}
 
Example 14
Source Project: Server.Java   File: JenaTDBDataSourceTest.java    License: MIT License 5 votes vote down vote up
/**
 *
 * @throws Exception
 */
@BeforeClass
public static void setUpClass() throws Exception {
    final String typeName = "JenaSourceType";
    if ( ! DataSourceTypesRegistry.isRegistered(typeName) ) {
        DataSourceTypesRegistry.register( typeName,
                                          new JenaTDBDataSourceType() );
    }

    String tmpdir = System.getProperty("java.io.tmpdir");
    jena = new File(tmpdir, "ldf-jena-test");
    jena.mkdir();
    
    dataset = TDBFactory.createDataset(jena.getAbsolutePath());

    Model model = dataset.getDefaultModel();
    InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
    RDFDataMgr.read(model, in, Lang.NTRIPLES);

    // Everything is in place, now create the LDF datasource                
    JsonObject config = createConfig("jena tdb test", "jena tdb test",
                                     typeName);
    
    JsonObject settings = new JsonObject();
    settings.addProperty("directory", jena.getAbsolutePath());
    config.add("settings", settings);

    setDatasource(DataSourceFactory.create(config));
}
 
Example 15
Source Project: Server.Java   File: SparqlDataSourceTest.java    License: MIT License 5 votes vote down vote up
/**
 *
 * @throws Exception
 */
@BeforeClass
public static void setUpClass() throws Exception {
    final String typeName = "SparqlSourceType";
    if ( ! DataSourceTypesRegistry.isRegistered(typeName) ) {
        DataSourceTypesRegistry.register( typeName,
                                          new SparqlDataSourceType() );
    }

    String tmpdir = System.getProperty("java.io.tmpdir");
    jena = new File(tmpdir, "ldf-sparql-test");
    jena.mkdir();
    
    dataset = TDBFactory.createDataset(jena.getAbsolutePath());

    Model model = dataset.getDefaultModel();
    InputStream in = ClassLoader.getSystemResourceAsStream("demo.nt");
    RDFDataMgr.read(model, in, Lang.NTRIPLES);

    // Dynamically-generated port comes from pom.xml configuration: build-helper-maven-plugin
    int fusekiPort = Integer.parseInt(System.getProperty("fuseki.port"));

    // Create Fuseki, loaded with the test dataset
    fuseki = FusekiServer.create().setPort(fusekiPort).add("/ds", dataset).build();
    fuseki.start();

    // Everything is in place, now create the LDF datasource                
    JsonObject config = createConfig("sparql test", "sparql test",
                                     typeName);
    
    JsonObject settings = new JsonObject();
    settings.addProperty("endpoint", "http://localhost:" + fusekiPort + "/ds");
    config.add("settings", settings);

    setDatasource(DataSourceFactory.create(config));
}
 
Example 16
Source Project: RDFUnit   File: RdfStreamReader.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void read(Model model) throws RdfReaderException {
    try {
        RDFDataMgr.read(model, inputStream, null, RDFLanguages.nameToLang(format));
    } catch (Exception e) {
        throw new RdfReaderException(e.getMessage(), e);
    }

}
 
Example 17
Source Project: RDFUnit   File: JenaUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static Model readModel(URI uri) {

        Model model = RdfUnitModelFactory.createDefaultModel();
        RDFDataMgr.read(model, uri.toString());
        return model;
    }
 
Example 18
/**
 * Workaround for TAVERNA-71 to find annotations in WorkflowBundle
 * <p>
 * FIXME: The annotation links should instead be stored in the 
 * {@link Manifest} using taverna-robundle - see TAVERNA-71
 * 
 * @param wb
 * @throws IOException
 */
private void parseAnnotations(final WorkflowBundle wb) throws IOException {
	if (! wb.getAnnotations().isEmpty()) {
		// Assume already parsed
		return;
	}
	URITools uriTools = new URITools();		
	for (ResourceEntry resource : wb.getResources().listResources("annotation").values()) {
		Lang lang = RDFLanguages.contentTypeToLang(resource.getMediaType());
		if (lang == null) {
			// Not a semantic annotation
			continue;
		}
		//System.out.println(resource.getPath());
		//System.out.println(resource.getMediaType());
		Annotation ann = new Annotation();
		// Hackish way to generate a name from the annotation filename
		// as these typically are UUIDs
		String name = resource.getPath().replace("annotation/", "").replaceAll("\\..*", ""); // strip extension
		ann.setName(name);
		ann.setParent(wb);
		
		String path = resource.getPath();
		ann.setBody(URI.create("/" + path));		
		URI base = wb.getGlobalBaseURI().resolve(path);
		Model model = ModelFactory.createDefaultModel();			
		InputStream inputStream = resource.getUcfPackage().getResourceAsInputStream(path);
		if (inputStream == null) {
			// Not found!
			continue;
		}
		RDFDataMgr.read(model, inputStream, 
				base.toASCIIString(), lang);
		ResIterator subjs = model.listSubjects();			
		while (subjs.hasNext()) { 
			Resource r = subjs.next();
			//System.out.println(r);
			WorkflowBean b = uriTools.resolveUri(URI.create(r.getURI()), wb);
			//System.out.println(b);
			if (b != null) {
				ann.setTarget(b);
			}
			break;
		}
	}
}
 
Example 19
@Override
public void readFrom(Reader reader, Syntax syntax, String baseURI) {
	assertModel();

	RDFDataMgr.read(this.jenaModel, reader, baseURI, getJenaLang(syntax));
}
 
Example 20
/**
 * Load dataset from CLOB field in database
 * 
 * @param store
 * @param schema
 * @return
 */
public static DatasetGraph loadDataset(InputStream inputStream) {

	// Read the dataset from CLOB
	DatasetGraph dsg = DatasetGraphFactory.createMem();
	RDFDataMgr.read(dsg,inputStream,  Lang.NQUADS);
	
	
	return dsg;
}