annotate jamendo/sparql-archived/SeRQL/rdf_html.pl @ 27:d95e683fbd35 tip

Enable CORS on urispace redirects as well
author Chris Cannam
date Tue, 20 Feb 2018 14:52:02 +0000
parents df9685986338
children
rev   line source
Chris@0 1 /* $Id$
Chris@0 2
Chris@0 3 Part of SWI-Prolog
Chris@0 4
Chris@0 5 Author: Jan Wielemaker
Chris@0 6 E-mail: jan@swi.psy.uva.nl
Chris@0 7 WWW: http://www.swi-prolog.org
Chris@0 8 Copyright (C): 1985-2004, University of Amsterdam
Chris@0 9
Chris@0 10 This program is free software; you can redistribute it and/or
Chris@0 11 modify it under the terms of the GNU General Public License
Chris@0 12 as published by the Free Software Foundation; either version 2
Chris@0 13 of the License, or (at your option) any later version.
Chris@0 14
Chris@0 15 This program is distributed in the hope that it will be useful,
Chris@0 16 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 18 GNU General Public License for more details.
Chris@0 19
Chris@0 20 You should have received a copy of the GNU Lesser General Public
Chris@0 21 License along with this library; if not, write to the Free Software
Chris@0 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Chris@0 23
Chris@0 24 As a special exception, if you link this library with other files,
Chris@0 25 compiled with a Free Software compiler, to produce an executable, this
Chris@0 26 library does not by itself cause the resulting executable to be covered
Chris@0 27 by the GNU General Public License. This exception does not however
Chris@0 28 invalidate any other reasons why the executable file might be covered by
Chris@0 29 the GNU General Public License.
Chris@0 30 */
Chris@0 31
Chris@0 32 :- module(rdf_html,
Chris@0 33 [
Chris@0 34 ]).
Chris@0 35 :- use_module(library('http/html_write')).
Chris@0 36 :- use_module(library('semweb/rdf_db')).
Chris@0 37 :- use_module(library('semweb/rdfs')).
Chris@0 38
Chris@0 39
Chris@0 40 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Chris@0 41 This library provides primitives based on the html_write library dealing
Chris@0 42 with emitting RDF related material in HTML human-readable format.
Chris@0 43 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Chris@0 44
Chris@0 45 /*******************************
Chris@0 46 * RESULT TABLES *
Chris@0 47 *******************************/
Chris@0 48
Chris@0 49 %% write_table(+Format, +Serialization, +Rows, +Options)
Chris@0 50 %
Chris@0 51 % Write a result-table in human-readable HTML format
Chris@0 52 %
Chris@0 53 % @param Format Must be =html=
Chris@0 54
Chris@0 55 :- multifile
Chris@0 56 rdf_io:write_table/4,
Chris@0 57 rdf_io:write_graph/4.
Chris@0 58
Chris@0 59 rdf_io:write_table(html, _Serialization, Rows, Options) :- !,
Chris@0 60 length(Rows, Count),
Chris@0 61 phrase(page(title('Query result'),
Chris@0 62 [ \query_statistics([count(Count)|Options], rows),
Chris@0 63 table([ align(center),
Chris@0 64 border(1)
Chris@0 65 ],
Chris@0 66 [ \variables(Options)
Chris@0 67 | \rows(Rows, Options)
Chris@0 68 ])
Chris@0 69 ]), HTML),
Chris@0 70 format('Content-type: text/html~n~n'),
Chris@0 71 print_html(HTML).
Chris@0 72
Chris@0 73 variables(Options) -->
Chris@0 74 { memberchk(variables(Vars), Options),
Chris@0 75 Vars =.. [_|Names]
Chris@0 76 }, !,
Chris@0 77 html(tr(\varnames(Names))).
Chris@0 78
Chris@0 79 varnames([]) -->
Chris@0 80 [].
Chris@0 81 varnames([Name|T]) -->
Chris@0 82 html(th(Name)),
Chris@0 83 varnames(T).
Chris@0 84
Chris@0 85 rows([], _) -->
Chris@0 86 [].
Chris@0 87 rows([H|T], Options) -->
Chris@0 88 { H =.. [_|Cells] },
Chris@0 89 html(tr(\cells(Cells, Options))),
Chris@0 90 rows(T, Options).
Chris@0 91
Chris@0 92 cells([], _) -->
Chris@0 93 [].
Chris@0 94 cells([H|T], Options) -->
Chris@0 95 html(td(\object(H, Options))),
Chris@0 96 cells(T, Options).
Chris@0 97
Chris@0 98
Chris@0 99 /*******************************
Chris@0 100 * GRAPH OUTPUT *
Chris@0 101 *******************************/
Chris@0 102
Chris@0 103 rdf_io:write_graph(html, _Serialization, Triples, Options) :-
Chris@0 104 length(Triples, Count),
Chris@0 105 phrase(page(title('Query result'),
Chris@0 106 [ \query_statistics([count(Count)|Options], triples),
Chris@0 107 table([ align(center),
Chris@0 108 border(1)
Chris@0 109 ],
Chris@0 110 [ tr([th('Subject'), th('Predicate'), th('Object')])
Chris@0 111 | \triples(Triples, Options)
Chris@0 112 ])
Chris@0 113 ]), HTML),
Chris@0 114 format('Content-type: text/html~n~n'),
Chris@0 115 print_html(HTML).
Chris@0 116
Chris@0 117 triples([], _) -->
Chris@0 118 [].
Chris@0 119 triples([H|T], Options) -->
Chris@0 120 triple(H, Options),
Chris@0 121 triples(T, Options).
Chris@0 122
Chris@0 123 triple(rdf(S,P,O), Options) -->
Chris@0 124 html(tr([ td(\resource(S, Options)),
Chris@0 125 td(\resource(P, Options)),
Chris@0 126 td(\object(O, Options))])).
Chris@0 127
Chris@0 128 resource(R, Options) -->
Chris@0 129 { memberchk(resource_format(Fmt), Options)
Chris@0 130 -> true
Chris@0 131 ; Fmt = plain
Chris@0 132 },
Chris@0 133 resource(Fmt, R, Options).
Chris@0 134
Chris@0 135 resource(plain, R, _) --> !,
Chris@0 136 html(R).
Chris@0 137 resource(_, R, _) -->
Chris@0 138 { atomic(R), sub_atom(R, 0, L, _, '__file:/'), !, % bnode
Chris@0 139 sub_atom(R, L, _, 0, Path),
Chris@0 140 file_base_name(Path, Base),
Chris@0 141 atom_concat('__', Base, Label)
Chris@0 142 },
Chris@0 143 html(Label).
Chris@0 144 resource(ns, R, _) --> !,
Chris@0 145 ( { rdf_global_id(NS:Local, R) }
Chris@0 146 -> html([NS, :, Local])
Chris@0 147 ; html(R)
Chris@0 148 ).
Chris@0 149 resource(nslabel, R, _) --> !,
Chris@0 150 { rdfs_ns_label(R, Label)
Chris@0 151 },
Chris@0 152 html(Label).
Chris@0 153
Chris@0 154 object(V,Options) --> {var(V)}, !.
Chris@0 155 object(literal(type(T, V)), Options) --> !,
Chris@0 156 html(['"', V, '"^^', \resource(T, Options)]).
Chris@0 157 object(literal(lang(L, V)), _) --> !,
Chris@0 158 html(['"', V, '"@', L]).
Chris@0 159 object(literal(V), _) --> !,
Chris@0 160 html(['"', V, '"']).
Chris@0 161 object(R, Options) -->
Chris@0 162 resource(R, Options).
Chris@0 163
Chris@0 164
Chris@0 165 %% query_statistics(+Options, +Units)// is det.
Chris@0 166 %
Chris@0 167 % Emit a short line above the page summarizing resource usage
Chris@0 168 % and result size.
Chris@0 169
Chris@0 170 query_statistics(Options, Units) -->
Chris@0 171 { memberchk(cputime(CPU), Options),
Chris@0 172 sformat(T, '~2f', CPU),
Chris@0 173 memberchk(count(Count), Options),
Chris@0 174 sformat(C, '~D', Count)
Chris@0 175 }, !,
Chris@0 176 html(center(blockquote(i(['Query completed in ', T, ' seconds, ',
Chris@0 177 C, ' ', Units])))).
Chris@0 178 query_statistics(_, _) -->
Chris@0 179 [].