瀏覽代碼

Change behavior of URI string de/normalization:

If a NSM is not passed (which shouldn't happen but it does),
or if a matching entry is not found in the map, or if the source
prefixed string has no valid prefix, the output parameter is set to
NULL.
scossu 3 天之前
父節點
當前提交
34cab7efbf
共有 3 個文件被更改,包括 30 次插入27 次删除
  1. 10 6
      include/lsup/namespace.h
  2. 1 1
      src/graph.c
  3. 19 20
      src/namespace.c

+ 10 - 6
include/lsup/namespace.h

@@ -108,9 +108,11 @@ LSUP_nsmap_get_pfx (const LSUP_NSMap *map, const char *ns);
  *
  * @param[in] pfx_uri URI string to denormalize.
  *
- * @param[out] fq_uri String pointer to be filled with the FQ URI. If the
- *  namespace is not in the map or an error occurred, this will be NULL. The
- *  caller is in charge of freeing the memory.
+ * @param[out] fq_uri String pointer to be filled with the FQ URI. The caller
+ *  is in charge of freeing the memory. If the namespace is not in the map or
+ *  an error occurred, this will be NULL. This is to inform the caller that the
+ *  result is not normalized and a TERM_NS_IRIREF should not be construed with
+ *  it.
  *
  * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  *  LSUP_MEM_ERR if a memory allocation error ocurred.
@@ -132,9 +134,11 @@ LSUP_nsmap_normalize_uri (
  *
  * @param[in] fq_uri URI string to normalize.
  *
- * @param[out] pfx_uri String pointer to be filled with the prefixed URI. If
- *  the namespace is not in the map, this will be a duplicate of the original
- *  FQ URI. The caller is in charge of freeing the memory.
+ * @param[out] pfx_uri String pointer to be filled with the prefixed URI. The
+ *  caller is in charge of freeing the memory. If the namespace is not in the
+ *  map or an error occurred, this will be NULL. This is to inform the caller
+ *  that the result is not denormalized and a TERM_IRIREF should not be
+ *  construed with it.
  *
  * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  *  LSUP_MEM_ERR if a memory allocation error ocurred.

+ 1 - 1
src/graph.c

@@ -57,7 +57,7 @@ LSUP_graph_new (LSUP_Store *store, const char *uri_str, LSUP_NSMap *nsm)
         uri_str? LSUP_iriref_new (uri_str, gr->nsm) :
         LSUP_iriref_new (NULL, NULL);
 
-    LOG_DEBUG("Graph created.");
+    LOG_DEBUG ("Graph created.");
     return gr;
 }
 

+ 19 - 20
src/namespace.c

@@ -142,15 +142,20 @@ LSUP_rc
 LSUP_nsmap_normalize_uri (
         const LSUP_NSMap *map, const char *pfx_uri, char **fq_uri_p)
 {
-    LSUP_rc rc;
+    LSUP_rc rc = LSUP_NORESULT;
     char *fq_uri = NULL;
 
+    if (UNLIKELY (!map)) {
+        log_warn ("No namespace map was passed for normalization.");
+        goto finally;
+    }
+
     size_t pfx_len = strcspn (pfx_uri, ":");
     if (pfx_len >= PFX_LEN || pfx_len == strlen (pfx_uri)) {
         log_warn (
                 "No prefix separator detected in URI `%s` within maximum "
                 "prefix length (%d characters).", pfx_uri, PFX_LEN - 1);
-        goto no_prefix;
+        goto finally;
     }
 
     LSUP_ns_pfx pfx;
@@ -169,16 +174,9 @@ LSUP_nsmap_normalize_uri (
         strcat (fq_uri, pfx_uri + pfx_len + 1);
 
         rc = LSUP_OK;
-    } else {
-        log_warn ("No NS prefix found in map to normalize %s", pfx_uri);
-
-no_prefix:
-        fq_uri = malloc (strlen(pfx_uri) + 1);
-        if (UNLIKELY (! (fq_uri))) return LSUP_MEM_ERR;
-        strcpy (fq_uri, pfx_uri);
-        rc = LSUP_NORESULT;
-    }
+    } else log_warn ("No NS prefix found in map to normalize %s", pfx_uri);
 
+finally:
     *fq_uri_p = fq_uri;
 
     return rc;
@@ -195,11 +193,17 @@ LSUP_nsmap_denormalize_uri (
      * This function has to count the characters left over from the match in
      * order to add the URI suffix.
      */
-    LSUP_rc rc;
+    LSUP_rc rc = LSUP_NORESULT;
     const NSEntry *entry;
     const char *pfx = NULL;
-    size_t i = 0, offset;
+    char *pfx_uri = NULL;
 
+    if (UNLIKELY (!map)) {
+        log_warn ("No namespace map was passed for denormalization.");
+        goto finally;
+    }
+
+    size_t i = 0, offset;
     while (hashmap_iter ((LSUP_NSMap *)map, &i, (void **) &entry)) {
         offset = strlen (entry->ns);
         if (strncmp (entry->ns, fq_uri, offset) == 0) {
@@ -207,8 +211,6 @@ LSUP_nsmap_denormalize_uri (
             break;
         }
     }
-    char *pfx_uri = NULL;
-
     if (pfx) {
         // +2: one for terminating \x00, one for the colon.
         pfx_uri = malloc (strlen (pfx) + strlen (fq_uri) - offset + 2);
@@ -217,12 +219,9 @@ LSUP_nsmap_denormalize_uri (
         sprintf (pfx_uri, "%s:%s", pfx, fq_uri + offset);
 
         rc = LSUP_OK;
+    } else log_warn ("No NS prefix found in map to denormalize %s", fq_uri);
 
-    } else {
-        pfx_uri = strdup (fq_uri);
-        rc = LSUP_NORESULT;
-    }
-
+finally:
     *pfx_uri_p = pfx_uri;
 
     return rc;