View source with raw comments or as raw
   1/*  Part of ClioPatria
   2
   3    Author:        Jan Wielemaker
   4    E-mail:        J.Wielemaker@cs.vu.nl
   5    WWW:           http://www.swi-prolog.org
   6    Copyright (C): 2012 VU University Amsterdam
   7
   8    This program is free software; you can redistribute it and/or
   9    modify it under the terms of the GNU General Public License
  10    as published by the Free Software Foundation; either version 2
  11    of the License, or (at your option) any later version.
  12
  13    This program is distributed in the hope that it will be useful,
  14    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16    GNU General Public License for more details.
  17
  18    You should have received a copy of the GNU General Public
  19    License along with this library; if not, write to the Free Software
  20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21
  22    As a special exception, if you link this library with other files,
  23    compiled with a Free Software compiler, to produce an executable, this
  24    library does not by itself cause the resulting executable to be covered
  25    by the GNU General Public License. This exception does not however
  26    invalidate any other reasons why the executable file might be covered by
  27    the GNU General Public License.
  28*/
  29
  30:- module(flint, []).
  31:- use_module(library(semweb/rdf_db)).
  32:- use_module(library(http/http_dispatch)).
  33:- use_module(library(http/http_files)).
  34:- use_module(library(http/html_write)).
  35:- use_module(library(http/html_head)).
  36:- use_module(library(http/json)).
  37:- use_module(cliopatria(hooks)).
  38
  39:- http_handler(flint('index.html'), sparql_editor, []).
  40:- http_handler(flint('config.js'), flint_config, []).
  41:- http_handler(flint(.), http_reply_from_files(flint(.), []), [prefix]).
  42
  43:- html_resource(flint('config.js'),
  44		 [ requires([ flint('flint-editor.js'),
  45
  46			      flint('css/sparqlcolors.css'),
  47			      flint('css/docs.css')
  48			    ])
  49		 ]).
  50
  51
  52:- html_resource(flint('lib/codemirror.js'),
  53		 [ requires([ flint('jquery-1.5.2.min.js'),
  54			      flint('lib/codemirror.css')
  55			    ])
  56		 ]).
  57
  58:- html_resource(flint('flint-editor.js'),
  59		 [ requires([ flint('lib/codemirror.js'),
  60			      flint('sparql10querymode_ll1.js'),
  61			      flint('sparql11querymode_ll1.js'),
  62			      flint('sparql11updatemode_ll1.js')
  63			    ])
  64		 ]).
  65
  66%%	sparql_editor(+Request)
  67%
  68%	HTTP handler that presents the flint SPARQL editor.
  69
  70sparql_editor(_Request) :-
  71	\+ absolute_file_name(flint('flint-editor.js'), _,
  72			      [ access(read),
  73				file_errors(fail)
  74			      ]), !,
  75	reply_html_page(cliopatria(default),
  76			title('No Flint installed'),
  77			\no_flint).
  78sparql_editor(_Request) :-
  79	reply_html_page(
  80	    cliopatria(plain),
  81	    title('Flint SPARQL Editor'),
  82	    \flint_page).
  83
  84flint_page -->
  85	html_requires(flint('config.js')),
  86	html(div(id(flint), [])).
  87
  88%%	flint_config(+Request)
  89%
  90%	HTTP handler that serves the   flint SPARQL editor configuration
  91%	and initialization.
  92
  93flint_config(_Request) :-
  94	config(Config),
  95	format('Content-type: text/javascript~n~n'),
  96	format('$(document).ready(function() {~n'),
  97	write_config(Config),
  98	write_init,
  99	format('});~n').
 100
 101write_config(Config) :-
 102	format('  var flintConfig = '),
 103	json_write(current_output, Config, [width(72)]),
 104	format(';').
 105
 106write_init :-
 107	format('  var fint = new FlintEditor("flint", "images", flintConfig);~n').
 108
 109%%	config(-Config) is det.
 110%
 111%	Produce a JSON document holding the FlintEditor configuration.
 112
 113config(json([ interface = json([ toolbar=true,
 114				 menu=true
 115			       ]),
 116	      namespaces = NameSpaces,
 117	      defaultEndpointParameters = EndpointParameters,
 118	      endpoints = EndPoints,
 119	      defaultModes = Modes
 120	    ])) :-
 121	namespaces(NameSpaces),
 122	endpoint_parameters(EndpointParameters),
 123	endpoints(EndPoints),
 124	modes(Modes).
 125
 126namespaces(NameSpaces) :-
 127	setof(NameSpace, namespace(NameSpace), NameSpaces).
 128
 129namespace(json([ name(Prefix),
 130		 prefix(Prefix),
 131		 uri(URI)
 132	       ])) :-
 133	rdf_current_ns(Prefix, URI).
 134
 135:- if(\+current_predicate(rdf_current_ns/2)).
 136rdf_current_ns(Prefix, URI) :- rdf_current_prefix(Prefix, URI).
 137:- endif.
 138
 139endpoint_parameters(
 140    json([ queryParameters =
 141	       json([ format(output),
 142		      query(query),
 143		      update(update)
 144		    ]),
 145	   selectFormats =
 146	     [ json([ name('Plain text'),
 147		      format(text),
 148		      type('text/plain')
 149		    ]),
 150	       json([ name('SPARQL-XML'),
 151		      format(sparql),
 152		      type('application/sparql-results+xml')
 153		    ]),
 154	       json([ name('JSON'),
 155		      format(json),
 156		      type('application/sparql-results+json')
 157		    ])
 158	     ],
 159	   constructFormats =
 160	     [ json([ name('Plain text'),
 161		      format(text),
 162		      type('text/plain')
 163		    ]),
 164	       json([ name('RDF/XML'),
 165		      format(rdfxml),
 166		      type('application/rdf+xml')
 167		    ]),
 168	       json([ name('Turtle'),
 169		      format(turtle),
 170		      type('application/turtle')
 171		    ])
 172	     ]
 173	 ])).
 174
 175
 176endpoints([ json([ name('ClioPatria'),
 177		   uri(EndPoint),
 178		   modes([ sparql11query, sparql10 ])
 179		 ]),
 180	    json([ name('Update'),
 181		   uri(UpdateEndPoint),
 182		   modes([ sparql11update ])
 183		 ])
 184	  ]) :-
 185	http_link_to_id(sparql_query,  [], EndPoint),
 186	http_link_to_id(sparql_update, [], UpdateEndPoint).
 187
 188
 189modes([ json([ name('SPARQL 1.1 Query'),
 190	       mode(sparql11query)
 191	     ]),
 192	json([ name('SPARQL 1.1 Update'),
 193	       mode(sparql11update)
 194	     ]),
 195	json([ name('SPARQL 1.0'),
 196	       mode(sparql10)
 197	     ])
 198      ]).
 199
 200
 201%%	no_flint//
 202%
 203%	Display a message indicating the user how to install Flint
 204
 205no_flint -->
 206	{ absolute_file_name(cliopatria(.), CD0,
 207			     [ file_type(directory),
 208			       access(read)
 209			     ]),
 210	  prolog_to_os_filename(CD0, ClioHome)
 211	},
 212	html_requires(pldoc),
 213	html([ h1('Flint SPARQL Editor is not installed'),
 214	       p([ 'Please run the following command in the ClioPatria ',
 215		   'installation directory "~w" to install Flint.'-[ClioHome]
 216		 ]),
 217	       pre(class(code),
 218		   [ 'git submodule update --init web/FlintSparqlEditor'
 219		   ])
 220	     ]).
 221
 222
 223		 /*******************************
 224		 *	 REGISTER WITH MENU	*
 225		 *******************************/
 226
 227cliopatria:menu_item(150=query/sparql_editor,  'Flint SPARQL Editor').