2018-03-08 16:33:20 +00:00
|
|
|
defmodule RDF.Serialization.Reader do
|
2017-06-16 15:33:05 +00:00
|
|
|
@moduledoc """
|
2018-03-08 16:33:20 +00:00
|
|
|
General functions for reading a `RDF.Graph` or `RDF.Dataset` from a serialization file or encoded-string.
|
2017-04-10 19:24:43 +00:00
|
|
|
|
2018-08-16 23:31:08 +00:00
|
|
|
If no base IRI is provided with the `base` option on any of these functions,
|
|
|
|
the `RDF.IRI.default_base/0` is used.
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
You probably won't use these functions directly, but instead use the automatically
|
2018-03-08 16:33:20 +00:00
|
|
|
generated functions with same name on a `RDF.Serialization.Format`, which implicitly
|
2017-06-16 15:33:05 +00:00
|
|
|
use the proper `RDF.Serialization.Decoder` module.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a string.
|
|
|
|
|
|
|
|
It returns an `{:ok, data}` tuple, with `data` being the deserialized graph or
|
|
|
|
dataset, or `{:error, reason}` if an error occurs.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string(decoder, content, opts \\ []) do
|
2018-08-16 23:31:08 +00:00
|
|
|
decoder.decode(content, with_base(opts))
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a string.
|
|
|
|
|
|
|
|
As opposed to `read_string`, it raises an exception if an error occurs.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_string!(decoder, content, opts \\ []) do
|
2018-08-16 23:31:08 +00:00
|
|
|
decoder.decode!(content, with_base(opts))
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a file.
|
|
|
|
|
|
|
|
It returns an `{:ok, data}` tuple, with `data` being the deserialized graph or
|
|
|
|
dataset, or `{:error, reason}` if an error occurs.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_file(decoder, file, opts \\ []) do
|
|
|
|
case File.read(file) do
|
2018-08-16 23:31:08 +00:00
|
|
|
{:ok, content} -> read_string(decoder, content, with_base(opts))
|
2017-04-10 19:24:43 +00:00
|
|
|
{:error, reason} -> {:error, reason}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-16 15:33:05 +00:00
|
|
|
@doc """
|
|
|
|
Reads and decodes a serialized graph or dataset from a file.
|
|
|
|
|
|
|
|
As opposed to `read_file`, it raises an exception if an error occurs.
|
|
|
|
"""
|
2017-04-10 19:24:43 +00:00
|
|
|
def read_file!(decoder, file, opts \\ []) do
|
2017-05-15 20:48:55 +00:00
|
|
|
with content = File.read!(file) do
|
2018-08-16 23:31:08 +00:00
|
|
|
read_string!(decoder, content, with_base(opts))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp with_base(opts) do
|
|
|
|
cond do
|
|
|
|
Keyword.has_key?(opts, :base) -> opts
|
|
|
|
base = RDF.IRI.default_base() -> Keyword.put(opts, :base, base)
|
|
|
|
true -> opts
|
2017-04-10 19:24:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|