Strabon
changeset 1015:f2f0cc6cff02
use sesame libraries to convert xml to bindingSet
author | Kallirroi Dogani <kallirroi@di.uoa.gr> |
---|---|
date | Mon Apr 08 18:16:06 2013 +0300 (2013-04-08) |
parents | 608f58bfb469 |
children | 25935f0ef8d7 |
files | endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSpatialEndpoint.java |
line diff
1.1 --- a/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java Mon Apr 08 15:59:48 2013 +0300 1.2 +++ b/endpoint-client/src/main/java/eu/earthobservatory/org/StrabonEndpoint/client/SpatialEndpoint.java Mon Apr 08 18:16:06 2013 +0300 1.3 @@ -9,27 +9,21 @@ 1.4 */ 1.5 package eu.earthobservatory.org.StrabonEndpoint.client; 1.6 1.7 +import java.io.ByteArrayInputStream; 1.8 import java.io.ByteArrayOutputStream; 1.9 import java.io.IOException; 1.10 -import java.io.StringReader; 1.11 +import java.io.InputStream; 1.12 import java.util.Vector; 1.13 1.14 -import javax.xml.parsers.DocumentBuilder; 1.15 -import javax.xml.parsers.DocumentBuilderFactory; 1.16 - 1.17 -import org.openrdf.model.URI; 1.18 -import org.openrdf.model.impl.LiteralImpl; 1.19 -import org.openrdf.model.impl.ValueFactoryImpl; 1.20 -import org.openrdf.query.BindingSet; 1.21 +import org.openrdf.query.QueryEvaluationException; 1.22 +import org.openrdf.query.TupleQueryResult; 1.23 import org.openrdf.query.TupleQueryResultHandlerException; 1.24 -import org.openrdf.query.algebra.evaluation.QueryBindingSet; 1.25 +import org.openrdf.query.resultio.QueryResultIO; 1.26 +import org.openrdf.query.resultio.QueryResultParseException; 1.27 +import org.openrdf.query.resultio.TupleQueryResultFormat; 1.28 +import org.openrdf.query.resultio.UnsupportedQueryResultFormatException; 1.29 import org.openrdf.query.resultio.stSPARQLQueryResultFormat; 1.30 import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter; 1.31 -import org.w3c.dom.Document; 1.32 -import org.w3c.dom.Element; 1.33 -import org.w3c.dom.Node; 1.34 -import org.w3c.dom.NodeList; 1.35 -import org.xml.sax.InputSource; 1.36 1.37 /** 1.38 * SpatialEndpoint is a SPARQLEndpoint which can store and 1.39 @@ -49,7 +43,7 @@ 1.40 super(host, port, endpointName); 1.41 } 1.42 1.43 - public EndpointResult queryForKML(String sparqlQuery) throws IOException, TupleQueryResultHandlerException{ 1.44 + public EndpointResult queryForKML(String sparqlQuery) throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException, QueryEvaluationException{ 1.45 1.46 EndpointResult xmlResult = query(sparqlQuery, stSPARQLQueryResultFormat.XML); 1.47 1.48 @@ -59,88 +53,23 @@ 1.49 1.50 String xml = xmlResult.getResponse(); 1.51 1.52 - Vector<BindingSet> bindingSets = xmlToBindingSet(xml); 1.53 + InputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8")); 1.54 + TupleQueryResult results = QueryResultIO.parse(inputStream, TupleQueryResultFormat.SPARQL); 1.55 1.56 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 1.57 stSPARQLResultsKMLWriter kmlWriter = new stSPARQLResultsKMLWriter(outputStream); 1.58 1.59 kmlWriter.startQueryResult(new Vector<String>()); 1.60 1.61 - for(int i=0; i<bindingSets.size(); i++){ 1.62 + while(results.hasNext()){ 1.63 1.64 - kmlWriter.handleSolution(bindingSets.get(i)); 1.65 - } 1.66 + kmlWriter.handleSolution(results.next()); 1.67 + } 1.68 1.69 kmlWriter.endQueryResult(); 1.70 - 1.71 - 1.72 + 1.73 EndpointResult kmlResult = new EndpointResult(xmlResult.getStatusCode(), xmlResult.getStatusText(), outputStream.toString()); 1.74 return kmlResult; 1.75 } 1.76 - 1.77 - 1.78 -private Vector<BindingSet> xmlToBindingSet(String xml){ 1.79 - 1.80 - Vector<BindingSet> bindingSetList = new Vector<BindingSet>(); 1.81 - 1.82 - try { 1.83 - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 1.84 - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 1.85 - Document doc = dBuilder.parse(new InputSource(new StringReader(xml))); 1.86 - 1.87 - doc.getDocumentElement().normalize(); 1.88 - 1.89 - Node resultsNode = doc.getElementsByTagName("results").item(0); 1.90 - Element resultsElement = (Element) resultsNode; 1.91 - NodeList resultsList = resultsElement.getElementsByTagName("result"); 1.92 - 1.93 - for (int i = 0; i < resultsList.getLength(); i++) { 1.94 - 1.95 - Node resultItem = resultsList.item(i); 1.96 - Element resultElement = (Element) resultItem; 1.97 - NodeList bindingNamesList = resultElement.getElementsByTagName("binding"); 1.98 - 1.99 - QueryBindingSet bindingSet = new QueryBindingSet(); 1.100 - ValueFactoryImpl valueFactImpl = new ValueFactoryImpl(); 1.101 - 1.102 - for (int j=0; j<bindingNamesList.getLength(); j++){ 1.103 - 1.104 - Node bindingNameItem = bindingNamesList.item(j); 1.105 - Element bindingNameElement = (Element) bindingNameItem; 1.106 - 1.107 - String bindingName = bindingNameElement.getAttribute("name"); 1.108 - Node child = bindingNameItem.getFirstChild(); 1.109 - Element childElement = (Element) child; 1.110 - String childName = child.getNodeName(); 1.111 - 1.112 - if(childName.equals("uri")){ 1.113 - URI uri = valueFactImpl.createURI(bindingNameElement.getElementsByTagName("uri").item(0).getTextContent()); 1.114 - bindingSet.addBinding(bindingName, uri); 1.115 - } 1.116 - else if (childName.equals("literal")){ 1.117 - URI datatype = valueFactImpl.createURI(childElement.getAttribute("datatype")); 1.118 - String value = bindingNameElement.getElementsByTagName("literal").item(0).getTextContent(); 1.119 - LiteralImpl literal= new LiteralImpl(value, datatype); 1.120 - bindingSet.addBinding(bindingName, literal); 1.121 - 1.122 - } 1.123 - else{ 1.124 - System.out.println("Parse error: Unkown xml"); 1.125 - return null; 1.126 - } 1.127 - 1.128 - } 1.129 - 1.130 - bindingSetList.add(bindingSet); 1.131 - 1.132 - } 1.133 - 1.134 - 1.135 - } 1.136 - catch (Exception e) { 1.137 - e.printStackTrace(); 1.138 - } 1.139 - 1.140 - return bindingSetList; 1.141 - } 1.142 + 1.143 }
2.1 --- a/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSpatialEndpoint.java Mon Apr 08 15:59:48 2013 +0300 2.2 +++ b/endpoint-client/src/test/java/eu/earthobservatory/org/StrabonEndpoint/client/TestSpatialEndpoint.java Mon Apr 08 18:16:06 2013 +0300 2.3 @@ -14,7 +14,10 @@ 2.4 2.5 import org.junit.Before; 2.6 import org.junit.Test; 2.7 +import org.openrdf.query.QueryEvaluationException; 2.8 import org.openrdf.query.TupleQueryResultHandlerException; 2.9 +import org.openrdf.query.resultio.QueryResultParseException; 2.10 +import org.openrdf.query.resultio.UnsupportedQueryResultFormatException; 2.11 2.12 /** 2.13 * @author Kallirroi Dogani <kallirroi@di.uoa.gr> 2.14 @@ -36,7 +39,7 @@ 2.15 "SELECT ?k ?g WHERE {\n" + 2.16 " ?k ex:geometry ?g\n" + 2.17 "}" + 2.18 - "\nLIMIT 1"; 2.19 + "\nLIMIT 2"; 2.20 } 2.21 2.22 /** 2.23 @@ -46,25 +49,33 @@ 2.24 @Test 2.25 public void testQuery() { 2.26 try { 2.27 - EndpointResult response = endpoint.queryForKML(query); 2.28 + EndpointResult response; 2.29 2.30 - System.out.println("KML format:"); 2.31 - System.out.println(response.getResponse()); 2.32 + response = endpoint.queryForKML(query); 2.33 + 2.34 + System.out.println("KML format:"); 2.35 + System.out.println(response.getResponse()); 2.36 + 2.37 + if (response.getStatusCode() != 200) { 2.38 + System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); 2.39 + 2.40 + } 2.41 + 2.42 + assertTrue(response.getStatusCode() == 200); 2.43 2.44 - if (response.getStatusCode() != 200) { 2.45 - System.err.println("Status code ("+response.getStatusCode()+"):" + response.getStatusText()); 2.46 - 2.47 - } 2.48 - 2.49 - assertTrue(response.getStatusCode() == 200); 2.50 - 2.51 + }catch (QueryResultParseException e) { 2.52 + e.printStackTrace(); 2.53 + } catch (UnsupportedQueryResultFormatException e) { 2.54 + e.printStackTrace(); 2.55 } 2.56 catch (TupleQueryResultHandlerException e) { 2.57 e.printStackTrace(); 2.58 } 2.59 catch (IOException e) { 2.60 - e.printStackTrace(); 2.61 - } 2.62 + e.printStackTrace(); 2.63 + } catch (QueryEvaluationException e) { 2.64 + e.printStackTrace(); 2.65 + } 2.66 2.67 } 2.68 }