use of org.apache.jena.query.Query in project jena by apache.
the class RDFConnectionExample3 method main.
public static void main(String... args) {
Query query = QueryFactory.create("SELECT * { <http://example.org/book/book1> ?p ?o }");
String queryService = "http://sparql.org/books/query";
// Query service, no update, no graph store protocol.
try (RDFConnection conn = RDFConnectionFactory.connect(queryService, null, null)) {
conn.queryResultSet(query, ResultSetFormatter::out);
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class TestSerialization method runTestForReal.
// A serialization test is:
// Read query in.
// Serialize to string.
// Parse again.
// Are they equal?
@Override
protected void runTestForReal() {
Query query = null;
if (queryString == null)
query = queryFromTestItem(testItem);
else
query = queryFromString(queryString);
// Whatever was read in.
runTestWorker(query, query.getSyntax());
}
use of org.apache.jena.query.Query in project jena by apache.
the class QueryValidator method execute.
//static final String paramSyntaxExtended = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
try {
// if ( log.isInfoEnabled() )
// log.info("validation request") ;
String[] args = httpRequest.getParameterValues(paramQuery);
if (args == null || args.length == 0) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query parameter to validator");
return;
}
if (args.length > 1) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many query parameters");
return;
}
final String queryString = httpRequest.getParameter(paramQuery).replaceAll("(\r|\n| )*$", "");
// queryString = queryString.replace("\r\n", "\n") ;
// queryString.replaceAll("(\r|\n| )*$", "") ;
String querySyntax = httpRequest.getParameter(paramSyntax);
if (querySyntax == null || querySyntax.equals(""))
querySyntax = "SPARQL";
Syntax language = Syntax.lookup(querySyntax);
if (language == null) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + querySyntax);
return;
}
String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
String[] a = httpRequest.getParameterValues(paramFormat);
boolean outputSPARQL = false;
boolean outputPrefix = false;
boolean outputAlgebra = false;
boolean outputQuads = false;
boolean outputOptimized = false;
boolean outputOptimizedQuads = false;
if (a != null) {
for (String anA : a) {
if (anA.equals("sparql")) {
outputSPARQL = true;
}
if (anA.equals("prefix")) {
outputPrefix = true;
}
if (anA.equals("algebra")) {
outputAlgebra = true;
}
if (anA.equals("quads")) {
outputQuads = true;
}
if (anA.equals("opt")) {
outputOptimized = true;
}
if (anA.equals("optquads")) {
outputOptimizedQuads = true;
}
}
}
// if ( ! outputSPARQL && ! outputPrefix )
// outputSPARQL = true ;
boolean lineNumbers = true;
if (lineNumbersArg != null)
lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
// Headers
setHeaders(httpResponse);
ServletOutputStream outStream = httpResponse.getOutputStream();
outStream.println("<html>");
printHead(outStream, "SPARQL Query Validation Report");
outStream.println("<body>");
outStream.println("<h1>SPARQL Query Validator</h1>");
// Print query as received
{
outStream.println("<p>Input:</p>");
// Not Java's finest hour.
Content c = new Content() {
@Override
public void print(IndentedWriter out) {
out.print(queryString);
}
};
output(outStream, c, lineNumbers);
}
// Attempt to parse it.
Query query = null;
try {
query = QueryFactory.create(queryString, "http://example/base/", language);
} catch (ARQException ex) {
// Over generous exception (should be QueryException)
// but this makes the code robust.
outStream.println("<p>Syntax error:</p>");
startFixed(outStream);
outStream.println(ex.getMessage());
finishFixed(outStream);
} catch (RuntimeException ex) {
outStream.println("<p>Internal error:</p>");
startFixed(outStream);
outStream.println(ex.getMessage());
finishFixed(outStream);
}
if (query != null) {
if (outputSPARQL)
outputSyntax(outStream, query, lineNumbers);
if (outputAlgebra)
outputAlgebra(outStream, query, lineNumbers);
if (outputQuads)
outputAlgebraQuads(outStream, query, lineNumbers);
if (outputOptimized)
outputAlgebraOpt(outStream, query, lineNumbers);
if (outputOptimizedQuads)
outputAlgebraOptQuads(outStream, query, lineNumbers);
}
outStream.println("</body>");
outStream.println("</html>");
} catch (Exception ex) {
serviceLog.warn("Exception in doGet", ex);
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class QueryCommandAssembler method open.
@Override
public Object open(Assembler a, Resource root, Mode mode) {
// Query
Resource queryDesc = getUniqueResource(root, AssemblerVocab.pQuery);
Query query = (Query) a.open(a, queryDesc, mode);
// Dataset
Resource datasetDesc = getUniqueResource(root, AssemblerVocab.pDataset);
Dataset dataset = (Dataset) a.open(a, datasetDesc, mode);
// Output format
String s = GraphUtils.getStringValue(root, AssemblerVocab.pOutputFormat);
if (s == null)
s = "text";
ResultsFormat format = ResultsFormat.lookup(s);
QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
return new QExec(query, qExec, format);
}
use of org.apache.jena.query.Query in project jena by apache.
the class sdbprint method execCmd.
@Override
protected void execCmd(List<String> positionalArgs) {
Query query = modQuery.getQuery();
compilePrint(getStore(), query);
}
Aggregations