use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExJsonLD method parse.
/** Parse a jsonld string into a Model */
private Model parse(String jsonld) {
Model m = ModelFactory.createDefaultModel();
StringReader reader = new StringReader(jsonld);
m.read(reader, null, "JSON-LD");
return m;
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExJsonLD method moreControl.
/**
* To get more control about the output,
* we have to use a mechanism provided by jena to pass information
* to the writing process
*
* This requires a few lines of code, see {@link #write(DatasetGraph, RDFFormat, Context)}
*
* Here we use this write method to see what can be customized.
*/
public void moreControl() {
Model m = aSimpleModel();
m.setNsPrefix("ex", "http://www.ex.com/");
m.setNsPrefix("sh", "http://schema.org/");
// the write method takes a DatasetGraph as input to represent the data that we want to output
// Let's create one from our model:
DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
// and let's use the write method to output the data in json-ld compact format,
// passing a null Context for the moment
// (remember, "Context" here is not to be confused with "@context" in JSON-LD,
// see {@link #write(DatasetGraph, RDFFormat, Context)})
System.out.println("\n--- COMPACT with a null Context: same result as default ---");
write(g, RDFFormat.JSONLD_COMPACT_PRETTY, null);
// A Context is just a way to pass implementation-specific parameters as named values
// to a given general interface (the WriterDatasetRIOT, in this case).
// In order to make it easier to define the named values relevant here,
// there is a subclass of Context that defines setters for the values:
// the JsonLDWriteContext class
// so, the way to proceed is:
// JsonLDWriteContext ctx = new JsonLDWriteContext();
// ctx.setSomething(...)
// write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
// let's see now what can be customized with the JsonLDWriteContext object
controllingAtContext();
settingAtContextToURI();
frame();
controllingJsonLDApiOptions();
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExRIOT_1 method main.
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);
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExRIOT_out1 method main.
public static void main(String[] args) {
Model model = RDFDataMgr.loadModel("D.ttl");
System.out.println();
System.out.println("#### ---- Write as Turtle");
System.out.println();
RDFDataMgr.write(System.out, model, Lang.TURTLE);
System.out.println();
System.out.println("#### ---- Write as Turtle (streaming)");
System.out.println();
RDFDataMgr.write(System.out, model, RDFFormat.TURTLE_BLOCKS);
System.out.println();
System.out.println("#### ---- Write as Turtle via model.write");
System.out.println();
model.write(System.out, "TTL");
}
use of org.apache.jena.rdf.model.Model in project jena by apache.
the class ExRIOT_out2 method main.
public static void main(String[] args) {
Model model = RDFDataMgr.loadModel("D.ttl");
System.out.println();
System.out.println("#### ---- Write as TriG");
System.out.println();
// This wil be the default graph of the dataset written.
RDFDataMgr.write(System.out, model, Lang.TRIG);
// Loading Turtle as Trig reads into the default graph.
Dataset dataset = RDFDataMgr.loadDataset("D.ttl");
System.out.println();
System.out.println("#### ---- Write as NQuads");
System.out.println();
RDFDataMgr.write(System.out, dataset, Lang.NQUADS);
}
Aggregations