/* Copyright 2011-2020 David Robillard 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 "node.h" #include "serd/serd.h" #include #include #include #include typedef struct { SerdNode* name; SerdNode* uri; } SerdPrefix; struct SerdEnvImpl { SerdPrefix* prefixes; size_t n_prefixes; SerdNode* base_uri_node; SerdURIView base_uri; }; SerdEnv* serd_env_new(const SerdNode* base_uri) { SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); if (env && base_uri) { serd_env_set_base_uri(env, base_uri); } return env; } void serd_env_free(SerdEnv* env) { if (!env) { return; } for (size_t i = 0; i < env->n_prefixes; ++i) { serd_node_free(env->prefixes[i].name); serd_node_free(env->prefixes[i].uri); } free(env->prefixes); serd_node_free(env->base_uri_node); free(env); } const SerdNode* serd_env_base_uri(const SerdEnv* env, SerdURIView* out) { if (out) { *out = env->base_uri; } return env->base_uri_node; } SerdStatus serd_env_set_base_uri(SerdEnv* env, const SerdNode* uri) { if (!env || (uri && uri->type != SERD_URI)) { return SERD_ERR_BAD_ARG; } if (!uri) { serd_node_free(env->base_uri_node); env->base_uri_node = NULL; env->base_uri = SERD_URI_NULL; return SERD_SUCCESS; } // Resolve base URI and create a new node and URI for it SerdURIView base_uri; SerdNode* base_uri_node = serd_new_uri_from_node(uri, &env->base_uri, &base_uri); // Replace the current base URI serd_node_free(env->base_uri_node); env->base_uri_node = base_uri_node; env->base_uri = base_uri; return SERD_SUCCESS; } static inline SERD_PURE_FUNC SerdPrefix* serd_env_find(const SerdEnv* env, const char* name, size_t name_len) { for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_name = env->prefixes[i].name; if (prefix_name->n_bytes == name_len) { if (!memcmp(serd_node_string(prefix_name), name, name_len)) { return &env->prefixes[i]; } } } return NULL; } static void serd_env_add(SerdEnv* env, const SerdNode* name, const SerdNode* uri) { const char* name_str = serd_node_string(name); SerdPrefix* const prefix = serd_env_find(env, name_str, name->n_bytes); if (prefix) { if (!serd_node_equals(prefix->uri, uri)) { SerdNode* old_prefix_uri = prefix->uri; prefix->uri = serd_node_copy(uri); serd_node_free(old_prefix_uri); } } else { env->prefixes = (SerdPrefix*)realloc( env->prefixes, (++env->n_prefixes) * sizeof(SerdPrefix)); env->prefixes[env->n_prefixes - 1].name = serd_node_copy(name); env->prefixes[env->n_prefixes - 1].uri = serd_node_copy(uri); } } SerdStatus serd_env_set_prefix(SerdEnv* env, const SerdNode* name, const SerdNode* uri) { if (!name || uri->type != SERD_URI) { return SERD_ERR_BAD_ARG; } if (serd_uri_string_has_scheme(serd_node_string(uri))) { // Set prefix to absolute URI serd_env_add(env, name, uri); } else { // Resolve relative URI and create a new node and URI for it SerdURIView abs_uri; SerdNode* abs_uri_node = serd_new_uri_from_node(uri, &env->base_uri, &abs_uri); // Set prefix to resolved (absolute) URI serd_env_add(env, name, abs_uri_node); serd_node_free(abs_uri_node); } return SERD_SUCCESS; } SerdStatus serd_env_set_prefix_from_strings(SerdEnv* env, const char* name, const char* uri) { SerdNode* name_node = serd_new_string(SERD_LITERAL, name); SerdNode* uri_node = serd_new_string(SERD_URI, uri); const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node); serd_node_free(name_node); serd_node_free(uri_node); return st; } bool serd_env_qualify(const SerdEnv* env, const SerdNode* uri, const SerdNode** prefix, SerdStringView* suffix) { for (size_t i = 0; i < env->n_prefixes; ++i) { const SerdNode* const prefix_uri = env->prefixes[i].uri; if (uri->n_bytes >= prefix_uri->n_bytes) { const char* prefix_str = serd_node_string(prefix_uri); const char* uri_str = serd_node_string(uri); if (!strncmp(uri_str, prefix_str, prefix_uri->n_bytes)) { *prefix = env->prefixes[i].name; suffix->buf = uri_str + prefix_uri->n_bytes; suffix->len = uri->n_bytes - prefix_uri->n_bytes; return true; } } } return false; } SerdStatus serd_env_expand(const SerdEnv* env, const SerdNode* curie, SerdStringView* uri_prefix, SerdStringView* uri_suffix) { const char* const str = serd_node_string(curie); const char* const colon = (const char*)memchr(str, ':', curie->n_bytes + 1); if (curie->type != SERD_CURIE || !colon) { return SERD_ERR_BAD_ARG; } const size_t name_len = (size_t)(colon - str); const SerdPrefix* const prefix = serd_env_find(env, str, name_len); if (prefix) { uri_prefix->buf = serd_node_string(prefix->uri); uri_prefix->len = prefix->uri ? prefix->uri->n_bytes : 0; uri_suffix->buf = colon + 1; uri_suffix->len = curie->n_bytes - name_len - 1; return SERD_SUCCESS; } return SERD_ERR_BAD_CURIE; } SerdNode* serd_env_expand_node(const SerdEnv* env, const SerdNode* node) { switch (node->type) { case SERD_LITERAL: break; case SERD_URI: { SerdURIView ignored; return serd_new_uri_from_node(node, &env->base_uri, &ignored); } case SERD_CURIE: { SerdStringView prefix; SerdStringView suffix; if (serd_env_expand(env, node, &prefix, &suffix)) { return NULL; } const size_t len = prefix.len + suffix.len; SerdNode* ret = serd_node_malloc(len, 0, SERD_URI); char* buf = serd_node_buffer(ret); snprintf(buf, len + 1, "%s%s", prefix.buf, suffix.buf); ret->n_bytes = len; return ret; } case SERD_BLANK: break; } return NULL; } void serd_env_foreach(const SerdEnv* env, SerdPrefixSink func, void* handle) { for (size_t i = 0; i < env->n_prefixes; ++i) { func(handle, env->prefixes[i].name, env->prefixes[i].uri); } }