Strabon

changeset 337:6acd5a415449

copied all functionality for stSPARQLResultsGeoJSONWriter from SPARQLResultsJSONWriter class
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Jun 24 14:17:03 2012 +0300 (2012-06-24)
parents a605ffb0e55b
children d54aacf6aae7
files resultio/src/main/java/org/openrdf/query/resultio/sparqljson/stSPARQLResultsGeoJSONWriter.java
line diff
     1.1 --- a/resultio/src/main/java/org/openrdf/query/resultio/sparqljson/stSPARQLResultsGeoJSONWriter.java	Sun Jun 24 14:11:53 2012 +0300
     1.2 +++ b/resultio/src/main/java/org/openrdf/query/resultio/sparqljson/stSPARQLResultsGeoJSONWriter.java	Sun Jun 24 14:17:03 2012 +0300
     1.3 @@ -1,21 +1,50 @@
     1.4  package org.openrdf.query.resultio.sparqljson;
     1.5  
     1.6 +import info.aduna.io.IndentingWriter;
     1.7 +import info.aduna.text.StringUtil;
     1.8 +
     1.9 +import java.io.BufferedWriter;
    1.10 +import java.io.IOException;
    1.11  import java.io.OutputStream;
    1.12 +import java.io.OutputStreamWriter;
    1.13 +import java.io.Writer;
    1.14 +import java.nio.charset.Charset;
    1.15 +import java.util.Iterator;
    1.16  import java.util.List;
    1.17  
    1.18 +import org.openrdf.model.BNode;
    1.19 +import org.openrdf.model.Literal;
    1.20 +import org.openrdf.model.URI;
    1.21 +import org.openrdf.model.Value;
    1.22  import org.openrdf.query.BindingSet;
    1.23  import org.openrdf.query.TupleQueryResultHandlerException;
    1.24  import org.openrdf.query.resultio.TupleQueryResultFormat;
    1.25  import org.openrdf.query.resultio.TupleQueryResultWriter;
    1.26  
    1.27 +/**
    1.28 + * A TupleQueryResultWriter that writes query results in the <a
    1.29 + * href="http://www.geojson.org/geojson-spec.html/">GeoJSON Format</a>.
    1.30 + * 
    1.31 + * @author Charalampos Nikolaou <charnik@di.uoa.gr>
    1.32 + */
    1.33  public class stSPARQLResultsGeoJSONWriter implements TupleQueryResultWriter {
    1.34  
    1.35 +	private IndentingWriter writer;
    1.36 +	
    1.37  	public stSPARQLResultsGeoJSONWriter(OutputStream out) {
    1.38 +		Writer w = new OutputStreamWriter(out, Charset.forName("UTF-8"));
    1.39 +		w = new BufferedWriter(w, 1024);
    1.40 +		writer = new IndentingWriter(w);
    1.41  	}
    1.42  
    1.43  	@Override
    1.44  	public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    1.45 -		
    1.46 +		try {
    1.47 +			openBraces();
    1.48 +			
    1.49 +		} catch (IOException e) {
    1.50 +			throw new TupleQueryResultHandlerException(e);
    1.51 +		}
    1.52  	}
    1.53  
    1.54  	@Override
    1.55 @@ -30,4 +59,148 @@
    1.56  	public TupleQueryResultFormat getTupleQueryResultFormat() {
    1.57  		return TupleQueryResultFormat.JSON;
    1.58  	}
    1.59 +	
    1.60 +
    1.61 +	protected void writeKeyValue(String key, String value)
    1.62 +		throws IOException
    1.63 +	{
    1.64 +		writeKey(key);
    1.65 +		writeString(value);
    1.66 +	}
    1.67 +
    1.68 +	protected void writeKeyValue(String key, Value value)
    1.69 +		throws IOException, TupleQueryResultHandlerException
    1.70 +	{
    1.71 +		writeKey(key);
    1.72 +		writeValue(value);
    1.73 +	}
    1.74 +
    1.75 +	protected void writeKeyValue(String key, Iterable<String> array)
    1.76 +		throws IOException
    1.77 +	{
    1.78 +		writeKey(key);
    1.79 +		writeArray(array);
    1.80 +	}
    1.81 +
    1.82 +	protected void writeKey(String key)
    1.83 +		throws IOException
    1.84 +	{
    1.85 +		writeString(key);
    1.86 +		writer.write(": ");
    1.87 +	}
    1.88 +
    1.89 +	protected void writeValue(Value value)
    1.90 +		throws IOException, TupleQueryResultHandlerException
    1.91 +	{
    1.92 +		writer.write("{ ");
    1.93 +
    1.94 +		if (value instanceof URI) {
    1.95 +			writeKeyValue("type", "uri");
    1.96 +			writer.write(", ");
    1.97 +			writeKeyValue("value", ((URI)value).toString());
    1.98 +		}
    1.99 +		else if (value instanceof BNode) {
   1.100 +			writeKeyValue("type", "bnode");
   1.101 +			writer.write(", ");
   1.102 +			writeKeyValue("value", ((BNode)value).getID());
   1.103 +		}
   1.104 +		else if (value instanceof Literal) {
   1.105 +			Literal lit = (Literal)value;
   1.106 +
   1.107 +			if (lit.getDatatype() != null) {
   1.108 +				writeKeyValue("type", "typed-literal");
   1.109 +				writer.write(", ");
   1.110 +				writeKeyValue("datatype", lit.getDatatype().toString());
   1.111 +			}
   1.112 +			else {
   1.113 +				writeKeyValue("type", "literal");
   1.114 +				if (lit.getLanguage() != null) {
   1.115 +					writer.write(", ");
   1.116 +					writeKeyValue("xml:lang", lit.getLanguage());
   1.117 +				}
   1.118 +			}
   1.119 +
   1.120 +			writer.write(", ");
   1.121 +			writeKeyValue("value", lit.getLabel());
   1.122 +		}
   1.123 +		else {
   1.124 +			throw new TupleQueryResultHandlerException("Unknown Value object type: " + value.getClass());
   1.125 +		}
   1.126 +
   1.127 +		writer.write(" }");
   1.128 +	}
   1.129 +
   1.130 +	protected void writeString(String value) throws IOException
   1.131 +	{
   1.132 +		// Escape special characters
   1.133 +		value = StringUtil.gsub("\\", "\\\\", value);
   1.134 +		value = StringUtil.gsub("\"", "\\\"", value);
   1.135 +		value = StringUtil.gsub("/", "\\/", value);
   1.136 +		value = StringUtil.gsub("\b", "\\b", value);
   1.137 +		value = StringUtil.gsub("\f", "\\f", value);
   1.138 +		value = StringUtil.gsub("\n", "\\n", value);
   1.139 +		value = StringUtil.gsub("\r", "\\r", value);
   1.140 +		value = StringUtil.gsub("\t", "\\t", value);
   1.141 +
   1.142 +		writer.write("\"");
   1.143 +		writer.write(value);
   1.144 +		writer.write("\"");
   1.145 +	}
   1.146 +
   1.147 +	protected void writeArray(Iterable<String> array)
   1.148 +		throws IOException
   1.149 +	{
   1.150 +		writer.write("[ ");
   1.151 +
   1.152 +		Iterator<String> iter = array.iterator();
   1.153 +		while (iter.hasNext()) {
   1.154 +			String value = iter.next();
   1.155 +
   1.156 +			writeString(value);
   1.157 +
   1.158 +			if (iter.hasNext()) {
   1.159 +				writer.write(", ");
   1.160 +			}
   1.161 +		}
   1.162 +
   1.163 +		writer.write(" ]");
   1.164 +	}
   1.165 +
   1.166 +	protected void openArray() throws IOException
   1.167 +	{
   1.168 +		writer.write("[");
   1.169 +		writer.writeEOL();
   1.170 +		writer.increaseIndentation();
   1.171 +	}
   1.172 +
   1.173 +	protected void closeArray() throws IOException
   1.174 +	{
   1.175 +		writer.writeEOL();
   1.176 +		writer.decreaseIndentation();
   1.177 +		writer.write("]");
   1.178 +	}
   1.179 +
   1.180 +	protected void openBraces()
   1.181 +		throws IOException
   1.182 +	{
   1.183 +		writer.write("{");
   1.184 +		writer.writeEOL();
   1.185 +		writer.increaseIndentation();
   1.186 +	}
   1.187 +
   1.188 +	protected void closeBraces()
   1.189 +		throws IOException
   1.190 +	{
   1.191 +		writer.writeEOL();
   1.192 +		writer.decreaseIndentation();
   1.193 +		writer.write("}");
   1.194 +	}
   1.195 +
   1.196 +	protected void writeComma()
   1.197 +		throws IOException
   1.198 +	{
   1.199 +		writer.write(", ");
   1.200 +		writer.writeEOL();
   1.201 +	}
   1.202 +
   1.203  }