use of org.apache.jena.sparql.ARQException in project jena by apache.
the class ElementData method add.
public void add(Binding binding) {
Iterator<Var> iter = binding.vars();
while (iter.hasNext()) {
Var v = iter.next();
if (!vars.contains(v))
throw new ARQException("Variable " + v + " not already declared for ElementData");
}
rows.add(binding);
}
use of org.apache.jena.sparql.ARQException in project jena by apache.
the class QueryValidatorHTML method executeHTML.
// static final String paramSyntaxExtended = "extendedSyntax";
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
try {
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");
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>");
output(outStream, (out) -> out.print(queryString), 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.sparql.ARQException in project jena by apache.
the class RDFLinkDataset method doPutPost.
private void doPutPost(Node graphName, String file, boolean replace) {
Objects.requireNonNull(file);
Lang lang = RDFLanguages.filenameToLang(file);
Txn.executeWrite(dataset, () -> {
if (RDFLanguages.isTriples(lang)) {
Graph graph = LibRDFLink.isDefault(graphName) ? dataset.getDefaultGraph() : dataset.getGraph(graphName);
if (replace)
clear(graph);
RDFDataMgr.read(graph, file);
} else if (RDFLanguages.isQuads(lang)) {
if (replace)
dataset.clear();
// Try to POST to the dataset.
RDFDataMgr.read(dataset, file);
} else
throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")");
});
}
use of org.apache.jena.sparql.ARQException in project jena by apache.
the class QueryExecUtils method getOne.
/**
* Execute, expecting the result to be one row, one column. Return that one
* RDFNode or null Throw excpetion if more than one.
*/
public static RDFNode getOne(QueryExecution qExec, String varname) {
try {
ResultSet rs = qExec.execSelect();
if (!rs.hasNext())
return null;
QuerySolution qs = rs.nextSolution();
RDFNode r = qs.get(varname);
if (rs.hasNext()) {
QuerySolution qs2 = rs.next();
RDFNode r2 = qs2.get(varname);
if (rs.hasNext())
throw new ARQException("More than one: var ?" + varname + " -> " + r + ", " + r2 + ", ...");
else
throw new ARQException("Found two matches: var ?" + varname + " -> " + r + ", " + r2);
}
return r;
} finally {
qExec.close();
}
}
use of org.apache.jena.sparql.ARQException in project jena by apache.
the class RDFConnectionRemote method upload.
/** Send a file to named graph (or "default" or null for the default graph).
* <p>
* The Content-Type is inferred from the file extension.
* <p>
* "Replace" means overwrite existing data, othewise the date is added to the target.
*/
protected void upload(String graph, String file, boolean replace) {
// if triples
Lang lang = RDFLanguages.filenameToLang(file);
if (RDFLanguages.isQuads(lang))
throw new ARQException("Can't load quads into a graph");
if (!RDFLanguages.isTriples(lang))
throw new ARQException("Not an RDF format: " + file + " (lang=" + lang + ")");
String url = RDFConn.urlForGraph(svcGraphStore, graph);
doPutPost(url, file, lang, replace);
}
Aggregations