use of edu.mit.simile.babel.BabelReader in project backstage by zepheira.
the class DataLoadingUtilities method loadDataFromStream.
public static void loadDataFromStream(InputStream stream, String sourceURL, String lang, Sail sail) throws Exception {
RepoSailTuple rs = createMemoryRepository(null);
Repository r = rs.repository;
lang = lang.toLowerCase();
if ("exhibit/json".equals(lang)) {
Properties properties = new Properties();
BabelReader reader = new ExhibitJsonReader();
try {
if (reader.takesReader()) {
InputStreamReader isr = new InputStreamReader(stream);
reader.read(isr, sail, properties, Locale.getDefault());
} else {
reader.read(stream, sail, properties, Locale.getDefault());
}
} finally {
stream.close();
}
} else {
RDFParser parser = null;
if ("rdfxml".equals(lang)) {
parser = new RDFXMLParser(r.getValueFactory());
} else if ("n3".equals(lang) || "turtle".equals(lang)) {
parser = new TurtleParser(r.getValueFactory());
} else if ("ntriples".equals(lang)) {
parser = new NTriplesParser(r.getValueFactory());
}
try {
SailConnection c = null;
try {
c = sail.getConnection();
BNodeConverterStatementHandler handler = new BNodeConverterStatementHandler(c);
parser.setRDFHandler(handler);
parser.setParseErrorListener(new LoggingParseErrorListener(sourceURL));
parser.setVerifyData(false);
parser.setStopAtFirstError(false);
parser.parse(stream, sourceURL);
c.commit();
_logger.info("Read " + handler.m_count + " statements from '" + sourceURL + "'");
} catch (RepositoryException e) {
if (c != null)
c.rollback();
} finally {
if (c != null)
c.close();
}
} catch (Exception e) {
throw new ModelReadFromFileException("Failed to read data from '" + sourceURL + "'", e);
} finally {
stream.close();
}
}
}
Aggregations