Skip to content

Commit

Permalink
Merge pull request #71 from Swirrl/support-paths
Browse files Browse the repository at this point in the history
Support java.nio.Path objects via API
  • Loading branch information
lkitching authored Feb 21, 2022
2 parents adc95df + 25929b2 commit f354944
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
41 changes: 36 additions & 5 deletions src/csv2rdf/csvw.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,42 @@
(table-statements context table annotated-rows)))

(defn csv->rdf
"Runs the CSVW process for the given tabular or metadata data sources and options. If metadata-source
is non-nil then processing will start from the asscociated metadata document, otherwise it will start
from tabular-source. Returns a lazy sequence of statements containing the CSVW output for the specified
CSVW mode. Mode can be specified by the :mode key of the options map if provided, otherwise standard mode
will be used."
"Runs the CSVW process for the given tabular or metadata data sources
and options.
`tabular-source` and `metadata-source` can be any of the following
types:
- java.io.File
- java.lang.String
- java.net.URI
- java.nio.file.Path (including nio Paths that are inside zip filesystems)
If metadata-source is non-nil then processing will start from the
asscociated metadata document, otherwise it will start from
tabular-source. Returns a lazy sequence of statements containing the
CSVW output for the specified CSVW mode.
The processing mode can be specified by the :mode key of the options
map if provided, otherwise `:standard` mode will be used. Valid
`:mode` options are:
- `:standard` this mode corresponds to the standard mode specified
in the \"Generating RDF from Tabular Data on the Web\" specification.
It outputs triples for all information gleaned from the cells of the
tabular data with details of the rows, tables, and table groups.
This mode yields the most data.
- `:minimal` this mode corresponds to the minimal mode specified in
the \"Generating RDF from Tabular Data on the Web\" specification.
It essentially yields the salient RDF; but omits the tabular structure.
- `:annotated` a custom mode, not part of the standard, which is
like `:minimal`, but it also includes RDF data from the CSVW metadata
json file."
([tabular-source metadata-source] (csv->rdf tabular-source metadata-source {}))
([tabular-source metadata-source {:keys [mode] :as options}]
(let [mode (or mode :standard)
Expand Down
19 changes: 16 additions & 3 deletions src/csv2rdf/source.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[clojure.data.json :as json]
[clojure.string :as string])
(:import [java.net URI]
[java.io File InputStream ByteArrayInputStream]))
[java.io File InputStream ByteArrayInputStream]
[java.nio.file Files Path OpenOption StandardOpenOption]))

(defprotocol URIable
"Represents an object with an associated URI."
Expand All @@ -18,7 +19,11 @@
(->uri [file] (.toURI file))

URI
(->uri [uri] uri))
(->uri [uri] uri)

Path
(->uri [p]
(.toUri p)))

(defprotocol JSONSource
"Protocol for loading a JSON map from a given source"
Expand Down Expand Up @@ -58,7 +63,11 @@
(get-json [f] (read-json f))

String
(get-json [s] (json/read-str s)))
(get-json [s] (json/read-str s))

Path
(get-json [p]
(read-json (Files/newInputStream p (into-array OpenOption [StandardOpenOption/READ])))))

(defrecord MapMetadataSource [uri json]
URIable
Expand Down Expand Up @@ -111,6 +120,10 @@
(defmethod request-uri-input-stream :file [uri]
{:headers {} :stream (io/input-stream uri)})

;; support reading files in a zip (via API) via the jar protocol
(defmethod request-uri-input-stream :jar [uri]
{:headers {} :stream (io/input-stream uri)})

(extend-protocol InputStreamRequestable
File
(request-input-stream [f] {:headers {}
Expand Down
44 changes: 44 additions & 0 deletions test/csv2rdf/csvw_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(ns csv2rdf.csvw-test
(:require [clojure.test :refer [deftest is testing]]
[clojure.java.io :as io]
[csv2rdf.csvw :as csvw])
(:import [java.net URI]
[java.nio.file FileSystems]
[grafter_2.rdf.protocols Quad]))

(def w3c-dir "w3c-csvw")

(defn- make-path [& path]
(.getPath (FileSystems/getDefault) w3c-dir (into-array String path)))

(defn make-file [& path]
(apply io/file w3c-dir path))

(defn csv->rdf? [csv metadata]
(instance? Quad
(first (csvw/csv->rdf csv metadata))))

(deftest csv->rdf-supported-types-test
(testing "with java.io.File"
(let [csv (make-file "./tests" "test011" "tree-ops.csv")
metadata (make-file "./tests" "test011" "tree-ops.csv-metadata.json")]

(is (csv->rdf? csv metadata))
(is (csv->rdf? nil metadata)
"Resolves csv")))

(testing "with URI"
(let [csv (.toURI (make-file "./tests" "test011" "tree-ops.csv"))
metadata (.toURI (make-file "./tests" "test011" "tree-ops.csv-metadata.json"))]

(is (csv->rdf? csv metadata))
(is (csv->rdf? nil metadata)
"Resolves csv")))

(testing "with java.nio.Path"
(let [csv (make-path "tests" "test011" "tree-ops.csv")
metadata (make-path "tests" "test011" "tree-ops.csv-metadata.json")]

(is (csv->rdf? csv metadata))
(is (csv->rdf? nil metadata)
"Resolves csv"))))

0 comments on commit f354944

Please sign in to comment.