01: package org.ontoware.rdfreactor.generator;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.FileNotFoundException;
06: import java.io.IOException;
07:
08: import org.ontoware.rdf2go.RDF2Go;
09: import org.ontoware.rdf2go.exception.ModelRuntimeException;
10: import org.ontoware.rdf2go.model.Model;
11: import org.ontoware.rdf2go.model.Syntax;
12:
13: /**
14: * Helper class to read and load a schema file into a Jena RDF model.
15: *
16: * @author mvo
17: */
18: public class ModelReaderUtils {
19:
20: /**
21: * Load a RDF schema file into a Jena RDF model and return it
22: *
23: * @param filename -
24: * location of the schema file. can end be in N3 (*.n3) or
25: * N-TRIPLE (*.nt) notation
26: * @return a Jena Model
27: */
28: public static Model read(String filename) {
29: Model m = RDF2Go.getModelFactory().createModel();
30: m.open();
31:
32: FileInputStream fis;
33: File f = new File(filename);
34: try {
35: fis = new FileInputStream(f);
36:
37: Syntax syntax = Syntax.RdfXml;
38:
39: if (filename.endsWith(".n3"))
40: syntax = Syntax.Turtle;
41: else if (filename.endsWith(".nt"))
42: syntax = Syntax.Ntriples;
43:
44: try {
45: m.readFrom(fis, syntax);
46: } catch (ModelRuntimeException e) {
47: throw new RuntimeException(e);
48: } catch (IOException e) {
49: throw new RuntimeException(e);
50: }
51:
52: return m;
53: } catch (FileNotFoundException e) {
54: throw new RuntimeException("Could not find file: "
55: + f.getAbsolutePath() + " Exception: " + e);
56: }
57:
58: }
59:
60: }
|