use of org.openrdf.rio.RDFParser in project blueprints by tinkerpop.
the class SailTest method addFile.
protected void addFile(final InputStream in, final RDFFormat format) throws Exception {
try {
SailConnection sc = sail.getConnection();
sc.begin();
try {
RDFHandler h = new SailAdder(sc);
RDFParser p = Rio.createParser(format);
p.setRDFHandler(h);
p.parse(in, "http://example.org/bogusBaseURI/");
sc.commit();
} finally {
sc.rollback();
sc.close();
}
} finally {
in.close();
}
}
use of org.openrdf.rio.RDFParser in project blueprints by tinkerpop.
the class SparqlRepositorySailGraph method ignoreDatatypesInAllParsers.
// wrap RDF parser factories such that they ignore invalid values in data-typed literals
// (e.g. a value of "fish" for an xsd:integer literal,
// or a value of 1995-01-01T00:00:00+02:00 for an xsd:gYear literal).
// The default behavior is to throw an exception when bad literals are encountered,
// resulting in failure.
private static void ignoreDatatypesInAllParsers() {
RDFParserRegistry r = RDFParserRegistry.getInstance();
Collection<RDFParserFactory> oldFactories = new LinkedList<RDFParserFactory>();
Collection<RDFParserFactory> newFactories = new LinkedList<RDFParserFactory>();
for (final RDFFormat f : r.getKeys()) {
final RDFParserFactory pf = r.get(f);
pf.getParser().setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
RDFParserFactory pfn = new RDFParserFactory() {
public RDFFormat getRDFFormat() {
return f;
}
public RDFParser getParser() {
RDFParser p = pf.getParser();
p.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
return p;
}
};
oldFactories.add(pf);
newFactories.add(pfn);
}
for (RDFParserFactory pf : oldFactories) {
r.remove(pf);
}
for (RDFParserFactory pfn : newFactories) {
r.add(pfn);
}
}
use of org.openrdf.rio.RDFParser in project blueprints by tinkerpop.
the class SailLoader method loadFile.
private long loadFile(final File fileOrDirectory, final SailConnection c) throws IOException {
if (fileOrDirectory.isDirectory()) {
long count = 0;
for (File child : fileOrDirectory.listFiles()) {
count += loadFile(child, c);
}
return count;
} else {
RDFFormat format;
long startTime = System.currentTimeMillis();
if (verbose) {
LOGGER.info("loading file: " + fileOrDirectory);
}
String n = fileOrDirectory.getName();
InputStream is;
if (n.endsWith(".gz")) {
String n0 = n.substring(0, n.lastIndexOf("."));
format = RDFFormat.forFileName(n0);
is = new GZIPInputStream(new FileInputStream(fileOrDirectory));
} else {
format = RDFFormat.forFileName(n);
is = new FileInputStream(fileOrDirectory);
}
try {
if (null == format) {
LOGGER.warning("could not guess format of file: " + n);
return 0;
}
RDFParser p = Rio.createParser(format);
p.setStopAtFirstError(false);
SailConnectionAdder adder = new SailConnectionAdder(c);
p.setRDFHandler(adder);
try {
p.parse(is, baseUri);
} catch (Throwable t) {
// Attempt to recover.
t.printStackTrace(System.err);
} finally {
is.close();
}
long endTime = System.currentTimeMillis();
if (verbose) {
LOGGER.info("\tfinished in " + (endTime - startTime) + "ms");
}
return adder.count;
} finally {
is.close();
}
}
}
use of org.openrdf.rio.RDFParser 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