From 0094dfffead41a6c3e84f9aad70911ffcb587fbd Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 3 May 2019 20:18:43 +0200 Subject: Add input base URI option --- doc/serdi.1 | 9 +++++++++ src/serdi.c | 17 +++++++++++------ test/meson.build | 1 + test/run_test_suite.py | 20 ++++++++++++++------ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/doc/serdi.1 b/doc/serdi.1 index dba9b34c..c5d409bc 100644 --- a/doc/serdi.1 +++ b/doc/serdi.1 @@ -7,6 +7,7 @@ .Sh SYNOPSIS .Nm serdi .Op Fl abefhlqv +.Op Fl I Ar base .Op Fl c Ar prefix .Op Fl i Ar syntax .Op Fl k Ar bytes @@ -34,6 +35,14 @@ or transform URIs and blank node IDs. The options are as follows: .Pp .Bl -tag -compact -width 3n +.It Fl I Ar base +Input base URI. +Relative URI references in the input will be resolved against this. +When the input is a file, +the URI of the file is automatically used as the base URI. +This option can be used to override that, +or to provide a base URI for input from stdin or a string. +.Pp .It Fl a Write ASCII output. If this is enabled, all non-ASCII characters will be escaped, even if the output syntax allows them to be written in UTF-8. diff --git a/src/serdi.c b/src/serdi.c index 8b72945a..e93d6bcc 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -52,9 +52,10 @@ print_usage(const char* name, bool error) { FILE* const os = error ? stderr : stdout; fprintf(os, "%s", error ? "\n" : ""); - fprintf(os, "Usage: %s [OPTION]... INPUT [BASE_URI]\n", name); + fprintf(os, "Usage: %s [OPTION]... INPUT...\n", name); fprintf(os, "Read and write RDF syntax.\n"); fprintf(os, "Use - for INPUT to read from standard input.\n\n"); + fprintf(os, " -I BASE_URI Input base URI.\n"); fprintf(os, " -a Write ASCII output if possible.\n"); fprintf(os, " -b Fast bulk output for large serialisations.\n"); fprintf(os, " -c PREFIX Chop PREFIX from matching blank node IDs.\n"); @@ -96,6 +97,7 @@ main(int argc, char** argv) return print_usage(argv[0], true); } + SerdNode* base = NULL; SerdSyntax input_syntax = SERD_SYNTAX_EMPTY; SerdSyntax output_syntax = SERD_SYNTAX_EMPTY; SerdReaderFlags reader_flags = 0; @@ -118,7 +120,13 @@ main(int argc, char** argv) break; } - if (argv[a][1] == 'a') { + if (argv[a][1] == 'I') { + if (++a == argc) { + return missing_arg(argv[0], 'I'); + } + + base = serd_new_uri(SERD_MEASURE_STRING(argv[a])); + } else if (argv[a][1] == 'a') { writer_flags |= SERD_WRITE_ASCII; } else if (argv[a][1] == 'b') { bulk_write = true; @@ -216,10 +224,7 @@ main(int argc, char** argv) output_syntax = input_has_graphs ? SERD_NQUADS : SERD_NTRIPLES; } - SerdNode* base = NULL; - if (a < argc) { // Base URI given on command line - base = serd_new_uri(SERD_MEASURE_STRING(argv[a])); - } else if (!from_string && !from_stdin) { // Use input file URI + if (!base && !from_string && !from_stdin) { // Use input file URI base = serd_new_file_uri(SERD_MEASURE_STRING(input), SERD_EMPTY_STRING()); } diff --git a/test/meson.build b/test/meson.build index 49a3fe0c..8c09a91a 100644 --- a/test/meson.build +++ b/test/meson.build @@ -54,6 +54,7 @@ if get_option('utils') endforeach bad_args = [ + ['-I'], ['-c'], ['-i', 'unknown'], ['-i'], diff --git a/test/run_test_suite.py b/test/run_test_suite.py index 6a595e98..d25fb3a1 100755 --- a/test/run_test_suite.py +++ b/test/run_test_suite.py @@ -83,8 +83,9 @@ def test_thru( isyntax, "-p", "foo", - path, + "-I", base_uri, + path, ] ) @@ -99,8 +100,9 @@ def test_thru( "-c", "foo", "-a", - out_path, + "-I", base_uri, + out_path, ] ) @@ -146,7 +148,7 @@ def _load_rdf(filename, base_uri, command_prefix): model = {} instances = {} - cmd = command_prefix + [filename, base_uri] + cmd = command_prefix + ["-I", base_uri, filename] proc = subprocess.run(cmd, capture_output=True, check=True) for line in proc.stdout.splitlines(): matches = re.match( @@ -248,9 +250,15 @@ def test_suite( test_name = os.path.basename(test_uri_path) test_path = os.path.join(test_dir, test_name) - command = ( - command_prefix + ["-a", "-o", osyntax] + [test_path, test_uri] - ) + command = command_prefix + [ + "-a", + "-o", + osyntax, + "-I", + test_uri, + test_path, + ] + command_string = " ".join(shlex.quote(c) for c in command) out_filename = os.path.join(out_test_dir, test_name + ".out") -- cgit v1.2.1