aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-02-16 18:06:05 +0000
committerDavid Robillard <d@drobilla.net>2011-02-16 18:06:05 +0000
commite196a94abaddb2a90c34d735f232ee84d149a362 (patch)
tree822d09e8ce085c6565d38af0652805334b4a1a0c
parent0e7dfee8bae6655b316f1d0cabbad7df4dba6105 (diff)
downloadserd-e196a94abaddb2a90c34d735f232ee84d149a362.tar.gz
serd-e196a94abaddb2a90c34d735f232ee84d149a362.tar.bz2
serd-e196a94abaddb2a90c34d735f232ee84d149a362.zip
Make serd_writer_set_prefix return bool so it is compatible with SerdPrefixSink and can be used with serd_env_foreach.
Add SERD_STYLE_CURIED option to shorten URIs in output where possible. Fix serd_env_qualify. git-svn-id: http://svn.drobilla.net/serd/trunk@102 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r--serd/serd.h5
-rw-r--r--src/env.c4
-rw-r--r--src/writer.c27
3 files changed, 23 insertions, 13 deletions
diff --git a/serd/serd.h b/serd/serd.h
index 9655cdd2..9409e782 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -354,7 +354,8 @@ serd_reader_free(SerdReader reader);
typedef enum {
SERD_STYLE_ABBREVIATED = 1, /**< Abbreviate triples when possible. */
SERD_STYLE_ASCII = 1 << 1, /**< Escape all non-ASCII characters. */
- SERD_STYLE_RESOLVED = 1 << 2 /**< Resolve relative URIs against base. */
+ SERD_STYLE_RESOLVED = 1 << 2, /**< Resolve relative URIs against base. */
+ SERD_STYLE_CURIED = 1 << 3 /**< Shorted URIs into CURIEs. */
} SerdStyle;
/** Create a new RDF writer. */
@@ -380,7 +381,7 @@ serd_writer_set_base_uri(SerdWriter writer,
/** Set a namespace prefix (and emit directive if applicable). */
SERD_API
-void
+bool
serd_writer_set_prefix(SerdWriter writer,
const SerdNode* name,
const SerdNode* uri);
diff --git a/src/env.c b/src/env.c
index 9d3b3ca4..1becaa12 100644
--- a/src/env.c
+++ b/src/env.c
@@ -103,9 +103,9 @@ serd_env_qualify(const SerdEnv env,
if (uri->n_bytes >= prefix_uri->n_bytes) {
if (!strncmp((const char*)uri->buf,
(const char*)prefix_uri->buf,
- prefix_uri->n_bytes)) {
+ prefix_uri->n_bytes - 1)) {
*prefix_name = env->prefixes[i].name;
- suffix->buf = uri->buf + prefix_uri->n_bytes;
+ suffix->buf = uri->buf + prefix_uri->n_bytes - 1;
suffix->len = uri->n_bytes - prefix_uri->n_bytes;
return true;
}
diff --git a/src/writer.c b/src/writer.c
index 699cbaf7..21c3734b 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -231,10 +231,20 @@ write_node(SerdWriter writer,
case SERD_URI:
if ((writer->syntax == SERD_TURTLE)
&& !strcmp((const char*)node->buf, NS_RDF "type")) {
- writer->sink("a", 1, writer->stream);
+ writer->sink("a", 1, writer->stream);
+ return true;
+ } else if ((writer->style & SERD_STYLE_CURIED)
+ && serd_uri_string_has_scheme(node->buf)) {
+ SerdNode prefix;
+ SerdChunk suffix;
+ if (serd_env_qualify(writer->env, node, &prefix, &suffix)) {
+ write_text(writer, WRITE_URI, prefix.buf, prefix.n_bytes - 1, '>');
+ writer->sink(":", 1, writer->stream);
+ write_text(writer, WRITE_URI, suffix.buf, suffix.len, '>');
return true;
+ }
} else if ((writer->style & SERD_STYLE_RESOLVED)
- && !serd_uri_string_has_scheme(node->buf)) {
+ && !serd_uri_string_has_scheme(node->buf)) {
SerdURI uri;
if (serd_uri_parse(node->buf, &uri)) {
SerdURI abs_uri;
@@ -244,13 +254,11 @@ write_node(SerdWriter writer,
writer->sink(">", 1, writer->stream);
return true;
}
- } else {
- writer->sink("<", 1, writer->stream);
- write_text(writer, WRITE_URI, node->buf, node->n_bytes - 1, '>');
- writer->sink(">", 1, writer->stream);
- return true;
}
- return false;
+ writer->sink("<", 1, writer->stream);
+ write_text(writer, WRITE_URI, node->buf, node->n_bytes - 1, '>');
+ writer->sink(">", 1, writer->stream);
+ return true;
}
return true;
}
@@ -416,7 +424,7 @@ serd_writer_set_base_uri(SerdWriter writer,
}
SERD_API
-void
+bool
serd_writer_set_prefix(SerdWriter writer,
const SerdNode* name,
const SerdNode* uri)
@@ -433,6 +441,7 @@ serd_writer_set_prefix(SerdWriter writer,
writer->sink("> .\n", 4, writer->stream);
}
writer->context = WRITE_CONTEXT_NULL;
+ return true;
}
SERD_API