From 38880c5123586259cd8bf8a6c0ca07632db1ca93 Mon Sep 17 00:00:00 2001 From: Egon Willighagen Date: Sun, 4 Mar 2018 16:27:27 +0100 Subject: [PATCH] Added a helper method to process XML results returned by a SPARQL end point --- .../bioclipse/rdf/business/IRDFManager.java | 9 +++++++ .../bioclipse/rdf/business/RDFManager.java | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/IRDFManager.java b/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/IRDFManager.java index bdf615c..10790f1 100644 --- a/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/IRDFManager.java +++ b/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/IRDFManager.java @@ -262,6 +262,15 @@ public void saveRDFNTriple(IRDFStore store, String fileName) public IStringMatrix sparqlRemote(String url, String SPARQL) throws BioclipseException; + @Recorded + @PublishedMethod( + params = "String queryResults, String originalQuery", + methodSummary = "Parses the XML-formatted SPARQL end point results into an IStringMatrix. " + + "The originalQuery is used to determine prefixes." + ) + public IStringMatrix processSPARQLXML(byte[] queryResults, String originalQuery) + throws BioclipseException; + @Recorded @PublishedMethod( params = "String url, String SPARQL", diff --git a/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/RDFManager.java b/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/RDFManager.java index 0c2306a..79644e5 100644 --- a/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/RDFManager.java +++ b/plugins/net.bioclipse.rdf/src/net/bioclipse/rdf/business/RDFManager.java @@ -52,6 +52,7 @@ import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.ResultSetFactory; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Property; @@ -64,6 +65,7 @@ import com.hp.hpl.jena.shared.NoReaderForLangException; import com.hp.hpl.jena.shared.PrefixMapping; import com.hp.hpl.jena.shared.SyntaxError; +import com.hp.hpl.jena.shared.impl.PrefixMappingImpl; import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; import com.hp.hpl.jena.util.iterator.Filter; @@ -503,6 +505,31 @@ public StringMatrix sparqlRemote( return table; } + public StringMatrix processSPARQLXML( + byte[] queryResults, String originalQuery, IProgressMonitor monitor) { + if (monitor == null) monitor = new NullProgressMonitor(); + + monitor.beginTask("Processing XML results returned by a SPARQL end point..", 100); + PrefixMapping prefixMap = null; + if (originalQuery != null) { + try { + Query query = QueryFactory.create(originalQuery); + prefixMap = query.getPrefixMapping(); + } catch (Exception exception) { + // could not parse the query for namespaces + prefixMap = new PrefixMappingImpl(); + } + } + monitor.worked(20); + + // now the Jena part + ResultSet results = ResultSetFactory.fromXML(new ByteArrayInputStream(queryResults)); + StringMatrix table = StringMatrixHelper.convertIntoTable(prefixMap, results); + monitor.worked(80); + + return table; + } + public IRDFStore sparqlConstructRemote( String serviceURL, String sparqlQueryString, IProgressMonitor monitor)