Strabon

changeset 362:de06ce11788d

added KMZ query results writer and factory
author Babis Nikolaou <charnik@di.uoa.gr>
date Mon Jun 25 21:22:31 2012 +0300 (2012-06-25)
parents d6319e10dbcc
children e3ed13f91924
files resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriter.java resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java 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/sparqlkml/stSPARQLResultsKMZWriter.java	Mon Jun 25 21:22:31 2012 +0300
     1.3 @@ -0,0 +1,92 @@
     1.4 +package org.openrdf.query.resultio.sparqlkml;
     1.5 +
     1.6 +import java.io.IOException;
     1.7 +import java.io.OutputStream;
     1.8 +import java.util.List;
     1.9 +import java.util.zip.ZipEntry;
    1.10 +import java.util.zip.ZipOutputStream;
    1.11 +
    1.12 +import org.openrdf.query.BindingSet;
    1.13 +import org.openrdf.query.TupleQueryResultHandlerException;
    1.14 +import org.openrdf.query.resultio.TupleQueryResultFormat;
    1.15 +import org.openrdf.query.resultio.TupleQueryResultWriter;
    1.16 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    1.17 +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriter;
    1.18 +
    1.19 +/**
    1.20 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    1.21 + *
    1.22 + */
    1.23 +public class stSPARQLResultsKMZWriter implements TupleQueryResultWriter {
    1.24 +
    1.25 +	private static final String ZIP_ENTRY_FILENAME = "sparql_results.kml";
    1.26 +	/**
    1.27 +	 * After all a KMZ file is a zipped KML one
    1.28 +	 */
    1.29 +	private stSPARQLResultsKMLWriter kmlWriter;
    1.30 +	
    1.31 +	/**
    1.32 +	 *  The zipped output stream to wrap the original one
    1.33 +	 */
    1.34 +	private ZipOutputStream kmzout;
    1.35 +	
    1.36 +	/**
    1.37 +	 * The zip entry
    1.38 +	 */
    1.39 +	private ZipEntry entry;
    1.40 +	
    1.41 +	public stSPARQLResultsKMZWriter(OutputStream out) {
    1.42 +		// create a zip stream on the given output stream
    1.43 +		kmzout = new ZipOutputStream(out);
    1.44 +		
    1.45 +		// initialize the KMLWriter with that stream instead passing the original
    1.46 +		kmlWriter = new stSPARQLResultsKMLWriter(kmzout);
    1.47 +	}
    1.48 +
    1.49 +	@Override
    1.50 +	public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    1.51 +		try {
    1.52 +			// create a zip entry
    1.53 +			entry = new ZipEntry(ZIP_ENTRY_FILENAME);
    1.54 +			
    1.55 +			// add the zip entry in it
    1.56 +			kmzout.putNextEntry(entry);
    1.57 +			
    1.58 +			// now pass execution to KMLWriter
    1.59 +			kmlWriter.startQueryResult(bindingNames);
    1.60 +			
    1.61 +		} catch (IOException e) {
    1.62 +			throw new TupleQueryResultHandlerException(e);
    1.63 +		}
    1.64 +	}
    1.65 +
    1.66 +	@Override
    1.67 +	public void endQueryResult() throws TupleQueryResultHandlerException {
    1.68 +
    1.69 +		try {
    1.70 +			// pass execution to KMLWriter
    1.71 +			kmlWriter.endQueryResult();
    1.72 +			
    1.73 +			// close the zip entry
    1.74 +			kmzout.closeEntry();
    1.75 +			
    1.76 +			// close the zip stream
    1.77 +			kmzout.close();
    1.78 +			
    1.79 +		} catch (IOException e) {
    1.80 +			throw new TupleQueryResultHandlerException(e);
    1.81 +		}
    1.82 +	}
    1.83 +
    1.84 +	@Override
    1.85 +	public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    1.86 +		// pass execution to KMLWriter
    1.87 +		kmlWriter.handleSolution(bindingSet);
    1.88 +	}
    1.89 +
    1.90 +	@Override
    1.91 +	public TupleQueryResultFormat getTupleQueryResultFormat() {
    1.92 +		return stSPARQLQueryResultFormat.KMZ;
    1.93 +	}
    1.94 +
    1.95 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java	Mon Jun 25 21:22:31 2012 +0300
     2.3 @@ -0,0 +1,26 @@
     2.4 +package org.openrdf.query.resultio.sparqlkml;
     2.5 +
     2.6 +import java.io.OutputStream;
     2.7 +
     2.8 +import org.openrdf.query.resultio.TupleQueryResultFormat;
     2.9 +import org.openrdf.query.resultio.TupleQueryResultWriter;
    2.10 +import org.openrdf.query.resultio.TupleQueryResultWriterFactory;
    2.11 +import org.openrdf.query.resultio.stSPARQLQueryResultFormat;
    2.12 +
    2.13 +/**
    2.14 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    2.15 + *
    2.16 + */
    2.17 +public class stSPARQLResultsKMZWriterFactory implements TupleQueryResultWriterFactory {
    2.18 +
    2.19 +	@Override
    2.20 +	public TupleQueryResultFormat getTupleQueryResultFormat() {
    2.21 +		return stSPARQLQueryResultFormat.KMZ;
    2.22 +	}
    2.23 +
    2.24 +	@Override
    2.25 +	public TupleQueryResultWriter getWriter(OutputStream out) {
    2.26 +		return new stSPARQLResultsKMZWriter(out);
    2.27 +	}
    2.28 +
    2.29 +}
     3.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java	Mon Jun 25 20:58:45 2012 +0300
     3.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java	Mon Jun 25 21:22:31 2012 +0300
     3.3 @@ -1,12 +1,10 @@
     3.4 -/**
     3.5 - * 
     3.6 - */
     3.7  package org.openrdf.query.resultio;
     3.8  
     3.9  import java.io.OutputStream;
    3.10  
    3.11  import org.openrdf.query.resultio.sparqlgeojson.stSPARQLResultsGeoJSONWriterFactory;
    3.12  import org.openrdf.query.resultio.sparqlhtml.stSPARQLResultsHTMLWriterFactory;
    3.13 +import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMZWriterFactory;
    3.14  import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsKMLWriterFactory;
    3.15  import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriterFactory;
    3.16  import org.openrdf.query.resultio.text.stSPARQLResultsTSVWriterFactory;
    3.17 @@ -23,6 +21,7 @@
    3.18  	private static TupleQueryResultWriterFactory html = new stSPARQLResultsHTMLWriterFactory();
    3.19  	private static TupleQueryResultWriterFactory xml = new stSPARQLResultsXMLWriterFactory();
    3.20  	private static TupleQueryResultWriterFactory kml = new stSPARQLResultsKMLWriterFactory();
    3.21 +	private static TupleQueryResultWriterFactory kmz = new stSPARQLResultsKMZWriterFactory();
    3.22  	private static TupleQueryResultWriterFactory tsv = new stSPARQLResultsTSVWriterFactory();
    3.23  	private static TupleQueryResultWriterFactory geojson = new stSPARQLResultsGeoJSONWriterFactory();
    3.24  	
    3.25 @@ -43,7 +42,7 @@
    3.26  				break;
    3.27  				
    3.28  			case KMZ:
    3.29 -				writer = kml.getWriter(out);
    3.30 +				writer = kmz.getWriter(out);
    3.31  				break;
    3.32  				
    3.33  			case GEOJSON:
     4.1 --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 20:58:45 2012 +0300
     4.2 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java	Mon Jun 25 21:22:31 2012 +0300
     4.3 @@ -248,26 +248,6 @@
     4.4  				//return new long[]{t2-t1, t3-t2, t3-t1, results};
     4.5  				break;
     4.6  				
     4.7 -			case KMZ:
     4.8 -				// create a zip entry
     4.9 -				ZipEntry entry = new ZipEntry("sparql_results.kml");
    4.10 -				
    4.11 -				// create a zip stream on standard output
    4.12 -				ZipOutputStream kmzout = new ZipOutputStream(out);
    4.13 -				
    4.14 -				// add the zip entry in it
    4.15 -				kmzout.putNextEntry(entry);
    4.16 -				
    4.17 -				// pass the zip stream for evaluation
    4.18 -				tupleQuery.evaluate(new stSPARQLResultsKMLWriterFactory().getWriter(kmzout));
    4.19 -
    4.20 -				// close the zip entry
    4.21 -				kmzout.closeEntry();
    4.22 -				
    4.23 -				// close the zip stream
    4.24 -				kmzout.close();
    4.25 -				break;
    4.26 -			
    4.27  		default:
    4.28  			// get the writer for the specified format
    4.29  			TupleQueryResultWriter resultWriter = stSPARQLQueryResultWriterFactory.createstSPARQLQueryResultWriter(resultsFormat, out);