2017-03-21 00:01:55 +00:00
|
|
|
defmodule JSON.LD.Utils do
|
|
|
|
|
2017-08-24 17:10:02 +00:00
|
|
|
alias RDF.IRI
|
|
|
|
|
2017-03-21 00:01:55 +00:00
|
|
|
@doc """
|
|
|
|
Resolves a relative IRI against a base IRI.
|
|
|
|
|
|
|
|
as specified in [section 5.1 Establishing a Base URI of RFC3986](http://tools.ietf.org/html/rfc3986#section-5.1).
|
|
|
|
Only the basic algorithm in [section 5.2 of RFC3986](http://tools.ietf.org/html/rfc3986#section-5.2)
|
|
|
|
is used; neither Syntax-Based Normalization nor Scheme-Based Normalization are performed.
|
|
|
|
|
|
|
|
Characters additionally allowed in IRI references are treated in the same way that unreserved
|
|
|
|
characters are treated in URI references, per [section 6.5 of RFC3987](http://tools.ietf.org/html/rfc3987#section-6.5)
|
|
|
|
"""
|
|
|
|
def absolute_iri(value, base_iri)
|
|
|
|
|
2017-08-24 17:10:02 +00:00
|
|
|
def absolute_iri(value, nil),
|
|
|
|
do: value
|
|
|
|
def absolute_iri(value, base_iri),
|
|
|
|
do: value |> RDF.IRI.absolute(base_iri) |> to_string
|
2017-03-21 00:01:55 +00:00
|
|
|
|
|
|
|
|
2017-04-09 20:55:54 +00:00
|
|
|
def relative_iri?(value),
|
2017-08-24 17:10:02 +00:00
|
|
|
do: not (JSON.LD.keyword?(value) or IRI.absolute?(value) or blank_node_id?(value))
|
2017-04-09 20:55:54 +00:00
|
|
|
|
2017-03-21 00:01:55 +00:00
|
|
|
def compact_iri_parts(compact_iri, exclude_bnode \\ true) do
|
2017-04-13 21:17:40 +00:00
|
|
|
with [prefix, suffix] <- String.split(compact_iri, ":", parts: 2) do
|
|
|
|
if not(String.starts_with?(suffix, "//")) and
|
|
|
|
not(exclude_bnode and prefix == "_"),
|
|
|
|
do: [prefix, suffix]
|
2017-03-21 00:01:55 +00:00
|
|
|
else
|
2017-04-13 21:17:40 +00:00
|
|
|
_ -> nil
|
2017-03-21 00:01:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Checks if the given value is a blank node identifier.
|
|
|
|
|
|
|
|
A blank node identifier is a string that can be used as an identifier for a
|
|
|
|
blank node within the scope of a JSON-LD document.
|
|
|
|
|
|
|
|
Blank node identifiers begin with `_:`
|
|
|
|
|
|
|
|
see <https://www.w3.org/TR/json-ld-api/#dfn-blank-node-identifier>
|
|
|
|
"""
|
|
|
|
def blank_node_id?("_:" <> _), do: true
|
|
|
|
def blank_node_id?(_), do: false
|
|
|
|
|
|
|
|
|
|
|
|
def scalar?(value) when is_binary(value) or is_number(value) or
|
|
|
|
is_boolean(value), do: true
|
|
|
|
def scalar?(_), do: false
|
|
|
|
|
2018-01-27 22:32:41 +00:00
|
|
|
def list?(%{"@list" => _}), do: true
|
|
|
|
def list?(_), do: false
|
2017-03-21 00:01:55 +00:00
|
|
|
def index?(%{"@index" => _}), do: true
|
2018-01-27 22:32:41 +00:00
|
|
|
def index?(_), do: false
|
2017-03-21 00:01:55 +00:00
|
|
|
def value?(%{"@value" => _}), do: true
|
2018-01-27 22:32:41 +00:00
|
|
|
def value?(_), do: false
|
2017-03-21 00:01:55 +00:00
|
|
|
|
|
|
|
end
|