View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    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(sparql_xml_result,
   37          [ sparql_write_xml_result/3   % +Stream, +Result
   38          ]).   39:- use_module(library(sgml)).   40:- use_module(library(assoc)).   41:- use_module(library(option)).   42:- use_module(library('semweb/rdf_db'), [rdf_is_bnode/1, rdf_equal/2]).   43
   44ns(sparql, 'http://www.w3.org/2005/sparql-results#').
   45
   46/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   47Read/write   the   SPARQL   XML   result     format    as   defined   in
   48http://www.w3.org/TR/rdf-sparql-XMLres/, version 6 April 2006.
   49- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   50
   51                 /*******************************
   52                 *            WRITING           *
   53                 *******************************/
 sparql_write_xml_result(+Out, +Term, +Options)
Write SPARQL XML result data.
   59sparql_write_xml_result(Out, ask(TrueFalse), _Options) :-
   60    !,
   61    write_header(Out),
   62    format(Out, '  <head/>~n', []),
   63    format(Out, '  <boolean>~w</boolean>~n', [TrueFalse]),
   64    format(Out, '</sparql>~n', []).
   65sparql_write_xml_result(Out, update(TrueFalse), Options) :-
   66    !,
   67    sparql_write_xml_result(Out, ask(TrueFalse), Options).
   68sparql_write_xml_result(Out, select(VarTerm, Rows), Options) :-
   69    VarTerm =.. [_|VarNames],
   70    option(ordered(Ordered), Options, false),
   71    option(distinct(Distinct), Options, false),
   72    write_header(Out),
   73    format(Out, '  <head>~n', []),
   74    write_varnames(VarNames, Out),
   75    format(Out, '  </head>~n', []),
   76    format(Out, '  <results ordered="~w" distinct="~w">~n',
   77           [Ordered, Distinct]),
   78    write_rows(Rows, VarNames, Out),
   79    format(Out, '  </results>~n', []),
   80    format(Out, '</sparql>~n', []).
   81
   82write_header(Out) :-
   83    xml_encoding(Out, Encoding),
   84    format(Out, '<?xml version="1.0" encoding="~w"?>~n', [Encoding]),
   85    ns(sparql, Prefix),
   86    format(Out, '<sparql xmlns="~w">~n', [Prefix]).
   87
   88xml_encoding(Out, Encoding) :-
   89    stream_property(Out, encoding(Enc)),
   90    (   xml_encoding_name(Enc, Encoding)
   91    ->  true
   92    ;   throw(error(domain_error(rdf_encoding, Enc), _))
   93    ).
   94
   95xml_encoding_name(ascii,       'US-ASCII').
   96xml_encoding_name(iso_latin_1, 'ISO-8859-1').
   97xml_encoding_name(utf8,        'UTF-8').
   98
   99write_varnames([], _).
  100write_varnames([H|T], Out) :-
  101    stream_property(Out, encoding(Encoding)),
  102    xml_quote_attribute(H, Q, Encoding),
  103    format(Out, '    <variable name="~w"/>~n', [Q]),
  104    write_varnames(T, Out).
  105
  106write_rows(Rows, VarNames, Out) :-
  107    empty_assoc(BNodes),
  108    write_rows(Rows, VarNames, Out, state(0, BNodes), _).
  109
  110write_rows([], _, _, State, State).
  111write_rows([H|T], VarNames, Out, State0, State) :-
  112    write_row(H, VarNames, Out, State0, State1),
  113    write_rows(T, VarNames, Out, State1, State).
  114
  115write_row(Row, VarNames, Out, State0, State) :-
  116    format(Out, '    <result>~n', []),
  117    write_bindings(VarNames, 1, Row, Out, State0, State),
  118    format(Out, '    </result>~n', []).
  119
  120write_bindings([], _, _, _, State, State).
  121write_bindings([Name|T], I, Row, Out, State0, State) :-
  122    arg(I, Row, Value),
  123    write_binding(Value, Name, Out, State0, State1),
  124    I2 is I + 1,
  125    write_bindings(T, I2, Row, Out, State1, State).
  126
  127write_binding(Var, _, _, S, S) :- var(Var), !.
  128write_binding('$null$', _, _, S, S) :- !.
  129write_binding(Value, Name, Out, State0, State) :-
  130    stream_property(Out, encoding(Encoding)),
  131    xml_quote_attribute(Name, Q, Encoding),
  132    format(Out, '      <binding name="~w">~n', [Q]),
  133    write_binding_value(Value, Out, State0, State),
  134    format(Out, '      </binding>~n', []).
  135
  136write_binding_value(literal(Lit), Out, State, State) :-
  137    !,
  138    write_binding_literal(Lit, Out).
  139write_binding_value(URI, Out, State0, State) :-
  140    rdf_is_bnode(URI),
  141    !,
  142    bnode_id(URI, Id, State0, State),
  143    format(Out, '        <bnode>~w</bnode>~n', [Id]).
  144write_binding_value(URI, Out, State, State) :-
  145    stream_property(Out, encoding(Encoding)),
  146    xml_quote_cdata(URI, Q, Encoding),
  147    format(Out, '        <uri>~w</uri>~n', [Q]).
 write_binding_literal(+Literal, +Out) is det
Write Literal to Out. The first clause deals with XMLLiteral fields. The SPARQL documentation is rather vaque about how this should be handled. It might well be that we should write the xml into an atom and xml-escape that.
  156write_binding_literal(type(Type, DOM), Out) :-
  157    rdf_equal(Type, rdf:'XMLLiteral'),
  158    xml_is_dom(DOM),
  159    !,
  160    with_output_to(string(S),
  161                   xml_write(current_output, DOM,
  162                             [ header(false),
  163                               layout(false)
  164                             ])),
  165    stream_property(Out, encoding(Encoding)),
  166    xml_quote_cdata(S, QV, Encoding),
  167    format(Out, '        <literal datatype="~w">~w</literal>~n', [Type, QV]).
  168write_binding_literal(type(Type, Value), Out) :-
  169    !,
  170    stream_property(Out, encoding(Encoding)),
  171    xml_quote_attribute(Type, QT, Encoding),
  172    (   atom(Value)
  173    ->  xml_quote_cdata(Value, QV, Encoding)
  174    ;   QV = Value                  % ok for numbers, etc.
  175    ),
  176    format(Out, '        <literal datatype="~w">~w</literal>~n', [QT, QV]).
  177write_binding_literal(lang(L, Value), Out) :-
  178    !,
  179    stream_property(Out, encoding(Encoding)),
  180    xml_quote_cdata(Value, QV, Encoding),
  181    format(Out, '        <literal xml:lang="~w">~w</literal>~n', [L, QV]).
  182write_binding_literal(Value, Out) :-
  183    !,
  184    stream_property(Out, encoding(Encoding)),
  185    xml_quote_cdata(Value, QV, Encoding),
  186    format(Out, '        <literal>~w</literal>~n', [QV]).
  187
  188bnode_id(URI, Id, State0, State) :-
  189    State0 = state(N, Assoc),
  190    (   get_assoc(URI, Assoc, Id)
  191    ->  State = State0
  192    ;   N2 is N + 1,
  193        Id = N2,                    % number 1, ...
  194        put_assoc(URI, Assoc, Id, Assoc2),
  195        State = state(N2, Assoc2)
  196    )