Strabon

view scripts/endpoint @ 281:21e67e71f209

Added log4j dependencies.
Please update your project configuration in eclipse.
author Kostis Kyzirakos <kkyzir@di.uoa.gr>
date Mon Jun 18 16:02:15 2012 +0300 (2012-06-18)
parents 6503f99b6b54
children 1a40315b8cb4
line source
1 #!/bin/bash
2 #
3 # Script for executing SPARQL queries and SPARQL Update queries
4 # as well as storing RDF triples on a Strabon Endpoint.
5 #
6 # Author: Charalampos (Babis) Nikolaou <charnik@di.uoa.gr>
7 #
9 # this command
10 CMD="$(basename ${0})"
12 function help() {
13 echo "Usage: ${CMD} [OPTIONS] COMMAND ENDPOINT ARGS"
14 echo
15 echo "Execute SPARQL and SPARQL Update queries as well as store RDF triples on a Strabon endpoint."
16 echo
17 echo " COMMAND : one of query, update, store, or help"
18 echo " ENDPOINT : the URL of the Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint)"
19 echo " ARGS : arguments according to selected command"
20 echo
21 echo "OPTIONS can be any of the following"
22 echo " -d : don't run, just print what shall be executed"
23 }
25 function help_query() {
26 echo "Usage: ${CMD} query ENDPOINT SPARQL_QUERY [RESULT_FORMAT]"
27 echo
28 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
29 echo " SPARQL_QUERY : the SPARQL query to execute or the alias name corresponding to a"
30 echo " predefined query such as:"
31 echo " \`size': SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
32 echo " RESULT_FORMAT : the format of the result. Possible values are \`KMLMAP\, \`GEOJSON', "
33 echo " \`HTML', \`KMZMAP', \`XML' (default), or \`KML'."
34 }
36 function help_update() {
37 echo "Usage: ${CMD} update ENDPOINT SPARQL_QUERY"
38 echo
39 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
40 echo " SPARQL_QUERY : the SPARQL update query to execute or the alias name corresponding to a"
41 echo " predefined query such as:"
42 echo " \`clear': DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
43 }
45 function help_store() {
46 echo "Usage: ${CMD} store ENDPOINT FORMAT -t TRIPLES|-u TRIPLES_URL"
47 echo
48 echo " ENDPOINT : the URL of Strabon Endpoint (e.g., http://localhost:8080/StrabonEndpoint/)"
49 echo " FORMAT : the RDF format of the input (one of RDF/XML, N-Triples, Turtle, N3, TriX, TriG, or BinaryRDF)"
50 echo " TRIPLES : the RDF triples to store"
51 echo " TRIPLES_URL : the URL containing the RDF triples to store"
52 }
54 CURL_OPTS="-w HTTP_CODE='%{http_code}\n' -H \"Content-Type:application/x-www-form-urlencoded\" -H \"Accept:text/xml\""
56 # if set to 1, then only the command to be executed is printed
57 DEBUG=0
59 case "${1}" in
60 -d)
61 shift
62 DEBUG=1
63 ;;
64 esac
66 case "${1}" in
67 help)
68 shift
69 if test $# -eq 1; then
70 case "${1}" in
71 query)
72 help_query
73 ;;
74 update)
75 help_update
76 ;;
77 store)
78 help_store
79 ;;
80 *)
81 help
82 ;;
83 esac
84 exit 1
85 fi
86 help
87 exit 0
88 ;;
89 query)
90 shift
91 if ! test $# -ge 2; then
92 help_query
93 exit 1
94 fi
95 URL="${1}/Query"
96 QUERY="${2}"
98 shift
99 shift
100 # predefined queries
101 case "${QUERY}" in
102 size)
103 QUERY="SELECT (count(*) as ?c) WHERE {?s ?p ?o}"
104 ;;
105 esac
107 # set default format
108 FORMAT="XML"
109 if test $# -eq 1; then
110 FORMAT="${1}"
111 fi
113 case "${FORMAT}" in
114 [Kk][Mm][Ll][Mm][Aa][Pp]|[Gg][Ee][Oo][Jj][Ss][Oo][Nn]|[Hh][Tt][Mm][Ll]|[Kk][mM][Zz][Mm][Aa][Pp]|[Xx][Mm][Ll]|[Kk][Mm][Ll])
115 shift
116 ;;
117 *)
118 echo "${CMD}: unknown format \"${FORMAT}\"."
119 echo "${CMD}: possible values are \`KMLMAP\, \`GEOJSON' \`HTML', \`KMZMAP', \`XML' (default), or \`KML'"
120 exit 2
121 ;;
122 esac
124 EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode SPARQLQuery='${QUERY}' ${URL}"
125 ;;
126 update)
127 shift
128 if ! test $# -eq 2; then
129 help_update
130 exit 1
131 fi
132 URL="${1}/Update"
133 QUERY="${2}"
135 # predefined queries
136 case "${QUERY}" in
137 clear)
138 QUERY="DELETE {?s ?p ?o} WHERE {?s ?p ?o}"
139 ;;
140 esac
142 EXEC="curl ${CURL_OPTS} --data-urlencode SPARQLQuery='${QUERY}' ${URL}"
143 ;;
144 store)
145 shift
146 if ! test $# -eq 4; then
147 help_store
148 exit 1
149 fi
150 URL="${1}/Store"
151 FORMAT="${2}"
152 case "${3}" in
153 -t)
154 TRIPLES="${4}"
155 EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode data='${TRIPLES}' ${URL}"
156 ;;
157 -u)
158 URL_TRIPLES="${4}"
159 EXEC="curl ${CURL_OPTS} -d format='${FORMAT}' --data-urlencode url='${URL_TRIPLES}' -d fromurl='' ${URL}"
160 ;;
161 *)
162 help_store
163 exit 1
164 ;;
165 esac
166 ;;
167 *)
168 help
169 echo
170 echo "${CMD}: unknown command \"${1}\"."
171 exit 1
172 ;;
173 esac
175 # execute or debug
176 if test $DEBUG -eq 1; then
177 echo "${CMD}: Debug is ON"
178 echo "${CMD}: Printing command for execution"
179 echo " $EXEC"
180 else
181 eval ${EXEC}
182 fi