Strabon

changeset 640:61c165c2e0b7

Merge with default
author Kostis Kyzirakos <kkyzir@di.uoa.gr>
date Fri Oct 12 16:06:32 2012 +0300 (2012-10-12)
parents 46fdd39f45c7 0a2d39b80321
children 333e06b3e474 300e958d034b
files README
line diff
     1.1 --- a/README	Fri Oct 12 16:06:04 2012 +0300
     1.2 +++ b/README	Fri Oct 12 16:06:32 2012 +0300
     1.3 @@ -213,6 +213,20 @@
     1.4  the Strabon-devel mailing-list Strabon-devel@di.uoa.gr.
     1.5  
     1.6  
     1.7 +Known Issues
     1.8 +============
     1.9 +By default, Tomcat uses ISO-8859-1 character encoding when decoding URLs received 
    1.10 +from a browser. This can cause problems when encoding is UTF-8, and you are using 
    1.11 +international characters. In order to fix this, edit conf/server.xml and find the 
    1.12 +line where the Connector is defined. Add the parameter URIEncoding and set it to 
    1.13 +UTF-8. For example:
    1.14 +
    1.15 +<Connector port="8080" protocol="HTTP/1.1" 
    1.16 +               connectionTimeout="20000" 
    1.17 +               URIEncoding="UTF-8"
    1.18 +               redirectPort="8443" />
    1.19 +
    1.20 +
    1.21  License
    1.22  =======
    1.23  This Source Code Form is subject to the terms of the Mozilla Public
     2.1 --- a/endpoint/WebContent/WEB-INF/beans.xml	Fri Oct 12 16:06:04 2012 +0300
     2.2 +++ b/endpoint/WebContent/WEB-INF/beans.xml	Fri Oct 12 16:06:32 2012 +0300
     2.3 @@ -54,6 +54,29 @@
     2.4              <value>${dbengine}</value>
     2.5          </constructor-arg>
     2.6          
     2.7 +        <constructor-arg type="int" >
     2.8 +            <!-- Query limit -->
     2.9 +            <value>1000</value>
    2.10 +        </constructor-arg>
    2.11 +        
    2.12 +        <constructor-arg type="java.lang.String" >
    2.13 +            <!-- Default prefixes -->
    2.14 +            <value><![CDATA[PREFIX lgd:<http://linkedgeodata.org/triplify/>
    2.15 +PREFIX lgdgeo:<http://www.w3.org/2003/01/geo/wgs84_pos#>
    2.16 +PREFIX lgdont:<http://linkedgeodata.org/ontology/>
    2.17 +PREFIX geonames:<http://www.geonames.org/ontology#>
    2.18 +PREFIX clc: <http://geo.linkedopendata.gr/corine/ontology#>
    2.19 +PREFIX gag: <http://teleios.di.uoa.gr/ontologies/gagKallikratis.rdf#>
    2.20 +PREFIX noa: <http://teleios.di.uoa.gr/ontologies/noaOntology.owl#>
    2.21 +PREFIX dlr: <http://www.earthobservatory.eu/ontologies/dlrOntology-v2.owl#>
    2.22 +PREFIX strdf: <http://strdf.di.uoa.gr/ontology#>
    2.23 +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    2.24 +PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    2.25 +PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    2.26 +]]>
    2.27 +</value>
    2.28 +        </constructor-arg>
    2.29 +        
    2.30          <constructor-arg>
    2.31               <list>
    2.32               	<list>
     3.1 --- a/endpoint/WebContent/query.jsp	Fri Oct 12 16:06:04 2012 +0300
     3.2 +++ b/endpoint/WebContent/query.jsp	Fri Oct 12 16:06:32 2012 +0300
     3.3 @@ -64,8 +64,15 @@
     3.4  	});
     3.5  </script>
     3.6  	<%
     3.7 +	// get the reference to StrabonBeanWrapper
     3.8 +	StrabonBeanWrapper strabonWrapper;
     3.9 +	ServletContext context;
    3.10 +	context = getServletContext();
    3.11 +	WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    3.12 +	strabonWrapper=(StrabonBeanWrapper) applicationContext.getBean("strabonBean");
    3.13 +	
    3.14  	// get query parameter or attribute (the attribute comes from ConnectionBean)
    3.15 -	String query = "";
    3.16 +	String query = strabonWrapper.getPrefixes();
    3.17  	if (request.getParameter("query") != null) {
    3.18  		query = request.getParameter("query");
    3.19  		
    3.20 @@ -206,11 +213,6 @@
    3.21  		<div class="container">
    3.22  		<div id="accordion">
    3.23  		<%
    3.24 -					StrabonBeanWrapper strabonWrapper;
    3.25 -							ServletContext context;
    3.26 -							context = getServletContext();
    3.27 -							WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    3.28 -							strabonWrapper=(StrabonBeanWrapper) applicationContext.getBean("strabonBean");
    3.29  							
    3.30  							Iterator <StrabonBeanWrapperConfiguration> entryListIterator = strabonWrapper.getEntries().iterator();
    3.31  							boolean first = true;
     4.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Fri Oct 12 16:06:04 2012 +0300
     4.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java	Fri Oct 12 16:06:32 2012 +0300
     4.3 @@ -36,7 +36,13 @@
     4.4  import org.springframework.web.context.WebApplicationContext;
     4.5  import org.springframework.web.context.support.WebApplicationContextUtils;
     4.6  
     4.7 -
     4.8 +/**
     4.9 + * 
    4.10 + * @author Kostis Kyzirakos <kkyzir@di.uoa.gr>
    4.11 + * @author Manos Karpathiotakis <mk@di.uoa.gr>
    4.12 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    4.13 + * @author Stella Giannakopoulou <sgian@di.uoa.gr>
    4.14 + */
    4.15  public class QueryBean extends HttpServlet {
    4.16  
    4.17  	private static final long serialVersionUID = -378175118289907707L;
    4.18 @@ -157,9 +163,11 @@
    4.19          
    4.20          // get the query
    4.21  		String query = request.getParameter("query");
    4.22 +		String maxLimit = request.getParameter("maxLimit");
    4.23      	
    4.24      	// check for required parameters
    4.25      	if (format == null || query == null) {
    4.26 +    		logger.error("[StrabonEndpoint.QueryBean] {}", PARAM_ERROR);
    4.27      		response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    4.28  			out.print(ResponseMessages.getXMLHeader());
    4.29  			out.print(ResponseMessages.getXMLException(PARAM_ERROR));
    4.30 @@ -171,10 +179,12 @@
    4.31      		
    4.32  	    	response.setContentType(format.getDefaultMIMEType());
    4.33  	    	try {
    4.34 +				query = strabonWrapper.addLimit(query, maxLimit);
    4.35  				strabonWrapper.query(query, format.getName(), out);
    4.36  				response.setStatus(HttpServletResponse.SC_OK);
    4.37  				
    4.38  			} catch (Exception e) {
    4.39 +				logger.error("[StrabonEndpoint.QueryBean] Error during querying.", e);
    4.40  				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    4.41  				out.print(ResponseMessages.getXMLHeader());
    4.42  				out.print(ResponseMessages.getXMLException(e.getMessage()));
    4.43 @@ -208,6 +218,7 @@
    4.44  			String query = URLDecoder.decode(request.getParameter("query"), "UTF-8");
    4.45  			String format = request.getParameter("format");
    4.46  			String handle = request.getParameter("handle");
    4.47 +			String maxLimit = request.getParameter("maxLimit");
    4.48  			
    4.49  			// get stSPARQLQueryResultFormat from given format name
    4.50  			stSPARQLQueryResultFormat queryResultFormat = stSPARQLQueryResultFormat.valueOf(format);
    4.51 @@ -218,6 +229,7 @@
    4.52  				dispatcher.forward(request, response);
    4.53  				
    4.54  			} else {
    4.55 +				query = strabonWrapper.addLimit(query, maxLimit);
    4.56  				if ("download".equals(handle)) { // download as attachment
    4.57  					ServletOutputStream out = response.getOutputStream();
    4.58  					
     5.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Fri Oct 12 16:06:04 2012 +0300
     5.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Fri Oct 12 16:06:32 2012 +0300
     5.3 @@ -16,6 +16,8 @@
     5.4  import java.util.ArrayList;
     5.5  import java.util.Iterator;
     5.6  import java.util.List;
     5.7 +import java.util.regex.Matcher;
     5.8 +import java.util.regex.Pattern;
     5.9  
    5.10  import org.openrdf.model.Resource;
    5.11  import org.openrdf.query.MalformedQueryException;
    5.12 @@ -44,6 +46,8 @@
    5.13  	private String user;
    5.14  	private String password;
    5.15  	private String dbBackend;
    5.16 +	private int maxLimit;
    5.17 +	private String prefixes;
    5.18  	
    5.19  	private Strabon strabon = null;
    5.20  	
    5.21 @@ -51,7 +55,8 @@
    5.22  	private List<StrabonBeanWrapperConfiguration> entries;
    5.23  
    5.24  	public StrabonBeanWrapper(String databaseName, String user, String password, 
    5.25 -			int port, String serverName, boolean checkForLockTable, String dbBackend, List<List<String>> args) {
    5.26 +			int port, String serverName, boolean checkForLockTable, String dbBackend, 
    5.27 +			int maxLimit, String prefixes, 	List<List<String>> args) {
    5.28  		this.serverName = serverName;
    5.29  		this.port = port;
    5.30  		this.databaseName = databaseName;
    5.31 @@ -59,6 +64,8 @@
    5.32  		this.password = password;
    5.33  		this.checkForLockTable = checkForLockTable;
    5.34  		this.dbBackend = dbBackend;
    5.35 +		this.maxLimit = maxLimit;
    5.36 +		this.prefixes = prefixes;
    5.37  		this.entries = new ArrayList<StrabonBeanWrapperConfiguration>(args.size());
    5.38  		
    5.39  		Iterator<List<String>> entryit = args.iterator();
    5.40 @@ -180,7 +187,6 @@
    5.41  		if ((this.strabon == null) && (!init())) {
    5.42  			throw new RepositoryException("Could not connect to Strabon.");
    5.43  		} 
    5.44 -
    5.45  		strabon.query(queryString, Format.fromString(answerFormatStrabon), strabon.getSailRepoConnection(), out);
    5.46  		
    5.47  	}
    5.48 @@ -338,5 +344,44 @@
    5.49  		return this.entries.get(i);
    5.50  	}
    5.51  	
    5.52 +	/*
    5.53 +	 * Limit the number of solutions returned.
    5.54 +	 * */
    5.55 +	public String addLimit(String queryString, String maxLimit){
    5.56 +		String limitedQuery = queryString;
    5.57 +		int max;
    5.58 +		
    5.59 +		if(maxLimit == null)
    5.60 +			max = this.maxLimit;
    5.61 +		else
    5.62 +			max = Integer.valueOf(maxLimit);		
    5.63 +		
    5.64 +		if(max > 0)
    5.65 +		{	
    5.66 +			Pattern limitPattern = Pattern.compile(".*limit \\d+", Pattern.DOTALL);							
    5.67 +			Matcher limitMatcher = limitPattern.matcher(queryString);
    5.68 +			
    5.69 +			// check whether the query contains a limit clause
    5.70 +			if(limitMatcher.matches())		
    5.71 +			{								
    5.72 +				Pattern rowsNumberPattern = Pattern.compile("\\d+$");				
    5.73 +				Matcher rowsNumberMatcher = rowsNumberPattern.matcher(queryString);
    5.74 +				rowsNumberMatcher.find();				
    5.75 +				
    5.76 +				// if the initial limit is greater than the maximum, set it to the maximum
    5.77 +				if(Integer.valueOf(rowsNumberMatcher.group()) > max)
    5.78 +					limitedQuery = rowsNumberMatcher.replaceAll(String.valueOf(max));			
    5.79 +			}	
    5.80 +			else // add a limit to the query 
    5.81 +				limitedQuery = queryString+" limit "+max;
    5.82 +		
    5.83 +		}
    5.84 +		return limitedQuery;
    5.85 +	}
    5.86 +	
    5.87 +	public String getPrefixes() {
    5.88 +		return prefixes;
    5.89 +	}
    5.90 +
    5.91  }
    5.92  
     6.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Fri Oct 12 16:06:04 2012 +0300
     6.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqlkml/stSPARQLResultsKMLWriter.java	Fri Oct 12 16:06:32 2012 +0300
     6.3 @@ -47,100 +47,89 @@
     6.4   * @author Manos Karpathiotakis <mk@di.uoa.gr>
     6.5   * @author Charalampos Nikolaou <charnik@di.uoa.gr>
     6.6   * @author Panayiotis Smeros <psmeros@di.uoa.gr>
     6.7 - *
     6.8 + * 
     6.9   */
    6.10  public class stSPARQLResultsKMLWriter implements TupleQueryResultWriter
    6.11  {
    6.12  	private static final Logger logger = LoggerFactory.getLogger(org.openrdf.query.resultio.sparqlkml.stSPARQLResultsKMLWriter.class);
    6.13 -	private static final String ROOT_TAG 			= "kml";
    6.14 -	private static final String NAMESPACE 			= "http://www.opengis.net/kml/2.2";
    6.15 -	private static final String RESULT_SET_TAG 		= "Folder";
    6.16 -	private static final String PLACEMARK_TAG 		= "Placemark";
    6.17 -	private static final String NAME_TAG 			= "name";
    6.18 -	private static final String DESC_TAG 			= "description";
    6.19 -	private static final String STYLE_TAG 			= "Style";
    6.20 -	private static final String STYLEMAP_TAG 		= "StyleMap";
    6.21 -	private static final String LINESTYLE_TAG 		= "LineStyle";
    6.22 -	private static final String POLYSTYLE_TAG 		= "PolyStyle";
    6.23 -	private static final String STYLE_ID 			= "resultStyle";
    6.24 -	private static final String TABLE_ROW_BEGIN 	= "<TR>";
    6.25 -	private static final String TABLE_ROW_END 		= "</TR>";
    6.26 -	private static final String TABLE_DATA_BEGIN 	= "<TD>";
    6.27 -	private static final String TABLE_DATA_END 		= "</TD>";
    6.28 -	private static final String NEWLINE 			= "\n";
    6.29 -	private static final String TABLE_DESC_BEGIN 	= "<![CDATA[<TABLE border=\"1\">" + NEWLINE;
    6.30 -	private static final String TABLE_DESC_END 		= "</TABLE>]]>" + NEWLINE;
    6.31 -	private static final String GEOMETRY_NAME 		= "Geometry";
    6.32 -	private static final String MULTIGEOMETRY 		= "MultiGeometry";
    6.33 -	
    6.34 +	private static final String ROOT_TAG = "kml";
    6.35 +	private static final String NAMESPACE = "http://www.opengis.net/kml/2.2";
    6.36 +	private static final String RESULT_SET_TAG = "Folder";
    6.37 +	private static final String PLACEMARK_TAG = "Placemark";
    6.38 +	private static final String NAME_TAG = "name";
    6.39 +	private static final String DESC_TAG = "description";
    6.40 +	private static final String STYLE_TAG = "Style";
    6.41 +	private static final String STYLEMAP_TAG = "StyleMap";
    6.42 +	private static final String LINESTYLE_TAG = "LineStyle";
    6.43 +	private static final String POLYSTYLE_TAG = "PolyStyle";
    6.44 +	private static final String STYLE_ID = "resultStyle";
    6.45 +	private static final String TABLE_ROW_BEGIN = "<TR>";
    6.46 +	private static final String TABLE_ROW_END = "</TR>";
    6.47 +	private static final String TABLE_DATA_BEGIN = "<TD>";
    6.48 +	private static final String TABLE_DATA_END = "</TD>";
    6.49 +	private static final String NEWLINE = "\n";
    6.50 +	private static final String TABLE_DESC_BEGIN = "<![CDATA[<TABLE border=\"1\">" + NEWLINE;
    6.51 +	private static final String TABLE_DESC_END = "</TABLE>]]>" + NEWLINE;
    6.52 +	private static final String GEOMETRY_NAME = "Geometry";
    6.53 +	private static final String MULTIGEOMETRY = "MultiGeometry";
    6.54  	// Styling options
    6.55 -	private static final int numOfStyles 			= 5;
    6.56 -	
    6.57 +	private static final int numOfStyles = 5;
    6.58  	private static final String[][] styles = {
    6.59  		// note that colors are encoded as "aabbggrr" strings where
    6.60 -		// aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); rr=red
    6.61 +		// aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff);
    6.62 +		// rr=red
    6.63  		// (00 to ff).
    6.64  		// id, line width, line color, polygon fill, mouse over line width,
    6.65  		// mouse over line color mouse over polygon fill
    6.66 -		{STYLE_ID + "1", "1.5", "7dff0000", "adff0000", "1.5", "7d0000ff", "ad0000ff"}, 
    6.67 -		{STYLE_ID + "2", "1.5", "7d00ff00", "ad00ff00", "1.5", "7d0000ff", "ad0000ff"}, 
    6.68 -		{STYLE_ID + "3", "1.5", "7d550000", "ad550000", "1.5", "7d0000ff", "ad0000ff"}, 
    6.69 -		{STYLE_ID + "4", "1.5", "7d005500", "ad005500", "1.5", "7d0000ff", "ad0000ff"}, 
    6.70 -		{STYLE_ID + "5", "1.5", "7d000055", "ad000055", "1.5", "7d0000ff", "ad0000ff"}};
    6.71 -
    6.72 +	//{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"}};
    6.73 +		{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"}};
    6.74  	/**
    6.75  	 * The underlying XML formatter.
    6.76  	 */
    6.77  	private stSPARQLXMLWriter xmlWriter;
    6.78 -	
    6.79  	/**
    6.80  	 * The number of results seen.
    6.81  	 */
    6.82  	private int nresults;
    6.83 -	
    6.84  	/**
    6.85  	 * The number of geometries seen.
    6.86  	 */
    6.87  	private int ngeometries;
    6.88 -	
    6.89  	/**
    6.90  	 * The JTS wrapper
    6.91  	 */
    6.92  	private JTSWrapper jts;
    6.93 -	
    6.94  	/**
    6.95  	 * Stream for manipulating geometries
    6.96  	 */
    6.97  	private ByteArrayOutputStream baos;
    6.98 -	
    6.99  	/**
   6.100 -	 * Description string holding the projected variables
   6.101 -	 * of the SPARQL query
   6.102 +	 * Description string holding the projected variables of the SPARQL query
   6.103  	 */
   6.104  	private StringBuilder descHeader;
   6.105 -	
   6.106  	/**
   6.107 -	 * Description string holding the values for the
   6.108 -	 * projected variables of the SPARQL query
   6.109 +	 * Description string holding the values for the projected variables of the
   6.110 +	 * SPARQL query
   6.111  	 */
   6.112  	private StringBuilder descData;
   6.113 -	
   6.114  	/**
   6.115  	 * Indentation used in tags that are constructed manually
   6.116  	 */
   6.117  	private int depth;
   6.118  
   6.119  	/**
   6.120 -	 * Creates an stSPARQLResultsKMLWriter that encodes the SPARQL
   6.121 -	 * results in KML.
   6.122 +	 * Creates an stSPARQLResultsKMLWriter that encodes the SPARQL results in
   6.123 +	 * KML.
   6.124  	 * 
   6.125  	 * @param out
   6.126  	 */
   6.127 -	public stSPARQLResultsKMLWriter(OutputStream out) {
   6.128 -		this (new stSPARQLXMLWriter(out));
   6.129 +	public stSPARQLResultsKMLWriter(OutputStream out)
   6.130 +	{
   6.131 +		this(new stSPARQLXMLWriter(out));
   6.132  	}
   6.133  
   6.134 -	public stSPARQLResultsKMLWriter(stSPARQLXMLWriter writer) {
   6.135 +	public stSPARQLResultsKMLWriter(stSPARQLXMLWriter writer)
   6.136 +	{
   6.137  		xmlWriter = writer;
   6.138  		xmlWriter.setPrettyPrint(true);
   6.139  		depth = 4;
   6.140 @@ -152,15 +141,17 @@
   6.141  		ngeometries = 0;
   6.142  	}
   6.143  
   6.144 -	@Override
   6.145 -	public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
   6.146 -		try {
   6.147 +	@Override public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException
   6.148 +	{
   6.149 +		try
   6.150 +		{
   6.151  			xmlWriter.startDocument();
   6.152  			xmlWriter.setAttribute("xmlns", NAMESPACE);
   6.153  			xmlWriter.startTag(ROOT_TAG);
   6.154  			xmlWriter.startTag(RESULT_SET_TAG);
   6.155  			// add default styles
   6.156 -			for (String[] style: styles) {
   6.157 +			for(String[] style: styles)
   6.158 +			{
   6.159  				String id = style[0];
   6.160  				String lineWidth = style[1];
   6.161  				String lineColor = style[2];
   6.162 @@ -168,7 +159,7 @@
   6.163  				String mouseOverLineWidth = style[4];
   6.164  				String mouseOverLineColor = style[5];
   6.165  				String mouseOverPolygonFill = style[6];
   6.166 -				//append normal style
   6.167 +				// append normal style
   6.168  				xmlWriter.setAttribute("id", "normal_" + id);
   6.169  				xmlWriter.startTag(STYLE_TAG);
   6.170  				xmlWriter.startTag(LINESTYLE_TAG);
   6.171 @@ -179,7 +170,7 @@
   6.172  				xmlWriter.textElement("color", polygonFill);
   6.173  				xmlWriter.endTag(POLYSTYLE_TAG);
   6.174  				xmlWriter.endTag(STYLE_TAG);
   6.175 -				//append highlight style
   6.176 +				// append highlight style
   6.177  				xmlWriter.setAttribute("id", "highlight_" + id);
   6.178  				xmlWriter.startTag(STYLE_TAG);
   6.179  				xmlWriter.startTag(LINESTYLE_TAG);
   6.180 @@ -190,7 +181,7 @@
   6.181  				xmlWriter.textElement("color", mouseOverPolygonFill);
   6.182  				xmlWriter.endTag(POLYSTYLE_TAG);
   6.183  				xmlWriter.endTag(STYLE_TAG);
   6.184 -				//define map style combining the above styles
   6.185 +				// define map style combining the above styles
   6.186  				xmlWriter.setAttribute("id", id);
   6.187  				xmlWriter.startTag(STYLEMAP_TAG);
   6.188  				xmlWriter.startTag("Pair");
   6.189 @@ -204,226 +195,252 @@
   6.190  				xmlWriter.endTag(STYLEMAP_TAG);
   6.191  			}
   6.192  			// end of default style definition
   6.193 -			
   6.194 -		} catch (IOException e) {
   6.195 +		}
   6.196 +		catch(IOException e)
   6.197 +		{
   6.198  			throw new TupleQueryResultHandlerException(e);
   6.199  		}
   6.200  	}
   6.201  
   6.202 -	@Override
   6.203 -	public void endQueryResult() throws TupleQueryResultHandlerException {
   6.204 -		try {
   6.205 +	@Override public void endQueryResult() throws TupleQueryResultHandlerException
   6.206 +	{
   6.207 +		try
   6.208 +		{
   6.209  			xmlWriter.endTag(RESULT_SET_TAG);
   6.210  			xmlWriter.endTag(ROOT_TAG);
   6.211  			xmlWriter.endDocument();
   6.212  			baos.close();
   6.213 -			
   6.214 -			if (ngeometries < nresults) {
   6.215 +			if(ngeometries < nresults)
   6.216 +			{
   6.217  				logger.warn("[Strabon.KMLWriter] No spatial binding found in the result. KML requires that at least one binding maps to a geometry.", nresults);
   6.218  			}
   6.219 -		} catch (IOException e) {
   6.220 +		}
   6.221 +		catch(IOException e)
   6.222 +		{
   6.223  			throw new TupleQueryResultHandlerException(e);
   6.224  		}
   6.225  	}
   6.226  
   6.227 -	@Override
   6.228 -	public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
   6.229 -		try {
   6.230 +	@Override public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException
   6.231 +	{
   6.232 +		try
   6.233 +		{
   6.234 +			int numOfGeometries=0;
   6.235  			// true if there are bindings that do not correspond to geometries
   6.236  			boolean hasDesc = false;
   6.237 -			
   6.238  			// increase result size
   6.239  			nresults++;
   6.240 -			
   6.241  			// create description table and header
   6.242  			indent(descHeader, depth);
   6.243  			descHeader.append(TABLE_DESC_BEGIN);
   6.244  			indent(descHeader, depth);
   6.245 -			
   6.246 -			List<String> polygons = new ArrayList<String>();
   6.247 -			
   6.248 +			List<String> geometries = new ArrayList<String>();
   6.249  			// parse binding set
   6.250 -			for (Binding binding: bindingSet) {
   6.251 +			for(Binding binding: bindingSet)
   6.252 +			{
   6.253  				Value value = binding.getValue();
   6.254  				// check for geometry value
   6.255 -				if (XMLGSDatatypeUtil.isGeometryValue(value)) {
   6.256 +				if(XMLGSDatatypeUtil.isGeometryValue(value))
   6.257 +				{
   6.258 +					numOfGeometries++;
   6.259  					ngeometries++;
   6.260 -					if (logger.isDebugEnabled()) {
   6.261 +					if(logger.isDebugEnabled())
   6.262 +					{
   6.263  						logger.debug("[Strabon] Found geometry: {}", value);
   6.264  					}
   6.265 -					polygons.add(getPolygon(value));
   6.266 -					
   6.267 -				} else { // URI, BlankNode, or Literal other than spatial literal
   6.268 -					if (logger.isDebugEnabled()) {
   6.269 +					geometries.add(getGeometry(value));
   6.270 +				}
   6.271 +				else
   6.272 +				{ // URI, BlankNode, or Literal other than spatial
   6.273 +					// literal
   6.274 +					if(logger.isDebugEnabled())
   6.275 +					{
   6.276  						logger.debug("[Strabon.KMLWriter] Found URI/BlankNode/Literal ({}): {}", value.getClass(), value);
   6.277  					}
   6.278 -					
   6.279  					// mark that we found sth corresponding to the description
   6.280  					hasDesc = true;
   6.281 -					
   6.282  					// write description
   6.283  					writeDesc(binding);
   6.284  				}
   6.285  			}
   6.286 -			
   6.287 -			// write each polygon in separate placemarks
   6.288 -			for (String polygon : polygons) {
   6.289 -				xmlWriter.startTag(PLACEMARK_TAG);			
   6.290 -				xmlWriter.textElement("styleUrl", "#" + styles[polygons.indexOf(polygon)%(numOfStyles-2)][0]);
   6.291 -
   6.292 -				xmlWriter.startTag(MULTIGEOMETRY);
   6.293 -				xmlWriter.unescapedText(polygon);
   6.294 -				xmlWriter.endTag(MULTIGEOMETRY);
   6.295 -				
   6.296 -				xmlWriter.endTag(PLACEMARK_TAG);
   6.297 +			if(numOfGeometries > 1)
   6.298 +			{
   6.299 +				// write each polygon in separate placemarks
   6.300 +				for(String geometry: geometries)
   6.301 +				{
   6.302 +					xmlWriter.startTag(PLACEMARK_TAG);
   6.303 +					xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME);
   6.304 +					xmlWriter.textElement("styleUrl", "#" + styles[geometries.indexOf(geometry) % (numOfStyles - 2)][0]);
   6.305 +					xmlWriter.startTag(MULTIGEOMETRY);
   6.306 +					xmlWriter.unescapedText(geometry);
   6.307 +					xmlWriter.endTag(MULTIGEOMETRY);
   6.308 +					xmlWriter.endTag(PLACEMARK_TAG);
   6.309 +				}
   6.310  			}
   6.311 -			
   6.312 -			//also write them in the same placemarks
   6.313 -			
   6.314 +			// also write them in the same placemarks
   6.315  			xmlWriter.startTag(PLACEMARK_TAG);
   6.316  			xmlWriter.textElement(NAME_TAG, GEOMETRY_NAME);
   6.317 -			xmlWriter.textElement("styleUrl", "#" + styles[(numOfStyles-1)][0]);
   6.318 -			
   6.319 +			xmlWriter.textElement("styleUrl", "#" + styles[(numOfStyles - 1)][0]);
   6.320  			xmlWriter.startTag(MULTIGEOMETRY);
   6.321 -			for(String polygon : polygons) {
   6.322 -				xmlWriter.unescapedText(polygon);
   6.323 +			for(String geometry: geometries)
   6.324 +			{
   6.325 +				xmlWriter.unescapedText(geometry);
   6.326  			}
   6.327  			xmlWriter.endTag(MULTIGEOMETRY);
   6.328 -			
   6.329  			// we have found and constructed a description for this result.
   6.330  			// Write it down.
   6.331 -			if (hasDesc) {
   6.332 +			if(hasDesc)
   6.333 +			{
   6.334  				// end the placeholder for the description data
   6.335  				indent(descData, depth);
   6.336 -				
   6.337  				// append to the table header the actual content from
   6.338  				// the bindings
   6.339  				descHeader.append(descData);
   6.340 -				
   6.341  				// close the table for the description
   6.342  				descHeader.append(NEWLINE);
   6.343  				indent(descHeader, depth);
   6.344  				descHeader.append(TABLE_DESC_END);
   6.345 -				
   6.346  				// begin the "description" tag
   6.347  				xmlWriter.startTag(DESC_TAG);
   6.348 -				
   6.349  				// write the actual description
   6.350  				xmlWriter.unescapedText(descHeader.toString());
   6.351 -				
   6.352  				// end the "description" tag
   6.353  				xmlWriter.endTag(DESC_TAG);
   6.354  			}
   6.355  			// clear description string builders
   6.356  			descHeader.setLength(0);
   6.357  			descData.setLength(0);
   6.358 -
   6.359  			xmlWriter.endTag(PLACEMARK_TAG);
   6.360 -			
   6.361 -		} catch (IOException e) {
   6.362 +		}
   6.363 +		catch(IOException e)
   6.364 +		{
   6.365  			throw new TupleQueryResultHandlerException(e);
   6.366  		}
   6.367  	}
   6.368  
   6.369 -	private String getPolygon(Value value) {
   6.370 -		String polygon = "";
   6.371 +	private String getGeometry(Value value)
   6.372 +	{
   6.373 +		String geometry = "";
   6.374  		QName geometryType = null;
   6.375 -		
   6.376  		// the underlying geometry in value
   6.377  		Geometry geom = null;
   6.378 -		
   6.379  		// the underlying SRID of the geometry
   6.380  		int srid = -1;
   6.381 -		
   6.382  		// get the KML encoder
   6.383  		Encoder encoder = null;
   6.384 -		
   6.385 -		try {
   6.386 +		try
   6.387 +		{
   6.388  			encoder = new Encoder(new KMLConfiguration());
   6.389  			encoder.setIndenting(true);
   6.390 -			
   6.391 -			if (value instanceof GeneralDBPolyhedron) {
   6.392 +			if(value instanceof GeneralDBPolyhedron)
   6.393 +			{
   6.394  				GeneralDBPolyhedron dbpolyhedron = (GeneralDBPolyhedron) value;
   6.395  				geom = dbpolyhedron.getPolyhedron().getGeometry();
   6.396  				srid = dbpolyhedron.getPolyhedron().getGeometry().getSRID();
   6.397 -				
   6.398 -			} else { // spatial literal
   6.399 +			}
   6.400 +			else
   6.401 +			{ // spatial literal
   6.402  				Literal spatial = (Literal) value;
   6.403  				String geomRep = spatial.stringValue();
   6.404 -				
   6.405 -				if (XMLGSDatatypeUtil.isWKTLiteral(spatial)) { // WKT
   6.406 +				if(XMLGSDatatypeUtil.isWKTLiteral(spatial))
   6.407 +				{ // WKT
   6.408  					geom = jts.WKTread(WKTHelper.getWithoutSRID(geomRep));
   6.409  					srid = WKTHelper.getSRID(geomRep);
   6.410 -				} else { // GML
   6.411 +				}
   6.412 +				else
   6.413 +				{ // GML
   6.414  					geom = jts.GMLread(geomRep);
   6.415  					srid = geom.getSRID();
   6.416  				}
   6.417  			}
   6.418 -
   6.419  			// transform the geometry to {@link GeoConstants#defaultSRID}
   6.420  			geom = jts.transform(geom, srid, GeoConstants.defaultSRID);
   6.421 -			if (geom instanceof Point) {
   6.422 +			if(geom instanceof Point)
   6.423 +			{
   6.424  				geometryType = KML.Point;
   6.425 -				
   6.426 -			} else if (geom instanceof Polygon) {
   6.427 +			}
   6.428 +			else if(geom instanceof Polygon)
   6.429 +			{
   6.430  				geometryType = KML.Polygon;
   6.431 -				
   6.432 -			} else if (geom instanceof LineString) {
   6.433 +			}
   6.434 +			else if(geom instanceof LineString)
   6.435 +			{
   6.436  				geometryType = KML.LineString;
   6.437 -				
   6.438 -			} else if (geom instanceof MultiPoint) {
   6.439 -				geometryType = KML.MultiGeometry;
   6.440 -				
   6.441 -			} else if (geom instanceof MultiLineString) {
   6.442 -				geometryType = KML.MultiGeometry;
   6.443 -				
   6.444 -			} else if (geom instanceof MultiPolygon) {
   6.445 -				geometryType = KML.MultiGeometry;
   6.446 -				
   6.447 -			} else if (geom instanceof GeometryCollection) {
   6.448 +			}
   6.449 +			else if(geom instanceof MultiPoint)
   6.450 +			{
   6.451  				geometryType = KML.MultiGeometry;
   6.452  			}
   6.453 -			
   6.454 -			if (geometryType == null) {
   6.455 +			else if(geom instanceof MultiLineString)
   6.456 +			{
   6.457 +				geometryType = KML.MultiGeometry;
   6.458 +			}
   6.459 +			else if(geom instanceof MultiPolygon)
   6.460 +			{
   6.461 +				geometryType = KML.MultiGeometry;
   6.462 +			}
   6.463 +			else if(geom instanceof GeometryCollection)
   6.464 +			{
   6.465 +				geometryType = KML.MultiGeometry;
   6.466 +			}
   6.467 +			if(geometryType == null)
   6.468 +			{
   6.469  				logger.warn("[Strabon.KMLWriter] Found unknown geometry type.");
   6.470 -				
   6.471 -			} else {
   6.472 +			}
   6.473 +			else
   6.474 +			{
   6.475  				encoder.encode(geom, geometryType, baos);
   6.476 -				polygon = baos.toString().substring(38).replaceAll(" xmlns:kml=\"http://earth.google.com/kml/2.1\"", "").replaceAll("kml:", "");
   6.477 -				
   6.478 -				// get the polygon from the kml
   6.479 -				//FIXME: test
   6.480 -				//polygon = polygon.substring(polygon.indexOf("<Polygon>"), polygon.indexOf("</Polygon>")+10);
   6.481 -				if (geom instanceof Point) {
   6.482 -					polygon = polygon.substring(polygon.indexOf("<Point>"), polygon.indexOf("</Point>")+8);
   6.483 -				} else if (geom instanceof Polygon) {
   6.484 -					polygon = polygon.substring(polygon.indexOf("<Polygon>"), polygon.indexOf("</Polygon>")+10);
   6.485 -				} else if (geom instanceof LineString) {
   6.486 -					polygon = polygon.substring(polygon.indexOf("<LineString>"), polygon.indexOf("</LineString>")+13);
   6.487 -				} else if (geom instanceof MultiPoint) {
   6.488 -					polygon = polygon.substring(polygon.indexOf("<MultiPoint>"), polygon.indexOf("</MultiPoint>")+13);
   6.489 -				} else if (geom instanceof MultiLineString) {
   6.490 -					polygon = polygon.substring(polygon.indexOf("<MultiLineString>"), polygon.indexOf("</MultiLineString>")+18);
   6.491 -				} else if (geom instanceof MultiPolygon) {
   6.492 -					polygon = polygon.substring(polygon.indexOf("<MultiPolygon>"), polygon.indexOf("</MultiPolygon>")+15);
   6.493 -				} else if (geom instanceof GeometryCollection) {
   6.494 -					polygon = polygon.substring(polygon.indexOf("<GeometryCollection>"), polygon.indexOf("</GeometryCollection>")+21);
   6.495 +				geometry = baos.toString().substring(38).replaceAll(" xmlns:kml=\"http://earth.google.com/kml/2.1\"", "").replaceAll("kml:", "");
   6.496 +
   6.497 +				if(geometryType == KML.MultiGeometry)
   6.498 +				{
   6.499 +					geometry = geometry.substring(geometry.indexOf("<MultiGeometry>") + 15, geometry.indexOf("</MultiGeometry>"));
   6.500  				}
   6.501 -				//FIXME: test end 
   6.502 +/*				if(geom instanceof Point)
   6.503 +				{
   6.504 +					geometry = geometry.substring(geometry.indexOf("<Point>"), geometry.indexOf("</Point>") + 8);
   6.505 +				}
   6.506 +				else if(geom instanceof Polygon)
   6.507 +				{
   6.508 +					geometry = geometry.substring(geometry.indexOf("<Polygon>"), geometry.indexOf("</Polygon>") + 10);
   6.509 +				}
   6.510 +				else if(geom instanceof LineString)
   6.511 +				{
   6.512 +					geometry = geometry.substring(geometry.indexOf("<LineString>"), geometry.indexOf("</LineString>") + 13);
   6.513 +				}
   6.514 +				else if(geom instanceof MultiPoint)
   6.515 +				{
   6.516 +					geometry = geometry.substring(geometry.indexOf("<MultiPoint>"), geometry.indexOf("</MultiPoint>") + 13);
   6.517 +				}
   6.518 +				else if(geom instanceof MultiLineString)
   6.519 +				{
   6.520 +					geometry = geometry.substring(geometry.indexOf("<MultiLineString>"), geometry.indexOf("</MultiLineString>") + 18);
   6.521 +				}
   6.522 +				else if(geom instanceof MultiPolygon)
   6.523 +				{
   6.524 +					geometry = geometry.substring(geometry.indexOf("<MultiPolygon>"), geometry.indexOf("</MultiPolygon>") + 15);
   6.525 +				}
   6.526 +				else if(geom instanceof GeometryCollection)
   6.527 +				{
   6.528 +					geometry = geometry.substring(geometry.indexOf("<GeometryCollection>"), geometry.indexOf("</GeometryCollection>") + 21);
   6.529 +				}
   6.530 +*/				
   6.531  				baos.reset();
   6.532  			}
   6.533 -		} catch (ParseException e) {
   6.534 +		}
   6.535 +		catch(ParseException e)
   6.536 +		{
   6.537  			logger.error("[Strabon.KMLWriter] Parse error exception of geometry: {}", e.getMessage());
   6.538 -			
   6.539 -		} catch (IOException e) {
   6.540 +		}
   6.541 +		catch(IOException e)
   6.542 +		{
   6.543  			logger.error("[Strabon.KMLWriter] IOException during KML encoding of geometry: {}", e.getMessage());
   6.544 -			
   6.545 -		} catch (JAXBException e) {
   6.546 +		}
   6.547 +		catch(JAXBException e)
   6.548 +		{
   6.549  			logger.error("[Strabon.KMLWriter] Exception during GML parsing: {}", e.getMessage());
   6.550 -			
   6.551  		}
   6.552 -		
   6.553 -		return polygon;
   6.554 +		return geometry;
   6.555  	}
   6.556  
   6.557  	/**
   6.558 @@ -431,41 +448,40 @@
   6.559  	 * 
   6.560  	 * @param binding
   6.561  	 */
   6.562 -	private void writeDesc(Binding binding) {
   6.563 +	private void writeDesc(Binding binding)
   6.564 +	{
   6.565  		descData.append(NEWLINE);
   6.566  		indent(descData, depth + 1);
   6.567  		descData.append(TABLE_ROW_BEGIN);
   6.568 -
   6.569  		descData.append(TABLE_DATA_BEGIN);
   6.570  		descData.append(binding.getName());
   6.571  		descData.append(TABLE_DATA_END);
   6.572 -		
   6.573  		descData.append(TABLE_DATA_BEGIN);
   6.574 -		
   6.575 -		if (binding.getValue() instanceof BNode) {
   6.576 +		if(binding.getValue() instanceof BNode)
   6.577 +		{
   6.578  			descData.append("_:");
   6.579  		}
   6.580 -		
   6.581  		descData.append(binding.getValue().stringValue());
   6.582  		descData.append(TABLE_DATA_END);
   6.583 -		
   6.584  		descData.append(TABLE_ROW_END);
   6.585  	}
   6.586  
   6.587 -	@Override
   6.588 -	public TupleQueryResultFormat getTupleQueryResultFormat() {
   6.589 +	@Override public TupleQueryResultFormat getTupleQueryResultFormat()
   6.590 +	{
   6.591  		return stSPARQLQueryResultFormat.KML;
   6.592  	}
   6.593  
   6.594  	/**
   6.595 -	 * Adds indentation to the given string builder according to 
   6.596 -	 * the specified depth.
   6.597 +	 * Adds indentation to the given string builder according to the specified
   6.598 +	 * depth.
   6.599  	 * 
   6.600  	 * @param sb
   6.601  	 * @param depth
   6.602  	 */
   6.603 -	private void indent(StringBuilder sb, int depth) {
   6.604 -		for (int i = 0; i < depth; i++) {
   6.605 +	private void indent(StringBuilder sb, int depth)
   6.606 +	{
   6.607 +		for(int i = 0; i < depth; i++)
   6.608 +		{
   6.609  			sb.append(xmlWriter.getIndentString());
   6.610  		}
   6.611  	}
     7.1 --- a/scripts/endpoint	Fri Oct 12 16:06:04 2012 +0300
     7.2 +++ b/scripts/endpoint	Fri Oct 12 16:06:32 2012 +0300
     7.3 @@ -91,7 +91,8 @@
     7.4  
     7.5  # if set to 1, then only the command to be executed is printed
     7.6  DEBUG=0
     7.7 -
     7.8 +# set default limit
     7.9 +MAXLIMIT=0
    7.10  case "${1}" in
    7.11  	-d)
    7.12  		shift
    7.13 @@ -102,6 +103,11 @@
    7.14  		PREFIXES="$(cat ${PREFIXES_FILE})
    7.15  "
    7.16  		;;
    7.17 +	-l)
    7.18 +		MAXLIMIT="${2}"	
    7.19 +		shift
    7.20 +		shift
    7.21 +		;;
    7.22  esac
    7.23  
    7.24  case "${1}" in
    7.25 @@ -183,7 +189,7 @@
    7.26  				;;
    7.27  		esac
    7.28  
    7.29 -		EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${QUERY}' ${URL}"
    7.30 +		EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${QUERY}' --data maxLimit='${MAXLIMIT}' ${URL}"
    7.31  		;;
    7.32  	queryfile)
    7.33  		shift