Hi,
it admittedly took me some time to get my hands on the JAVA source for
this item…
Attached the full file (~200 lines) for reference purposes.
*Anticipated JENA / ARQ versions*
JENA: 2.5.5
ARQ: 2.6
The questionable code part goes:
public void generate() throws SAXException, IOException {
worldgraphModel = ModelRDB.open(dbCon, rdfGraph);
try {
Query query = QueryFactory.read(sourcefileLocation.getURI());
QueryExecution qexec = QueryExecutionFactory.create(query,
worldgraphModel);
if (query.getQueryType() == Query.QueryTypeSelect) {
Model outputModel =
ModelFactory.createDefaultModel(ReificationStyle.Minimal);
StringWriter sw = new StringWriter();
ResultSet resultSet = qexec.execSelect();
ResultSetFormatter.asRDF(outputModel, resultSet);
outputModel.write(sw, outputType);
outputModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeConstruct) {
Model resultModel = qexec.execConstruct();
StringWriter sw = new StringWriter();
resultModel.write(sw, outputType);
resultModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeDescribe) {
Model resultModel = qexec.execDescribe();
StringWriter sw = new StringWriter();
resultModel.write(sw, outputType);
resultModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeAsk) {
Model outputModel =
ModelFactory.createDefaultModel(ReificationStyle.Minimal);
StringWriter sw = new StringWriter();
boolean resultBool = qexec.execAsk();
ResultSetFormatter.asRDF(outputModel, resultBool);
outputModel.write(sw, outputType);
outputModel.close();
doSAX(sw.toString());
}
qexec.close();
}
catch (QueryParseException qpe) {
worldgraphModel.begin();
try {
UpdateAction.parseExecute(sparqlString, worldgraphModel);
}
catch (QueryParseException upe) {
throw new QueryParseException("can't parse data source – syntax error
? ", 0, 0);
}
worldgraphModel.commit();
doSAX("<updatesuccess value=\"true\"/>");
}
finally {
worldgraphModel.close();
}
}
Note: Had the entire RDF tree validated @ W3C as well before trying.
Any idea, what could have gone wrong here?
In advance, thanks for all your time and suggestions.
Bardo
Am 23.01.2013 11:38, schrieb Andy Seaborne:
On 23/01/13 10:28, Bardo Nelgen wrote:
Hi Andy,
thanks for the fast reply and thank you very much for the hint.
I'll send a copy of this to our programmers then. Just originally
assumed the error resided inside the SPARQLing (I'm just the markup-guy…
;-) )
Nonetheless, here comes the stacktrace (short version):
com.hp.hpl.jena.query.QueryParseException: Syntaxfehler
at com.semaworx.ARQGenerator.generate(ARQGenerator.java:170)
Time to look at line 170 of the (non-Jena) class ARQGenerator!
Looks like it is throwing the exception (maybe because it caught an
execption from Jena) but "Syntaxfehler" isn't from Jena. So it may be
hiding the details.
(and which version of Jena are you running?)
Andy
package com.semaworx;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.db.ModelRDB;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryParseException;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.shared.ReificationStyle;
import com.hp.hpl.jena.update.UpdateAction;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.generation.AbstractGenerator;
import org.apache.cocoon.xml.StringXMLizable;
import org.apache.excalibur.source.Source;
import org.xml.sax.SAXException;
public class ARQGenerator extends AbstractGenerator {
private static final String PROPFILE = "RDFDB.properties";
private String outputType;
private Source sourcefileLocation;
private IDBConnection dbCon;
private String rdfGraph;
private Model worldgraphModel;
private String sparqlString;
private boolean hasRdftype = false;
@Override
public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
throws SAXException, IOException, ProcessingException {
String rdftype;
super.setup(resolver, objectModel, src, par);
// get source file location
sourcefileLocation = resolver.resolveURI(src);
// file content to string
InputStream iStream = sourcefileLocation.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sparqlBuffer = new StringBuffer();
while (br.ready()) {
sparqlBuffer.append(br.readLine());
sparqlBuffer.append("\n");
}
br.close();
sparqlString = sparqlBuffer.toString();
// collect sitemap parameters
try {
String[] parNames = par.getNames();
for (int i = 0; i < parNames.length; i++) {
if (parNames[i].equalsIgnoreCase("rdftype")) {
hasRdftype = true;
rdftype = par.getParameter("rdftype");
if (rdftype.equalsIgnoreCase("pretty")) {
outputType = "RDF/XML-ABBREV";
}
if (rdftype.equalsIgnoreCase("strict")) {
outputType = "RDF/XML";
}
if (!rdftype.equalsIgnoreCase("pretty") & !rdftype.equalsIgnoreCase("strict")) {
throw new ProcessingException("parameter \"rdftype\" required to be \"strict\" or \"pretty\"");
}
}
if (!parNames[i].equalsIgnoreCase("rdftype")) {
// try {
sparqlString = sparqlString.replace(parNames[i], par.getParameter(parNames[i]));
// } catch (NullPointerException ne) {}
}
}
if (!hasRdftype) {
throw new ProcessingException("parameter \"rdftype\" missing");
}
} catch (ParameterException pe) {
throw new ProcessingException(pe.getMessage());
}
//initialize settings fromproperties file
Properties dbProps = new Properties();
try {
dbProps.load(this.getClass().getResourceAsStream(PROPFILE));
}
catch (NullPointerException ne) {
throw new FileNotFoundException("file \"" + PROPFILE + "\" needs to be located in class folder");
}
String dbHost = dbProps.getProperty("db.host");
String dbType = dbProps.getProperty("db.type");
String dbName = dbProps.getProperty("db.name");
String dbUser = dbProps.getProperty("db.user");
String dbPass = dbProps.getProperty("db.pass");
rdfGraph = dbProps.getProperty("rdf.graph");
try {
dbCon = initDB(dbHost, dbType, dbName, dbUser, dbPass, rdfGraph);
}
catch (ClassNotFoundException cnfe) {
throw new IOException(cnfe.getMessage());
}
}
@Override
public void generate() throws SAXException, IOException {
worldgraphModel = ModelRDB.open(dbCon, rdfGraph);
try {
Query query = QueryFactory.read(sourcefileLocation.getURI());
QueryExecution qexec = QueryExecutionFactory.create(query, worldgraphModel);
if (query.getQueryType() == Query.QueryTypeSelect) {
Model outputModel = ModelFactory.createDefaultModel(ReificationStyle.Minimal);
StringWriter sw = new StringWriter();
ResultSet resultSet = qexec.execSelect();
ResultSetFormatter.asRDF(outputModel, resultSet);
outputModel.write(sw, outputType);
outputModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeConstruct) {
Model resultModel = qexec.execConstruct();
StringWriter sw = new StringWriter();
resultModel.write(sw, outputType);
resultModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeDescribe) {
Model resultModel = qexec.execDescribe();
StringWriter sw = new StringWriter();
resultModel.write(sw, outputType);
resultModel.close();
doSAX(sw.toString());
}
if (query.getQueryType() == Query.QueryTypeAsk) {
Model outputModel = ModelFactory.createDefaultModel(ReificationStyle.Minimal);
StringWriter sw = new StringWriter();
boolean resultBool = qexec.execAsk();
ResultSetFormatter.asRDF(outputModel, resultBool);
outputModel.write(sw, outputType);
outputModel.close();
doSAX(sw.toString());
}
qexec.close();
}
catch (QueryParseException qpe) {
worldgraphModel.begin();
try {
UpdateAction.parseExecute(sparqlString, worldgraphModel);
}
catch (QueryParseException upe) {
throw new QueryParseException("can't parse data source â syntax error ? ", 0, 0);
}
worldgraphModel.commit();
doSAX("<updatesuccess value=\"true\"/>");
}
finally {
worldgraphModel.close();
}
}
@Override
public void recycle() {
super.recycle();
resolver.release(sourcefileLocation);
if (!worldgraphModel.isClosed()) {
worldgraphModel.close();
}
try {
dbCon.close();
}
catch (SQLException se) {
}
}
private IDBConnection initDB(String host, String type, String name, String user, String pass,
String graphname) throws ClassNotFoundException {
String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
String MYSQL_SCHEME = "jdbc:mysql://";
String POSTGRES_DRIVER = "org.postgresql.Driver";
String POSTGRES_SCHEME = "jdbc:postgresql://";
String scheme = "";
if (type.equals("MySQL")) {
Class.forName(MYSQL_DRIVER);
scheme = MYSQL_SCHEME;
}
else if (type.equals("PostgreSQL")) {
Class.forName(POSTGRES_DRIVER);
scheme = POSTGRES_SCHEME;
}
else {
throw new Error("unkown database type; please heck \"RDFDB.properties\"");
}
IDBConnection dbcon = new DBConnection(scheme + host + "/" + name, user, pass, type);
if (!dbcon.containsModel(graphname)) {
Model worldgraphmodel = ModelRDB.createModel(dbcon, graphname);
worldgraphmodel.close();
}
return dbcon;
}
private void doSAX(String input) throws SAXException {
StringXMLizable saxfutter = new StringXMLizable(input);
saxfutter.toSAX(contentHandler);
}
}