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

The following examples show how to use org.openrdf.rio.Rio#getParserFormatForFileName() . 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: 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 3
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 4
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 5
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);

            }
            
        }