Strabon

changeset 1352:7c0e94008b62

made KML and XML writers output the variables in the same order as they appear in the query.
Reported by Sam Tomilson and David Leaver from Centre for Ecology and Hydrology (MELODIES).
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Wed Sep 10 11:46:14 2014 +0300 (2014-09-10)
parents 4c69c17e2fbe
children a4c6d008a576 8d50279a662c
files resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java
line diff
     1.1 --- a/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Mon Jul 21 12:00:26 2014 +0300
     1.2 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Wed Sep 10 11:46:14 2014 +0300
     1.3 @@ -91,6 +91,11 @@
     1.4  	private stSPARQLXMLWriter xmlWriter;
     1.5  
     1.6  	/**
     1.7 +	 * The ordered list of binding names of the result.
     1.8 +	 */
     1.9 +	private List<String> bindingNames;
    1.10 +	
    1.11 +	/**
    1.12  	 * The number of results seen.
    1.13  	 */
    1.14  	private int nresults;
    1.15 @@ -158,6 +163,8 @@
    1.16  	@Override
    1.17  	public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    1.18  		try {
    1.19 +			// keep the order of binding names
    1.20 +			this.bindingNames = bindingNames;
    1.21  			xmlWriter.startDocument();
    1.22  			xmlWriter.setAttribute("xmlns", NAMESPACE);
    1.23  			xmlWriter.startTag(ROOT_TAG);
    1.24 @@ -214,34 +221,37 @@
    1.25  			xmlWriter.textElement(NAME_TAG, "Result" + nresults);
    1.26  			
    1.27  			// parse binding set
    1.28 -			for (Binding binding : bindingSet) {
    1.29 -
    1.30 -				Value value = binding.getValue();
    1.31 +			for (String bindingName : bindingNames) {
    1.32  				
    1.33 -				// check for geometry value
    1.34 -				if (XMLGSDatatypeUtil.isGeometryValue(value)) {
    1.35 -					hasGeometry=true;
    1.36 -
    1.37 -					if (logger.isDebugEnabled()) {
    1.38 -						logger.debug("[Strabon] Found geometry: {}", value);
    1.39 +				Binding binding = bindingSet.getBinding(bindingName);
    1.40 +				if(binding != null) {
    1.41 +					Value value = binding.getValue();
    1.42 +					
    1.43 +					// check for geometry value
    1.44 +					if (XMLGSDatatypeUtil.isGeometryValue(value)) {
    1.45 +						hasGeometry=true;
    1.46 +	
    1.47 +						if (logger.isDebugEnabled()) {
    1.48 +							logger.debug("[Strabon] Found geometry: {}", value);
    1.49 +						}
    1.50 +						
    1.51 +						xmlWriter.unescapedText(getKML(value));
    1.52 +						
    1.53 +					} else { // URI, BlankNode, or Literal other than spatial literal
    1.54 +						
    1.55 +						if (logger.isDebugEnabled()) {
    1.56 +							logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal ({}): {}", value.getClass(), value);
    1.57 +						}
    1.58 +						
    1.59 +						// mark that we found sth corresponding to the description
    1.60 +						hasDesc = true;
    1.61 +						
    1.62 +						// write description
    1.63 +						writeDesc(binding);
    1.64 +						
    1.65 +						// fill also the extended data attribute of the Placemark
    1.66 +						extData.put(binding.getName(), getBindingValue(binding));
    1.67  					}
    1.68 -					
    1.69 -					xmlWriter.unescapedText(getKML(value));
    1.70 -					
    1.71 -				} else { // URI, BlankNode, or Literal other than spatial literal
    1.72 -					
    1.73 -					if (logger.isDebugEnabled()) {
    1.74 -						logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal ({}): {}", value.getClass(), value);
    1.75 -					}
    1.76 -					
    1.77 -					// mark that we found sth corresponding to the description
    1.78 -					hasDesc = true;
    1.79 -					
    1.80 -					// write description
    1.81 -					writeDesc(binding);
    1.82 -					
    1.83 -					// fill also the extended data attribute of the Placemark
    1.84 -					extData.put(binding.getName(), getBindingValue(binding));
    1.85  				}
    1.86  			}
    1.87  			
     2.1 --- a/resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java	Mon Jul 21 12:00:26 2014 +0300
     2.2 +++ b/resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java	Wed Sep 10 11:46:14 2014 +0300
     2.3 @@ -39,7 +39,6 @@
     2.4  import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
     2.5  import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron;
     2.6  
     2.7 -
     2.8  import eu.earthobservatory.constants.GeoConstants;
     2.9  
    2.10  /**
    2.11 @@ -60,6 +59,11 @@
    2.12  	 */
    2.13  	private XMLWriter xmlWriter;
    2.14  
    2.15 +	/**
    2.16 +	 * The ordered list of binding names of the result.
    2.17 +	 */
    2.18 +	private List<String> bindingNames;
    2.19 +	
    2.20  	/*--------------*
    2.21  	 * Constructors *
    2.22  	 *--------------*/
    2.23 @@ -92,6 +96,9 @@
    2.24  		throws TupleQueryResultHandlerException
    2.25  	{
    2.26  		try {
    2.27 +			// keep the order of binding names
    2.28 +			this.bindingNames = bindingNames;	
    2.29 +			
    2.30  			xmlWriter.startDocument();
    2.31  
    2.32  			xmlWriter.setAttribute("xmlns", NAMESPACE);
    2.33 @@ -133,13 +140,17 @@
    2.34  		try {
    2.35  			xmlWriter.startTag(RESULT_TAG);
    2.36  
    2.37 -			for (Binding binding : bindingSet) {
    2.38 -				xmlWriter.setAttribute(BINDING_NAME_ATT, binding.getName());
    2.39 -				xmlWriter.startTag(BINDING_TAG);
    2.40 -
    2.41 -				writeValue(binding.getValue());
    2.42 -
    2.43 -				xmlWriter.endTag(BINDING_TAG);
    2.44 +			for (String bindingName : bindingNames) {
    2.45 +				Binding binding = bindingSet.getBinding(bindingName);
    2.46 +				if(binding != null)
    2.47 +				{
    2.48 +					xmlWriter.setAttribute(BINDING_NAME_ATT, binding.getName());
    2.49 +					xmlWriter.startTag(BINDING_TAG);
    2.50 +	
    2.51 +					writeValue(binding.getValue());
    2.52 +	
    2.53 +					xmlWriter.endTag(BINDING_TAG);
    2.54 +				}
    2.55  			}
    2.56  
    2.57  			xmlWriter.endTag(RESULT_TAG);