# HG changeset patch # User Babis Nikolaou # Date 1352222683 -7200 # Node ID 9a4bb8e81273bf792dea2f696482a44980759055 # Parent 706b99be90785557602c87e362d0092185dc6199 split queryresultio-spatial into submodules (one for API that provides the stSPARQLQueryResultFormat and one for each writer implementation, e.g., XML, HTML, etc.) Renamed resultio to resultio-spatial. Renamed endpoint-client artifact to strabon-endpoint-client. Moved org.openrdf.resultio.Format to eu.earthobservatory.utils. Renamed and moved org.openrdf.resultio.stSPARQLQueryResultWriter to eu.earthobservatory.utils.stSPARQLQueryResultToFormatAdapter. This is a factory class for stSPARQLQueryResults. NOTICE: You should import the whole Strabon project into Eclipse to work with this commit diff -r 706b99be9078 -r 9a4bb8e81273 ChangeLog --- a/ChangeLog Mon Nov 05 20:40:17 2012 +0200 +++ b/ChangeLog Tue Nov 06 19:24:43 2012 +0200 @@ -2,6 +2,11 @@ * Version 3.2.5 released. + * Implemented a Java client for Strabon Endpoint. The client should be + used only with endpoint versions >=3.2.5. The implementation may be + found int the `endpoint-client' submodule of maven. Currently, only + querying of Strabon Endpoints is supported. + * Added support for requesting the capabilities of Strabon Endpoint (fixes Bug #20 ). See changesets f840796400bf and ?? for specific details and how you diff -r 706b99be9078 -r 9a4bb8e81273 endpoint-client/pom.xml --- a/endpoint-client/pom.xml Mon Nov 05 20:40:17 2012 +0200 +++ b/endpoint-client/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -8,7 +8,7 @@ 3.2.5-SNAPSHOT - endpoint-client + strabon-endpoint-client Strabon: Endpoint client A java client for Strabon endpoint @@ -17,53 +17,18 @@ org.openrdf.sesame - sesame-queryresultio-spatial - - - + --> commons-httpclient commons-httpclient diff -r 706b99be9078 -r 9a4bb8e81273 endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Mon Nov 05 20:40:17 2012 +0200 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Tue Nov 06 19:24:43 2012 +0200 @@ -9,6 +9,8 @@ */ package eu.earthobservatory.org.StrabonEndpoint; +import eu.earthobservatory.utils.Format; + import java.io.IOException; import java.io.OutputStream; import java.io.StringReader; @@ -23,7 +25,6 @@ import org.openrdf.query.MalformedQueryException; import org.openrdf.query.QueryEvaluationException; import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.resultio.Format; import org.openrdf.repository.RepositoryException; import org.openrdf.repository.sail.SailRepositoryConnection; import org.openrdf.rio.RDFFormat; diff -r 706b99be9078 -r 9a4bb8e81273 evaluation/pom.xml --- a/evaluation/pom.xml Mon Nov 05 20:40:17 2012 +0200 +++ b/evaluation/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -10,7 +10,7 @@ org.openrdf.sesame sesame-queryalgebra-evaluation-spatial - OpenRDF Sesame: Query algebra - evaluation -spatial + OpenRDF Sesame: Query algebra - evaluation - spatial Spatial Extension Functions - stSPARQL jar diff -r 706b99be9078 -r 9a4bb8e81273 pom.xml --- a/pom.xml Mon Nov 05 20:40:17 2012 +0200 +++ b/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -24,13 +24,13 @@ postgis monetdb generaldb - resultio + resultio-spatial endpoint endpoint-client - 3.2.5-SNAPSHOT + 3.2.5-SNAPSHOT 2.6.3 3.5.0 @@ -54,11 +54,57 @@ ${eu.earthobservatory.version} + + org.openrdf.sesame - sesame-queryresultio-spatial + sesame-queryresultio-spatial-api ${eu.earthobservatory.version} + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlgeojson + ${eu.earthobservatory.version} + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlhtml + ${eu.earthobservatory.version} + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlkml + ${eu.earthobservatory.version} + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlxml + ${eu.earthobservatory.version} + + + org.openrdf.sesame + sesame-queryresultio-spatial-text + ${eu.earthobservatory.version} + + + + + + org.openrdf.sesame + sesame-queryresultio-api + ${sesame.version} + + + org.openrdf.sesame + sesame-queryresultio-sparqlxml + ${sesame.version} + + + org.openrdf.sesame + sesame-queryresultio-text + ${sesame.version} + + org.openrdf.sesame @@ -210,6 +256,11 @@ org.openrdf.sesame + sesame-rio-api + ${sesame.version} + + + org.openrdf.sesame sesame-rio-rdfxml ${sesame.version} @@ -496,16 +547,16 @@ 2.2 - - org.jvnet.ogc - ogc-tools-gml-jts - 1.0.2 - - - com.sun.xml.bind - jaxb-impl - 2.1.1 - + + org.jvnet.ogc + ogc-tools-gml-jts + 1.0.2 + + + com.sun.xml.bind + jaxb-impl + 2.1.1 + javax diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,22 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-api + + OpenRDF Sesame: Spatial Query result IO - API + Spatial Query result IO API + + + + org.openrdf.sesame + sesame-queryresultio-api + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,143 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +/** + * Represents the concept of an tuple query result serialization format for + * stSPARQL/GeoSPARQL. Tuple query result formats are identified by a + * {@link #getName() name} and can have one or more associated MIME types, + * zero or more associated file extensions and can specify a (default) + * character encoding. + * + * In contrast to formats mentioned in class {@link #TupleQueryResultFormat}, + * stSPARQL/GeoSPARQL formats do not adhere to any specification for SPARQL + * except for those that are extension of the respective formats in class + * {@link #TupleQueryResultFormat}, such as TSV. + * For example, the projected variables in a stSPARQL/GeoSPARQL query are + * not included in the beginning of these formats. Instead, they are provided + * as an additional description for a feature (e.g., a tuple query result with + * a projected variable corresponding to a geometry). + * + * @author Charalampos Nikolaou + * + */ +public class stSPARQLQueryResultFormat extends TupleQueryResultFormat { + + /** + * XML format (extension of {@link TupleQueryResultFormat#SPARQL} format to include geometries) + */ + public static final stSPARQLQueryResultFormat XML = new stSPARQLQueryResultFormat("XML", + Arrays.asList("application/sparql-results+xml", "application/xml"), Charset.forName("UTF-8"), Arrays.asList("xml")); + + /** + * KML format (see http://www.opengeospatial.org/standards/kml/) + */ + public static final stSPARQLQueryResultFormat KML = new stSPARQLQueryResultFormat("KML", + Arrays.asList("application/vnd.google-earth.kml+xml", "application/kml"), Charset.forName("UTF-8"), Arrays.asList("kml")); + + /** + * KMZ format (a zipped KML content) + */ + public static final stSPARQLQueryResultFormat KMZ = new stSPARQLQueryResultFormat("KMZ", + Arrays.asList("application/vnd.google-earth.kmz", "application/kmz"), Charset.forName("UTF-8"), Arrays.asList("kmz")); + + /** + * GeoJSON format (see http://www.geojson.org/geojson-spec.html) + */ + public static final stSPARQLQueryResultFormat GEOJSON = new stSPARQLQueryResultFormat("GeoJSON", + Arrays.asList("application/json", "application/geojson"), Charset.forName("UTF-8"), Arrays.asList("json")); + + /** + * Tab separated value format (extension of {@link TupleQueryResultFormat#TSV} format to include geometries) + */ + public static final stSPARQLQueryResultFormat TSV = new stSPARQLQueryResultFormat("TSV", + Arrays.asList("text/tab-separated-values"), Charset.forName("UTF-8"), Arrays.asList("tsv")); + + /** + * HTML format (encoded as an HTML table, without the <TABLE> tag) + */ + public static final stSPARQLQueryResultFormat HTML = new stSPARQLQueryResultFormat("HTML", + Arrays.asList("text/html"), Charset.forName("UTF-8"), Arrays.asList("html", "htm")); + + /** + * The available stSPARQLQuery Result Formats + */ + private static final List VALUES = new ArrayList(6); + + // registers stSPARQL/GeoSPARQL formats + static { + register(XML); + register(KML); + register(KMZ); + register(GEOJSON); + register(TSV); + register(HTML); + } + + /** + * Register the specified stSPARQLQueryResultFormat. + * + * @param format + */ + public static void register(stSPARQLQueryResultFormat format) { + TupleQueryResultFormat.register(format); + VALUES.add(format); + } + + /** + * Gets the stSPARQLQueryResultFormat given its name. + * + * @param formatName + * @return + */ + public static stSPARQLQueryResultFormat valueOf(String formatName) { + for (TupleQueryResultFormat format : values()) { + if (format instanceof stSPARQLQueryResultFormat && + format.getName().equalsIgnoreCase(formatName)) { + return (stSPARQLQueryResultFormat) format; + } + } + + return null; + } + + /** + * Returns all known/registered tuple query result formats. + */ + public static Collection values() { + return TupleQueryResultFormat.values(); + } + + public static stSPARQLQueryResultFormat forMIMEType(String mimeType) { + return forMIMEType(mimeType, null); + } + + public static stSPARQLQueryResultFormat forMIMEType(String mimeType, stSPARQLQueryResultFormat fallback) { + return matchMIMEType(mimeType, VALUES, fallback); + } + + public stSPARQLQueryResultFormat(String name, String mimeType, String fileExt) { + super(name, mimeType, fileExt); + } + + public stSPARQLQueryResultFormat(String name, String mimeType, Charset charset, String fileExt) { + super(name, mimeType, charset, fileExt); + } + + public stSPARQLQueryResultFormat(String name, Collection mimeTypes, Charset charset, Collection fileExtensions) { + super(name, mimeTypes, charset, fileExtensions); + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParser.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,64 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import java.io.IOException; +import java.io.InputStream; + +import org.openrdf.model.ValueFactory; +import org.openrdf.query.TupleQueryResultHandler; +import org.openrdf.query.TupleQueryResultHandlerException; + +/** + * A general interface for stSPARQL tuple query result parsers. + * + * @see {@link TupleQueryResultParser} + * + * @author Charalampos Nikolaou + */ +public interface stSPARQLQueryResultParser { + + /** + * Gets the query result format that this parser can parse. + */ + public stSPARQLQueryResultFormat getTupleQueryResultFormat(); + + /** + * Sets the ValueFactory that the parser will use to create Value objects for + * the parsed query result. + * + * @param valueFactory + * The value factory that the parser should use. + */ + public void setValueFactory(ValueFactory valueFactory); + + /** + * Sets the TupleQueryResultHandler that will handle the parsed query result + * data. + */ + public void setTupleQueryResultHandler(TupleQueryResultHandler handler); + + /** + * Parses the data from the supplied InputStream. + * + * @param in + * The InputStream from which to read the data. + * @throws IOException + * If an I/O error occurred while data was read from the InputStream. + * @throws QueryResultParseException + * If the parser has encountered an unrecoverable parse error. + * @throws TupleQueryResultHandlerException + * If the configured query result handler has encountered an + * unrecoverable error. + */ + public void parse(InputStream in) + throws IOException, QueryResultParseException, TupleQueryResultHandlerException; + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserBase.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserBase.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,71 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import org.openrdf.model.ValueFactory; +import org.openrdf.model.impl.ValueFactoryImpl; +import org.openrdf.query.TupleQueryResultHandler; + +/** + * Base class for {@link stSPARQLQueryResultParser}s offering common functionality for + * query result parsers. + * + * @author Charalampos Nikolaou + * + */ +public abstract class stSPARQLQueryResultParserBase implements stSPARQLQueryResultParser { + + /*-----------* + * Variables * + *-----------*/ + + /** + * The ValueFactory to use for creating RDF model objects. + */ + protected ValueFactory valueFactory; + + /** + * The TupleQueryResultHandler that will handle the parsed query results. + */ + protected TupleQueryResultHandler handler; + + /*--------------* + * Constructors * + *--------------*/ + + /** + * Creates a new parser base that, by default, will use an instance of + * {@link ValueFactoryImpl} to create Value objects. + */ + public stSPARQLQueryResultParserBase() { + this(new ValueFactoryImpl()); + } + + /** + * Creates a new parser base that will use the supplied ValueFactory to + * create Value objects. + */ + public stSPARQLQueryResultParserBase(ValueFactory valueFactory) { + setValueFactory(valueFactory); + } + + /*---------* + * Methods * + *---------*/ + + public void setValueFactory(ValueFactory valueFactory) { + this.valueFactory = valueFactory; + } + + public void setTupleQueryResultHandler(TupleQueryResultHandler handler) { + this.handler = handler; + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,32 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +/** + * A stSPARQLQueryResultParserFactory returns {@link stSPARQLQueryResultParser}s for + * a specific tuple query result format. + * + * @see {@link TupleQueryResultParserFactory} + * + * @author Charalampos Nikolaou + * + */ +public interface stSPARQLQueryResultParserFactory { + /** + * Returns the tuple query result format for this factory. + */ + public stSPARQLQueryResultFormat getTupleQueryResultFormat(); + + /** + * Returns a TupleQueryResultParser instance. + */ + public stSPARQLQueryResultParser getParser(); + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserRegistry.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultParserRegistry.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,47 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import info.aduna.lang.service.FileFormatServiceRegistry; + +/** + * A registry that keeps track of the available + * {@link stSPARQLQueryResultParserFactory}s. + * + * @author Charalampos Nikolaou + * + */ +public class stSPARQLQueryResultParserRegistry extends FileFormatServiceRegistry { + + private static stSPARQLQueryResultParserRegistry defaultRegistry; + + /** + * Gets the default stSPARQLQueryResultParserRegistry. + * + * @return The default registry. + */ + public static synchronized stSPARQLQueryResultParserRegistry getInstance() { + if (defaultRegistry == null) { + defaultRegistry = new stSPARQLQueryResultParserRegistry(); + } + + return defaultRegistry; + } + + public stSPARQLQueryResultParserRegistry() { + super(stSPARQLQueryResultParserFactory.class); + } + + @Override + protected stSPARQLQueryResultFormat getKey(stSPARQLQueryResultParserFactory factory) { + return factory.getTupleQueryResultFormat(); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,26 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import org.openrdf.query.TupleQueryResultHandler; + +/** + * The interface of objects that writer query results in a specific query result + * format. + * + * @author Charalampos Nikolaou + */ +public interface stSPARQLQueryResultWriter extends TupleQueryResultHandler { + + /** + * Gets the query result format that this writer uses. + */ + public stSPARQLQueryResultFormat getTupleQueryResultFormat(); +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,38 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import java.io.OutputStream; + +/** + * Returns {@link stSPARQLQueryResultWriter}s for a specific tuple query result + * format. + * + * @see {@link TupleQueryResultWriterFactory} + * + * @author Charalampos Nikolaou + */ +public interface stSPARQLQueryResultWriterFactory { + + /** + * Returns the tuple query result format for this factory. + */ + public stSPARQLQueryResultFormat getTupleQueryResultFormat(); + + /** + * Returns a stSPARQLQueryResultWriter instance that will write to the supplied + * output stream. + * + * @param out + * The OutputStream to write the result to. + */ + public stSPARQLQueryResultWriter getWriter(OutputStream out); + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterRegistry.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/api/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterRegistry.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,47 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio; + +import info.aduna.lang.service.FileFormatServiceRegistry; + +/** + * A registry that keeps track of the available + * {@link stSPARQLQueryResultWriterFactory}s. + * + * @see {@link TupleQueryResultWriterRegistry} + * + * @author Charalampos Nikolaou + */ +public class stSPARQLQueryResultWriterRegistry extends FileFormatServiceRegistry { + + private static stSPARQLQueryResultWriterRegistry defaultRegistry; + + /** + * Gets the default stSPARQLQueryResultWriterRegistry. + * + * @return The default registry. + */ + public static synchronized stSPARQLQueryResultWriterRegistry getInstance() { + if (defaultRegistry == null) { + defaultRegistry = new stSPARQLQueryResultWriterRegistry(); + } + + return defaultRegistry; + } + + public stSPARQLQueryResultWriterRegistry() { + super(stSPARQLQueryResultWriterFactory.class); + } + + @Override + protected stSPARQLQueryResultFormat getKey(stSPARQLQueryResultWriterFactory factory) { + return factory.getTupleQueryResultFormat(); + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,119 @@ + + + 4.0.0 + + + eu.earthobservatory + strabon + 3.2.5-SNAPSHOT + + + org.openrdf.sesame + sesame-queryresultio-spatial + OpenRDF Sesame: Spatial Query result IO + Minimal spatial extensions of Query result parser and writer implementation for the SPARQL Query Results Format. + pom + + + api + sparqlgeojson + sparqlhtml + sparqlkml + sparqlxml + text + + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlgeojson/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlgeojson/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,76 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-sparqlgeojson + + OpenRDF Sesame: Spatial Query result IO - GeoJSON + Spatial Query result IO API + + + + + org.openrdf.sesame + sesame-queryalgebra-evaluation-spatial + + + org.openrdf.sesame + sesame-sail-generaldb + + + + org.openrdf.sesame + sesame-queryresultio-spatial-api + + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + org.geotools + gt-geojson + + + + + org.geotools + gt-xml + + + + org.geotools + gt-epsg-hsql + + + + org.geotools + gt-shapefile + + + + org.geotools + gt-opengis + + + com.vividsolutions + jts + jar + compile + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlgeojson/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlgeojson/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,236 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlgeojson; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +import org.geotools.data.simple.SimpleFeatureCollection; +import org.geotools.feature.FeatureCollections; +import org.geotools.feature.simple.SimpleFeatureBuilder; +import org.geotools.feature.simple.SimpleFeatureTypeBuilder; +import org.geotools.geojson.feature.FeatureJSON; +import org.geotools.referencing.CRS; +import org.opengis.feature.simple.SimpleFeature; +import org.opengis.feature.simple.SimpleFeatureType; +import org.openrdf.model.Literal; +import org.openrdf.model.Value; +import org.openrdf.query.Binding; +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.algebra.evaluation.function.spatial.WKTHelper; +import org.openrdf.query.algebra.evaluation.util.JTSWrapper; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; +import org.openrdf.sail.generaldb.model.XMLGSDatatypeUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.vividsolutions.jts.geom.Geometry; + +/** + * A TupleQueryResultWriter that writes query results in the GeoJSON Format. + * + * @author Manos Karpathiotakis + * @author Charalampos Nikolaou + */ +public class stSPARQLResultsGeoJSONWriter implements TupleQueryResultWriter { + + private static final Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.sparqlgeojson.stSPARQLResultsGeoJSONWriter.class); + + /** + * The underlying output stream to write + */ + private OutputStream out; + + /** + * Set a Feature Collection + */ + private SimpleFeatureCollection sfCollection; + + /** + * The wrapper of JTS library + */ + private JTSWrapper jts; + + /** + * Keep track of the number of results + */ + private int nresults; + + /** + * The class to use for serializing to GeoJSON + */ + private FeatureJSON fjson; + + /** + * Keep track of the number of features + */ + private int nfeatures; + + public stSPARQLResultsGeoJSONWriter(OutputStream out) { + this.out = out; + + // set the feature collection + sfCollection = FeatureCollections.newCollection("geomOutput"); + + // get the instance of JTSWrapper + jts = JTSWrapper.getInstance(); + + // initialize results/features + nresults = 0; + nfeatures = 0; + } + + @Override + public void startQueryResult(List bindingNames) throws TupleQueryResultHandlerException { + fjson = new FeatureJSON(); + fjson.setEncodeFeatureCRS(true); + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + try { + fjson.writeFeatureCollection(sfCollection, out); + out.write("\n".getBytes(Charset.defaultCharset())); + + // write a warning when there are no features in the answer + if (nfeatures < nresults) { + logger.warn("[Strabon.GeoJSONWriter] No spatial binding found in the result, hence the result is empty eventhough query evaluation produced {} results. GeoJSON requires that at least one binding maps to a geometry.", nresults); + + } + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { + try { + nresults++; + + // list keeping binding names that are not binded to geometries + ArrayList properties = new ArrayList(); + + // list keeping values for binding names + ArrayList values = new ArrayList(); + + // list keeping the features of the result + ArrayList features = new ArrayList(); + + // list keeping the geometries of features + ArrayList geometries = new ArrayList(); + + // parse binding set + for (Binding binding : bindingSet) { + Value value = binding.getValue(); + + if (XMLGSDatatypeUtil.isGeometryValue(value)) { + // it's a spatial value + if (logger.isDebugEnabled()) { + logger.debug("[Strabon.GeoJSON] Found geometry: {}", value); + } + + nfeatures++; + + // we need the geometry and the SRID + Geometry geom = null; + int srid = -1; + + if (value instanceof GeneralDBPolyhedron) { + GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value; + + geom = dbpolyhedron.getPolyhedron().getGeometry(); + srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID(); + + } else { // spatial literal WKT or GML + // get the textual representation of the geometry (WKT or GML) + String geoText = value.stringValue(); + + if (XMLGSDatatypeUtil.isWKTLiteral((Literal) value)) {// WKT + // get its geometry + geom = jts.WKTread(WKTHelper.getWithoutSRID(geoText)); + + // get its SRID + srid = WKTHelper.getSRID(geoText); + + } else { // GML + // get its geometry + geom = jts.GMLread(geoText); + + // get its SRID + srid = geom.getSRID(); + + } + } + + SimpleFeatureTypeBuilder sftb = new SimpleFeatureTypeBuilder(); + sftb.setName("Feature_" + nresults + "_" + nfeatures); + sftb.setCRS(CRS.decode("EPSG:" + srid)); + sftb.setSRS("EPSG:" + srid); + sftb.add("geometry", Geometry.class); + + // add the feature in the list of features + features.add(sftb); + + // add the geometry of the feature in the list of geometries + geometries.add(geom); + + } else { // URI, BlankNode, or Literal other than geometry + if (logger.isDebugEnabled()) { + logger.debug("[Strabon.GeoJSON] Found resource: {}", value); + } + + properties.add(binding.getName()); + values.add(value); + } + } + + // construct the feature of the result + for (int i = 0; i < features.size(); i++) { + SimpleFeatureTypeBuilder sftb = features.get(i); + + // add the properties + for (int p = 0; p < properties.size(); p++) { + sftb.add(properties.get(p), String.class); + } + + SimpleFeatureType featureType = sftb.buildFeatureType(); + SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); + + // add the geometry to the builder of the feature + featureBuilder.add(geometries.get(i)); + + // add the values to the builder of the feature + for (int v = 0; v < values.size(); v++) { + featureBuilder.add(values.get(v)); + } + + SimpleFeature feature = featureBuilder.buildFeature(null); + sfCollection.add(feature); + } + + } catch (Exception e) { + throw new TupleQueryResultHandlerException(e); + } + + } + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.GEOJSON; + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlgeojson/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlgeojson/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlgeojson; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsGeoJSONWriterFactory implements TupleQueryResultWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.GEOJSON; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsGeoJSONWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlhtml/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlhtml/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,59 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-sparqlhtml + + OpenRDF Sesame: Spatial Query result IO - HTML + Spatial Query result IO API + + + + org.openrdf.sesame + sesame-queryresultio-spatial-api + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlxml + + + org.openrdf.sesame + sesame-queryalgebra-evaluation-spatial + + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlhtml/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlhtml/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,170 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlhtml; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.URLEncoder; +import java.util.List; + +import org.openrdf.model.BNode; +import org.openrdf.query.Binding; +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.query.resultio.sparqlxml.stSPARQLXMLWriter; +import org.openrdf.model.Value; +import org.openrdf.model.URI; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsHTMLWriter implements TupleQueryResultWriter { + + public static final String TABLE = "TABLE"; + public static final String TABLE_ROW_TAG = "TR"; + public static final String TABLE_HEADER_TAG = "TH"; + public static final String TABLE_DATA_TAG = "TD"; + public static final String LINK = "A"; + public static final String LINK_REF = "HREF"; + public static final String STYLE = "class"; + public static final String ID = "id"; + public static final String LINK_ID = "uri"; + public static final String TABLE_HEADER_CLASS = "query_results_header"; + public static final String TABLE_DATA_CLASS = "query_results_data"; + public static final String TABLE_CLASS = "query_results_table"; + public static final String MORE_LINK = "comment more"; + + /** + * The underlying XML formatter. + */ + private stSPARQLXMLWriter xmlWriter; + + /** + * The ordered list of binding names of the result. + */ + private List bindingNames; + + public stSPARQLResultsHTMLWriter(OutputStream out) { + this(new stSPARQLXMLWriter(out)); + } + + public stSPARQLResultsHTMLWriter(stSPARQLXMLWriter writer) { + xmlWriter = writer; + xmlWriter.setPrettyPrint(true); + } + + @Override + public void startQueryResult(List bindingNames) + throws TupleQueryResultHandlerException { + + try { + // keep the order of binding names + this.bindingNames = bindingNames; + // set style for table + xmlWriter.setAttribute(STYLE, TABLE_CLASS); + // write start of table + xmlWriter.startTag(TABLE); + // write Table header containing the bindings + xmlWriter.startTag(TABLE_ROW_TAG); + for (String bindingName: bindingNames) { + // set style for header + xmlWriter.setAttribute(STYLE, TABLE_HEADER_CLASS); + xmlWriter.textElement(TABLE_HEADER_TAG, bindingName); + } + + xmlWriter.endTag(TABLE_ROW_TAG); + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + try { + + // write end of table + xmlWriter.endTag(TABLE); + + // needed to flush data + xmlWriter.endDocument(); + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { + try { + StringBuilder value = new StringBuilder(); + Value boundValue = null; + + xmlWriter.startTag(TABLE_ROW_TAG); + for (String bindingName : bindingNames) { + Binding binding = bindingSet.getBinding(bindingName); + if(binding != null) + { + boundValue = binding.getValue(); + value.append(boundValue.stringValue()); + + if(boundValue instanceof BNode) { + value.insert(0, "_:"); + } + + // If the value is a uri, make it link + if(boundValue instanceof URI) + { + xmlWriter.setAttribute(STYLE, TABLE_DATA_CLASS); + xmlWriter.startTag(TABLE_DATA_TAG); + + // select all the triples that contain the boundValue + String query= "select * " + + "where " + + "{ " + + "?subject ?predicate ?object . "+ + "FILTER((?subject = <"+ boundValue.toString()+ ">) || "+ + "(?predicate = <"+ boundValue.toString()+ ">) || "+ + "(?object = <"+ boundValue.toString()+ ">)) " + + "}"; + + String href = "Browse?view=HTML&query="+URLEncoder.encode(query, "UTF-8")+"&format=HTML&resource="+boundValue.toString(); + xmlWriter.setAttribute(LINK_REF, href); + xmlWriter.startTag(LINK); + xmlWriter.text(boundValue.toString()); + xmlWriter.endTag(LINK); + } + else + { + xmlWriter.setAttribute(STYLE, TABLE_DATA_CLASS+" "+MORE_LINK); + xmlWriter.startTag(TABLE_DATA_TAG); + xmlWriter.text(boundValue.toString()); + } + xmlWriter.endTag(TABLE_DATA_TAG); + } + value.setLength(0); + } + xmlWriter.endTag(TABLE_ROW_TAG); + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.HTML; + } + +} \ No newline at end of file diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlhtml/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlhtml/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlhtml; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsHTMLWriterFactory implements TupleQueryResultWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.HTML; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsHTMLWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlkml/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlkml/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,69 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-sparqlkml + + OpenRDF Sesame: Spatial Query result IO - KML/KMZ + Spatial Query result IO API + + + + org.openrdf.sesame + sesame-queryresultio-spatial-api + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlxml + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + org.geotools.xsd + gt-xsd-kml + + + + org.openrdf.sesame + sesame-queryalgebra-evaluation-spatial + + + org.openrdf.sesame + sesame-sail-generaldb + + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,522 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. Copyright (C) 2010, 2011, 2012, + * Pyravlos Team http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlkml; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; + +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; + +import org.geotools.kml.KML; +import org.geotools.kml.KMLConfiguration; +import org.geotools.xml.Encoder; +import org.openrdf.model.BNode; +import org.openrdf.model.Literal; +import org.openrdf.model.Value; +import org.openrdf.query.Binding; +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants; +import org.openrdf.query.algebra.evaluation.function.spatial.WKTHelper; +import org.openrdf.query.algebra.evaluation.util.JTSWrapper; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.query.resultio.sparqlxml.stSPARQLXMLWriter; +import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; +import org.openrdf.sail.generaldb.model.XMLGSDatatypeUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.GeometryCollection; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.MultiPoint; +import com.vividsolutions.jts.geom.MultiPolygon; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; +import com.vividsolutions.jts.io.ParseException; + +/** + * @author Manos Karpathiotakis + * @author Charalampos Nikolaou + * @author Panayiotis Smeros + * + */ +public class stSPARQLResultsKMLWriter implements TupleQueryResultWriter { + private static final Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter.class); + + // KML tags/attributes + private static final String ROOT_TAG = "kml"; + private static final String NAMESPACE = "http://www.opengis.net/kml/2.2"; + private static final String RESULT_SET_TAG = "Folder"; + private static final String PLACEMARK_TAG = "Placemark"; + private static final String NAME_TAG = "name"; + private static final String DESC_TAG = "description"; + private static final String STYLE_TAG = "Style"; + private static final String STYLEMAP_TAG = "StyleMap"; + private static final String LINESTYLE_TAG = "LineStyle"; + private static final String POLYSTYLE_TAG = "PolyStyle"; + private static final String EXT_DATA_TAG = "ExtendedData"; + private static final String DATA_TAG = "Data"; + private static final String VALUE_TAG = "value"; + private static final String NAME_ATTR = NAME_TAG; + + private static final String STYLE_ID = "resultStyle"; + private static final String TABLE_ROW_BEGIN = ""; + private static final String TABLE_ROW_END = ""; + private static final String TABLE_DATA_BEGIN = ""; + private static final String TABLE_DATA_END = ""; + private static final String NEWLINE = "\n"; + private static final String TABLE_DESC_BEGIN = ""+ NEWLINE; + private static final String TABLE_DESC_END = "]]>" + NEWLINE; + + private static final String GEOMETRY_NAME = "Geometry"; + private static final String MULTIGEOMETRY = "MultiGeometry"; + + // Styling options + private static final int numOfStyles = 5; + private static final String[][] styles = { + // note that colors are encoded as "aabbggrr" strings where + // aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); + // rr=red + // (00 to ff). + // id, line width, line color, polygon fill, mouse over line width, + // mouse over line color mouse over polygon fill + // {STYLE_ID + "1", "1.5", "7d0000ff", "ad0000ff", "1.5", + // "7d0000ff", "ad0000ff"}, {STYLE_ID + "2", "1.5", "7d0000ff", + // "ad0000ff", "1.5", "7d0000ff", "ad0000ff"}, {STYLE_ID + "3", + // "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", "ad0000ff"}, + // {STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", + // "7d0000ff", "ad0000ff"}, {STYLE_ID + "5", "1.5", "7d000055", + // "ad000055", "1.5", "7d0000ff", "ad0000ff"}}; + { STYLE_ID + "1", "1.5", "000000ff", "000000ff", "1.5", "000000ff", + "000000ff" }, + { STYLE_ID + "2", "1.5", "000000ff", "000000ff", "1.5", "000000ff", + "000000ff" }, + { STYLE_ID + "3", "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", + "ad0000ff" }, + { STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", "7d0000ff", + "ad0000ff" }, + { STYLE_ID + "5", "1.5", "7dff0000", "adff0000", "1.5", "7dff0000", + "adff0000" } }; + + /** + * The underlying XML formatter. + */ + private stSPARQLXMLWriter xmlWriter; + + /** + * The number of results seen. + */ + private int nresults; + + /** + * The number of geometries seen. + */ + private int ngeometries; + + /** + * The JTS wrapper + */ + private JTSWrapper jts; + + /** + * Stream for manipulating geometries + */ + private ByteArrayOutputStream baos; + + /** + * Description string holding the projected variables of the SPARQL query + */ + private StringBuilder descHeader; + + /** + * Description string holding the values for the projected variables of the + * SPARQL query + */ + private StringBuilder descData; + + /** + * Indentation used in tags that are constructed manually + */ + private int depth; + + /** + * Creates an stSPARQLResultsKMLWriter that encodes the SPARQL results in + * KML. + * + * @param out + */ + public stSPARQLResultsKMLWriter(OutputStream out) { + this(new stSPARQLXMLWriter(out)); + } + + public stSPARQLResultsKMLWriter(stSPARQLXMLWriter writer) { + xmlWriter = writer; + xmlWriter.setPrettyPrint(true); + depth = 4; + jts = JTSWrapper.getInstance(); + baos = new ByteArrayOutputStream(); + descHeader = new StringBuilder(); + descData = new StringBuilder(); + nresults = 0; + ngeometries = 0; + } + + @Override + public void startQueryResult(List bindingNames) + throws TupleQueryResultHandlerException { + try { + + xmlWriter.startDocument(); + xmlWriter.setAttribute("xmlns", NAMESPACE); + xmlWriter.startTag(ROOT_TAG); + xmlWriter.startTag(RESULT_SET_TAG); + + // add default styles + for (String[] style : styles) { + String id = style[0]; + String lineWidth = style[1]; + String lineColor = style[2]; + String polygonFill = style[3]; + String mouseOverLineWidth = style[4]; + String mouseOverLineColor = style[5]; + String mouseOverPolygonFill = style[6]; + + // append normal style + xmlWriter.setAttribute("id", "normal_" + id); + xmlWriter.startTag(STYLE_TAG); + xmlWriter.startTag(LINESTYLE_TAG); + xmlWriter.textElement("width", lineWidth); + xmlWriter.textElement("color", lineColor); + xmlWriter.endTag(LINESTYLE_TAG); + xmlWriter.startTag(POLYSTYLE_TAG); + xmlWriter.textElement("color", polygonFill); + xmlWriter.endTag(POLYSTYLE_TAG); + xmlWriter.endTag(STYLE_TAG); + + // append highlight style + xmlWriter.setAttribute("id", "highlight_" + id); + xmlWriter.startTag(STYLE_TAG); + xmlWriter.startTag(LINESTYLE_TAG); + xmlWriter.textElement("width", mouseOverLineWidth); + xmlWriter.textElement("color", mouseOverLineColor); + xmlWriter.endTag(LINESTYLE_TAG); + xmlWriter.startTag(POLYSTYLE_TAG); + xmlWriter.textElement("color", mouseOverPolygonFill); + xmlWriter.endTag(POLYSTYLE_TAG); + xmlWriter.endTag(STYLE_TAG); + + // define map style combining the above styles + xmlWriter.setAttribute("id", id); + xmlWriter.startTag(STYLEMAP_TAG); + xmlWriter.startTag("Pair"); + xmlWriter.textElement("key", "normal"); + xmlWriter.textElement("styleUrl", "#normal_" + id); + xmlWriter.endTag("Pair"); + xmlWriter.startTag("Pair"); + xmlWriter.textElement("key", "highlight"); + xmlWriter.textElement("styleUrl", "#highlight_" + id); + xmlWriter.endTag("Pair"); + xmlWriter.endTag(STYLEMAP_TAG); + } + // end of default style definition + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + try { + + xmlWriter.endTag(RESULT_SET_TAG); + xmlWriter.endTag(ROOT_TAG); + xmlWriter.endDocument(); + baos.close(); + + if (ngeometries < nresults) { + logger.warn("[Strabon.KMLWriter] No spatial binding found in the result. KML requires that at least one binding maps to a geometry.", nresults); + } + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { + try { + int numOfGeometries = 0; + + // true if there are bindings that do not correspond to geometries + boolean hasDesc = false; + + // increase result size + nresults++; + + // create description table and header + indent(descHeader, depth); + descHeader.append(TABLE_DESC_BEGIN); + indent(descHeader, depth); + + List geometries = new ArrayList(); + Hashtable extData = new Hashtable(); + + // parse binding set + for (Binding binding : bindingSet) { + + Value value = binding.getValue(); + + // check for geometry value + if (XMLGSDatatypeUtil.isGeometryValue(value)) { + numOfGeometries++; + ngeometries++; + if (logger.isDebugEnabled()) { + logger.debug("[Strabon] Found geometry: {}", value); + } + + geometries.add(getGeometry(value)); + + } else { // URI, BlankNode, or Literal other than spatial literal + if (logger.isDebugEnabled()) { + logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal ({}): {}", value.getClass(), value); + } + + // mark that we found sth corresponding to the description + hasDesc = true; + + // write description + writeDesc(binding); + + // fill also the extended data attribute of the Placemark + extData.put(binding.getName(), getBindingValue(binding)); + } + } + + if (numOfGeometries > 1) { + // write each polygon in separate placemarks + for (String geometry : geometries) { + xmlWriter.startTag(PLACEMARK_TAG); + xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME); + xmlWriter.textElement("styleUrl", "#"+ styles[geometries.indexOf(geometry) % (numOfStyles - 2)][0]); + xmlWriter.startTag(MULTIGEOMETRY); + xmlWriter.unescapedText(geometry); + xmlWriter.endTag(MULTIGEOMETRY); + xmlWriter.endTag(PLACEMARK_TAG); + } + } + + // also write them in the same placemarks + xmlWriter.startTag(PLACEMARK_TAG); + xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME); + xmlWriter.textElement("styleUrl", "#" + styles[(numOfStyles - 1)][0]); + xmlWriter.startTag(MULTIGEOMETRY); + + for (String geometry : geometries) { + xmlWriter.unescapedText(geometry); + } + + xmlWriter.endTag(MULTIGEOMETRY); + + // we have found and constructed a description for this result. + // Write it down. + if (hasDesc) { + // end the placeholder for the description data + indent(descData, depth); + + // append to the table header the actual content from + // the bindings + descHeader.append(descData); + + // close the table for the description + descHeader.append(NEWLINE); + indent(descHeader, depth); + descHeader.append(TABLE_DESC_END); + + // begin the "description" tag + xmlWriter.startTag(DESC_TAG); + + // write the actual description + xmlWriter.unescapedText(descHeader.toString()); + + // end the "description" tag + xmlWriter.endTag(DESC_TAG); + } + + // add the extended data + if (extData.size() > 0) { + xmlWriter.startTag(EXT_DATA_TAG); + for (String key : extData.keySet()) { + xmlWriter.setAttribute(NAME_ATTR, key); + xmlWriter.startTag(DATA_TAG); + xmlWriter.textElement(VALUE_TAG, extData.get(key)); + xmlWriter.endTag(DATA_TAG); + } + xmlWriter.endTag(EXT_DATA_TAG); + } + + // end Placemark + xmlWriter.endTag(PLACEMARK_TAG); + + // clear description string builders + descHeader.setLength(0); + descData.setLength(0); + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + private String getGeometry(Value value) { + String geometry = ""; + QName geometryType = null; + // the underlying geometry in value + Geometry geom = null; + // the underlying SRID of the geometry + int srid = -1; + // get the KML encoder + Encoder encoder = null; + try { + encoder = new Encoder(new KMLConfiguration()); + encoder.setIndenting(true); + if (value instanceof GeneralDBPolyhedron) { + GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value; + geom = dbpolyhedron.getPolyhedron().getGeometry(); + srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID(); + } else { // spatial literal + Literal spatial = (Literal) value; + String geomRep = spatial.stringValue(); + if (XMLGSDatatypeUtil.isWKTLiteral(spatial)) { // WKT + geom = jts.WKTread(WKTHelper.getWithoutSRID(geomRep)); + srid = WKTHelper.getSRID(geomRep); + } else { // GML + geom = jts.GMLread(geomRep); + srid = geom.getSRID(); + } + } + // transform the geometry to {@link GeoConstants#defaultSRID} + geom = jts.transform(geom, srid, GeoConstants.defaultSRID); + if (geom instanceof Point) { + geometryType = KML.Point; + } else if (geom instanceof Polygon) { + geometryType = KML.Polygon; + } else if (geom instanceof LineString) { + geometryType = KML.LineString; + } else if (geom instanceof MultiPoint) { + geometryType = KML.MultiGeometry; + } else if (geom instanceof MultiLineString) { + geometryType = KML.MultiGeometry; + } else if (geom instanceof MultiPolygon) { + geometryType = KML.MultiGeometry; + } else if (geom instanceof GeometryCollection) { + geometryType = KML.MultiGeometry; + } + if (geometryType == null) { + logger.warn("[Strabon.KMLWriter] Found unknown geometry type."); + + } else { + encoder.encode(geom, geometryType, baos); + geometry = baos.toString().substring(38).replaceAll(" xmlns:kml=\"http://earth.google.com/kml/2.1\"", "").replaceAll("kml:", ""); + + if (geometryType == KML.MultiGeometry) { + geometry = geometry.substring(geometry.indexOf("") + 15, geometry.indexOf("")); + } + + /* + * if(geom instanceof Point) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 8); } else if(geom instanceof + * Polygon) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 10); } else if(geom + * instanceof LineString) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 13); } else if(geom + * instanceof MultiPoint) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 13); } else if(geom + * instanceof MultiLineString) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 18); } else if(geom + * instanceof MultiPolygon) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 15); } else if(geom + * instanceof GeometryCollection) { geometry = + * geometry.substring(geometry.indexOf(""), + * geometry.indexOf("") + 21); } + */ + baos.reset(); + } + } catch (ParseException e) { + logger.error("[Strabon.KMLWriter] Parse error exception of geometry: {}", e.getMessage()); + + } catch (IOException e) { + logger.error("[Strabon.KMLWriter] IOException during KML encoding of geometry: {}", e.getMessage()); + + } catch (JAXBException e) { + logger.error("[Strabon.KMLWriter] Exception during GML parsing: {}", e.getMessage()); + } + + return geometry; + } + + /** + * Adds to the description table information for a binding. + * + * @param binding + */ + private void writeDesc(Binding binding) { + descData.append(NEWLINE); + indent(descData, depth + 1); + descData.append(TABLE_ROW_BEGIN); + descData.append(TABLE_DATA_BEGIN); + descData.append(binding.getName()); + descData.append(TABLE_DATA_END); + descData.append(TABLE_DATA_BEGIN); + if (binding.getValue() instanceof BNode) { + descData.append("_:"); + } + descData.append(binding.getValue().stringValue()); + descData.append(TABLE_DATA_END); + descData.append(TABLE_ROW_END); + } + + private String getBindingValue(Binding binding) { + String val = binding.getValue().stringValue(); + if (binding.getValue() instanceof BNode) { + val = "_:" + val; + } + + return val; + } + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.KML; + } + + /** + * Adds indentation to the given string builder according to the specified + * depth. + * + * @param sb + * @param depth + */ + private void indent(StringBuilder sb, int depth) { + for (int i = 0; i < depth; i++) { + sb.append(xmlWriter.getIndentString()); + } + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlkml; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsKMLWriterFactory implements TupleQueryResultWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.KML; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsKMLWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,106 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlkml; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsKMZWriter implements TupleQueryResultWriter { + + /** + * The name of the KML file that shall be zipped + * (by convention corresponds to "doc") + */ + private static final String ZIP_ENTRY_FILENAME = "doc.kml"; + + /** + * After all a KMZ file is a zipped KML one + */ + private stSPARQLResultsKMLWriter kmlWriter; + + /** + * The zipped output stream to wrap the original one + */ + private ZipOutputStream kmzout; + + /** + * The zip entry + */ + private ZipEntry entry; + + public stSPARQLResultsKMZWriter(OutputStream out) { + // create a zip stream on the given output stream + kmzout = new ZipOutputStream(out); + + // initialize the KMLWriter with that stream instead passing the original + kmlWriter = new stSPARQLResultsKMLWriter(kmzout); + } + + @Override + public void startQueryResult(List bindingNames) throws TupleQueryResultHandlerException { + try { + // create a zip entry + entry = new ZipEntry(ZIP_ENTRY_FILENAME); + + // add the zip entry in it + kmzout.putNextEntry(entry); + + // now pass execution to KMLWriter + kmlWriter.startQueryResult(bindingNames); + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void endQueryResult() throws TupleQueryResultHandlerException { + + try { + // pass execution to KMLWriter + kmlWriter.endQueryResult(); + + // close the zip entry + kmzout.closeEntry(); + + // close the zip stream + kmzout.close(); + + } catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + @Override + public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { + // pass execution to KMLWriter + kmlWriter.handleSolution(bindingSet); + } + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.KMZ; + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlkml/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlkml; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsKMZWriterFactory implements TupleQueryResultWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.KMZ; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsKMZWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlxml/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlxml/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,59 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-sparqlxml + + OpenRDF Sesame: Spatial Query result IO - XML + Spatial Query result IO API + + + + org.openrdf.sesame + sesame-queryresultio-spatial-api + + + org.openrdf.sesame + sesame-queryresultio-sparqlxml + + + org.openrdf.sesame + sesame-queryalgebra-evaluation-spatial + + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,190 @@ +/* + * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007. + * + * Licensed under the Aduna BSD-style license. + */ +package org.openrdf.query.resultio.sparqlxml; + +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BINDING_NAME_ATT; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BINDING_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BNODE_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.HEAD_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_DATATYPE_ATT; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_LANG_ATT; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.NAMESPACE; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_SET_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.ROOT_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.URI_TAG; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_NAME_ATT; +import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_TAG; +import info.aduna.xml.XMLWriter; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.List; + +import org.openrdf.model.BNode; +import org.openrdf.model.Literal; +import org.openrdf.model.URI; +import org.openrdf.model.Value; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.model.impl.URIImpl; +import org.openrdf.query.Binding; +import org.openrdf.query.BindingSet; +import org.openrdf.query.TupleQueryResultHandlerException; +import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants; +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * A {@link TupleQueryResultWriter} that writes tuple query results in the SPARQL Query Results XML + * Format. + * + * @author Manos Karpathiotakis + */ +public class stSPARQLResultsXMLWriter implements TupleQueryResultWriter { + + /*-----------* + * Variables * + *-----------*/ + + /** + * XMLWriter to write XML to. + */ + private XMLWriter xmlWriter; + + /*--------------* + * Constructors * + *--------------*/ + + public stSPARQLResultsXMLWriter(OutputStream out) { + this(new XMLWriter(out)); + } + + public stSPARQLResultsXMLWriter(XMLWriter xmlWriter) { + this.xmlWriter = xmlWriter; + this.xmlWriter.setPrettyPrint(true); + } + + public final TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.XML; + } + + /** + * Enables/disables addition of indentation characters and newlines in the + * XML document. By default, pretty-printing is set to true. If + * set to false, no indentation and newlines are added to the XML + * document. This method has to be used before writing starts (that is, + * before {@link #startQueryResult(List)} is called). + */ + public void setPrettyPrint(boolean prettyPrint) { + xmlWriter.setPrettyPrint(prettyPrint); + } + + public void startQueryResult(List bindingNames) + throws TupleQueryResultHandlerException + { + try { + xmlWriter.startDocument(); + + xmlWriter.setAttribute("xmlns", NAMESPACE); + xmlWriter.startTag(ROOT_TAG); + + // Write header + xmlWriter.startTag(HEAD_TAG); + for (String name : bindingNames) { + xmlWriter.setAttribute(VAR_NAME_ATT, name); + xmlWriter.emptyElement(VAR_TAG); + } + xmlWriter.endTag(HEAD_TAG); + + // Write start of results + xmlWriter.startTag(RESULT_SET_TAG); + } + catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + public void endQueryResult() + throws TupleQueryResultHandlerException + { + try { + xmlWriter.endTag(RESULT_SET_TAG); + xmlWriter.endTag(ROOT_TAG); + + xmlWriter.endDocument(); + } + catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + public void handleSolution(BindingSet bindingSet) + throws TupleQueryResultHandlerException + { + try { + xmlWriter.startTag(RESULT_TAG); + + for (Binding binding : bindingSet) { + xmlWriter.setAttribute(BINDING_NAME_ATT, binding.getName()); + xmlWriter.startTag(BINDING_TAG); + + writeValue(binding.getValue()); + + xmlWriter.endTag(BINDING_TAG); + } + + xmlWriter.endTag(RESULT_TAG); + } + catch (IOException e) { + throw new TupleQueryResultHandlerException(e); + } + } + + private void writeValue(Value value) throws IOException { + if (value instanceof URI) { + writeURI((URI) value); + } else if (value instanceof BNode) { + writeBNode((BNode) value); + } else if (value instanceof Literal) { + writeLiteral((Literal) value); + } + else { // spatial literal + // else if (value instanceof RdbmsPolyhedron) + URI datatype = new URIImpl(GeoConstants.WKT); + Literal literal = new LiteralImpl(value.stringValue(), datatype); + writeLiteral(literal); + } + } + + private void writeURI(URI uri) + throws IOException + { + xmlWriter.textElement(URI_TAG, uri.toString()); + } + + private void writeBNode(BNode bNode) + throws IOException + { + xmlWriter.textElement(BNODE_TAG, bNode.getID()); + } + + private void writeLiteral(Literal literal) + throws IOException + { + if (literal.getLanguage() != null) { + xmlWriter.setAttribute(LITERAL_LANG_ATT, literal.getLanguage()); + } + else if (literal.getDatatype() != null) { + URI datatype = literal.getDatatype(); + xmlWriter.setAttribute(LITERAL_DATATYPE_ATT, datatype.toString()); + } + + xmlWriter.textElement(LITERAL_TAG, literal.getLabel()); + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlxml; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsXMLWriterFactory implements TupleQueryResultWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.XML; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsXMLWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLXMLWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/sparqlxml/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLXMLWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,55 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.sparqlxml; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.io.Writer; + +import info.aduna.xml.XMLWriter; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLXMLWriter extends XMLWriter { + + /** + * @param writer + */ + public stSPARQLXMLWriter(Writer writer) { + super(writer); + } + + /** + * @param outputStream + */ + public stSPARQLXMLWriter(OutputStream outputStream) { + super(outputStream); + } + + /** + * @param outputStream + * @param charEncoding + * @throws UnsupportedEncodingException + */ + public stSPARQLXMLWriter(OutputStream outputStream, String charEncoding) throws UnsupportedEncodingException { + super(outputStream, charEncoding); + } + + /** + * Like XMLWriter.text(String text) but without escaping the string. + */ + public void unescapedText(String text) throws IOException { + _write(text); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/text/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/text/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,63 @@ + + + 4.0.0 + + + org.openrdf.sesame + sesame-queryresultio-spatial + 3.2.5-SNAPSHOT + + + sesame-queryresultio-spatial-text + + OpenRDF Sesame: Spatial Query result IO - Text + Spatial Query result IO API + + + + org.openrdf.sesame + sesame-queryresultio-spatial-api + + + org.openrdf.sesame + sesame-queryalgebra-evaluation-spatial + + + org.openrdf.sesame + sesame-sail-generaldb + + + org.openrdf.sesame + sesame-queryresultio-text + + + + org.slf4j + slf4j-log4j12 + + + + junit + junit + test + + + + diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/text/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/text/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,43 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.text; + +import java.io.IOException; +import java.io.OutputStream; + +import org.openrdf.model.Value; +import org.openrdf.model.impl.LiteralImpl; +import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriter; +import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsTSVWriter extends SPARQLResultsTSVWriter { + + public stSPARQLResultsTSVWriter(OutputStream out) { + super(out); + } + + @Override + protected void writeValue(Value val) throws IOException { + if (val instanceof GeneralDBPolyhedron) { + // catch the spatial case and create a new literal + // constructing a new literal is the only way if we want to reuse the {@link #writeValue(Value)} method + GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) val; + val = new LiteralImpl(dbpolyhedron.getPolyhedronStringRep(), dbpolyhedron.getDatatype()); + } + + // write value + super.writeValue(val); + + } +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio-spatial/text/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriterFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resultio-spatial/text/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriterFactory.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package org.openrdf.query.resultio.text; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultFormat; +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.stSPARQLQueryResultFormat; +import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriterFactory; + +/** + * @author Charalampos Nikolaou + * + */ +public class stSPARQLResultsTSVWriterFactory extends SPARQLResultsTSVWriterFactory { + + @Override + public TupleQueryResultFormat getTupleQueryResultFormat() { + return stSPARQLQueryResultFormat.TSV; + } + + @Override + public TupleQueryResultWriter getWriter(OutputStream out) { + return new stSPARQLResultsTSVWriter(out); + } + +} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/pom.xml --- a/resultio/pom.xml Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ - - - 4.0.0 - - - eu.earthobservatory - strabon - 3.2.5-SNAPSHOT - - - org.openrdf.sesame - sesame-queryresultio-spatial - OpenRDF Sesame: Query result IO - Spatial - Minimal spatial extensions of Query result parser and writer implementation for the SPARQL Query Results Format. - jar - - - - - org.openrdf.sesame - sesame-queryalgebra-evaluation-spatial - - - - org.openrdf.sesame - sesame-sail-generaldb - - - - org.openrdf.sesame - sesame-queryresultio-sparqlxml - ${sesame.version} - - - - org.openrdf.sesame - sesame-queryresultio-sparqljson - ${sesame.version} - - - - org.openrdf.sesame - sesame-queryresultio-text - ${sesame.version} - - - - org.openrdf.sesame - sesame-queryalgebra-model - - - - org.openrdf.sesame - sesame-model - - - - org.openrdf.sesame - sesame-query - - - - org.openrdf.sesame - sesame-repository-sparql - - - - org.slf4j - slf4j-log4j12 - - - - junit - junit - - - - - org.geotools - gt-geojson - - - - org.geotools.xsd - gt-xsd-kml - - - - org.geotools - gt-xml - - - - org.geotools - gt-epsg-hsql - - - - org.geotools - gt-shapefile - - - - org.geotools - gt-opengis - - - com.vividsolutions - jts - jar - compile - - - - diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/Format.java --- a/resultio/src/main/java/org/openrdf/query/resultio/Format.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio; - -import java.util.HashMap; -import java.util.Map; - -/** - * This enumeration type represents the available formats - * for the results of the evaluation of a SPARQL query. - * - * @author Charalampos Nikolaou - * - */ -public enum Format { - - /** - * Default format - */ - DEFAULT(""), - - /** - * XML format - */ - XML("XML"), - - /** - * KML format - */ - KML("KML"), - - /** - * KMZ format (compressed KML) - */ - KMZ("KMZ"), - - /** - * GeoJSON format - */ - GEOJSON("GeoJSON"), - - /** - * Format for experiments - */ - EXP("EXP"), - - /** - * HTML format - */ - HTML("HTML"), - - /** - * TSV (tab-separated values) format - */ - TSV("TSV"), - - /** - * Invalid format. - */ - INVALID("INVALID"); - - /** - * The string representation of this format - */ - private String name; - - /** - * Map a string constant to a Format - */ - private static final Map stringToEnum = new HashMap(); - - - static { // initialize map from constant name to enum constant - for (Format format : values()) { - // add both upper- and lower-case versions of the format - stringToEnum.put(format.toString(), format); - stringToEnum.put(format.toString().toLowerCase(), format); - } - } - - /** - * Format constructor. - * - * @param name - */ - Format(String name) { - this.name = name; - } - - @Override - public String toString() { - return name; - } - - /** - * Returns a Format enum given a format string. - * - * @param lang - * @return - */ - public static Format fromString(String format) { - return (stringToEnum.get(format) == null) ? INVALID:stringToEnum.get(format); - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,236 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlgeojson; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; - -import org.geotools.data.simple.SimpleFeatureCollection; -import org.geotools.feature.FeatureCollections; -import org.geotools.feature.simple.SimpleFeatureBuilder; -import org.geotools.feature.simple.SimpleFeatureTypeBuilder; -import org.geotools.geojson.feature.FeatureJSON; -import org.geotools.referencing.CRS; -import org.opengis.feature.simple.SimpleFeature; -import org.opengis.feature.simple.SimpleFeatureType; -import org.openrdf.model.Literal; -import org.openrdf.model.Value; -import org.openrdf.query.Binding; -import org.openrdf.query.BindingSet; -import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.algebra.evaluation.function.spatial.WKTHelper; -import org.openrdf.query.algebra.evaluation.util.JTSWrapper; -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; -import org.openrdf.sail.generaldb.model.XMLGSDatatypeUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.vividsolutions.jts.geom.Geometry; - -/** - * A TupleQueryResultWriter that writes query results in the GeoJSON Format. - * - * @author Manos Karpathiotakis - * @author Charalampos Nikolaou - */ -public class stSPARQLResultsGeoJSONWriter implements TupleQueryResultWriter { - - private static final Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.sparqlgeojson.stSPARQLResultsGeoJSONWriter.class); - - /** - * The underlying output stream to write - */ - private OutputStream out; - - /** - * Set a Feature Collection - */ - private SimpleFeatureCollection sfCollection; - - /** - * The wrapper of JTS library - */ - private JTSWrapper jts; - - /** - * Keep track of the number of results - */ - private int nresults; - - /** - * The class to use for serializing to GeoJSON - */ - private FeatureJSON fjson; - - /** - * Keep track of the number of features - */ - private int nfeatures; - - public stSPARQLResultsGeoJSONWriter(OutputStream out) { - this.out = out; - - // set the feature collection - sfCollection = FeatureCollections.newCollection("geomOutput"); - - // get the instance of JTSWrapper - jts = JTSWrapper.getInstance(); - - // initialize results/features - nresults = 0; - nfeatures = 0; - } - - @Override - public void startQueryResult(List bindingNames) throws TupleQueryResultHandlerException { - fjson = new FeatureJSON(); - fjson.setEncodeFeatureCRS(true); - } - - @Override - public void endQueryResult() throws TupleQueryResultHandlerException { - try { - fjson.writeFeatureCollection(sfCollection, out); - out.write("\n".getBytes(Charset.defaultCharset())); - - // write a warning when there are no features in the answer - if (nfeatures < nresults) { - logger.warn("[Strabon.GeoJSONWriter] No spatial binding found in the result, hence the result is empty eventhough query evaluation produced {} results. GeoJSON requires that at least one binding maps to a geometry.", nresults); - - } - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { - try { - nresults++; - - // list keeping binding names that are not binded to geometries - ArrayList properties = new ArrayList(); - - // list keeping values for binding names - ArrayList values = new ArrayList(); - - // list keeping the features of the result - ArrayList features = new ArrayList(); - - // list keeping the geometries of features - ArrayList geometries = new ArrayList(); - - // parse binding set - for (Binding binding : bindingSet) { - Value value = binding.getValue(); - - if (XMLGSDatatypeUtil.isGeometryValue(value)) { - // it's a spatial value - if (logger.isDebugEnabled()) { - logger.debug("[Strabon.GeoJSON] Found geometry: {}", value); - } - - nfeatures++; - - // we need the geometry and the SRID - Geometry geom = null; - int srid = -1; - - if (value instanceof GeneralDBPolyhedron) { - GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value; - - geom = dbpolyhedron.getPolyhedron().getGeometry(); - srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID(); - - } else { // spatial literal WKT or GML - // get the textual representation of the geometry (WKT or GML) - String geoText = value.stringValue(); - - if (XMLGSDatatypeUtil.isWKTLiteral((Literal) value)) {// WKT - // get its geometry - geom = jts.WKTread(WKTHelper.getWithoutSRID(geoText)); - - // get its SRID - srid = WKTHelper.getSRID(geoText); - - } else { // GML - // get its geometry - geom = jts.GMLread(geoText); - - // get its SRID - srid = geom.getSRID(); - - } - } - - SimpleFeatureTypeBuilder sftb = new SimpleFeatureTypeBuilder(); - sftb.setName("Feature_" + nresults + "_" + nfeatures); - sftb.setCRS(CRS.decode("EPSG:" + srid)); - sftb.setSRS("EPSG:" + srid); - sftb.add("geometry", Geometry.class); - - // add the feature in the list of features - features.add(sftb); - - // add the geometry of the feature in the list of geometries - geometries.add(geom); - - } else { // URI, BlankNode, or Literal other than geometry - if (logger.isDebugEnabled()) { - logger.debug("[Strabon.GeoJSON] Found resource: {}", value); - } - - properties.add(binding.getName()); - values.add(value); - } - } - - // construct the feature of the result - for (int i = 0; i < features.size(); i++) { - SimpleFeatureTypeBuilder sftb = features.get(i); - - // add the properties - for (int p = 0; p < properties.size(); p++) { - sftb.add(properties.get(p), String.class); - } - - SimpleFeatureType featureType = sftb.buildFeatureType(); - SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType); - - // add the geometry to the builder of the feature - featureBuilder.add(geometries.get(i)); - - // add the values to the builder of the feature - for (int v = 0; v < values.size(); v++) { - featureBuilder.add(values.get(v)); - } - - SimpleFeature feature = featureBuilder.buildFeature(null); - sfCollection.add(feature); - } - - } catch (Exception e) { - throw new TupleQueryResultHandlerException(e); - } - - } - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.GEOJSON; - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlgeojson/stSPARQLResultsGeoJSONWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlgeojson; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.TupleQueryResultWriterFactory; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsGeoJSONWriterFactory implements TupleQueryResultWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.GEOJSON; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsGeoJSONWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,170 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlhtml; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URLEncoder; -import java.util.List; - -import org.openrdf.model.BNode; -import org.openrdf.query.Binding; -import org.openrdf.query.BindingSet; -import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -import org.openrdf.query.resultio.sparqlxml.stSPARQLXMLWriter; -import org.openrdf.model.Value; -import org.openrdf.model.URI; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsHTMLWriter implements TupleQueryResultWriter { - - public static final String TABLE = "TABLE"; - public static final String TABLE_ROW_TAG = "TR"; - public static final String TABLE_HEADER_TAG = "TH"; - public static final String TABLE_DATA_TAG = "TD"; - public static final String LINK = "A"; - public static final String LINK_REF = "HREF"; - public static final String STYLE = "class"; - public static final String ID = "id"; - public static final String LINK_ID = "uri"; - public static final String TABLE_HEADER_CLASS = "query_results_header"; - public static final String TABLE_DATA_CLASS = "query_results_data"; - public static final String TABLE_CLASS = "query_results_table"; - public static final String MORE_LINK = "comment more"; - - /** - * The underlying XML formatter. - */ - private stSPARQLXMLWriter xmlWriter; - - /** - * The ordered list of binding names of the result. - */ - private List bindingNames; - - public stSPARQLResultsHTMLWriter(OutputStream out) { - this(new stSPARQLXMLWriter(out)); - } - - public stSPARQLResultsHTMLWriter(stSPARQLXMLWriter writer) { - xmlWriter = writer; - xmlWriter.setPrettyPrint(true); - } - - @Override - public void startQueryResult(List bindingNames) - throws TupleQueryResultHandlerException { - - try { - // keep the order of binding names - this.bindingNames = bindingNames; - // set style for table - xmlWriter.setAttribute(STYLE, TABLE_CLASS); - // write start of table - xmlWriter.startTag(TABLE); - // write Table header containing the bindings - xmlWriter.startTag(TABLE_ROW_TAG); - for (String bindingName: bindingNames) { - // set style for header - xmlWriter.setAttribute(STYLE, TABLE_HEADER_CLASS); - xmlWriter.textElement(TABLE_HEADER_TAG, bindingName); - } - - xmlWriter.endTag(TABLE_ROW_TAG); - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - - } - - @Override - public void endQueryResult() throws TupleQueryResultHandlerException { - try { - - // write end of table - xmlWriter.endTag(TABLE); - - // needed to flush data - xmlWriter.endDocument(); - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { - try { - StringBuilder value = new StringBuilder(); - Value boundValue = null; - - xmlWriter.startTag(TABLE_ROW_TAG); - for (String bindingName : bindingNames) { - Binding binding = bindingSet.getBinding(bindingName); - if(binding != null) - { - boundValue = binding.getValue(); - value.append(boundValue.stringValue()); - - if(boundValue instanceof BNode) { - value.insert(0, "_:"); - } - - // If the value is a uri, make it link - if(boundValue instanceof URI) - { - xmlWriter.setAttribute(STYLE, TABLE_DATA_CLASS); - xmlWriter.startTag(TABLE_DATA_TAG); - - // select all the triples that contain the boundValue - String query= "select * " + - "where " + - "{ " + - "?subject ?predicate ?object . "+ - "FILTER((?subject = <"+ boundValue.toString()+ ">) || "+ - "(?predicate = <"+ boundValue.toString()+ ">) || "+ - "(?object = <"+ boundValue.toString()+ ">)) " + - "}"; - - String href = "Browse?view=HTML&query="+URLEncoder.encode(query, "UTF-8")+"&format=HTML&resource="+boundValue.toString(); - xmlWriter.setAttribute(LINK_REF, href); - xmlWriter.startTag(LINK); - xmlWriter.text(boundValue.toString()); - xmlWriter.endTag(LINK); - } - else - { - xmlWriter.setAttribute(STYLE, TABLE_DATA_CLASS+" "+MORE_LINK); - xmlWriter.startTag(TABLE_DATA_TAG); - xmlWriter.text(boundValue.toString()); - } - xmlWriter.endTag(TABLE_DATA_TAG); - } - value.setLength(0); - } - xmlWriter.endTag(TABLE_ROW_TAG); - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.HTML; - } - -} \ No newline at end of file diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlhtml/stSPARQLResultsHTMLWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlhtml; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.TupleQueryResultWriterFactory; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsHTMLWriterFactory implements TupleQueryResultWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.HTML; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsHTMLWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,522 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. Copyright (C) 2010, 2011, 2012, - * Pyravlos Team http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlkml; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; - -import javax.xml.bind.JAXBException; -import javax.xml.namespace.QName; - -import org.geotools.kml.KML; -import org.geotools.kml.KMLConfiguration; -import org.geotools.xml.Encoder; -import org.openrdf.model.BNode; -import org.openrdf.model.Literal; -import org.openrdf.model.Value; -import org.openrdf.query.Binding; -import org.openrdf.query.BindingSet; -import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants; -import org.openrdf.query.algebra.evaluation.function.spatial.WKTHelper; -import org.openrdf.query.algebra.evaluation.util.JTSWrapper; -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -import org.openrdf.query.resultio.sparqlxml.stSPARQLXMLWriter; -import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; -import org.openrdf.sail.generaldb.model.XMLGSDatatypeUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryCollection; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.MultiLineString; -import com.vividsolutions.jts.geom.MultiPoint; -import com.vividsolutions.jts.geom.MultiPolygon; -import com.vividsolutions.jts.geom.Point; -import com.vividsolutions.jts.geom.Polygon; -import com.vividsolutions.jts.io.ParseException; - -/** - * @author Manos Karpathiotakis - * @author Charalampos Nikolaou - * @author Panayiotis Smeros - * - */ -public class stSPARQLResultsKMLWriter implements TupleQueryResultWriter { - private static final Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter.class); - - // KML tags/attributes - private static final String ROOT_TAG = "kml"; - private static final String NAMESPACE = "http://www.opengis.net/kml/2.2"; - private static final String RESULT_SET_TAG = "Folder"; - private static final String PLACEMARK_TAG = "Placemark"; - private static final String NAME_TAG = "name"; - private static final String DESC_TAG = "description"; - private static final String STYLE_TAG = "Style"; - private static final String STYLEMAP_TAG = "StyleMap"; - private static final String LINESTYLE_TAG = "LineStyle"; - private static final String POLYSTYLE_TAG = "PolyStyle"; - private static final String EXT_DATA_TAG = "ExtendedData"; - private static final String DATA_TAG = "Data"; - private static final String VALUE_TAG = "value"; - private static final String NAME_ATTR = NAME_TAG; - - private static final String STYLE_ID = "resultStyle"; - private static final String TABLE_ROW_BEGIN = ""; - private static final String TABLE_ROW_END = ""; - private static final String TABLE_DATA_BEGIN = ""; - private static final String TABLE_DATA_END = ""; - private static final String NEWLINE = "\n"; - private static final String TABLE_DESC_BEGIN = ""+ NEWLINE; - private static final String TABLE_DESC_END = "]]>" + NEWLINE; - - private static final String GEOMETRY_NAME = "Geometry"; - private static final String MULTIGEOMETRY = "MultiGeometry"; - - // Styling options - private static final int numOfStyles = 5; - private static final String[][] styles = { - // note that colors are encoded as "aabbggrr" strings where - // aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); - // rr=red - // (00 to ff). - // id, line width, line color, polygon fill, mouse over line width, - // mouse over line color mouse over polygon fill - // {STYLE_ID + "1", "1.5", "7d0000ff", "ad0000ff", "1.5", - // "7d0000ff", "ad0000ff"}, {STYLE_ID + "2", "1.5", "7d0000ff", - // "ad0000ff", "1.5", "7d0000ff", "ad0000ff"}, {STYLE_ID + "3", - // "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", "ad0000ff"}, - // {STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", - // "7d0000ff", "ad0000ff"}, {STYLE_ID + "5", "1.5", "7d000055", - // "ad000055", "1.5", "7d0000ff", "ad0000ff"}}; - { STYLE_ID + "1", "1.5", "000000ff", "000000ff", "1.5", "000000ff", - "000000ff" }, - { STYLE_ID + "2", "1.5", "000000ff", "000000ff", "1.5", "000000ff", - "000000ff" }, - { STYLE_ID + "3", "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", - "ad0000ff" }, - { STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", "7d0000ff", - "ad0000ff" }, - { STYLE_ID + "5", "1.5", "7dff0000", "adff0000", "1.5", "7dff0000", - "adff0000" } }; - - /** - * The underlying XML formatter. - */ - private stSPARQLXMLWriter xmlWriter; - - /** - * The number of results seen. - */ - private int nresults; - - /** - * The number of geometries seen. - */ - private int ngeometries; - - /** - * The JTS wrapper - */ - private JTSWrapper jts; - - /** - * Stream for manipulating geometries - */ - private ByteArrayOutputStream baos; - - /** - * Description string holding the projected variables of the SPARQL query - */ - private StringBuilder descHeader; - - /** - * Description string holding the values for the projected variables of the - * SPARQL query - */ - private StringBuilder descData; - - /** - * Indentation used in tags that are constructed manually - */ - private int depth; - - /** - * Creates an stSPARQLResultsKMLWriter that encodes the SPARQL results in - * KML. - * - * @param out - */ - public stSPARQLResultsKMLWriter(OutputStream out) { - this(new stSPARQLXMLWriter(out)); - } - - public stSPARQLResultsKMLWriter(stSPARQLXMLWriter writer) { - xmlWriter = writer; - xmlWriter.setPrettyPrint(true); - depth = 4; - jts = JTSWrapper.getInstance(); - baos = new ByteArrayOutputStream(); - descHeader = new StringBuilder(); - descData = new StringBuilder(); - nresults = 0; - ngeometries = 0; - } - - @Override - public void startQueryResult(List bindingNames) - throws TupleQueryResultHandlerException { - try { - - xmlWriter.startDocument(); - xmlWriter.setAttribute("xmlns", NAMESPACE); - xmlWriter.startTag(ROOT_TAG); - xmlWriter.startTag(RESULT_SET_TAG); - - // add default styles - for (String[] style : styles) { - String id = style[0]; - String lineWidth = style[1]; - String lineColor = style[2]; - String polygonFill = style[3]; - String mouseOverLineWidth = style[4]; - String mouseOverLineColor = style[5]; - String mouseOverPolygonFill = style[6]; - - // append normal style - xmlWriter.setAttribute("id", "normal_" + id); - xmlWriter.startTag(STYLE_TAG); - xmlWriter.startTag(LINESTYLE_TAG); - xmlWriter.textElement("width", lineWidth); - xmlWriter.textElement("color", lineColor); - xmlWriter.endTag(LINESTYLE_TAG); - xmlWriter.startTag(POLYSTYLE_TAG); - xmlWriter.textElement("color", polygonFill); - xmlWriter.endTag(POLYSTYLE_TAG); - xmlWriter.endTag(STYLE_TAG); - - // append highlight style - xmlWriter.setAttribute("id", "highlight_" + id); - xmlWriter.startTag(STYLE_TAG); - xmlWriter.startTag(LINESTYLE_TAG); - xmlWriter.textElement("width", mouseOverLineWidth); - xmlWriter.textElement("color", mouseOverLineColor); - xmlWriter.endTag(LINESTYLE_TAG); - xmlWriter.startTag(POLYSTYLE_TAG); - xmlWriter.textElement("color", mouseOverPolygonFill); - xmlWriter.endTag(POLYSTYLE_TAG); - xmlWriter.endTag(STYLE_TAG); - - // define map style combining the above styles - xmlWriter.setAttribute("id", id); - xmlWriter.startTag(STYLEMAP_TAG); - xmlWriter.startTag("Pair"); - xmlWriter.textElement("key", "normal"); - xmlWriter.textElement("styleUrl", "#normal_" + id); - xmlWriter.endTag("Pair"); - xmlWriter.startTag("Pair"); - xmlWriter.textElement("key", "highlight"); - xmlWriter.textElement("styleUrl", "#highlight_" + id); - xmlWriter.endTag("Pair"); - xmlWriter.endTag(STYLEMAP_TAG); - } - // end of default style definition - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void endQueryResult() throws TupleQueryResultHandlerException { - try { - - xmlWriter.endTag(RESULT_SET_TAG); - xmlWriter.endTag(ROOT_TAG); - xmlWriter.endDocument(); - baos.close(); - - if (ngeometries < nresults) { - logger.warn("[Strabon.KMLWriter] No spatial binding found in the result. KML requires that at least one binding maps to a geometry.", nresults); - } - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { - try { - int numOfGeometries = 0; - - // true if there are bindings that do not correspond to geometries - boolean hasDesc = false; - - // increase result size - nresults++; - - // create description table and header - indent(descHeader, depth); - descHeader.append(TABLE_DESC_BEGIN); - indent(descHeader, depth); - - List geometries = new ArrayList(); - Hashtable extData = new Hashtable(); - - // parse binding set - for (Binding binding : bindingSet) { - - Value value = binding.getValue(); - - // check for geometry value - if (XMLGSDatatypeUtil.isGeometryValue(value)) { - numOfGeometries++; - ngeometries++; - if (logger.isDebugEnabled()) { - logger.debug("[Strabon] Found geometry: {}", value); - } - - geometries.add(getGeometry(value)); - - } else { // URI, BlankNode, or Literal other than spatial literal - if (logger.isDebugEnabled()) { - logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal ({}): {}", value.getClass(), value); - } - - // mark that we found sth corresponding to the description - hasDesc = true; - - // write description - writeDesc(binding); - - // fill also the extended data attribute of the Placemark - extData.put(binding.getName(), getBindingValue(binding)); - } - } - - if (numOfGeometries > 1) { - // write each polygon in separate placemarks - for (String geometry : geometries) { - xmlWriter.startTag(PLACEMARK_TAG); - xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME); - xmlWriter.textElement("styleUrl", "#"+ styles[geometries.indexOf(geometry) % (numOfStyles - 2)][0]); - xmlWriter.startTag(MULTIGEOMETRY); - xmlWriter.unescapedText(geometry); - xmlWriter.endTag(MULTIGEOMETRY); - xmlWriter.endTag(PLACEMARK_TAG); - } - } - - // also write them in the same placemarks - xmlWriter.startTag(PLACEMARK_TAG); - xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME); - xmlWriter.textElement("styleUrl", "#" + styles[(numOfStyles - 1)][0]); - xmlWriter.startTag(MULTIGEOMETRY); - - for (String geometry : geometries) { - xmlWriter.unescapedText(geometry); - } - - xmlWriter.endTag(MULTIGEOMETRY); - - // we have found and constructed a description for this result. - // Write it down. - if (hasDesc) { - // end the placeholder for the description data - indent(descData, depth); - - // append to the table header the actual content from - // the bindings - descHeader.append(descData); - - // close the table for the description - descHeader.append(NEWLINE); - indent(descHeader, depth); - descHeader.append(TABLE_DESC_END); - - // begin the "description" tag - xmlWriter.startTag(DESC_TAG); - - // write the actual description - xmlWriter.unescapedText(descHeader.toString()); - - // end the "description" tag - xmlWriter.endTag(DESC_TAG); - } - - // add the extended data - if (extData.size() > 0) { - xmlWriter.startTag(EXT_DATA_TAG); - for (String key : extData.keySet()) { - xmlWriter.setAttribute(NAME_ATTR, key); - xmlWriter.startTag(DATA_TAG); - xmlWriter.textElement(VALUE_TAG, extData.get(key)); - xmlWriter.endTag(DATA_TAG); - } - xmlWriter.endTag(EXT_DATA_TAG); - } - - // end Placemark - xmlWriter.endTag(PLACEMARK_TAG); - - // clear description string builders - descHeader.setLength(0); - descData.setLength(0); - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - private String getGeometry(Value value) { - String geometry = ""; - QName geometryType = null; - // the underlying geometry in value - Geometry geom = null; - // the underlying SRID of the geometry - int srid = -1; - // get the KML encoder - Encoder encoder = null; - try { - encoder = new Encoder(new KMLConfiguration()); - encoder.setIndenting(true); - if (value instanceof GeneralDBPolyhedron) { - GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value; - geom = dbpolyhedron.getPolyhedron().getGeometry(); - srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID(); - } else { // spatial literal - Literal spatial = (Literal) value; - String geomRep = spatial.stringValue(); - if (XMLGSDatatypeUtil.isWKTLiteral(spatial)) { // WKT - geom = jts.WKTread(WKTHelper.getWithoutSRID(geomRep)); - srid = WKTHelper.getSRID(geomRep); - } else { // GML - geom = jts.GMLread(geomRep); - srid = geom.getSRID(); - } - } - // transform the geometry to {@link GeoConstants#defaultSRID} - geom = jts.transform(geom, srid, GeoConstants.defaultSRID); - if (geom instanceof Point) { - geometryType = KML.Point; - } else if (geom instanceof Polygon) { - geometryType = KML.Polygon; - } else if (geom instanceof LineString) { - geometryType = KML.LineString; - } else if (geom instanceof MultiPoint) { - geometryType = KML.MultiGeometry; - } else if (geom instanceof MultiLineString) { - geometryType = KML.MultiGeometry; - } else if (geom instanceof MultiPolygon) { - geometryType = KML.MultiGeometry; - } else if (geom instanceof GeometryCollection) { - geometryType = KML.MultiGeometry; - } - if (geometryType == null) { - logger.warn("[Strabon.KMLWriter] Found unknown geometry type."); - - } else { - encoder.encode(geom, geometryType, baos); - geometry = baos.toString().substring(38).replaceAll(" xmlns:kml=\"http://earth.google.com/kml/2.1\"", "").replaceAll("kml:", ""); - - if (geometryType == KML.MultiGeometry) { - geometry = geometry.substring(geometry.indexOf("") + 15, geometry.indexOf("")); - } - - /* - * if(geom instanceof Point) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 8); } else if(geom instanceof - * Polygon) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 10); } else if(geom - * instanceof LineString) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 13); } else if(geom - * instanceof MultiPoint) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 13); } else if(geom - * instanceof MultiLineString) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 18); } else if(geom - * instanceof MultiPolygon) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 15); } else if(geom - * instanceof GeometryCollection) { geometry = - * geometry.substring(geometry.indexOf(""), - * geometry.indexOf("") + 21); } - */ - baos.reset(); - } - } catch (ParseException e) { - logger.error("[Strabon.KMLWriter] Parse error exception of geometry: {}", e.getMessage()); - - } catch (IOException e) { - logger.error("[Strabon.KMLWriter] IOException during KML encoding of geometry: {}", e.getMessage()); - - } catch (JAXBException e) { - logger.error("[Strabon.KMLWriter] Exception during GML parsing: {}", e.getMessage()); - } - - return geometry; - } - - /** - * Adds to the description table information for a binding. - * - * @param binding - */ - private void writeDesc(Binding binding) { - descData.append(NEWLINE); - indent(descData, depth + 1); - descData.append(TABLE_ROW_BEGIN); - descData.append(TABLE_DATA_BEGIN); - descData.append(binding.getName()); - descData.append(TABLE_DATA_END); - descData.append(TABLE_DATA_BEGIN); - if (binding.getValue() instanceof BNode) { - descData.append("_:"); - } - descData.append(binding.getValue().stringValue()); - descData.append(TABLE_DATA_END); - descData.append(TABLE_ROW_END); - } - - private String getBindingValue(Binding binding) { - String val = binding.getValue().stringValue(); - if (binding.getValue() instanceof BNode) { - val = "_:" + val; - } - - return val; - } - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.KML; - } - - /** - * Adds indentation to the given string builder according to the specified - * depth. - * - * @param sb - * @param depth - */ - private void indent(StringBuilder sb, int depth) { - for (int i = 0; i < depth; i++) { - sb.append(xmlWriter.getIndentString()); - } - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlkml; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.TupleQueryResultWriterFactory; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsKMLWriterFactory implements TupleQueryResultWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.KML; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsKMLWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlkml; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -import org.openrdf.query.BindingSet; -import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsKMZWriter implements TupleQueryResultWriter { - - /** - * The name of the KML file that shall be zipped - * (by convention corresponds to "doc") - */ - private static final String ZIP_ENTRY_FILENAME = "doc.kml"; - - /** - * After all a KMZ file is a zipped KML one - */ - private stSPARQLResultsKMLWriter kmlWriter; - - /** - * The zipped output stream to wrap the original one - */ - private ZipOutputStream kmzout; - - /** - * The zip entry - */ - private ZipEntry entry; - - public stSPARQLResultsKMZWriter(OutputStream out) { - // create a zip stream on the given output stream - kmzout = new ZipOutputStream(out); - - // initialize the KMLWriter with that stream instead passing the original - kmlWriter = new stSPARQLResultsKMLWriter(kmzout); - } - - @Override - public void startQueryResult(List bindingNames) throws TupleQueryResultHandlerException { - try { - // create a zip entry - entry = new ZipEntry(ZIP_ENTRY_FILENAME); - - // add the zip entry in it - kmzout.putNextEntry(entry); - - // now pass execution to KMLWriter - kmlWriter.startQueryResult(bindingNames); - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void endQueryResult() throws TupleQueryResultHandlerException { - - try { - // pass execution to KMLWriter - kmlWriter.endQueryResult(); - - // close the zip entry - kmzout.closeEntry(); - - // close the zip stream - kmzout.close(); - - } catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - @Override - public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException { - // pass execution to KMLWriter - kmlWriter.handleSolution(bindingSet); - } - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.KMZ; - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMZWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlkml; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.TupleQueryResultWriterFactory; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsKMZWriterFactory implements TupleQueryResultWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.KMZ; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsKMZWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -/* - * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007. - * - * Licensed under the Aduna BSD-style license. - */ -package org.openrdf.query.resultio.sparqlxml; - -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BINDING_NAME_ATT; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BINDING_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.BNODE_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.HEAD_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_DATATYPE_ATT; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_LANG_ATT; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.LITERAL_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.NAMESPACE; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_SET_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.RESULT_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.ROOT_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.URI_TAG; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_NAME_ATT; -import static org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLConstants.VAR_TAG; -import info.aduna.xml.XMLWriter; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.List; - -import org.openrdf.model.BNode; -import org.openrdf.model.Literal; -import org.openrdf.model.URI; -import org.openrdf.model.Value; -import org.openrdf.model.impl.LiteralImpl; -import org.openrdf.model.impl.URIImpl; -import org.openrdf.query.Binding; -import org.openrdf.query.BindingSet; -import org.openrdf.query.TupleQueryResultHandlerException; -import org.openrdf.query.algebra.evaluation.function.spatial.GeoConstants; -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * A {@link TupleQueryResultWriter} that writes tuple query results in the SPARQL Query Results XML - * Format. - * - * @author Manos Karpathiotakis - */ -public class stSPARQLResultsXMLWriter implements TupleQueryResultWriter { - - /*-----------* - * Variables * - *-----------*/ - - /** - * XMLWriter to write XML to. - */ - private XMLWriter xmlWriter; - - /*--------------* - * Constructors * - *--------------*/ - - public stSPARQLResultsXMLWriter(OutputStream out) { - this(new XMLWriter(out)); - } - - public stSPARQLResultsXMLWriter(XMLWriter xmlWriter) { - this.xmlWriter = xmlWriter; - this.xmlWriter.setPrettyPrint(true); - } - - public final TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.XML; - } - - /** - * Enables/disables addition of indentation characters and newlines in the - * XML document. By default, pretty-printing is set to true. If - * set to false, no indentation and newlines are added to the XML - * document. This method has to be used before writing starts (that is, - * before {@link #startQueryResult(List)} is called). - */ - public void setPrettyPrint(boolean prettyPrint) { - xmlWriter.setPrettyPrint(prettyPrint); - } - - public void startQueryResult(List bindingNames) - throws TupleQueryResultHandlerException - { - try { - xmlWriter.startDocument(); - - xmlWriter.setAttribute("xmlns", NAMESPACE); - xmlWriter.startTag(ROOT_TAG); - - // Write header - xmlWriter.startTag(HEAD_TAG); - for (String name : bindingNames) { - xmlWriter.setAttribute(VAR_NAME_ATT, name); - xmlWriter.emptyElement(VAR_TAG); - } - xmlWriter.endTag(HEAD_TAG); - - // Write start of results - xmlWriter.startTag(RESULT_SET_TAG); - } - catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - public void endQueryResult() - throws TupleQueryResultHandlerException - { - try { - xmlWriter.endTag(RESULT_SET_TAG); - xmlWriter.endTag(ROOT_TAG); - - xmlWriter.endDocument(); - } - catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - public void handleSolution(BindingSet bindingSet) - throws TupleQueryResultHandlerException - { - try { - xmlWriter.startTag(RESULT_TAG); - - for (Binding binding : bindingSet) { - xmlWriter.setAttribute(BINDING_NAME_ATT, binding.getName()); - xmlWriter.startTag(BINDING_TAG); - - writeValue(binding.getValue()); - - xmlWriter.endTag(BINDING_TAG); - } - - xmlWriter.endTag(RESULT_TAG); - } - catch (IOException e) { - throw new TupleQueryResultHandlerException(e); - } - } - - private void writeValue(Value value) throws IOException { - if (value instanceof URI) { - writeURI((URI) value); - } else if (value instanceof BNode) { - writeBNode((BNode) value); - } else if (value instanceof Literal) { - writeLiteral((Literal) value); - } - else { // spatial literal - // else if (value instanceof RdbmsPolyhedron) - URI datatype = new URIImpl(GeoConstants.WKT); - Literal literal = new LiteralImpl(value.stringValue(), datatype); - writeLiteral(literal); - } - } - - private void writeURI(URI uri) - throws IOException - { - xmlWriter.textElement(URI_TAG, uri.toString()); - } - - private void writeBNode(BNode bNode) - throws IOException - { - xmlWriter.textElement(BNODE_TAG, bNode.getID()); - } - - private void writeLiteral(Literal literal) - throws IOException - { - if (literal.getLanguage() != null) { - xmlWriter.setAttribute(LITERAL_LANG_ATT, literal.getLanguage()); - } - else if (literal.getDatatype() != null) { - URI datatype = literal.getDatatype(); - xmlWriter.setAttribute(LITERAL_DATATYPE_ATT, datatype.toString()); - } - - xmlWriter.textElement(LITERAL_TAG, literal.getLabel()); - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLResultsXMLWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlxml; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.TupleQueryResultWriterFactory; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsXMLWriterFactory implements TupleQueryResultWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.XML; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsXMLWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLXMLWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlxml/stSPARQLXMLWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.sparqlxml; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.io.Writer; - -import info.aduna.xml.XMLWriter; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLXMLWriter extends XMLWriter { - - /** - * @param writer - */ - public stSPARQLXMLWriter(Writer writer) { - super(writer); - } - - /** - * @param outputStream - */ - public stSPARQLXMLWriter(OutputStream outputStream) { - super(outputStream); - } - - /** - * @param outputStream - * @param charEncoding - * @throws UnsupportedEncodingException - */ - public stSPARQLXMLWriter(OutputStream outputStream, String charEncoding) throws UnsupportedEncodingException { - super(outputStream, charEncoding); - } - - /** - * Like XMLWriter.text(String text) but without escaping the string. - */ - public void unescapedText(String text) throws IOException { - _write(text); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java --- a/resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultFormat.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,143 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio; - -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -/** - * Represents the concept of an tuple query result serialization format for - * stSPARQL/GeoSPARQL. Tuple query result formats are identified by a - * {@link #getName() name} and can have one or more associated MIME types, - * zero or more associated file extensions and can specify a (default) - * character encoding. - * - * In contrast to formats mentioned in class {@link #TupleQueryResultFormat}, - * stSPARQL/GeoSPARQL formats do not adhere to any specification for SPARQL - * except for those that are extension of the respective formats in class - * {@link #TupleQueryResultFormat}, such as TSV. - * For example, the projected variables in a stSPARQL/GeoSPARQL query are - * not included in the beginning of these formats. Instead, they are provided - * as an additional description for a feature (e.g., a tuple query result with - * a projected variable corresponding to a geometry). - * - * @author Charalampos Nikolaou - * - */ -public class stSPARQLQueryResultFormat extends TupleQueryResultFormat { - - /** - * XML format (extension of {@link TupleQueryResultFormat#SPARQL} format to include geometries) - */ - public static final stSPARQLQueryResultFormat XML = new stSPARQLQueryResultFormat("XML", - Arrays.asList("application/sparql-results+xml", "application/xml"), Charset.forName("UTF-8"), Arrays.asList("xml")); - - /** - * KML format (see http://www.opengeospatial.org/standards/kml/) - */ - public static final stSPARQLQueryResultFormat KML = new stSPARQLQueryResultFormat("KML", - Arrays.asList("application/vnd.google-earth.kml+xml", "application/kml"), Charset.forName("UTF-8"), Arrays.asList("kml")); - - /** - * KMZ format (a zipped KML content) - */ - public static final stSPARQLQueryResultFormat KMZ = new stSPARQLQueryResultFormat("KMZ", - Arrays.asList("application/vnd.google-earth.kmz", "application/kmz"), Charset.forName("UTF-8"), Arrays.asList("kmz")); - - /** - * GeoJSON format (see http://www.geojson.org/geojson-spec.html) - */ - public static final stSPARQLQueryResultFormat GEOJSON = new stSPARQLQueryResultFormat("GeoJSON", - Arrays.asList("application/json", "application/geojson"), Charset.forName("UTF-8"), Arrays.asList("json")); - - /** - * Tab separated value format (extension of {@link TupleQueryResultFormat#TSV} format to include geometries) - */ - public static final stSPARQLQueryResultFormat TSV = new stSPARQLQueryResultFormat("TSV", - Arrays.asList("text/tab-separated-values"), Charset.forName("UTF-8"), Arrays.asList("tsv")); - - /** - * HTML format (encoded as an HTML table, without the <TABLE> tag) - */ - public static final stSPARQLQueryResultFormat HTML = new stSPARQLQueryResultFormat("HTML", - Arrays.asList("text/html"), Charset.forName("UTF-8"), Arrays.asList("html", "htm")); - - /** - * The available stSPARQLQuery Result Formats - */ - private static final List VALUES = new ArrayList(6); - - // registers stSPARQL/GeoSPARQL formats - static { - register(XML); - register(KML); - register(KMZ); - register(GEOJSON); - register(TSV); - register(HTML); - } - - /** - * Register the specified stSPARQLQueryResultFormat. - * - * @param format - */ - public static void register(stSPARQLQueryResultFormat format) { - TupleQueryResultFormat.register(format); - VALUES.add(format); - } - - /** - * Gets the stSPARQLQueryResultFormat given its name. - * - * @param formatName - * @return - */ - public static stSPARQLQueryResultFormat valueOf(String formatName) { - for (TupleQueryResultFormat format : values()) { - if (format instanceof stSPARQLQueryResultFormat && - format.getName().equalsIgnoreCase(formatName)) { - return (stSPARQLQueryResultFormat) format; - } - } - - return null; - } - - /** - * Returns all known/registered tuple query result formats. - */ - public static Collection values() { - return TupleQueryResultFormat.values(); - } - - public static stSPARQLQueryResultFormat forMIMEType(String mimeType) { - return forMIMEType(mimeType, null); - } - - public static stSPARQLQueryResultFormat forMIMEType(String mimeType, stSPARQLQueryResultFormat fallback) { - return matchMIMEType(mimeType, VALUES, fallback); - } - - public stSPARQLQueryResultFormat(String name, String mimeType, String fileExt) { - super(name, mimeType, fileExt); - } - - public stSPARQLQueryResultFormat(String name, String mimeType, Charset charset, String fileExt) { - super(name, mimeType, charset, fileExt); - } - - public stSPARQLQueryResultFormat(String name, Collection mimeTypes, Charset charset, Collection fileExtensions) { - super(name, mimeTypes, charset, fileExtensions); - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/stSPARQLQueryResultWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.sparqlgeojson.stSPARQLResultsGeoJSONWriterFactory; -import org.openrdf.query.resultio.sparqlhtml.stSPARQLResultsHTMLWriterFactory; -import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMZWriterFactory; -import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriterFactory; -import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriterFactory; -import org.openrdf.query.resultio.text.stSPARQLResultsTSVWriterFactory; - -/** - * This is a factory class for creating stSPARQLQueryResultWriter - * instances according to a format in {@link org.openrdf.query.resultio.sparqlxml.Format}. - * - * @author Charalampos Nikolaou - * - */ -public class stSPARQLQueryResultWriterFactory { - - private static TupleQueryResultWriterFactory html = new stSPARQLResultsHTMLWriterFactory(); - private static TupleQueryResultWriterFactory xml = new stSPARQLResultsXMLWriterFactory(); - private static TupleQueryResultWriterFactory kml = new stSPARQLResultsKMLWriterFactory(); - private static TupleQueryResultWriterFactory kmz = new stSPARQLResultsKMZWriterFactory(); - private static TupleQueryResultWriterFactory tsv = new stSPARQLResultsTSVWriterFactory(); - private static TupleQueryResultWriterFactory geojson = new stSPARQLResultsGeoJSONWriterFactory(); - - public static TupleQueryResultWriter createstSPARQLQueryResultWriter(Format format, OutputStream out) { - TupleQueryResultWriter writer = null; - - switch (format) { - case DEFAULT: - writer = tsv.getWriter(out); - break; - - case XML: - writer = xml.getWriter(out); - break; - - case KML: - writer = kml.getWriter(out); - break; - - case KMZ: - writer = kmz.getWriter(out); - break; - - case GEOJSON: - writer = geojson.getWriter(out); - break; - - case EXP: - // TODO: add - break; - - case HTML: - writer = html.getWriter(out); - break; - - case TSV: - writer = tsv.getWriter(out); - } - - return writer; - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java --- a/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriter.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.text; - -import java.io.IOException; -import java.io.OutputStream; - -import org.openrdf.model.Value; -import org.openrdf.model.impl.LiteralImpl; -import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriter; -import org.openrdf.sail.generaldb.model.GeneralDBPolyhedron; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsTSVWriter extends SPARQLResultsTSVWriter { - - public stSPARQLResultsTSVWriter(OutputStream out) { - super(out); - } - - @Override - protected void writeValue(Value val) throws IOException { - if (val instanceof GeneralDBPolyhedron) { - // catch the spatial case and create a new literal - // constructing a new literal is the only way if we want to reuse the {@link #writeValue(Value)} method - GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) val; - val = new LiteralImpl(dbpolyhedron.getPolyhedronStringRep(), dbpolyhedron.getDatatype()); - } - - // write value - super.writeValue(val); - - } -} diff -r 706b99be9078 -r 9a4bb8e81273 resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriterFactory.java --- a/resultio/src/main/java/org/openrdf/query/resultio/text/stSPARQLResultsTSVWriterFactory.java Mon Nov 05 20:40:17 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * Copyright (C) 2010, 2011, 2012, Pyravlos Team - * - * http://www.strabon.di.uoa.gr/ - */ -package org.openrdf.query.resultio.text; - -import java.io.OutputStream; - -import org.openrdf.query.resultio.TupleQueryResultFormat; -import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultFormat; -import org.openrdf.query.resultio.text.tsv.SPARQLResultsTSVWriterFactory; - -/** - * @author Charalampos Nikolaou - * - */ -public class stSPARQLResultsTSVWriterFactory extends SPARQLResultsTSVWriterFactory { - - @Override - public TupleQueryResultFormat getTupleQueryResultFormat() { - return stSPARQLQueryResultFormat.TSV; - } - - @Override - public TupleQueryResultWriter getWriter(OutputStream out) { - return new stSPARQLResultsTSVWriter(out); - } - -} diff -r 706b99be9078 -r 9a4bb8e81273 runtime/pom.xml --- a/runtime/pom.xml Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/pom.xml Tue Nov 06 19:24:43 2012 +0200 @@ -22,8 +22,29 @@ org.openrdf.sesame - sesame-queryresultio-spatial + sesame-queryresultio-spatial-api + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlgeojson + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlkml + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlxml + + + org.openrdf.sesame + sesame-queryresultio-spatial-sparqlhtml + + + org.openrdf.sesame + sesame-queryresultio-spatial-text + + org.openrdf.sesame diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java --- a/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/generaldb/Strabon.java Tue Nov 06 19:24:43 2012 +0200 @@ -32,9 +32,7 @@ import org.openrdf.query.TupleQueryResultHandlerException; import org.openrdf.query.Update; import org.openrdf.query.UpdateExecutionException; -import org.openrdf.query.resultio.Format; import org.openrdf.query.resultio.TupleQueryResultWriter; -import org.openrdf.query.resultio.stSPARQLQueryResultWriterFactory; import org.openrdf.repository.RepositoryException; import org.openrdf.repository.sail.SailRepository; import org.openrdf.repository.sail.SailRepositoryConnection; @@ -47,7 +45,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import eu.earthobservatory.utils.Format; import eu.earthobservatory.utils.RDFHandlerFactory; +import eu.earthobservatory.utils.stSPARQLQueryResultToFormatAdapter; public abstract class Strabon { @@ -252,7 +252,7 @@ default: // get the writer for the specified format - TupleQueryResultWriter resultWriter = stSPARQLQueryResultWriterFactory.createstSPARQLQueryResultWriter(resultsFormat, out); + TupleQueryResultWriter resultWriter = stSPARQLQueryResultToFormatAdapter.createstSPARQLQueryResultWriter(resultsFormat, out); // check for null format if (resultWriter == null) { diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java --- a/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryDir.java Tue Nov 06 19:24:43 2012 +0200 @@ -9,13 +9,14 @@ */ package eu.earthobservatory.runtime.monetdb; +import eu.earthobservatory.utils.Format; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; -import org.openrdf.query.resultio.Format; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java --- a/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/monetdb/QueryOp.java Tue Nov 06 19:24:43 2012 +0200 @@ -9,7 +9,8 @@ */ package eu.earthobservatory.runtime.monetdb; -import org.openrdf.query.resultio.Format; +import eu.earthobservatory.utils.Format; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java --- a/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryDir.java Tue Nov 06 19:24:43 2012 +0200 @@ -9,13 +9,14 @@ */ package eu.earthobservatory.runtime.postgis; +import eu.earthobservatory.utils.Format; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FilenameFilter; import java.io.IOException; -import org.openrdf.query.resultio.Format; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java --- a/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java Mon Nov 05 20:40:17 2012 +0200 +++ b/runtime/src/main/java/eu/earthobservatory/runtime/postgis/QueryOp.java Tue Nov 06 19:24:43 2012 +0200 @@ -9,7 +9,8 @@ */ package eu.earthobservatory.runtime.postgis; -import org.openrdf.query.resultio.Format; +import eu.earthobservatory.utils.Format; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/utils/Format.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/runtime/src/main/java/eu/earthobservatory/utils/Format.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,111 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.utils; + +import java.util.HashMap; +import java.util.Map; + +/** + * This enumeration type represents the available formats + * for the results of the evaluation of a SPARQL query. + * + * @author Charalampos Nikolaou + * + */ +public enum Format { + + /** + * Default format + */ + DEFAULT(""), + + /** + * XML format + */ + XML("XML"), + + /** + * KML format + */ + KML("KML"), + + /** + * KMZ format (compressed KML) + */ + KMZ("KMZ"), + + /** + * GeoJSON format + */ + GEOJSON("GeoJSON"), + + /** + * Format for experiments + */ + EXP("EXP"), + + /** + * HTML format + */ + HTML("HTML"), + + /** + * TSV (tab-separated values) format + */ + TSV("TSV"), + + /** + * Invalid format. + */ + INVALID("INVALID"); + + /** + * The string representation of this format + */ + private String name; + + /** + * Map a string constant to a Format + */ + private static final Map stringToEnum = new HashMap(); + + + static { // initialize map from constant name to enum constant + for (Format format : values()) { + // add both upper- and lower-case versions of the format + stringToEnum.put(format.toString(), format); + stringToEnum.put(format.toString().toLowerCase(), format); + } + } + + /** + * Format constructor. + * + * @param name + */ + Format(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + + /** + * Returns a Format enum given a format string. + * + * @param lang + * @return + */ + public static Format fromString(String format) { + return (stringToEnum.get(format) == null) ? INVALID:stringToEnum.get(format); + } +} diff -r 706b99be9078 -r 9a4bb8e81273 runtime/src/main/java/eu/earthobservatory/utils/stSPARQLQueryResultToFormatAdapter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/runtime/src/main/java/eu/earthobservatory/utils/stSPARQLQueryResultToFormatAdapter.java Tue Nov 06 19:24:43 2012 +0200 @@ -0,0 +1,78 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright (C) 2010, 2011, 2012, Pyravlos Team + * + * http://www.strabon.di.uoa.gr/ + */ +package eu.earthobservatory.utils; + +import java.io.OutputStream; + +import org.openrdf.query.resultio.TupleQueryResultWriter; +import org.openrdf.query.resultio.TupleQueryResultWriterFactory; +import org.openrdf.query.resultio.sparqlgeojson.stSPARQLResultsGeoJSONWriterFactory; +import org.openrdf.query.resultio.sparqlhtml.stSPARQLResultsHTMLWriterFactory; +import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriterFactory; +import org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMZWriterFactory; +import org.openrdf.query.resultio.sparqlxml.stSPARQLResultsXMLWriterFactory; +import org.openrdf.query.resultio.text.stSPARQLResultsTSVWriterFactory; + +/** + * This is a factory class for creating stSPARQLQueryResultWriter + * instances according to a format in {@link org.openrdf.query.resultio.sparqlxml.Format}. + * + * @author Charalampos Nikolaou + * + */ +public class stSPARQLQueryResultToFormatAdapter { + + private static TupleQueryResultWriterFactory html = new stSPARQLResultsHTMLWriterFactory(); + private static TupleQueryResultWriterFactory xml = new stSPARQLResultsXMLWriterFactory(); + private static TupleQueryResultWriterFactory kml = new stSPARQLResultsKMLWriterFactory(); + private static TupleQueryResultWriterFactory kmz = new stSPARQLResultsKMZWriterFactory(); + private static TupleQueryResultWriterFactory tsv = new stSPARQLResultsTSVWriterFactory(); + private static TupleQueryResultWriterFactory geojson = new stSPARQLResultsGeoJSONWriterFactory(); + + public static TupleQueryResultWriter createstSPARQLQueryResultWriter(Format format, OutputStream out) { + TupleQueryResultWriter writer = null; + + switch (format) { + case DEFAULT: + writer = tsv.getWriter(out); + break; + + case XML: + writer = xml.getWriter(out); + break; + + case KML: + writer = kml.getWriter(out); + break; + + case KMZ: + writer = kmz.getWriter(out); + break; + + case GEOJSON: + writer = geojson.getWriter(out); + break; + + case EXP: + // TODO: add + break; + + case HTML: + writer = html.getWriter(out); + break; + + case TSV: + writer = tsv.getWriter(out); + } + + return writer; + } + +}