2016-12-07 23:20:12 +00:00
|
|
|
defmodule JSON.LD do
|
2017-02-10 14:16:51 +00:00
|
|
|
|
|
|
|
# see https://www.w3.org/TR/2014/REC-json-ld-20140116/#syntax-tokens-and-keywords
|
|
|
|
@keywords ~w[
|
|
|
|
@base
|
|
|
|
@container
|
|
|
|
@context
|
|
|
|
@graph
|
|
|
|
@id
|
|
|
|
@index
|
|
|
|
@language
|
|
|
|
@list
|
|
|
|
@reverse
|
|
|
|
@set
|
|
|
|
@type
|
|
|
|
@value
|
|
|
|
@vocab
|
|
|
|
:
|
|
|
|
]
|
|
|
|
|
|
|
|
def keywords, do: @keywords
|
|
|
|
|
|
|
|
def keyword?(value) when is_binary(value) and value in @keywords, do: true
|
|
|
|
def keyword?(value), do: false
|
|
|
|
|
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
defdelegate expand(json_ld_object, opts \\ []),
|
|
|
|
to: JSON.LD.Expansion
|
2017-02-10 14:16:51 +00:00
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
defdelegate expand_iri(value, active_context, doc_relative \\ false,
|
|
|
|
vocab \\ false, local_context \\ nil, defined \\ nil),
|
|
|
|
to: JSON.LD.IRIExpansion
|
2017-02-10 14:16:51 +00:00
|
|
|
|
2017-03-03 20:51:16 +00:00
|
|
|
defdelegate compact(json_ld_object, context, opts \\ []),
|
|
|
|
to: JSON.LD.Compaction
|
|
|
|
|
2017-02-10 14:16:51 +00:00
|
|
|
|
|
|
|
@doc """
|
2017-02-19 02:47:05 +00:00
|
|
|
Generator function for `JSON.LD.Context`s.
|
2017-02-10 14:16:51 +00:00
|
|
|
"""
|
2017-02-19 02:47:05 +00:00
|
|
|
def context(args, opts \\ [])
|
2017-02-10 14:16:51 +00:00
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
def context(%{"@context" => _} = object, opts),
|
|
|
|
do: JSON.LD.Context.create(object, opts)
|
2017-02-10 14:16:51 +00:00
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
def context(context, opts),
|
|
|
|
do: JSON.LD.Context.create(%{"@context" => context}, opts)
|
2017-02-10 14:16:51 +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)
|
|
|
|
"""
|
|
|
|
# TODO: This should be part of a dedicated URI/IRI implementation and properly tested.
|
|
|
|
def absolute_iri(value, base_iri)
|
|
|
|
|
|
|
|
def absolute_iri(value, nil), do: value
|
|
|
|
|
|
|
|
def absolute_iri(value, base_iri) do
|
|
|
|
case URI.parse(value) do
|
|
|
|
# absolute?
|
|
|
|
uri = %URI{scheme: scheme} when not is_nil(scheme) -> uri
|
|
|
|
# relative
|
|
|
|
_ ->
|
|
|
|
URI.merge(base_iri, value)
|
|
|
|
end
|
|
|
|
|> to_string
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Checks if the given value is an absolute IRI.
|
|
|
|
|
|
|
|
An absolute IRI is defined in [RFC3987](http://www.ietf.org/rfc/rfc3987.txt)
|
|
|
|
containing a scheme along with a path and optional query and fragment segments.
|
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
see <https://www.w3.org/TR/json-ld-api/#dfn-absolute-iri>
|
2017-02-10 14:16:51 +00:00
|
|
|
"""
|
|
|
|
# TODO: This should be part of a dedicated URI/IRI implementation and properly tested.
|
|
|
|
def absolute_iri?(value), do: RDF.uri?(value)
|
|
|
|
|
|
|
|
def compact_iri_parts(compact_iri, exclude_bnode \\ true) do
|
|
|
|
with [prefix, suffix] when not(binary_part(suffix, 0, 2) == "//") and
|
|
|
|
not(exclude_bnode and prefix == "_") <-
|
|
|
|
String.split(compact_iri, ":", parts: 2) do
|
|
|
|
[prefix, suffix]
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
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 `_:`
|
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
see <https://www.w3.org/TR/json-ld-api/#dfn-blank-node-identifier>
|
2017-02-10 14:16:51 +00:00
|
|
|
"""
|
|
|
|
def blank_node_id?("_:" <> _), do: true
|
|
|
|
def blank_node_id?(_), do: false
|
|
|
|
|
|
|
|
|
2017-02-19 02:47:05 +00:00
|
|
|
def scalar?(value) when is_binary(value) or is_number(value) or
|
2017-02-10 14:16:51 +00:00
|
|
|
is_boolean(value), do: true
|
2017-02-19 02:47:05 +00:00
|
|
|
def scalar?(_), do: false
|
2017-02-10 14:16:51 +00:00
|
|
|
|
2017-03-03 20:51:16 +00:00
|
|
|
def list?(%{"@list" => _}), do: true
|
|
|
|
def list?(_), do: false
|
|
|
|
def index?(%{"@index" => _}), do: true
|
|
|
|
def index?(_), do: false
|
|
|
|
def value?(%{"@value" => _}), do: true
|
|
|
|
def value?(_), do: false
|
|
|
|
|
2016-12-07 23:20:12 +00:00
|
|
|
end
|