aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2020-06-21 18:19:48 +0200
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:06 -0500
commit8a13a270d9150e0cd14a049c76c601d09ee539bf (patch)
treebc4a82f47bb296a6277a65fa97c42deb8b908788
parentdc8831a4ebfee8a61975123f969f737845ad537f (diff)
downloadserd-8a13a270d9150e0cd14a049c76c601d09ee539bf.tar.gz
serd-8a13a270d9150e0cd14a049c76c601d09ee539bf.tar.bz2
serd-8a13a270d9150e0cd14a049c76c601d09ee539bf.zip
WIP: Add serd_node_from_syntax() and serd_node_to_syntax()
-rw-r--r--include/serd/serd.h38
-rw-r--r--meson.build1
-rw-r--r--src/node_syntax.c96
-rw-r--r--src/world.c2
-rw-r--r--src/world.h1
-rw-r--r--src/writer.c8
-rw-r--r--src/writer.h20
-rw-r--r--test/meson.build1
-rw-r--r--test/test_node.c4
-rw-r--r--test/test_node_syntax.c153
10 files changed, 322 insertions, 2 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index f3402dd1..3c75518e 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -715,6 +715,44 @@ serd_write_uri(SerdURIView uri,
*/
/**
+ Create a node from a string representation in `syntax`.
+
+ The string should be a node as if written as an object in the given syntax,
+ without any extra quoting or punctuation, which is the format returned by
+ serd_node_to_syntax(). These two functions, when used with #SERD_TURTLE,
+ can be used to round-trip any node to a string and back.
+
+ @param str String representation of a node.
+
+ @param syntax Syntax to use. Should be either SERD_TURTLE or SERD_NTRIPLES
+ (the others are redundant). Note that namespaced (CURIE) nodes and relative
+ URIs can not be expressed in NTriples.
+
+ @return A newly allocated node that must be freed with serd_node_free().
+*/
+SERD_API
+SerdNode* SERD_ALLOCATED
+serd_node_from_syntax(const char* SERD_NONNULL str, SerdSyntax syntax);
+
+/**
+ Return a string representation of `node` in `syntax`.
+
+ The returned string represents that node as if written as an object in the
+ given syntax, without any extra quoting or punctuation.
+
+ @param node Node to serialise.
+
+ @param syntax Syntax to use. Should be either SERD_TURTLE or SERD_NTRIPLES
+ (the others are redundant). Note that namespaced (CURIE) nodes and relative
+ URIs can not be expressed in NTriples.
+
+ @return A newly allocated string that must be freed with serd_free().
+*/
+SERD_API
+char* SERD_ALLOCATED
+serd_node_to_syntax(const SerdNode* SERD_NONNULL node, SerdSyntax syntax);
+
+/**
Create a new "simple" node that is just a string.
This can be used to create blank, CURIE, or URI nodes from an already
diff --git a/meson.build b/meson.build
index 078b36a6..0a6efce2 100644
--- a/meson.build
+++ b/meson.build
@@ -98,6 +98,7 @@ sources = [
'src/env.c',
'src/n3.c',
'src/node.c',
+ 'src/node_syntax.c',
'src/nodes.c',
'src/reader.c',
'src/sink.c',
diff --git a/src/node_syntax.c b/src/node_syntax.c
new file mode 100644
index 00000000..88d360dd
--- /dev/null
+++ b/src/node_syntax.c
@@ -0,0 +1,96 @@
+/*
+ Copyright 2011-2020 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "writer.h"
+
+#include "serd/serd.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static SerdStatus
+quiet_error_func(void* const handle, const SerdError* const e)
+{
+ (void)handle;
+ (void)e;
+ return SERD_SUCCESS;
+}
+
+static SerdStatus
+on_node_string_event(void* const handle, const SerdEvent* const event)
+{
+ if (event->type == SERD_STATEMENT) {
+ *(SerdNode**)handle =
+ serd_node_copy(serd_statement_object(event->statement.statement));
+ }
+
+ return SERD_SUCCESS;
+}
+
+SerdNode*
+serd_node_from_syntax(const char* const str, const SerdSyntax syntax)
+{
+ static const char* const prelude =
+ "_:s <http://www.w3.org/2000/01/rdf-schema#object>";
+
+ const size_t str_len = strlen(str);
+ const size_t doc_len = strlen(prelude) + str_len + 4;
+ char* const doc = (char*)calloc(doc_len + 1, 1);
+
+ snprintf(doc, doc_len + 1, "%s %s .", prelude, str);
+
+ SerdNode* object = NULL;
+ SerdWorld* const world = serd_world_new();
+ SerdSink* const sink = serd_sink_new(&object, on_node_string_event, NULL);
+
+ SerdByteSource* const source = serd_byte_source_new_string(doc, NULL);
+ SerdReader* const reader = serd_reader_new(
+ world, syntax, SERD_READ_EXACT_BLANKS, sink, 1024 + doc_len);
+
+ serd_world_set_error_func(world, quiet_error_func, NULL);
+ serd_reader_start(reader, source);
+ serd_reader_read_document(reader);
+ serd_reader_finish(reader);
+ serd_byte_source_free(source);
+ serd_reader_free(reader);
+ serd_sink_free(sink);
+ serd_world_free(world);
+ free(doc);
+
+ return object;
+}
+
+char*
+serd_node_to_syntax(const SerdNode* const node, const SerdSyntax syntax)
+{
+ SerdWorld* const world = serd_world_new();
+ SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING());
+ SerdBuffer buffer = {NULL, 0};
+ SerdByteSink* const out = serd_byte_sink_new_buffer(&buffer);
+ SerdWriter* const writer = serd_writer_new(world, syntax, 0, env, out);
+
+ serd_world_set_error_func(world, quiet_error_func, NULL);
+
+ SerdStatus st = SERD_SUCCESS;
+ char* result = NULL;
+ if (!(st = serd_writer_write_node(writer, node)) &&
+ !(st = serd_writer_finish(writer))) {
+ result = serd_buffer_sink_finish(&buffer);
+ }
+
+ return result;
+}
diff --git a/src/world.c b/src/world.c
index 0e8df944..d166b1d3 100644
--- a/src/world.c
+++ b/src/world.c
@@ -73,6 +73,7 @@ serd_world_new(void)
const SerdStringView xsd_boolean = SERD_STATIC_STRING(NS_XSD "boolean");
const SerdStringView xsd_decimal = SERD_STATIC_STRING(NS_XSD "decimal");
const SerdStringView xsd_integer = SERD_STATIC_STRING(NS_XSD "integer");
+ const SerdStringView xsd_long = SERD_STATIC_STRING(NS_XSD "long");
world->rdf_first = serd_nodes_manage(nodes, serd_new_uri(rdf_first));
world->rdf_nil = serd_nodes_manage(nodes, serd_new_uri(rdf_nil));
@@ -81,6 +82,7 @@ serd_world_new(void)
world->xsd_boolean = serd_nodes_manage(nodes, serd_new_uri(xsd_boolean));
world->xsd_decimal = serd_nodes_manage(nodes, serd_new_uri(xsd_decimal));
world->xsd_integer = serd_nodes_manage(nodes, serd_new_uri(xsd_integer));
+ world->xsd_long = serd_nodes_manage(nodes, serd_new_uri(xsd_long));
world->blank_node = serd_new_blank(SERD_STATIC_STRING("b00000000000"));
world->nodes = nodes;
diff --git a/src/world.h b/src/world.h
index 4f4101e4..f76b9483 100644
--- a/src/world.h
+++ b/src/world.h
@@ -34,6 +34,7 @@ struct SerdWorldImpl {
const SerdNode* xsd_boolean;
const SerdNode* xsd_decimal;
const SerdNode* xsd_integer;
+ const SerdNode* xsd_long;
uint32_t next_blank_id;
};
diff --git a/src/writer.c b/src/writer.c
index fbc9dd5d..c520638e 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -14,6 +14,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include "writer.h"
+
#include "byte_sink.h"
#include "env.h"
#include "node.h"
@@ -1149,6 +1151,12 @@ serd_writer_on_event(SerdWriter* writer, const SerdEvent* event)
}
SerdStatus
+serd_writer_write_node(SerdWriter* writer, const SerdNode* node)
+{
+ return write_node(writer, node, SERD_OBJECT, 0);
+}
+
+SerdStatus
serd_writer_finish(SerdWriter* writer)
{
SerdStatus st = SERD_SUCCESS;
diff --git a/src/writer.h b/src/writer.h
new file mode 100644
index 00000000..c80fe49c
--- /dev/null
+++ b/src/writer.h
@@ -0,0 +1,20 @@
+/*
+ Copyright 2019-2020 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include "serd/serd.h"
+
+SerdStatus
+serd_writer_write_node(SerdWriter* writer, const SerdNode* node);
diff --git a/test/meson.build b/test/meson.build
index 3d12d211..79ffc85d 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -7,6 +7,7 @@ unit_tests = [
'env',
'free_null',
'node',
+ 'node_syntax',
'nodes',
'overflow',
'read_chunk',
diff --git a/test/test_node.c b/test/test_node.c
index deb1abd1..f942f242 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -298,7 +298,7 @@ test_node_equals(void)
}
static void
-test_node_from_string(void)
+test_node_from_syntax(void)
{
SerdNode* const hello = serd_new_string(SERD_STATIC_STRING("hello\""));
assert(serd_node_length(hello) == 6);
@@ -400,7 +400,7 @@ main(void)
test_get_integer();
test_blob_to_node();
test_node_equals();
- test_node_from_string();
+ test_node_from_syntax();
test_node_from_substring();
test_simple_node();
test_literal();
diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c
new file mode 100644
index 00000000..b16ee8d4
--- /dev/null
+++ b/test/test_node_syntax.c
@@ -0,0 +1,153 @@
+/*
+ Copyright 2020 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#undef NDEBUG
+
+#include "serd/serd.h"
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+static bool
+test(const SerdSyntax syntax, SerdNode* const node, const char* const expected)
+{
+ char* const str = serd_node_to_syntax(node, syntax);
+ SerdNode* const copy = serd_node_from_syntax(str, syntax);
+
+ const bool success = !strcmp(str, expected) && serd_node_equals(copy, node);
+
+ serd_node_free(copy);
+ serd_free(str);
+ serd_node_free(node);
+ return success;
+}
+
+static void
+test_common(const SerdSyntax syntax)
+{
+ static const int data[] = {4, 2};
+
+ static const SerdStringView datatype =
+ SERD_STATIC_STRING("http://example.org/Datatype");
+
+ SerdNode* const num_type =
+ serd_new_uri(SERD_STATIC_STRING("http://example.org/Decimal"));
+
+ assert(test(syntax, serd_new_string(SERD_STATIC_STRING("node")), "\"node\""));
+
+ assert(test(syntax,
+ serd_new_plain_literal(SERD_STATIC_STRING("hallo"),
+ SERD_STATIC_STRING("de")),
+ "\"hallo\"@de"));
+
+ assert(test(syntax,
+ serd_new_typed_literal(SERD_STATIC_STRING("X"), datatype),
+ "\"X\"^^<http://example.org/Datatype>"));
+
+ assert(test(syntax, serd_new_blank(SERD_STATIC_STRING("blank")), "_:blank"));
+ assert(test(syntax, serd_new_blank(SERD_STATIC_STRING("b0")), "_:b0"));
+
+ assert(test(syntax,
+ serd_new_uri(SERD_STATIC_STRING("http://example.org/")),
+ "<http://example.org/>"));
+
+ assert(test(syntax,
+ serd_new_decimal(1.25, num_type),
+ "\"1.25\"^^<http://example.org/Decimal>"));
+
+ assert(test(syntax,
+ serd_new_double(1.25),
+ "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#double>"));
+
+ assert(test(syntax,
+ serd_new_float(1.25),
+ "\"1.25E0\"^^<http://www.w3.org/2001/XMLSchema#float>"));
+
+ assert(test(syntax,
+ serd_new_integer(1234, num_type),
+ "\"1234\"^^<http://example.org/Decimal>"));
+
+ assert(
+ test(syntax,
+ serd_new_blob(data, sizeof(data), NULL),
+ "\"BAAAAAIAAAA=\"^^<http://www.w3.org/2001/XMLSchema#base64Binary>"));
+
+ serd_node_free(num_type);
+}
+
+static void
+test_ntriples(void)
+{
+ test_common(SERD_NTRIPLES);
+
+ {
+ // No namespace prefixes in NTriples
+ SerdNode* const curie = serd_new_curie(SERD_STATIC_STRING("cu:rie"));
+ assert(!serd_node_to_syntax(curie, SERD_NTRIPLES));
+ serd_node_free(curie);
+ }
+
+ {
+ // No relative URIs in NTriples
+ SerdNode* const uri = serd_new_uri(SERD_STATIC_STRING("rel/uri"));
+ assert(!serd_node_to_syntax(uri, SERD_NTRIPLES));
+ serd_node_free(uri);
+ }
+
+ assert(test(SERD_NTRIPLES,
+ serd_new_decimal(1.25, NULL),
+ "\"1.25\"^^<http://www.w3.org/2001/XMLSchema#decimal>"));
+
+ assert(test(SERD_NTRIPLES,
+ serd_new_integer(1234, NULL),
+ "\"1234\"^^<http://www.w3.org/2001/XMLSchema#integer>"));
+
+ assert(test(SERD_NTRIPLES,
+ serd_new_boolean(true),
+ "\"true\"^^<http://www.w3.org/2001/XMLSchema#boolean>"));
+
+ assert(test(SERD_NTRIPLES,
+ serd_new_boolean(false),
+ "\"false\"^^<http://www.w3.org/2001/XMLSchema#boolean>"));
+}
+
+static void
+test_turtle(void)
+{
+ test_common(SERD_TURTLE);
+
+ assert(
+ test(SERD_TURTLE, serd_new_curie(SERD_STATIC_STRING("cu:rie")), "cu:rie"));
+
+ assert(test(
+ SERD_TURTLE, serd_new_uri(SERD_STATIC_STRING("rel/uri")), "<rel/uri>"));
+
+ assert(test(SERD_TURTLE, serd_new_decimal(1.25, NULL), "1.25"));
+ assert(test(SERD_TURTLE, serd_new_integer(1234, NULL), "1234"));
+ assert(test(SERD_TURTLE, serd_new_boolean(true), "true"));
+ assert(test(SERD_TURTLE, serd_new_boolean(false), "false"));
+}
+
+int
+main(void)
+{
+ test_ntriples();
+ test_turtle();
+
+ return 0;
+}