|
1 |
package eu.dnetlib.parthenos.virtuoso;
|
|
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.io.InputStream;
|
|
5 |
|
|
6 |
import org.apache.jena.rdf.model.Model;
|
|
7 |
import org.apache.jena.rdf.model.ModelFactory;
|
|
8 |
import org.apache.jena.riot.RDFDataMgr;
|
|
9 |
import org.apache.jena.riot.RDFFormat;
|
|
10 |
import org.apache.jena.riot.RIOT;
|
|
11 |
import org.junit.Test;
|
|
12 |
import org.springframework.core.io.ClassPathResource;
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
16 |
* or more contributor license agreements. See the NOTICE file
|
|
17 |
* distributed with this work for additional information
|
|
18 |
* regarding copyright ownership. The ASF licenses this file
|
|
19 |
* to you under the Apache License, Version 2.0 (the
|
|
20 |
* "License"); you may not use this file except in compliance
|
|
21 |
* with the License. You may obtain a copy of the License at
|
|
22 |
* <p>
|
|
23 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
24 |
* <p>
|
|
25 |
* Unless required by applicable law or agreed to in writing, software
|
|
26 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
27 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
28 |
* See the License for the specific language governing permissions and
|
|
29 |
* limitations under the License.
|
|
30 |
*/
|
|
31 |
|
|
32 |
public class ExampleIO_01 {
|
|
33 |
|
|
34 |
@Test
|
|
35 |
public void test() throws IOException {
|
|
36 |
InputStream in = getResourceAsStream("eu/dnetlib/parthenos/virtuoso/data.ttl");
|
|
37 |
|
|
38 |
RIOT.init() ;
|
|
39 |
|
|
40 |
Model model = ModelFactory.createDefaultModel(); // creates an in-memory Jena Model
|
|
41 |
model.read(in, null, "TURTLE"); // parses an InputStream assuming RDF in Turtle format
|
|
42 |
|
|
43 |
// Write the Jena Model in Turtle, RDF/XML and N-Triples format
|
|
44 |
System.out.println("\n---- Turtle ----");
|
|
45 |
model.write(System.out, "TURTLE");
|
|
46 |
System.out.println("\n---- RDF/XML ----");
|
|
47 |
model.write(System.out, "RDF/XML");
|
|
48 |
System.out.println("\n---- RDF/XML Abbreviated ----");
|
|
49 |
model.write(System.out, "RDF/XML-ABBREV");
|
|
50 |
System.out.println("\n---- N-Triples ----");
|
|
51 |
model.write(System.out, "N-TRIPLES");
|
|
52 |
System.out.println("\n---- RDF/JSON ----");
|
|
53 |
model.write(System.out, "RDF/JSON");
|
|
54 |
System.out.println("\n---- RDF/XML_PLAIN ----");
|
|
55 |
model.write(System.out, "RDF/XML_PLAIN");
|
|
56 |
System.out.println("\n----RDFDataMgr with graph and RDFFormat.RDFXML_PLAIN ----");
|
|
57 |
RDFDataMgr.write(System.out, model.getGraph(), RDFFormat.RDFXML_PLAIN);
|
|
58 |
System.out.println("\n----RDFDataMgr with model and RDFFormat.RDFXML_PLAIN ----");
|
|
59 |
RDFDataMgr.write(System.out, model, RDFFormat.RDFXML_PLAIN);
|
|
60 |
|
|
61 |
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
private InputStream getResourceAsStream(final String classpath) throws IOException {
|
|
66 |
final ClassPathResource resource = new ClassPathResource(classpath);
|
|
67 |
return resource.getInputStream();
|
|
68 |
}
|
|
69 |
|
|
70 |
}
|
Testing RDF XML serialization