cannam@226: /* cannam@226: Copyright 2011-2016 David Robillard cannam@226: cannam@226: Permission to use, copy, modify, and/or distribute this software for any cannam@226: purpose with or without fee is hereby granted, provided that the above cannam@226: copyright notice and this permission notice appear in all copies. cannam@226: cannam@226: THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES cannam@226: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF cannam@226: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR cannam@226: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES cannam@226: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN cannam@226: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF cannam@226: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. cannam@226: */ cannam@226: cannam@226: #include "serd_internal.h" cannam@226: cannam@226: #include cannam@226: #include cannam@226: cannam@226: typedef struct { cannam@226: SerdNode name; cannam@226: SerdNode uri; cannam@226: } SerdPrefix; cannam@226: cannam@226: struct SerdEnvImpl { cannam@226: SerdPrefix* prefixes; cannam@226: size_t n_prefixes; cannam@226: SerdNode base_uri_node; cannam@226: SerdURI base_uri; cannam@226: }; cannam@226: cannam@226: SERD_API cannam@226: SerdEnv* cannam@226: serd_env_new(const SerdNode* base_uri) cannam@226: { cannam@226: SerdEnv* env = (SerdEnv*)calloc(sizeof(struct SerdEnvImpl), 1); cannam@226: if (env && base_uri) { cannam@226: serd_env_set_base_uri(env, base_uri); cannam@226: } cannam@226: return env; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: void cannam@226: serd_env_free(SerdEnv* env) cannam@226: { cannam@226: for (size_t i = 0; i < env->n_prefixes; ++i) { cannam@226: serd_node_free(&env->prefixes[i].name); cannam@226: serd_node_free(&env->prefixes[i].uri); cannam@226: } cannam@226: free(env->prefixes); cannam@226: serd_node_free(&env->base_uri_node); cannam@226: free(env); cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: const SerdNode* cannam@226: serd_env_get_base_uri(const SerdEnv* env, cannam@226: SerdURI* out) cannam@226: { cannam@226: if (out) { cannam@226: *out = env->base_uri; cannam@226: } cannam@226: return &env->base_uri_node; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: SerdStatus cannam@226: serd_env_set_base_uri(SerdEnv* env, cannam@226: const SerdNode* uri_node) cannam@226: { cannam@226: if (!env || !uri_node) { cannam@226: return SERD_ERR_BAD_ARG; cannam@226: } cannam@226: cannam@226: // Resolve base URI and create a new node and URI for it cannam@226: SerdURI base_uri; cannam@226: SerdNode base_uri_node = serd_node_new_uri_from_node( cannam@226: uri_node, &env->base_uri, &base_uri); cannam@226: cannam@226: if (base_uri_node.buf) { cannam@226: // Replace the current base URI cannam@226: serd_node_free(&env->base_uri_node); cannam@226: env->base_uri_node = base_uri_node; cannam@226: env->base_uri = base_uri; cannam@226: return SERD_SUCCESS; cannam@226: } cannam@226: return SERD_ERR_BAD_ARG; cannam@226: } cannam@226: cannam@226: static inline SerdPrefix* cannam@226: serd_env_find(const SerdEnv* env, cannam@226: const uint8_t* name, cannam@226: size_t name_len) cannam@226: { cannam@226: for (size_t i = 0; i < env->n_prefixes; ++i) { cannam@226: const SerdNode* const prefix_name = &env->prefixes[i].name; cannam@226: if (prefix_name->n_bytes == name_len) { cannam@226: if (!memcmp(prefix_name->buf, name, name_len)) { cannam@226: return &env->prefixes[i]; cannam@226: } cannam@226: } cannam@226: } cannam@226: return NULL; cannam@226: } cannam@226: cannam@226: static void cannam@226: serd_env_add(SerdEnv* env, cannam@226: const SerdNode* name, cannam@226: const SerdNode* uri) cannam@226: { cannam@226: SerdPrefix* const prefix = serd_env_find(env, name->buf, name->n_bytes); cannam@226: if (prefix) { cannam@226: SerdNode old_prefix_uri = prefix->uri; cannam@226: prefix->uri = serd_node_copy(uri); cannam@226: serd_node_free(&old_prefix_uri); cannam@226: } else { cannam@226: env->prefixes = (SerdPrefix*)realloc( cannam@226: env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); cannam@226: env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); cannam@226: env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); cannam@226: } cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: SerdStatus cannam@226: serd_env_set_prefix(SerdEnv* env, cannam@226: const SerdNode* name, cannam@226: const SerdNode* uri_node) cannam@226: { cannam@226: if (!name->buf || uri_node->type != SERD_URI) { cannam@226: return SERD_ERR_BAD_ARG; cannam@226: } else if (serd_uri_string_has_scheme(uri_node->buf)) { cannam@226: // Set prefix to absolute URI cannam@226: serd_env_add(env, name, uri_node); cannam@226: } else { cannam@226: // Resolve relative URI and create a new node and URI for it cannam@226: SerdURI abs_uri; cannam@226: SerdNode abs_uri_node = serd_node_new_uri_from_node( cannam@226: uri_node, &env->base_uri, &abs_uri); cannam@226: cannam@226: // Set prefix to resolved (absolute) URI cannam@226: serd_env_add(env, name, &abs_uri_node); cannam@226: serd_node_free(&abs_uri_node); cannam@226: } cannam@226: return SERD_SUCCESS; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: SerdStatus cannam@226: serd_env_set_prefix_from_strings(SerdEnv* env, cannam@226: const uint8_t* name, cannam@226: const uint8_t* uri) cannam@226: { cannam@226: const SerdNode name_node = serd_node_from_string(SERD_LITERAL, name); cannam@226: const SerdNode uri_node = serd_node_from_string(SERD_URI, uri); cannam@226: cannam@226: return serd_env_set_prefix(env, &name_node, &uri_node); cannam@226: } cannam@226: cannam@226: static inline bool cannam@226: is_nameChar(const uint8_t c) cannam@226: { cannam@226: return is_alpha(c) || is_digit(c) || c == '_'; cannam@226: } cannam@226: cannam@226: /** cannam@226: Return true iff `buf` is a valid prefixed name suffix. cannam@226: TODO: This is more strict than it should be. cannam@226: */ cannam@226: static inline bool cannam@226: is_name(const uint8_t* buf, size_t len) cannam@226: { cannam@226: for (size_t i = 0; i < len; ++i) { cannam@226: if (!is_nameChar(buf[i])) { cannam@226: return false; cannam@226: } cannam@226: } cannam@226: return true; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: bool cannam@226: serd_env_qualify(const SerdEnv* env, cannam@226: const SerdNode* uri, cannam@226: SerdNode* prefix_name, cannam@226: SerdChunk* suffix) cannam@226: { cannam@226: for (size_t i = 0; i < env->n_prefixes; ++i) { cannam@226: const SerdNode* const prefix_uri = &env->prefixes[i].uri; cannam@226: if (uri->n_bytes >= prefix_uri->n_bytes) { cannam@226: if (!strncmp((const char*)uri->buf, cannam@226: (const char*)prefix_uri->buf, cannam@226: prefix_uri->n_bytes)) { cannam@226: *prefix_name = env->prefixes[i].name; cannam@226: suffix->buf = uri->buf + prefix_uri->n_bytes; cannam@226: suffix->len = uri->n_bytes - prefix_uri->n_bytes; cannam@226: if (is_name(suffix->buf, suffix->len)) { cannam@226: return true; cannam@226: } cannam@226: } cannam@226: } cannam@226: } cannam@226: return false; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: SerdStatus cannam@226: serd_env_expand(const SerdEnv* env, cannam@226: const SerdNode* qname, cannam@226: SerdChunk* uri_prefix, cannam@226: SerdChunk* uri_suffix) cannam@226: { cannam@226: const uint8_t* const colon = (const uint8_t*)memchr( cannam@226: qname->buf, ':', qname->n_bytes + 1); cannam@226: if (!colon) { cannam@226: return SERD_ERR_BAD_ARG; // Invalid qname cannam@226: } cannam@226: cannam@226: const size_t name_len = colon - qname->buf; cannam@226: const SerdPrefix* const prefix = serd_env_find(env, qname->buf, name_len); cannam@226: if (prefix) { cannam@226: uri_prefix->buf = prefix->uri.buf; cannam@226: uri_prefix->len = prefix->uri.n_bytes; cannam@226: uri_suffix->buf = colon + 1; cannam@226: uri_suffix->len = qname->n_bytes - (colon - qname->buf) - 1; cannam@226: return SERD_SUCCESS; cannam@226: } cannam@226: return SERD_ERR_NOT_FOUND; cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: SerdNode cannam@226: serd_env_expand_node(const SerdEnv* env, cannam@226: const SerdNode* node) cannam@226: { cannam@226: switch (node->type) { cannam@226: case SERD_CURIE: { cannam@226: SerdChunk prefix; cannam@226: SerdChunk suffix; cannam@226: if (serd_env_expand(env, node, &prefix, &suffix)) { cannam@226: return SERD_NODE_NULL; cannam@226: } cannam@226: const size_t len = prefix.len + suffix.len; cannam@226: uint8_t* buf = (uint8_t*)malloc(len + 1); cannam@226: SerdNode ret = { buf, len, 0, 0, SERD_URI }; cannam@226: snprintf((char*)buf, len + 1, "%s%s", prefix.buf, suffix.buf); cannam@226: ret.n_chars = serd_strlen(buf, NULL, NULL); cannam@226: return ret; cannam@226: } cannam@226: case SERD_URI: { cannam@226: SerdURI ignored; cannam@226: return serd_node_new_uri_from_node(node, &env->base_uri, &ignored); cannam@226: } cannam@226: default: cannam@226: return SERD_NODE_NULL; cannam@226: } cannam@226: } cannam@226: cannam@226: SERD_API cannam@226: void cannam@226: serd_env_foreach(const SerdEnv* env, cannam@226: SerdPrefixSink func, cannam@226: void* handle) cannam@226: { cannam@226: for (size_t i = 0; i < env->n_prefixes; ++i) { cannam@226: func(handle, &env->prefixes[i].name, &env->prefixes[i].uri); cannam@226: } cannam@226: }