guile-rdf/nquads/fromrdf.scm

fromrdf.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (nquads fromrdf)
19
  #:use-module (ice-9 match)
20
  #:use-module (ice-9 textual-ports)
21
  #:use-module (iri iri)
22
  #:use-module (srfi srfi-1)
23
  #:use-module (srfi srfi-9)
24
  #:use-module (rdf rdf)
25
  #:export (rdf->nquads))
26
27
(define (nquads-escape str)
28
  "Escape a string for writing it to an nquads document."
29
  (list->string (append-map (lambda (c)
30
                              (match c
31
                                (#\\ (list #\\ #\\))
32
                                (#\" (list #\\ #\"))
33
                                (#\newline (list #\\ #\n))
34
                                (#\return (list #\\ #\r))
35
                                (#\tab (list #\\ #\t))
36
                                (#\backspace (list #\\ #\b))
37
                                (_ (list c))))
38
                            (string->list str))))
39
40
(define (node->nquads node)
41
  (cond
42
    ((blank-node? node)
43
     (string-append "_:" (number->string node)))
44
    ((rdf-datatype? node)
45
     (string-append "<" (car (rdf-datatype-iris node)) ">"))
46
    ((rdf-literal? node)
47
     (string-append "\"" (nquads-escape (rdf-literal-lexical-form node)) "\""
48
                    (if (rdf-literal-langtag node)
49
                        (string-append "@" (rdf-literal-langtag node))
50
                        (let ((type (rdf-literal-type node)))
51
                          (if (equal? type "http://www.w3.org/2001/XMLSchema#string")
52
                              ""
53
                              (string-append
54
                                "^^<"
55
                                (if (rdf-datatype? type)
56
                                    (car (rdf-datatype-iris type))
57
                                    type)
58
                                ">"))))))
59
    ((string? node)
60
     (string-append "<" node ">"))))
61
62
(define (rdf-triple->nquads t graph)
63
  (match t
64
    (($ rdf-triple subject predicate object)
65
     (format #f "~a ~a ~a ~a ."
66
             (node->nquads subject)
67
             (node->nquads predicate)
68
             (node->nquads object)
69
             (or graph "")))))
70
71
(define (rdf->nquads g)
72
  (match g
73
    (($ rdf-dataset default-graph named-graphs)
74
     (string-join
75
       (apply append
76
         (map (lambda (t) (rdf-triple->nquads t #f)) default-graph)
77
         (map
78
           (match-lambda
79
             ((name . graph)
80
              (let ((name (node->nquads name)))
81
                (map (lambda (t) (rdf-triple->nquads t name)) graph))))
82
           named-graphs))
83
       "\n"))))
84