View source with formatted comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2010, University of Amsterdam,
    7		   VU University Amsterdam
    8
    9    This program is free software; you can redistribute it and/or
   10    modify it under the terms of the GNU General Public License
   11    as published by the Free Software Foundation; either version 2
   12    of the License, or (at your option) any later version.
   13
   14    This program is distributed in the hope that it will be useful,
   15    but WITHOUT ANY WARRANTY; without even the implied warranty of
   16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17    GNU General Public License for more details.
   18
   19    You should have received a copy of the GNU General Public
   20    License along with this library; if not, write to the Free Software
   21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22
   23    As a special exception, if you link this library with other files,
   24    compiled with a Free Software compiler, to produce an executable, this
   25    library does not by itself cause the resulting executable to be covered
   26    by the GNU General Public License. This exception does not however
   27    invalidate any other reasons why the executable file might be covered by
   28    the GNU General Public License.
   29*/
   30
   31:- module(api_json,
   32	  [
   33	  ]).   34:- use_module(library(semweb/rdf_db)).   35:- use_module(library(semweb/rdf_json)).   36:- use_module(library(semweb/rdf_describe)).   37:- use_module(library(http/http_dispatch)).   38:- use_module(library(http/http_parameters)).   39:- use_module(library(http/http_json)).   40
   41:- http_handler(json(describe), json_describe, []).   42:- http_handler(json(prefixes), json_prefixes, []).   43:- http_handler(json(resource_representation), json_resource_representation, []).   44
   45/* <module> Describe resources in JSON
   46
   47This module produces a JSON description for a resource.
   48
   49@see	sparql.pl implements a SPARQL frontend.  The SPARQL DESCRIBE
   50	keyword provides similar functionality, but it has no arguments
   51	to specify how to describe a resource and it cannot return JSON.
   52@see	lod.pl implements Linked Open Data (LOD) descriptions.
   53*/
   54
   55%%	json_describe(+Request)
   56%
   57%	HTTP  handler  that  describes   a    resource   using   a  JSON
   58%	serialization.
   59%
   60%	@see	http://n2.talis.com/wiki/RDF_JSON_Specification describes
   61%		the used graph-serialization.
   62%	@see	http://n2.talis.com/wiki/Bounded_Descriptions_in_RDF for
   63%		a description of the various descriptions
   64%	@bug	Currently only supports =cbd=.
   65
   66json_describe(Request) :-
   67	http_parameters(Request,
   68			[ r(URI,
   69			    [ description('The resource to describe')
   70			    ]),
   71			  how(_How,
   72			      [ %oneof([cbd, scdb, ifcbd, lcbd, hcbd]),
   73				oneof([cbd]),
   74				default(cbd),
   75				description('Algorithm that determines \c
   76					     the description')
   77			      ])
   78			]),
   79	resource_CBD(rdf, URI, Graph),
   80	graph_json(Graph, JSON),
   81	reply_json(JSON).
   82
   83%%	json_prefixes(+Request)
   84%
   85%	Return a JSON object mapping prefixes to URIs.
   86
   87json_prefixes(_Request) :-
   88	findall(Prefix-URI,
   89		rdf_current_ns(Prefix, URI),
   90		Pairs),
   91	dict_pairs(Dict, prefixes, Pairs),
   92	reply_json(Dict).
   93
   94%%	json_resource_representation(+Request)
   95%
   96%	HTTP Handler to represent a resource in a given language
   97
   98json_resource_representation(Request) :-
   99	http_parameters(Request,
  100			[ r(URI,
  101			    [ description('The resource to format')
  102			    ]),
  103			  language(Lang,
  104			      [ oneof([sparql,turtle,prolog,xml]),
  105				default(turtle),
  106				description('Target language')
  107			      ])
  108			]),
  109	format_resource(Lang, URI, String),
  110	reply_json_dict(String).
  111
  112format_resource(sparql, URI, String) :- !,
  113	format_resource(turtle, URI, String).
  114format_resource(turtle, URI, String) :-
  115	(   rdf_global_id(Prefix:Local, URI)
  116	->  format(string(String), '~w:~w', [Prefix, Local])
  117	;   format(string(String), '<~w>', [URI])
  118	).
  119format_resource(xml, URI, String) :-
  120	(   rdf_global_id(Prefix:Local, URI),
  121	    xml_name(URI, utf8)
  122	->  format(string(String), '~w:~w', [Prefix, Local])
  123	;   format(string(String), '"~w"', [URI])
  124	).
  125format_resource(prolog, URI, String) :-
  126	(   rdf_global_id(Prefix:Local, URI)
  127	->  format(string(String), '~q', [Prefix:Local])
  128	;   format(string(String), '~q', [URI])
  129	)