Strabon

changeset 353:855f4232fefe

added factory class for creating TupleQueryWriters for different formats from Format
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Jun 25 18:32:44 2012 +0300 (2012-06-25)
parents 146feca5586f
children f5cfc1483751
files resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java	Mon Jun 25 18:32:44 2012 +0300
     1.3 @@ -0,0 +1,60 @@
     1.4 +/**
     1.5 + * 
     1.6 + */
     1.7 +package org.openrdf.query.resultio;
     1.8 +
     1.9 +import java.io.OutputStream;
    1.10 +
    1.11 +import org.openrdf.query.resultio.sparqljson.stSPARQLResultsGeoJSONWriterFactory;
    1.12 +import org.openrdf.query.resultio.sparqlxml.Format;
    1.13 +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriterFactory;
    1.14 +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriterFactory;
    1.15 +import org.openrdf.query.resultio.text.stSPARQLResultsTSVWriterFactory;
    1.16 +
    1.17 +/**
    1.18 + * This is a factory class for creating stSPARQLQueryResultWriter
    1.19 + * instances according to a format in @{link org.openrdf.query.resultio.sparqlxml.Format}.
    1.20 + * 
    1.21 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    1.22 + *
    1.23 + */
    1.24 +public class stSPARQLQueryResultWriterFactory {
    1.25 +
    1.26 +	private static TupleQueryResultWriterFactory xml = new stSPARQLResultsXMLWriterFactory();
    1.27 +	private static TupleQueryResultWriterFactory kml = new stSPARQLResultsKMLWriterFactory();
    1.28 +	private static TupleQueryResultWriterFactory tsv = new stSPARQLResultsTSVWriterFactory();
    1.29 +	private static TupleQueryResultWriterFactory geojson = new stSPARQLResultsGeoJSONWriterFactory();
    1.30 +	
    1.31 +	public static TupleQueryResultWriter createstSPARQLQueryResultWriter(Format format, OutputStream out) {
    1.32 +		TupleQueryResultWriter writer = null;
    1.33 +		
    1.34 +		switch (format) {
    1.35 +			case DEFAULT:
    1.36 +				writer = tsv.getWriter(out);
    1.37 +				break;
    1.38 +				
    1.39 +			case XML:
    1.40 +				writer = xml.getWriter(out);
    1.41 +				break;
    1.42 +				
    1.43 +			case KML:
    1.44 +				writer = kml.getWriter(out);
    1.45 +				break;
    1.46 +				
    1.47 +			case KMZ:
    1.48 +				writer = kml.getWriter(out);
    1.49 +				break;
    1.50 +				
    1.51 +			case GEOJSON:
    1.52 +				writer = geojson.getWriter(out);
    1.53 +				break;
    1.54 +				
    1.55 +				// TODO: add for the following two
    1.56 +			case EXP:
    1.57 +			case HTML:
    1.58 +		}
    1.59 +		
    1.60 +		return writer;
    1.61 +	}
    1.62 +	
    1.63 +}
     2.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 17:58:05 2012 +0300
     2.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 18:32:44 2012 +0300
     2.3 @@ -31,6 +31,8 @@
     2.4  import org.openrdf.query.TupleQueryResultHandlerException;
     2.5  import org.openrdf.query.Update;
     2.6  import org.openrdf.query.UpdateExecutionException;
     2.7 +import org.openrdf.query.resultio.TupleQueryResultWriter;
     2.8 +import org.openrdf.query.resultio.stSPARQLQueryResultWriterFactory;
     2.9  import org.openrdf.query.resultio.sparqljson.stSPARQLResultsGeoJSONWriterFactory;
    2.10  import org.openrdf.query.resultio.sparqlxml.Format;
    2.11  import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriterFactory;
    2.12 @@ -206,14 +208,15 @@
    2.13  	throws MalformedQueryException, QueryEvaluationException, IOException, TupleQueryResultHandlerException {
    2.14  		boolean status = true;
    2.15  		
    2.16 +		logger.info("[Strabon.query] Executing query: {}", queryString);
    2.17 +		
    2.18 +		// check for null stream
    2.19  		if (out == null) {
    2.20  			logger.error("[Strabon.query] Cannot write to null stream.");
    2.21  			
    2.22  			return false;
    2.23  		}
    2.24  		
    2.25 -		logger.info("[Strabon.query] Executing query: {}", queryString);
    2.26 -		
    2.27  		TupleQuery tupleQuery = null;
    2.28  		try {
    2.29  			tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    2.30 @@ -229,10 +232,6 @@
    2.31  		
    2.32  		TupleQueryResult result = null;
    2.33  		switch (resultsFormat) {
    2.34 -			case DEFAULT:
    2.35 -				tupleQuery.evaluate(new stSPARQLResultsTSVWriterFactory().getWriter(out));
    2.36 -				break;
    2.37 -				
    2.38  			case EXP:
    2.39  				long results = 0;
    2.40  				
    2.41 @@ -249,19 +248,6 @@
    2.42  				//return new long[]{t2-t1, t3-t2, t3-t1, results};
    2.43  				break;
    2.44  				
    2.45 -			case XML:
    2.46 -				tupleQuery.evaluate(new stSPARQLResultsXMLWriterFactory().getWriter(out));
    2.47 -				break;
    2.48 -				
    2.49 -			case KML:
    2.50 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriterFactory().getWriter(out));
    2.51 -				break;
    2.52 -				
    2.53 -			case GEOJSON:
    2.54 -				tupleQuery.evaluate(new stSPARQLResultsGeoJSONWriterFactory().getWriter(out));
    2.55 -				break;
    2.56 -				
    2.57 -				
    2.58  			case KMZ:
    2.59  				// create a zip entry
    2.60  				ZipEntry entry = new ZipEntry("sparql_results.kml");
    2.61 @@ -328,8 +314,16 @@
    2.62  				break;
    2.63  			
    2.64  		default:
    2.65 -			logger.warn("[Strabon.query] Invalid format.");
    2.66 -			status = false;
    2.67 +			// get the writer for the specified format
    2.68 +			TupleQueryResultWriter resultWriter = stSPARQLQueryResultWriterFactory.createstSPARQLQueryResultWriter(resultsFormat, out);
    2.69 +			
    2.70 +			// check for null format
    2.71 +			if (resultWriter == null) {
    2.72 +				logger.error("[Strabon.query] Invalid format.");
    2.73 +				return false;
    2.74 +			}
    2.75 +			
    2.76 +			tupleQuery.evaluate(resultWriter);
    2.77  		}
    2.78  
    2.79  		return status;