org.eclipse.rdf4j.query.parser.sparql.ast.ParseException Java Examples

The following examples show how to use org.eclipse.rdf4j.query.parser.sparql.ast.ParseException. 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: SPARQLQueryBuilderAsserts.java    From inception with Apache License 2.0 6 votes vote down vote up
private static <T extends RuntimeException> T handleParseException(SPARQLQuery aBuilder,
        T aException)
{
    String[] queryStringLines = aBuilder.selectQuery().getQueryString().split("\n");
    if (aException.getCause() instanceof ParseException) {
        ParseException cause = (ParseException) aException.getCause();
        String message = String.format(
                "Error: %s%n" +
                "Bad query part starting with: %s%n",
                aException.getMessage(),
                queryStringLines[cause.currentToken.beginLine - 1]
                        .substring(cause.currentToken.beginColumn - 1));
        return (T) new MalformedQueryException(message);
    }
    else {
        return aException;
    }
}
 
Example #2
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testServiceGraphPatternStringDetection1() throws TokenMgrError, ParseException, VisitorException {

	String servicePattern = "SERVICE <foo:bar> { ?x <foo:baz> ?y }";

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT * \n");
	qb.append("WHERE { \n");
	qb.append(" { ?s ?p ?o } \n");
	qb.append(" UNION \n");
	qb.append(" { ?p ?q ?r } \n");
	qb.append(servicePattern);
	qb.append("\n");
	qb.append(" FILTER (?s = <foo:bar>) ");
	qb.append(" } ");

	ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());

	ServiceNodeFinder f = new ServiceNodeFinder();
	f.visit(qc, null);

	assertTrue(f.getGraphPatterns().size() == 1);
	assertTrue(servicePattern.equals(f.getGraphPatterns().get(0)));
}
 
Example #3
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testServiceGraphPatternStringDetection2() throws TokenMgrError, ParseException, VisitorException {

	String servicePattern = "SERVICE <foo:bar> \r\n { ?x <foo:baz> ?y. \r\n \r\n }";

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT * \n");
	qb.append("WHERE { \n");
	qb.append(" { ?s ?p ?o } \n");
	qb.append(" UNION \n");
	qb.append(" { ?p ?q ?r } \n");
	qb.append(servicePattern);
	qb.append("\n");
	qb.append(" FILTER (?s = <foo:bar>) ");
	qb.append(" } ");

	ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());

	ServiceNodeFinder f = new ServiceNodeFinder();
	f.visit(qc, null);

	assertTrue(f.getGraphPatterns().size() == 1);
	assertTrue(servicePattern.equals(f.getGraphPatterns().get(0)));
}
 
Example #4
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testServiceGraphPatternStringDetection3() throws TokenMgrError, ParseException, VisitorException {

	String servicePattern1 = "SERVICE <foo:bar> \n { ?x <foo:baz> ?y. }";
	String servicePattern2 = "SERVICE <foo:bar2> \n { ?x <foo:baz> ?y. }";

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT * \n");
	qb.append("WHERE { \n");
	qb.append(servicePattern1);
	qb.append(" OPTIONAL { \n");
	qb.append(servicePattern2);
	qb.append("    } \n");
	qb.append(" } ");

	ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());

	ServiceNodeFinder f = new ServiceNodeFinder();
	f.visit(qc, null);

	assertTrue(f.getGraphPatterns().size() == 2);
	assertTrue(servicePattern1.equals(f.getGraphPatterns().get(0)));
	assertTrue(servicePattern2.equals(f.getGraphPatterns().get(1)));
}
 
Example #5
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testServiceGraphPatternStringDetection4() throws TokenMgrError, ParseException, VisitorException {

	String servicePattern1 = "SERVICE <http://localhost:18080/openrdf/repositories/endpoint1> {  ?s ?p ?o1 . "
			+ "OPTIONAL {	SERVICE SILENT <http://invalid.endpoint.org/sparql> { ?s ?p2 ?o2 } } }";

	String servicePattern2 = "SERVICE SILENT <http://invalid.endpoint.org/sparql> { ?s ?p2 ?o2 }";

	StringBuilder qb = new StringBuilder();
	qb.append("SELECT * \n");
	qb.append("WHERE { \n");
	qb.append(servicePattern1);
	qb.append(" } ");

	ASTQueryContainer qc = SyntaxTreeBuilder.parseQuery(qb.toString());

	ServiceNodeFinder f = new ServiceNodeFinder();
	f.visit(qc, null);

	assertTrue(f.getGraphPatterns().size() == 2);
	assertTrue(servicePattern1.equals(f.getGraphPatterns().get(0)));
	assertTrue(servicePattern2.equals(f.getGraphPatterns().get(1)));
}
 
Example #6
Source File: TupleExprBuilderTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Verifies that a missing close brace does not cause an endless loop. Timeout is set to avoid test itself endlessly
 * looping. See SES-2389.
 */
@Test(timeout = 1000)
public void testMissingCloseBrace() {
	String query = "INSERT DATA { <urn:a> <urn:b> <urn:c> .";
	try {
		final ASTUpdateSequence us = SyntaxTreeBuilder.parseUpdateSequence(query);
		fail("should result in parse error");
	} catch (ParseException e) {
		// fall through, expected
	}
}