View source with raw comments or as raw
    1/*  Part of ClioPatria SeRQL and SPARQL server
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(api_json,
   37          [
   38          ]).   39:- use_module(library(semweb/rdf_db)).   40:- use_module(library(semweb/rdf_json)).   41:- use_module(library(semweb/rdf_describe)).   42:- use_module(library(http/http_dispatch)).   43:- use_module(library(http/http_parameters)).   44:- use_module(library(http/http_json)).   45
   46:- http_handler(json(describe), json_describe, []).   47:- http_handler(json(prefixes), json_prefixes, []).   48:- http_handler(json(resource_representation), json_resource_representation, []).   49
   50/* <module> Describe resources in JSON
   51
   52This module produces a JSON description for a resource.
   53
   54@see    sparql.pl implements a SPARQL frontend.  The SPARQL DESCRIBE
   55        keyword provides similar functionality, but it has no arguments
   56        to specify how to describe a resource and it cannot return JSON.
   57@see    lod.pl implements Linked Open Data (LOD) descriptions.
   58*/
 json_describe(+Request)
HTTP handler that describes a resource using a JSON serialization.
See also
- http://n2.talis.com/wiki/RDF_JSON_Specification describes the used graph-serialization.
- http://n2.talis.com/wiki/Bounded_Descriptions_in_RDF for a description of the various descriptions
bug
- Currently only supports cbd.
   71json_describe(Request) :-
   72    http_parameters(Request,
   73                    [ r(URI,
   74                        [ description('The resource to describe')
   75                        ]),
   76                      how(_How,
   77                          [ %oneof([cbd, scdb, ifcbd, lcbd, hcbd]),
   78                            oneof([cbd]),
   79                            default(cbd),
   80                            description('Algorithm that determines \c
   81                                             the description')
   82                          ])
   83                    ]),
   84    resource_CBD(rdf, URI, Graph),
   85    graph_json(Graph, JSON),
   86    reply_json(JSON).
 json_prefixes(+Request)
Return a JSON object mapping prefixes to URIs.
   92json_prefixes(_Request) :-
   93    findall(Prefix-URI,
   94            rdf_current_ns(Prefix, URI),
   95            Pairs),
   96    dict_pairs(Dict, prefixes, Pairs),
   97    reply_json(Dict).
 json_resource_representation(+Request)
HTTP Handler to represent a resource in a given language
  103json_resource_representation(Request) :-
  104    http_parameters(Request,
  105                    [ r(URI,
  106                        [ description('The resource to format')
  107                        ]),
  108                      language(Lang,
  109                          [ oneof([sparql,turtle,prolog,xml]),
  110                            default(turtle),
  111                            description('Target language')
  112                          ])
  113                    ]),
  114    format_resource(Lang, URI, String),
  115    reply_json_dict(String).
  116
  117format_resource(sparql, URI, String) :-
  118    !,
  119    format_resource(turtle, URI, String).
  120format_resource(turtle, URI, String) :-
  121    (   rdf_global_id(Prefix:Local, URI)
  122    ->  format(string(String), '~w:~w', [Prefix, Local])
  123    ;   format(string(String), '<~w>', [URI])
  124    ).
  125format_resource(xml, URI, String) :-
  126    (   rdf_global_id(Prefix:Local, URI),
  127        xml_name(URI, utf8)
  128    ->  format(string(String), '~w:~w', [Prefix, Local])
  129    ;   format(string(String), '"~w"', [URI])
  130    ).
  131format_resource(prolog, URI, String) :-
  132    (   rdf_global_id(Prefix:Local, URI)
  133    ->  format(string(String), '~q', [Prefix:Local])
  134    ;   format(string(String), '~q', [URI])
  135    )