diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb62a1..89f6104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and - prefix management of `RDF.Graph`s: - the structure now has `prefixes` field with an optional `RDF.PrefixMap` - new functions `add_prefixes/2`, `delete_prefixes/2` and `clear_prefixes/1` -- configurable RDF.default_prefixes +- configurable `RDF.default_prefixes` ### Changed @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and `prefixes` field - when `RDF.Graph.add` and `RDF.Graph.put` are called with another graph, its prefixes are merged +- `RDF.Turtle.Decoder` saves the prefixes now diff --git a/lib/rdf/serializations/turtle_decoder.ex b/lib/rdf/serializations/turtle_decoder.ex index cfabde9..46bf513 100644 --- a/lib/rdf/serializations/turtle_decoder.ex +++ b/lib/rdf/serializations/turtle_decoder.ex @@ -47,7 +47,7 @@ defmodule RDF.Turtle.Decoder do def parse(tokens), do: tokens |> :turtle_parser.parse defp build_graph(ast, base) do - {graph, _} = + {graph, %State{namespaces: namespaces}} = Enum.reduce ast, {RDF.Graph.new, %State{base_iri: base}}, fn {:triples, triples_ast}, {graph, state} -> with {statements, state} = triples(triples_ast, state) do @@ -56,9 +56,15 @@ defmodule RDF.Turtle.Decoder do {:directive, directive_ast}, {graph, state} -> {graph, directive(directive_ast, state)} - end - {:ok, graph} + + {:ok, + if Enum.empty?(namespaces) do + graph + else + RDF.Graph.add_prefixes(graph, namespaces) + end + } rescue error -> {:error, Exception.message(error)} end diff --git a/test/acceptance/turtle_w3c_test.exs b/test/acceptance/turtle_w3c_test.exs index be9d63a..d60f172 100644 --- a/test/acceptance/turtle_w3c_test.exs +++ b/test/acceptance/turtle_w3c_test.exs @@ -53,8 +53,12 @@ defmodule RDF.Turtle.W3C.Test do test TestSuite.test_title(test_case), %{test_case: test_case} do with base = to_string(TestSuite.test_input_file(test_case)) do - assert (TestSuite.test_input_file_path(test_case, "Turtle") |> Turtle.read_file!(base: base)) == - (TestSuite.test_result_file_path(test_case, "Turtle") |> NTriples.read_file!) + assert (TestSuite.test_input_file_path(test_case, "Turtle") + |> Turtle.read_file!(base: base) + |> RDF.Graph.clear_prefixes() + ) == + (TestSuite.test_result_file_path(test_case, "Turtle") + |> NTriples.read_file!) end end end) diff --git a/test/unit/turtle_decoder_test.exs b/test/unit/turtle_decoder_test.exs index 45da44e..8356de4 100644 --- a/test/unit/turtle_decoder_test.exs +++ b/test/unit/turtle_decoder_test.exs @@ -218,7 +218,7 @@ defmodule RDF.Turtle.DecoderTest do assert Turtle.Decoder.decode!(""" PREFIX xsd: "42"^^xsd:integer . - """) == Graph.new({EX.spiderman, EX.p, RDF.literal(42)}) + """) == Graph.new({EX.spiderman, EX.p, RDF.literal(42)}, prefixes: %{xsd: XSD}) end test "a language tagged literal" do @@ -270,38 +270,39 @@ defmodule RDF.Turtle.DecoderTest do describe "prefixed names" do test "non-empty prefixed names" do + prefixes = RDF.PrefixMap.new(ex: ~I) assert Turtle.Decoder.decode!(""" @prefix ex: . ex:Aaron ex:Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) assert Turtle.Decoder.decode!(""" @prefix ex: . ex:Aaron ex:Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) assert Turtle.Decoder.decode!(""" PREFIX ex: ex:Aaron ex:Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) assert Turtle.Decoder.decode!(""" prefix ex: ex:Aaron ex:Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) - + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) end test "empty prefixed name" do + prefixes = RDF.PrefixMap.new("": ~I) assert Turtle.Decoder.decode!(""" @prefix : . :Aaron :Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) assert Turtle.Decoder.decode!(""" PREFIX : :Aaron :Person . - """) == Graph.new({EX.Aaron, RDF.type, EX.Person}) + """) == Graph.new({EX.Aaron, RDF.type, EX.Person}, prefixes: prefixes) end end @@ -318,14 +319,14 @@ defmodule RDF.Turtle.DecoderTest do {RDF.bnode("b1"), RDF.rest, RDF.bnode("b2")}, {RDF.bnode("b2"), RDF.first, EX.c}, {RDF.bnode("b2"), RDF.rest, RDF.nil}, - ]) + ], prefixes: %{"": ~I}) end test "empty collection" do assert Turtle.Decoder.decode!(""" @prefix : . :subject :predicate () . - """) == Graph.new({EX.subject, EX.predicate, RDF.nil}) + """) == Graph.new({EX.subject, EX.predicate, RDF.nil}, prefixes: %{"": ~I}) end test "nested collection" do @@ -343,7 +344,7 @@ defmodule RDF.Turtle.DecoderTest do {RDF.bnode("b1"), RDF.rest, RDF.bnode("b2")}, {RDF.bnode("b2"), RDF.first, EX.c}, {RDF.bnode("b2"), RDF.rest, RDF.nil}, - ]) + ], prefixes: %{"": ~I}) end end