Browse Source

Test and fix namespace de-normalization.

Stefano Cossu 3 years ago
parent
commit
926148d3bb
3 changed files with 45 additions and 18 deletions
  1. 35 16
      src/namespace.c
  2. 0 2
      test/test_graph.c
  3. 10 0
      test/test_namespace.c

+ 35 - 16
src/namespace.c

@@ -141,35 +141,43 @@ LSUP_nsmap_get (const NSMap *map, const ns_pfx pfx)
 
 LSUP_rc
 LSUP_nsmap_normalize_uri (
-        const NSMap *map, const char *uri, char **pfx_uri)
+        const NSMap *map, const char *fq_uri, char **pfx_uri_p)
 {
-    *pfx_uri = NULL;
+    char *pfx_uri = NULL;
 
     NSIndex *entry;
-    HASH_FIND_STR (map->np, uri, entry);
+    HASH_FIND_STR (map->np, fq_uri, entry);
+    for (entry = map->np; entry != NULL; entry = entry->hh.next) {
+        if (memcmp (entry->ns->ns, fq_uri, strlen (entry->ns->ns)) == 0)
+            break;
+    }
 
     if (entry) {
-        *pfx_uri = malloc (
+        pfx_uri = malloc (
                 strlen (entry->ns->pfx)
-                + strlen (uri) - strlen (entry->ns->ns)
+                + strlen (fq_uri) - strlen (entry->ns->ns)
                 + 2); // one for terminating \x00, one for the colon.
-        if (UNLIKELY (! (*pfx_uri))) return LSUP_MEM_ERR;
+        if (UNLIKELY (! (pfx_uri))) return LSUP_MEM_ERR;
 
         sprintf (
-                *pfx_uri, "%s:%s", 
-                entry->ns->pfx, uri + strlen (entry->ns->ns));
+                pfx_uri, "%s:%s",
+                entry->ns->pfx, fq_uri + strlen (entry->ns->ns));
 
-        return LSUP_OK;
+    }
 
-    } else return LSUP_NORESULT;
+    else pfx_uri = strdup (fq_uri);
+
+    *pfx_uri_p = pfx_uri;
+
+    return LSUP_OK;
 }
 
 
 LSUP_rc
 LSUP_nsmap_denormalize_uri (
-        const NSMap *map, const char *pfx_uri, char **uri)
+        const NSMap *map, const char *pfx_uri, char **fq_uri_p)
 {
-    *uri = NULL;
+    char *fq_uri = NULL;
 
     size_t pfx_len = strcspn (pfx_uri, ":");
     if (pfx_len >= PFX_LEN) pfx_len = PFX_LEN - 1;
@@ -179,12 +187,23 @@ LSUP_nsmap_denormalize_uri (
     pfx[pfx_len] = 0;
 
     Namespace *entry;
-    HASH_FIND_STR (map->pn, pfx, entry);
+    for (entry = map->pn; entry != NULL; entry = entry->hh.next) {
+        if (strncmp (entry->pfx, pfx_uri, strlen (entry->pfx)) == 0)
+            break;
+    }
+
+    if (entry) {
+        size_t fq_size = strlen (entry->ns) + strlen (pfx_uri) - pfx_len - 1;
+        fq_uri = malloc (fq_size);
+        if (UNLIKELY (! (fq_uri))) return LSUP_MEM_ERR;
+
+        strcpy (fq_uri, entry->ns);
+        strcat (fq_uri, pfx_uri + pfx_len + 1);
+    }
 
-    if (entry)
-        *uri = malloc (strlen (entry->ns) + strlen (pfx_uri) - pfx_len - 1);
+    else fq_uri = strdup (pfx_uri);
 
-    else *uri = strdup (pfx_uri);
+    *fq_uri_p = fq_uri;
 
     return LSUP_OK;
 }

+ 0 - 2
test/test_graph.c

@@ -111,8 +111,6 @@ _graph_lookup (LSUP_store_type type)
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
 
-    LSUP_Triple *spo;
-
     for (int i = 0; i < N_LUT; i++) {
         printf ("Checking tiple #%d on %d... ", i, type);
         LSUP_GraphIterator *it = LSUP_graph_lookup (

+ 10 - 0
test/test_namespace.c

@@ -21,6 +21,16 @@ test_namespace()
             "Non-existent NS found!");
     ASSERT (LSUP_nsmap_get (nsm, "none") == NULL, "Non-existent NS found!");
 
+    char *fq_uri, *pfx_uri;
+    fq_uri = "http://purl.org/dc/elements/1.1/title";
+
+    EXPECT_PASS (LSUP_nsmap_normalize_uri (nsm, fq_uri, &pfx_uri));
+    EXPECT_STR_EQ (pfx_uri, "dc:title");
+
+    fq_uri = NULL;
+    EXPECT_PASS (LSUP_nsmap_denormalize_uri (nsm, pfx_uri, &fq_uri));
+    EXPECT_STR_EQ (fq_uri, "http://purl.org/dc/elements/1.1/title");
+
     EXPECT_PASS (LSUP_nsmap_remove (nsm, "dc"));
     ASSERT (
             LSUP_nsmap_remove (nsm, "none") == LSUP_NOACTION,