Java Code Examples for org.openrdf.rio.Rio#createParser()

The following examples show how to use org.openrdf.rio.Rio#createParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryEvaluation.java    From CostFed with GNU Affero General Public License v3.0 7 votes vote down vote up
protected Graph parseConfig(File file) throws SailConfigException, IOException {

        RDFFormat format = Rio.getParserFormatForFileName(file.getAbsolutePath());
        if (format==null)
            throw new SailConfigException("Unsupported file format: " + file.getAbsolutePath());
        RDFParser parser = Rio.createParser(format);
        Graph model = new GraphImpl();
        parser.setRDFHandler(new StatementCollector(model));
        InputStream stream = new FileInputStream(file);

        try {
            parser.parse(stream, file.getAbsolutePath());
        } catch (Exception e) {
            throw new SailConfigException("Error parsing file!");
        }

        stream.close();
        return model;
    }
 
Example 2
Source File: TestTicket276.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void addData(final RepositoryConnection conn) throws IOException,
		RDFParseException, RepositoryException, RDFHandlerException {

    final RDFParser rdfParser = Rio.createParser(RDFFormat.NTRIPLES,
			conn.getValueFactory());
	rdfParser.setVerifyData(true);
	rdfParser.setStopAtFirstError(true);
	rdfParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	rdfParser.setRDFHandler(new RDFHandlerBase() {
		@Override
		public void handleStatement(Statement st)
				throws RDFHandlerException {
			try {
				conn.add(st);
			} catch (OpenRDFException e) {
				throw new RDFHandlerException(e);
			}
		}
	});
	rdfParser.parse(getClass().getResourceAsStream("TestTicket276.n3"), "");
}
 
Example 3
Source File: SPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
protected void upload(URI graphURI, Resource context)
	throws Exception
{
	RepositoryConnection con = dataRep.getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString(), RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		}
		finally {
			in.close();
		}

		con.commit();
	}
	catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	}
	finally {
		con.close();
	}
}
 
Example 4
Source File: SPARQLQueryTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
protected final Set<Statement> readExpectedGraphQueryResult()
	throws Exception
{
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFileURL);

	if (rdfFormat != null) {
		RDFParser parser = Rio.createParser(rdfFormat);
		parser.setDatatypeHandling(DatatypeHandling.IGNORE);
		parser.setPreserveBNodeIDs(true);
		parser.setValueFactory(dataRep.getValueFactory());

		Set<Statement> result = new LinkedHashSet<Statement>();
		parser.setRDFHandler(new StatementCollector(result));

		InputStream in = new URL(resultFileURL).openStream();
		try {
			parser.parse(in, resultFileURL);
		}
		finally {
			in.close();
		}

		return result;
	}
	else {
		throw new RuntimeException("Unable to determine file type of results file");
	}
}
 
Example 5
Source File: TestFederatedQuery.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
  * Read the expected graph query result from the specified resource
  * 
  * @param resultFile
  * @return
  * @throws Exception
  */
 private Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception
 {
     final RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile);
 
     if (rdfFormat != null) {
         final RDFParser parser = Rio.createParser(rdfFormat);
         parser.setDatatypeHandling(DatatypeHandling.IGNORE);
         parser.setPreserveBNodeIDs(true);
         parser.setValueFactory(ValueFactoryImpl.getInstance());
 
         final Set<Statement> result = new LinkedHashSet<Statement>();
         parser.setRDFHandler(new StatementCollector(result));
 
final InputStream in = TestFederatedQuery.class
		.getResourceAsStream(TEST_RESOURCE_PATH + resultFile);
         try {
             parser.parse(in, null);     // TODO check
         }
         finally {
             in.close();
         }
 
         return result;
     }
     else {
         throw new RuntimeException("Unable to determine file type of results file");
     }
 }
 
Example 6
Source File: SesameHTTPRepositoryTest.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * test get Statemetns.
 * @throws Exception The test would fails if Exception occurs
 */
@Test
public void testGetStatements() throws Exception {
	//insert some statments first
	for (final String tripleAsString : _data) {
		final Statement triple = parseNX(tripleAsString).iterator().next();
		TRIPLE_STORE.addData(triple);
	}
	//test get the statements
	String uri = BASE_URI + Protocol.REPOSITORIES + "/" + REPO_ID + "/" + Protocol.STATEMENTS;
	File tmp = WebTestUtils.tmpFile();
	final ServletOutputStream servletOutputStream = new StubServletOutputStream(tmp);
	final HttpServletRequest request = mockHttpRequest(uri, METHOD_GET);
	when(request.getParameter(Protocol.SUBJECT_PARAM_NAME)).thenReturn(DEVICE);
	when(request.getParameter(Protocol.ACCEPT_PARAM_NAME)).thenReturn(MIMETYPE_RDF_XML);
	when(request.getCharacterEncoding()).thenReturn(UTF_8);
	final HttpServletResponse response = mock(HttpServletResponse.class);
	when(response.getOutputStream()).thenReturn(servletOutputStream);

	GetMethod method = new GetMethod(uri);
	RDFFormat format = RDFFormat.RDFXML;
	RDFParser parser = Rio.createParser(format, valueFactory);
	parser.setParserConfig(parser.getParserConfig());
	StatementCollector collector = new StatementCollector();
	parser.setRDFHandler(collector);
	_classUnderTest.service(request, response);

	parser.parse(new FileInputStream(tmp), method.getURI().getURI());
	assertTrue(!collector.getStatements().isEmpty());
	verify(response).setStatus(HttpServletResponse.SC_OK);

	servletOutputStream.close();
}
 
Example 7
Source File: Util.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the incoming RDF stream according with a given format.
 * 
 * @param stream
 *            the RDF (input) stream.
 * @param format
 *            the {@link RDFFormat} that will be used for parsing.
 * @return a {@link List} with collected statements.
 */
public static List<Statement> parseAsList(final InputStream stream, final RDFFormat format) {
	final List<Statement> statements = new ArrayList<Statement>();
	try {
		final RDFParser parser = Rio.createParser(format, VALUE_FACTORY);
		parser.setRDFHandler(new StatementCollector(statements));
		parser.parse(stream, "");
	} catch (final Exception exception) {
		LOGGER.error(MessageCatalog._00029_RDF_PARSE_FAILURE, exception);
	}
	return statements;
}
 
Example 8
Source File: RDFModelParser.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@Override
public void injectTriples(TripleSink sink) throws IOException {
	try {
		Collector collector = new Collector();
		RDFParser parser =Rio.createParser(this.format);
		parser.setRDFHandler(collector);
		parser.parse(new StringReader(this.content), this.base);
		SesameModelParser tripleParser=new SesameModelParser(collector.getNamespaces());
		for(Statement st:collector.getStatements()) {
			sink.addTriple(tripleParser.parseStatement(st));
		}
	} catch (OpenRDFException e) {
		throw new IOException(e);
	}
}
 
Example 9
Source File: BasicRioLoader.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Choose the parser based on the {@link RDFFormat} specified to the
 * constructor.
 * 
 * @return The parser.
 */
final protected RDFParser getParser(final RDFFormat rdfFormat) {

    final RDFParser parser = Rio.createParser(rdfFormat, valueFactory);
    
    parser.setValueFactory(valueFactory);
    
    return parser;

}
 
Example 10
Source File: AbstractDataDrivenSPARQLTestCase.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public Set<Statement> readExpectedGraphQueryResult() throws Exception {

            if (expectedGraphQueryResult != null)
                return expectedGraphQueryResult;
            
            final RDFFormat rdfFormat = Rio
                    .getParserFormatForFileName(resultFileURL);

            if (rdfFormat != null) {

                final RDFParser parser = Rio.createParser(rdfFormat);
                parser.setDatatypeHandling(DatatypeHandling.IGNORE);
                parser.setPreserveBNodeIDs(true);
                parser.setValueFactory(store.getValueFactory());

                final Set<Statement> result = new LinkedHashSet<Statement>();
                
                parser.setRDFHandler(new StatementCollector(result));

                final InputStream in = getResourceAsStream(resultFileURL);
                
                try {
                
                    parser.parse(in, resultFileURL);
                    
                } finally {
                    
                    in.close();
                    
                }
                
                expectedGraphQueryResult = result;

                return result;
                
            } else {

                throw new RuntimeException(
                        "Unable to determine file type of results file: "
                                + resultFileURL);

            }
            
        }