Strabon

view scripts/endpoint @ 965:69a0a785ef8f

added new "constants" module with artifactId "spatial-temporal-constants" and moved GeoConstants there
author Babis Nikolaou <charnik@di.uoa.gr>
date Tue Apr 02 13:27:01 2013 +0300 (2013-04-02)
parents b1caa46da857
children 8b8c2efcc293 93f5d7b2d3cb
line source
1 #!/bin/bash
3 #
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 #
8 # Copyright (C) 2010, 2011, 2012, Pyravlos Team
9 #
10 # http://www.strabon.di.uoa.gr/
11 #
13 #
14 # Script for executing SPARQL queries and SPARQL Update queries
15 # as well as storing RDF triples on a Strabon Endpoint.
16 #
17 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
18 #
20 # absolute directory name of this command
21 LOC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23 # this command
24 CMD="$(basename ${0})"
26 # Filename containing the prefixes to be included in SPARQL queries
27 PREFIXES_FILE="${LOC}/prefixes.sparql"
29 function help() {
30 echo "Usage: ${CMD} [OPTIONS] COMMAND ENDPOINT ARGS"
31 echo
32 echo "Execute SPARQL and SPARQL Update queries as well as store RDF triples on a Strabon endpoint."
33 echo
34 echo " COMMAND : one of query, queryfile, update, store, describe, or help"
35 echo " ENDPOINT : the URL of the Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint)"
36 echo " ARGS : arguments according to selected command"
37 echo
38 echo "OPTIONS can be any of the following"
39 echo " -d : don't run, just print what shall be executed"
40 echo " -i : include URI prefixes in the SPARQL query. Prefixes are taken from file"
41 echo " \`prefixes.sparql'"
42 }
44 function help_query() {
45 echo "Usage: ${CMD} query ENDPOINT SPARQL_QUERY [RESULT_FORMAT]"
46 echo
47 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
48 echo " SPARQL_QUERY : the SPARQL query to execute or the alias name corresponding to a"
49 echo " predefined query such as:"
50 echo " \`size': SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
51 echo " RESULT_FORMAT : the format of the result. Possible values are \`XML' (default), \`KML', \`KMZ', \`GeoJSON', \`HTML', or \`TSV'"
52 }
54 function help_queryfile() {
55 echo "Usage: ${CMD} queryfile ENDPOINT SPARQL_QUERY_FILE [RESULT_FORMAT]"
56 echo
57 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
58 echo " SPARQL_QUERY_FILE : the file that contain the SPARQL query to execute"
59 echo " RESULT_FORMAT : the format of the result. Possible values are \`KMLMAP\, \`GEOJSON', "
60 echo " \`HTML', \`KMZMAP', \`XML' (default), or \`KML'."
61 }
63 function help_update() {
64 echo "Usage: ${CMD} update ENDPOINT SPARQL_QUERY"
65 echo
66 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
67 echo " SPARQL_QUERY : the SPARQL update query to execute or the alias name corresponding to a"
68 echo " predefined query such as:"
69 echo " \`clear': DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
70 }
72 function help_store() {
73 echo "Usage: ${CMD} store ENDPOINT FORMAT -t TRIPLES|-u TRIPLES_URL"
74 echo
75 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
76 echo " FORMAT : the RDF format of the input (one of RDF/XML, N-Triples, Turtle, N3, TriX, TriG, or BinaryRDF)"
77 echo " TRIPLES : the RDF triples to store"
78 echo " TRIPLES_URL : the URL containing the RDF triples to store"
79 }
81 function help_describe() {
82 echo "Usage: ${CMD} describe ENDPOINT DESCRIBE_QUERY [RESULT_FORMAT]"
83 echo
84 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
85 echo " DESCRIBE_QUERY : the SPARQL DESCRIBE query to execute"
86 echo " RESULT_FORMAT : the format of the result. Possible values are \`N-Triples' (default)"
87 echo " \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
88 }
90 CURL_OPTS="-w HTTP_CODE='%{http_code}\n' -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Accept:text/xml\""
92 # if set to 1, then only the command to be executed is printed
93 DEBUG=0
94 # set default limit
95 MAXLIMIT=0
96 case "${1}" in
97 -d)
98 shift
99 DEBUG=1
100 ;;
101 -i)
102 shift
103 PREFIXES="$(cat ${PREFIXES_FILE})
104 "
105 ;;
106 -l)
107 MAXLIMIT="${2}"
108 shift
109 shift
110 ;;
111 esac
113 case "${1}" in
114 help)
115 shift
116 if test $# -eq 1; then
117 case "${1}" in
118 query)
119 help_query
120 ;;
121 queryfile)
122 help_queryfile
123 ;;
124 update)
125 help_update
126 ;;
127 store)
128 help_store
129 ;;
130 describe)
131 help_describe
132 ;;
133 *)
134 help
135 ;;
136 esac
137 exit 1
138 fi
139 help
140 exit 0
141 ;;
142 query)
143 shift
144 if ! test $# -ge 2; then
145 help_query
146 exit 1
147 fi
148 URL="${1}/Query"
149 QUERY="${2}"
151 shift
152 shift
153 # predefined queries
154 case "${QUERY}" in
155 size)
156 QUERY="SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
157 ;;
158 esac
160 # set default format
161 FORMAT="XML"
162 if test $# -eq 1; then
163 FORMAT="${1}"
164 fi
166 case "${FORMAT}" in
167 [Xx][Mm][Ll])
168 MIME_TYPE="application/sparql-results+xml"
169 ;;
170 [Tt][sS][vV])
171 MIME_TYPE="text/tab-separated-values"
172 ;;
173 [Kk][Mm][Ll])
174 MIME_TYPE="application/vnd.google-earth.kml+xml"
175 ;;
176 [Kk][mM][Zz])
177 MIME_TYPE="application/vnd.google-earth.kmz"
178 ;;
179 [Hh][Tt][Mm][Ll])
180 MIME_TYPE="text/html"
181 ;;
182 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
183 MIME_TYPE="application/json"
184 ;;
185 *)
186 echo "${CMD}: unknown format \"${FORMAT}\"."
187 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
188 exit 2
189 ;;
190 esac
192 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${QUERY}' --data maxLimit='${MAXLIMIT}' ${URL}"
193 ;;
194 queryfile)
195 shift
196 if ! test $# -ge 2; then
197 help_queryfile
198 exit 1
199 fi
201 if ! test -f ${2}; then
202 echo "${CMD}: File not found."
203 exit 2
204 fi
206 URL="${1}/Query"
207 QUERY=`cat ${2} | sed 's/\"/\\\"/g'`
209 shift
210 shift
212 # set default format
213 FORMAT="XML"
214 if test $# -eq 1; then
215 FORMAT="${1}"
216 fi
218 case "${FORMAT}" in
219 [Xx][Mm][Ll])
220 MIME_TYPE="application/sparql-results+xml"
221 ;;
222 [Tt][sS][vV])
223 MIME_TYPE="text/tab-separated-values"
224 ;;
225 [Kk][Mm][Ll])
226 MIME_TYPE="application/vnd.google-earth.kml+xml"
227 ;;
228 [Kk][mM][Zz])
229 MIME_TYPE="application/vnd.google-earth.kmz"
230 ;;
231 [Hh][Tt][Mm][Ll])
232 MIME_TYPE="text/html"
233 ;;
234 [Gg][Ee][Oo][Jj][Ss][Oo][Nn])
235 MIME_TYPE="application/json"
236 ;;
237 *)
238 echo "${CMD}: unknown format \"${FORMAT}\"."
239 echo "${CMD}: possible values are \`KML', \`KMZ', \`GeoJSON', \`XML' (default), \`HTML', or \`TSV'"
240 exit 2
241 ;;
242 esac
244 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
245 ;;
247 update)
248 shift
249 if ! test $# -eq 2; then
250 help_update
251 exit 1
252 fi
253 URL="${1}/Update"
254 QUERY="${2}"
256 # predefined queries
257 case "${QUERY}" in
258 clear)
259 QUERY="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
260 ;;
261 esac
263 EXEC="curl -u endpoint:3ndpo1nt ${CURL_OPTS} --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
264 ;;
265 store)
266 shift
267 if ! test $# -eq 4; then
268 help_store
269 exit 1
270 fi
271 URL="${1}/Store"
272 FORMAT="${2}"
273 MIME_TYPE=
274 case "${FORMAT}" in
275 N-Triples)
276 MIME_TYPE="text/plain"
277 ;;
278 RDF/XML)
279 MIME_TYPE="application/rdf+xml"
280 ;;
281 N3)
282 MIME_TYPE="text/rdf+n3"
283 ;;
284 TURTLE)
285 MIME_TYPE="text/turtle"
286 ;;
287 TRIG)
288 MIME_TYPE="application/x-trig"
289 ;;
290 TRIX)
291 MIME_TYPE="application/trix"
292 ;;
293 BinaryRDF)
294 MIME_TYPE="application/x-binary-rdf"
295 ;;
296 *)
297 echo "${CMD}: unknown format \"${FORMAT}\"."
298 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
299 exit 2
300 ;;
301 esac
303 case "${3}" in
304 -t)
305 TRIPLES="${4}"
306 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode data='${TRIPLES}' ${URL}"
307 ;;
308 -u)
310 URL_TRIPLES="${4}"
311 EXEC="curl -u endpoint:3ndpo1nt -w '\nHTTP_CODE=%{http_code}\n' -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode url='${URL_TRIPLES}' -d fromurl='' ${URL}"
312 ;;
313 *)
314 help_store
315 exit 1
316 ;;
317 esac
318 ;;
319 describe)
320 shift
321 if ! test $# -ge 2; then
322 help_describe
323 exit 1
324 fi
325 URL="${1}/Describe"
326 QUERY="${2}"
328 shift
329 shift
331 # set default format
332 FORMAT="N-Triples"
333 if test $# -eq 1; then
334 FORMAT="${1}"
335 fi
337 MIME_TYPE=
338 case "${FORMAT}" in
339 N-Triples)
340 MIME_TYPE="text/plain"
341 ;;
342 RDF/XML)
343 MIME_TYPE="application/rdf+xml"
344 ;;
345 N3)
346 MIME_TYPE="text/rdf+n3"
347 ;;
348 TURTLE)
349 MIME_TYPE="text/turtle"
350 ;;
351 TRIG)
352 MIME_TYPE="application/x-trig"
353 ;;
354 TRIX)
355 MIME_TYPE="application/trix"
356 ;;
357 BinaryRDF)
358 MIME_TYPE="application/x-binary-rdf"
359 ;;
360 *)
361 echo "${CMD}: unknown format \"${FORMAT}\"."
362 echo "${CMD}: possible values are \`N-Triples' (default), \`RDM/XML', \`N3', \`TURTLE', \`TRIG', \`TRIX', and \`BinaryRDF'"
363 exit 2
364 ;;
365 esac
367 EXEC="curl -H 'Content-Type:application/x-www-form-urlencoded' -H 'Accept:${MIME_TYPE}' --data-urlencode query='${PREFIXES}${QUERY}' ${URL}"
368 ;;
369 *)
370 help
371 echo
372 echo "${CMD}: unknown command \"${1}\"."
373 exit 1
374 ;;
375 esac
377 # execute or debug
378 if test $DEBUG -eq 1; then
379 echo "${CMD}: Debug is ON"
380 echo "${CMD}: Printing command for execution"
381 echo " $EXEC"
382 else
383 eval "${EXEC}"
384 fi