You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2016/08/15 04:44:26 UTC
[1/2] clerezza-rdf-core git commit: Added support for queries
returning boolean results (i.e. ASK queries)
Repository: clerezza-rdf-core
Updated Branches:
refs/heads/master aa2c94534 -> 3da2f8bde
Added support for queries returning boolean results (i.e. ASK queries)
Project: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/commit/91aeda1e
Tree: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/tree/91aeda1e
Diff: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/diff/91aeda1e
Branch: refs/heads/master
Commit: 91aeda1ec4daf0644bf5f063ff861c3cdb6a179d
Parents: aa2c945
Author: Reto Gmuer <re...@apache.org>
Authored: Mon Aug 15 04:12:37 2016 +0000
Committer: Reto Gmuer <re...@apache.org>
Committed: Mon Aug 15 04:12:37 2016 +0000
----------------------------------------------------------------------
.../commons/rdf/impl/sparql/SparqlClient.java | 43 ++++++--
.../commons/rdf/impl/sparql/BNodeTest.java | 2 +-
.../rdf/impl/sparql/SparqlClientTest.java | 100 +++++++++++++++++++
3 files changed, 137 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/91aeda1e/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
index 2745a0e..4191748 100644
--- a/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
+++ b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
@@ -54,7 +54,11 @@ public class SparqlClient {
this.endpoint = endpoint;
}
- List<Map<String, RDFTerm>> queryResultSet(final String query) throws IOException {
+ public List<Map<String, RDFTerm>> queryResultSet(final String query) throws IOException {
+ return (List<Map<String, RDFTerm>>) queryResult(query);
+ }
+
+ public Object queryResult(final String query) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(endpoint);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
@@ -95,7 +99,7 @@ public class SparqlClient {
private String currentBindingName;
private Map<String, RDFTerm> currentResult = null;
- private final List<Map<String, RDFTerm>> results = new ArrayList<>();
+ private Object results = null;
private boolean readingValue;
private String lang; //the xml:lang attribute of a literal
private StringWriter valueWriter;
@@ -110,9 +114,13 @@ public class SparqlClient {
return bNodeMap.get(value);
}
- private List<Map<String, RDFTerm>> getResults() {
+ private Object getResults() {
return results;
}
+
+ private List<Map<String, RDFTerm>> getResultValueMaps() {
+ return (List<Map<String, RDFTerm>>)results;
+ }
enum BindingType {
@@ -130,12 +138,24 @@ public class SparqlClient {
String qName,
Attributes atts)
throws SAXException {
- if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
- if ("result".equals(localName)) {
+ if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
+ if ("boolean".equals(localName)) {
+ if (results != null) {
+ throw new SAXException("unexpected tag <boolean>");
+ }
+ //results will have Boolean value assigned once value is read
+ readingValue = true;
+ valueWriter = new StringWriter();
+ } else if ("results".equals(localName)) {
+ if (results != null) {
+ throw new SAXException("unexpected tag <result>");
+ }
+ results = new ArrayList<Map<String, RDFTerm>>();
+ } else if ("result".equals(localName)) {
if (currentResult != null) {
throw new SAXException("unexpected tag <result>");
}
- currentResult = new HashMap<>();
+ currentResult = new HashMap<String, RDFTerm>();
} else if ("binding".equals(localName)) {
if (currentResult == null) {
throw new SAXException("unexpected tag <binding>");
@@ -170,13 +190,17 @@ public class SparqlClient {
throws SAXException {
if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
if ("result".equals(localName)) {
- results.add(currentResult);
+ ((List<Map<String, RDFTerm>>)results).add(currentResult);
currentResult = null;
} else if ("binding".equals(localName)) {
if (currentBindingName == null) {
throw new SAXException("unexpected tag </binding>");
}
currentBindingName = null;
+ } else if ("boolean".equals(localName)) {
+ results = new Boolean(valueWriter.toString());
+ valueWriter = null;
+ readingValue = false;
} else {
try {
BindingType b = BindingType.valueOf(localName);
@@ -213,6 +237,11 @@ public class SparqlClient {
public Language getLanguage() {
return language;
}
+
+ @Override
+ public String toString() {
+ return "\""+getLexicalForm()+"\"@"+getLanguage();
+ }
};
break;
}
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/91aeda1e/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/BNodeTest.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/BNodeTest.java b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/BNodeTest.java
index e1fda73..168ef74 100644
--- a/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/BNodeTest.java
+++ b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/BNodeTest.java
@@ -66,7 +66,7 @@ public class BNodeTest {
@Test
public void graphSize() {
final Graph graph = new SparqlGraph("http://localhost:" + serverPort + "/ds/query");
- Assert.assertEquals("Graph not of the exepected size", 3, graph.size());
+ Assert.assertEquals("Graph not of the expected size", 3, graph.size());
}
/* Filtering with a Bode that cannot be in graph
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/91aeda1e/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
new file mode 100644
index 0000000..fcda4de
--- /dev/null
+++ b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.clerezza.commons.rdf.impl.sparql;
+
+import com.hp.hpl.jena.query.DatasetAccessor;
+import com.hp.hpl.jena.query.DatasetAccessorFactory;
+import java.io.IOException;
+import java.net.ServerSocket;
+import org.apache.jena.fuseki.EmbeddedFusekiServer;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.clerezza.commons.rdf.BlankNode;
+import org.apache.clerezza.commons.rdf.BlankNodeOrIRI;
+import org.apache.clerezza.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.RDFTerm;
+import org.apache.clerezza.commons.rdf.Triple;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author reto
+ */
+public class SparqlClientTest {
+
+ final static int serverPort = findFreePort();
+ static EmbeddedFusekiServer server;
+
+ @BeforeClass
+ public static void prepare() throws IOException {
+ final String serviceURI = "http://localhost:" + serverPort + "/ds/data";
+ final DatasetAccessor accessor = DatasetAccessorFactory.createHTTP(serviceURI);
+ final InputStream in = SparqlClientTest.class.getResourceAsStream("grounded.ttl");
+ final Model m = ModelFactory.createDefaultModel();
+ String base = "http://example.org/";
+ m.read(in, base, "TURTLE");
+ server = EmbeddedFusekiServer.memTDB(serverPort, "/ds");//dataSet.getAbsolutePath());
+ server.start();
+ System.out.println("Started fuseki on port " + serverPort);
+ accessor.putModel(m);
+ }
+
+ @AfterClass
+ public static void cleanup() {
+ server.stop();
+ }
+
+ @Test
+ public void select() throws IOException {
+ final SparqlClient sparqlClient = new SparqlClient(
+ "http://localhost:" + serverPort + "/ds/query");
+ List<Map<String, RDFTerm>> result = sparqlClient.queryResultSet(
+ "SELECT ?name WHERE { "
+ + "<http://example.org/#spiderman> "
+ + "<http://xmlns.com/foaf/0.1/name> ?name}");
+ Assert.assertEquals("There should be two names", 2, result.size());
+ }
+
+ @Test
+ public void ask() throws IOException {
+ final SparqlClient sparqlClient = new SparqlClient(
+ "http://localhost:" + serverPort + "/ds/query");
+ Object result = sparqlClient.queryResult(
+ "ASK { "
+ + "<http://example.org/#spiderman> "
+ + "<http://xmlns.com/foaf/0.1/name> ?name}");
+ Assert.assertEquals("ASK should result to true", Boolean.TRUE, result);
+ }
+
+ public static int findFreePort() {
+ int port = 0;
+ try (ServerSocket server = new ServerSocket(0);) {
+ port = server.getLocalPort();
+ } catch (Exception e) {
+ throw new RuntimeException("unable to find a free port");
+ }
+ return port;
+ }
+
+}
[2/2] clerezza-rdf-core git commit: Added support for queries
returning graphs
Posted by re...@apache.org.
Added support for queries returning graphs
Project: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/commit/3da2f8bd
Tree: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/tree/3da2f8bd
Diff: http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/diff/3da2f8bd
Branch: refs/heads/master
Commit: 3da2f8bde63419cfa82b61662c805a3c14276bc7
Parents: 91aeda1
Author: Reto Gmuer <re...@apache.org>
Authored: Mon Aug 15 04:44:08 2016 +0000
Committer: Reto Gmuer <re...@apache.org>
Committed: Mon Aug 15 04:44:08 2016 +0000
----------------------------------------------------------------------
impl.sparql/pom.xml | 7 +-
.../commons/rdf/impl/sparql/SparqlClient.java | 210 ++---------------
.../rdf/impl/sparql/SparqlResultParser.java | 225 +++++++++++++++++++
.../rdf/impl/sparql/SparqlClientTest.java | 11 +-
4 files changed, 254 insertions(+), 199 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/3da2f8bd/impl.sparql/pom.xml
----------------------------------------------------------------------
diff --git a/impl.sparql/pom.xml b/impl.sparql/pom.xml
index 8cde92d..dcf81fc 100644
--- a/impl.sparql/pom.xml
+++ b/impl.sparql/pom.xml
@@ -106,7 +106,12 @@
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
<version>1.0.0</version>
- <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
+ <artifactId>rdf.jena.parser</artifactId>
+ <version>1.1.1</version>
+ <scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/3da2f8bd/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
index 4191748..6379325 100644
--- a/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
+++ b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClient.java
@@ -15,13 +15,9 @@
*/
package org.apache.clerezza.commons.rdf.impl.sparql;
-import java.io.BufferedInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.StringWriter;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
@@ -33,14 +29,8 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
-import javax.xml.parsers.*;
-import org.apache.clerezza.commons.rdf.BlankNode;
-import org.apache.clerezza.commons.rdf.IRI;
-import org.apache.clerezza.commons.rdf.Language;
import org.apache.clerezza.commons.rdf.RDFTerm;
-import org.apache.clerezza.commons.rdf.impl.utils.AbstractLiteral;
-import org.xml.sax.*;
-import org.xml.sax.helpers.*;
+import org.apache.clerezza.rdf.core.serializedform.Parser;
/**
*
@@ -65,199 +55,25 @@ public class SparqlClient {
nvps.add(new BasicNameValuePair("query", query));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
-
+ HttpEntity entity2 = response2.getEntity();
try {
- HttpEntity entity2 = response2.getEntity();
InputStream in = entity2.getContent();
- SAXParserFactory spf = SAXParserFactory.newInstance();
- spf.setNamespaceAware(true);
- SAXParser saxParser = spf.newSAXParser();
- XMLReader xmlReader = saxParser.getXMLReader();
- final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
- xmlReader.setContentHandler(sparqlsResultsHandler);
- xmlReader.parse(new InputSource(in));
- /*
- for (int ch = in.read(); ch != -1; ch = in.read()) {
- System.out.print((char)ch);
- }
- */
- // do something useful with the response body
- // and ensure it is fully consumed
+ final String mediaType = entity2.getContentType().getValue();
+ if ("application/sparql-results+xml".equals(mediaType)) {
+ return SparqlResultParser.parse(in);
+ } else {
+ //assuming RDF response
+ //FIXME clerezza-core-rdf to clerezza dependency
+ Parser parser = Parser.getInstance();
+ return parser.parse(in, mediaType);
+ }
+ } finally {
EntityUtils.consume(entity2);
- return sparqlsResultsHandler.getResults();
- } catch (ParserConfigurationException ex) {
- throw new RuntimeException(ex);
- } catch (SAXException ex) {
- throw new RuntimeException(ex);
- } finally {
response2.close();
}
}
- final public static class SparqlsResultsHandler extends DefaultHandler {
-
- private String currentBindingName;
- private Map<String, RDFTerm> currentResult = null;
- private Object results = null;
- private boolean readingValue;
- private String lang; //the xml:lang attribute of a literal
- private StringWriter valueWriter;
- private Map<String, BlankNode> bNodeMap = new HashMap<>();
- private static final IRI XSD_STRING = new IRI("http://www.w3.org/2001/XMLSchema#string");
- private static final IRI RDF_LANG_STRING = new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
-
- private RDFTerm getBNode(String value) {
- if (!bNodeMap.containsKey(value)) {
- bNodeMap.put(value, new BlankNode());
- }
- return bNodeMap.get(value);
- }
-
- private Object getResults() {
- return results;
- }
-
- private List<Map<String, RDFTerm>> getResultValueMaps() {
- return (List<Map<String, RDFTerm>>)results;
- }
-
- enum BindingType {
-
- uri, bnode, literal;
- }
-
- @Override
- public void startDocument() throws SAXException {
-
- }
-
- @Override
- public void startElement(String namespaceURI,
- String localName,
- String qName,
- Attributes atts)
- throws SAXException {
- if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
- if ("boolean".equals(localName)) {
- if (results != null) {
- throw new SAXException("unexpected tag <boolean>");
- }
- //results will have Boolean value assigned once value is read
- readingValue = true;
- valueWriter = new StringWriter();
- } else if ("results".equals(localName)) {
- if (results != null) {
- throw new SAXException("unexpected tag <result>");
- }
- results = new ArrayList<Map<String, RDFTerm>>();
- } else if ("result".equals(localName)) {
- if (currentResult != null) {
- throw new SAXException("unexpected tag <result>");
- }
- currentResult = new HashMap<String, RDFTerm>();
- } else if ("binding".equals(localName)) {
- if (currentResult == null) {
- throw new SAXException("unexpected tag <binding>");
- }
- currentBindingName = atts.getValue("name");
- } else if ("uri".equals(localName) || "bnode".equals(localName) || "literal".equals(localName)) {
- if (readingValue) {
- throw new SAXException("unexpected tag <" + localName + ">");
- }
- lang = atts.getValue("http://www.w3.org/XML/1998/namespace", "lang");
- readingValue = true;
- valueWriter = new StringWriter();
- }
- }
-
- //System.out.println(namespaceURI);
- //System.out.println(qName);
- }
-
- @Override
- public void characters(char[] chars, int start, int length) throws SAXException {
- if (readingValue) {
- valueWriter.write(chars, start, length);
- //System.err.println(value + start + ", " + length);
- }
- }
-
- @Override
- public void endElement(String namespaceURI,
- String localName,
- String qName)
- throws SAXException {
- if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
- if ("result".equals(localName)) {
- ((List<Map<String, RDFTerm>>)results).add(currentResult);
- currentResult = null;
- } else if ("binding".equals(localName)) {
- if (currentBindingName == null) {
- throw new SAXException("unexpected tag </binding>");
- }
- currentBindingName = null;
- } else if ("boolean".equals(localName)) {
- results = new Boolean(valueWriter.toString());
- valueWriter = null;
- readingValue = false;
- } else {
- try {
- BindingType b = BindingType.valueOf(localName);
- RDFTerm rdfTerm = null;
- final Language language = lang == null? null : new Language(lang);;
- switch (b) {
- case uri:
- rdfTerm = new IRI(valueWriter.toString());
- valueWriter = null;
- break;
- case bnode:
- rdfTerm = getBNode(valueWriter.toString());
- valueWriter = null;
- break;
- case literal:
- final String lf = valueWriter.toString();
- rdfTerm = new AbstractLiteral() {
-
- @Override
- public String getLexicalForm() {
- return lf;
- }
-
- @Override
- public IRI getDataType() {
- if (language != null) {
- return RDF_LANG_STRING;
- }
- //TODO implement
- return XSD_STRING;
- }
-
- @Override
- public Language getLanguage() {
- return language;
- }
-
- @Override
- public String toString() {
- return "\""+getLexicalForm()+"\"@"+getLanguage();
- }
- };
- break;
- }
- currentResult.put(currentBindingName, rdfTerm);
- readingValue = false;
- } catch (IllegalArgumentException e) {
- //not uri|bnode|literal
- }
- }
- }
- }
-
- public void endDocument() throws SAXException {
- //System.out.println("results: " + results.size());
- }
-
- }
+
}
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/3da2f8bd/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlResultParser.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlResultParser.java b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlResultParser.java
new file mode 100644
index 0000000..43d48f0
--- /dev/null
+++ b/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlResultParser.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2016 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.clerezza.commons.rdf.impl.sparql;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.apache.clerezza.commons.rdf.BlankNode;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.Language;
+import org.apache.clerezza.commons.rdf.RDFTerm;
+import org.apache.clerezza.commons.rdf.impl.utils.AbstractLiteral;
+import org.apache.http.util.EntityUtils;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ *
+ * @author user
+ */
+public class SparqlResultParser {
+
+ static Object parse(InputStream in) throws IOException {
+ try {
+ SAXParserFactory spf = SAXParserFactory.newInstance();
+ spf.setNamespaceAware(true);
+ SAXParser saxParser = spf.newSAXParser();
+ XMLReader xmlReader = saxParser.getXMLReader();
+ final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
+ xmlReader.setContentHandler(sparqlsResultsHandler);
+ xmlReader.parse(new InputSource(in));
+ return sparqlsResultsHandler.getResults();
+ } catch (ParserConfigurationException | SAXException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ final public static class SparqlsResultsHandler extends DefaultHandler {
+
+ private String currentBindingName;
+ private Map<String, RDFTerm> currentResult = null;
+ private Object results = null;
+ private boolean readingValue;
+ private String lang; //the xml:lang attribute of a literal
+ private StringWriter valueWriter;
+ private Map<String, BlankNode> bNodeMap = new HashMap<>();
+ private static final IRI XSD_STRING = new IRI("http://www.w3.org/2001/XMLSchema#string");
+ private static final IRI RDF_LANG_STRING = new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
+
+ private RDFTerm getBNode(String value) {
+ if (!bNodeMap.containsKey(value)) {
+ bNodeMap.put(value, new BlankNode());
+ }
+ return bNodeMap.get(value);
+ }
+
+ private Object getResults() {
+ return results;
+ }
+
+ private List<Map<String, RDFTerm>> getResultValueMaps() {
+ return (List<Map<String, RDFTerm>>) results;
+ }
+
+ enum BindingType {
+
+ uri, bnode, literal;
+ }
+
+ @Override
+ public void startDocument() throws SAXException {
+
+ }
+
+ @Override
+ public void startElement(String namespaceURI,
+ String localName,
+ String qName,
+ Attributes atts)
+ throws SAXException {
+ if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
+ if ("boolean".equals(localName)) {
+ if (results != null) {
+ throw new SAXException("unexpected tag <boolean>");
+ }
+ //results will have Boolean value assigned once value is read
+ readingValue = true;
+ valueWriter = new StringWriter();
+ } else if ("results".equals(localName)) {
+ if (results != null) {
+ throw new SAXException("unexpected tag <result>");
+ }
+ results = new ArrayList<Map<String, RDFTerm>>();
+ } else if ("result".equals(localName)) {
+ if (currentResult != null) {
+ throw new SAXException("unexpected tag <result>");
+ }
+ currentResult = new HashMap<String, RDFTerm>();
+ } else if ("binding".equals(localName)) {
+ if (currentResult == null) {
+ throw new SAXException("unexpected tag <binding>");
+ }
+ currentBindingName = atts.getValue("name");
+ } else if ("uri".equals(localName) || "bnode".equals(localName) || "literal".equals(localName)) {
+ if (readingValue) {
+ throw new SAXException("unexpected tag <" + localName + ">");
+ }
+ lang = atts.getValue("http://www.w3.org/XML/1998/namespace", "lang");
+ readingValue = true;
+ valueWriter = new StringWriter();
+ }
+ }
+
+ //System.out.println(namespaceURI);
+ //System.out.println(qName);
+ }
+
+ @Override
+ public void characters(char[] chars, int start, int length) throws SAXException {
+ if (readingValue) {
+ valueWriter.write(chars, start, length);
+ //System.err.println(value + start + ", " + length);
+ }
+ }
+
+ @Override
+ public void endElement(String namespaceURI,
+ String localName,
+ String qName)
+ throws SAXException {
+ if ("http://www.w3.org/2005/sparql-results#".equals(namespaceURI)) {
+ if ("result".equals(localName)) {
+ ((List<Map<String, RDFTerm>>) results).add(currentResult);
+ currentResult = null;
+ } else if ("binding".equals(localName)) {
+ if (currentBindingName == null) {
+ throw new SAXException("unexpected tag </binding>");
+ }
+ currentBindingName = null;
+ } else if ("boolean".equals(localName)) {
+ results = new Boolean(valueWriter.toString());
+ valueWriter = null;
+ readingValue = false;
+ } else {
+ try {
+ BindingType b = BindingType.valueOf(localName);
+ RDFTerm rdfTerm = null;
+ final Language language = lang == null ? null : new Language(lang);;
+ switch (b) {
+ case uri:
+ rdfTerm = new IRI(valueWriter.toString());
+ valueWriter = null;
+ break;
+ case bnode:
+ rdfTerm = getBNode(valueWriter.toString());
+ valueWriter = null;
+ break;
+ case literal:
+ final String lf = valueWriter.toString();
+ rdfTerm = new AbstractLiteral() {
+
+ @Override
+ public String getLexicalForm() {
+ return lf;
+ }
+
+ @Override
+ public IRI getDataType() {
+ if (language != null) {
+ return RDF_LANG_STRING;
+ }
+ //TODO implement
+ return XSD_STRING;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return language;
+ }
+
+ @Override
+ public String toString() {
+ return "\"" + getLexicalForm() + "\"@" + getLanguage();
+ }
+ };
+ break;
+ }
+ currentResult.put(currentBindingName, rdfTerm);
+ readingValue = false;
+ } catch (IllegalArgumentException e) {
+ //not uri|bnode|literal
+ }
+ }
+ }
+ }
+
+ public void endDocument() throws SAXException {
+ //System.out.println("results: " + results.size());
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza-rdf-core/blob/3da2f8bd/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
----------------------------------------------------------------------
diff --git a/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
index fcda4de..2977f4e 100644
--- a/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
+++ b/impl.sparql/src/test/java/org/apache/clerezza/commons/rdf/impl/sparql/SparqlClientTest.java
@@ -85,7 +85,16 @@ public class SparqlClientTest {
+ "<http://example.org/#spiderman> "
+ "<http://xmlns.com/foaf/0.1/name> ?name}");
Assert.assertEquals("ASK should result to true", Boolean.TRUE, result);
- }
+ }
+
+ @Test
+ public void desribe() throws IOException {
+ final SparqlClient sparqlClient = new SparqlClient(
+ "http://localhost:" + serverPort + "/ds/query");
+ Object result = sparqlClient.queryResult(
+ "DESCRIBE <http://example.org/#spiderman>");
+ Assert.assertTrue("DESCRIBE should return a graph", result instanceof Graph);
+ }
public static int findFreePort() {
int port = 0;