-
Notifications
You must be signed in to change notification settings - Fork 14
/
FileStorage.java
70 lines (61 loc) · 2.38 KB
/
FileStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.Configuration;
import cz.cvut.kbss.ontodriver.exception.OntoDriverInitializationException;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.riot.RiotNotFoundException;
import org.apache.jena.util.FileUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* File storage accessor.
* <p>
* Note that currently this accessor does not support working with datasets. Only single graph can be present in the file.
*/
class FileStorage extends Storage {
private final String location;
FileStorage(Configuration configuration) {
this.location = configuration.getStorageProperties().getPhysicalURI().toString();
}
@Override
public void initialize() {
try {
try {
initDataset();
} catch (RiotNotFoundException e) {
tryCreatingFile();
}
} catch (RuntimeException e) {
throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
}
}
private void initDataset() {
final Model model = RDFDataMgr.loadModel(location);
this.dataset = DatasetFactory.create(model);
}
private void tryCreatingFile() {
final File file = new File(location);
try {
final boolean result = file.createNewFile();
assert result;
} catch (IOException e) {
LOG.error("Unable to create storage file {}.", location);
throw new OntoDriverInitializationException("Unable to initialize file storage at " + location, e);
}
initDataset();
}
@Override
void writeChanges() throws JenaDriverException {
try (final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location))) {
final String language = FileUtils.guessLang(location);
RDFDataMgr.write(out, dataset.getDefaultModel(), RDFLanguages.nameToLang(language));
} catch (IOException e) {
throw new JenaDriverException("Unable to write out dataset changes.", e);
}
}
}