001: package org.ontoware.rdf2go.impl.jena24;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileNotFoundException;
006: import java.io.FileOutputStream;
007: import java.io.FileWriter;
008: import java.io.IOException;
009: import java.io.InputStreamReader;
010: import java.io.OutputStreamWriter;
011: import java.io.Reader;
012: import java.io.UnsupportedEncodingException;
013: import java.io.Writer;
014: import java.net.URL;
015:
016: import org.apache.commons.logging.Log;
017: import org.apache.commons.logging.LogFactory;
018: import org.ontoware.rdf2go.Reasoning;
019: import org.ontoware.rdf2go.exception.ModelRuntimeException;
020: import org.ontoware.rdf2go.model.Model;
021:
022: import com.hp.hpl.jena.rdf.model.ModelFactory;
023:
024: /**
025: * @author voelkel
026: *
027: */
028: public class IOUtils {
029:
030: private static final Log log = LogFactory.getLog(IOUtils.class);
031:
032: /**
033: * Read the file. Assume RDF/XML, unless file extension is '.nt' (N-TRIPLES)
034: * or '.n3' (N3).
035: *
036: * @param filename
037: * @return a Jena Model
038: */
039: public static Model read(String filename) {
040: File f = new File(filename);
041: return read(f);
042: }
043:
044: /** assume RDF/XML */
045: public static void readIntoJenaModel(
046: com.hp.hpl.jena.rdf.model.Model m, Reader reader) {
047: m.read(reader, "", "RDF/XML");
048: }
049:
050: public static void readIntoJenaModel(
051: com.hp.hpl.jena.rdf.model.Model m, File f) {
052: log.warn("reading model from " + f.getAbsoluteFile());
053: try {
054: FileInputStream fis = new FileInputStream(f);
055: Reader r = new InputStreamReader(fis, "utf8");
056:
057: String lang = "RDF/XML";
058:
059: if (f.getName().endsWith(".n3")) {
060: log.warn("file end with '.n3', reading as N3");
061: lang = "N3";
062: } else if (f.getName().endsWith(".nt")) {
063: log.warn("file end with '.nt', reading as N-TRIPLE");
064: lang = "N-TRIPLE";
065: } else {
066: log.warn("default: read as RDF/XML");
067: }
068:
069: m.read(r, "", lang);
070:
071: } catch (FileNotFoundException e) {
072: throw new RuntimeException("Could not find file: "
073: + f.getAbsolutePath() + " Exception: " + e);
074: } catch (UnsupportedEncodingException e) {
075: throw new RuntimeException(e);
076: }
077: }
078:
079: public static Model read(File f) {
080: com.hp.hpl.jena.rdf.model.Model m = ModelFactory
081: .createDefaultModel();
082: readIntoJenaModel(m, f);
083: return new ModelImplJena24(null, m);
084: }
085:
086: public static Model read(URL url) {
087: com.hp.hpl.jena.rdf.model.Model m = ModelFactory
088: .createDefaultModel();
089:
090: // TODO guess language
091: m.read("" + url, "" + url, "RDF/XML");
092: return new ModelImplJena24(null, m);
093: }
094:
095: public static Model read(Reader reader) {
096: com.hp.hpl.jena.rdf.model.Model m = ModelFactory
097: .createDefaultModel();
098: readIntoJenaModel(m, reader);
099: return new ModelImplJena24(null, m);
100: }
101:
102: /** TODO test xml ecnoding
103: * @throws IOException */
104: public static void writeJenaModel(
105: com.hp.hpl.jena.rdf.model.Model jm, Writer w)
106: throws ModelRuntimeException, IOException {
107: DumpUtils.addCommonPrefixesToJenaModel(jm);
108:
109: log.error("Writing RDF/XML in UTF-8...");
110: // Jena is not adding this to the XML file, when choosing UTF-8
111: // encoding
112: w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
113: jm.write(w, "RDF/XML", "");
114: }
115:
116: public static void write(Model m, Writer w)
117: throws ModelRuntimeException {
118: Model jenaModel = new ModelImplJena24(Reasoning.none);
119: jenaModel.addAll(m.iterator());
120: com.hp.hpl.jena.rdf.model.Model jm = (com.hp.hpl.jena.rdf.model.Model) jenaModel
121: .getUnderlyingModelImplementation();
122: try {
123: writeJenaModel(jm, w);
124: } catch (IOException e) {
125: throw new ModelRuntimeException(e);
126: }
127: }
128:
129: public static void write(Model m, String filename, String format)
130: throws ModelRuntimeException {
131: Model jenaModel = new ModelImplJena24(Reasoning.none);
132: jenaModel.addAll(m.iterator());
133: com.hp.hpl.jena.rdf.model.Model jm = (com.hp.hpl.jena.rdf.model.Model) jenaModel
134: .getUnderlyingModelImplementation();
135: DumpUtils.addCommonPrefixesToJenaModel(jm);
136:
137: try {
138: // encoding!
139: if (format.equalsIgnoreCase("rdf/xml")) {
140: log.error("Writing RDF/XML in UTF-8...");
141: FileOutputStream fos;
142: fos = new FileOutputStream(new File(filename));
143: OutputStreamWriter osw = new OutputStreamWriter(fos,
144: "UTF-8");
145: // Jena is not adding this to the XML file, when choosing UTF-8
146: // encoding
147: osw
148: .write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
149: jm.write(osw, "RDF/XML", "");
150: } else
151: jm
152: .write(new FileWriter(new File(filename)),
153: format, "");
154:
155: } catch (FileNotFoundException e) {
156: throw new ModelRuntimeException(e);
157: } catch (IOException e) {
158: throw new ModelRuntimeException(e);
159: }
160: }
161:
162: }
|