I need to load a Turtle TTL file, add some triples, then save it again. The original TTL file has a big header containing the prefixes for some namespaces.
@prefix biro: <http://purl.org/spar/biro/> .
@prefix c4o: <http://purl.org/spar/c4o/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix deo: <http://purl.org/spar/deo/> .
@prefix doco: <http://purl.org/spar/doco/> .
...
After I load the file and add the triples, the saved TTL won't contain the prefixes, unless I specify them manually, one by one. Is there a way to keep the namespaces already present in the loaded file?
This is my Java code:
// Load input TTL file
InputStream stream = new FileInputStream(ttlFile);
RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
Model model = new LinkedHashModel();
rdfParser.setRDFHandler(new StatementCollector(model));
rdfParser.parse(stream);
stream.close();
// Code to add triples here
model.add(...);
// Save the new TTL file
FileOutputStream out = new FileOutputStream(ttlOutputFile);
RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, out);
writer.startRDF();
// Here I can add the prefixes manually
// writer.handleNamespace("dcterms", DCTERMS.NAMESPACE);
for (Statement st : model) {
writer.handleStatement(st);
}
writer.endRDF();