[tracker] Handle correctly namespaces without '#' at the end (like DC)
- From: Ivan Frade <ifrade src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [tracker] Handle correctly namespaces without '#' at the end (like DC)
- Date: Sat, 14 Nov 2009 17:52:58 +0000 (UTC)
commit 61e02311f752654d704dc4ee8ae1fc1020f55885
Author: Ivan Frade <ivan frade nokia com>
Date: Fri Nov 13 16:48:02 2009 +0200
Handle correctly namespaces without '#' at the end (like DC)
utils/services/qname-test.c | 52 +++++++++++++++++++++++++++++---
utils/services/qname.c | 68 +++++++++++++++++++++++++++---------------
2 files changed, 91 insertions(+), 29 deletions(-)
---
diff --git a/utils/services/qname-test.c b/utils/services/qname-test.c
index 1efee86..caf4ce2 100644
--- a/utils/services/qname-test.c
+++ b/utils/services/qname-test.c
@@ -7,6 +7,8 @@ test_qname_to_shortname (void)
{
gchar *result = NULL;
+ qname_init ("test://local_uri#", "local", "./file-class.cache.test");
+
result = qname_to_shortname ("http://purl.org/dc/elements/1.1/source");
g_assert_cmpstr (result, ==, "dc:source");
g_free (result);
@@ -22,6 +24,8 @@ test_qname_to_shortname (void)
result = qname_to_shortname ("test://doesnt_exists#Class");
g_assert_cmpstr (result, ==, "test://doesnt_exists#Class");
g_free (result);
+
+ qname_shutdown ();
}
static void
@@ -29,6 +33,8 @@ test_qname_to_classname (void)
{
gchar *result = NULL;
+ qname_init ("test://local_uri#", "local", "./file-class.cache.test");
+
result = qname_to_classname ("http://purl.org/dc/elements/1.1/source");
g_assert_cmpstr (result, ==, "source");
g_free (result);
@@ -44,6 +50,8 @@ test_qname_to_classname (void)
result = qname_to_classname ("test://doesnt_exists#Class");
g_assert_cmpstr (result, ==, "test://doesnt_exists#Class");
g_free (result);
+
+ qname_shutdown ();
}
static void
@@ -51,6 +59,8 @@ test_qname_to_link (void)
{
gchar *result = NULL;
+ qname_init ("test://local_uri#", "local", "./file-class.cache.test");
+
result = qname_to_link ("test://local_uri#Class");
g_assert_cmpstr (result, ==, "#Class");
g_free (result);
@@ -65,7 +75,40 @@ test_qname_to_link (void)
result = qname_to_link ("http://www.tracker-project.org/ontologies/tracker#Namespace");
g_assert_cmpstr (result, ==, "../rdf/index.html#Namespace");
g_free (result);
-
+
+ qname_shutdown ();
+}
+
+static void
+test_process_dc (void)
+{
+ /* DC is the only ontology namespaces without '#' at the end
+ This force us to handle it as special case in a lot of places
+ */
+
+ gchar *result = NULL;
+
+ qname_init ("test://dc_style/", "local", "./file-class.cache.test");
+
+ /* local */
+ result = qname_to_link ("test://dc_style/Class");
+ g_assert_cmpstr (result, ==, "#Class");
+ g_free (result);
+
+ /* out of this file */
+ result = qname_to_link ("http://purl.org/dc/elements/1.1/creator");
+ g_assert_cmpstr (result, ==, "../dc/index.html#creator");
+ g_free (result);
+
+ result = qname_to_shortname ("http://purl.org/dc/elements/1.1/creator");
+ g_assert_cmpstr (result, ==, "dc:creator");
+ g_free (result);
+
+ result = qname_to_classname ("http://purl.org/dc/elements/1.1/creator");
+ g_assert_cmpstr (result, ==, "creator");
+ g_free (result);
+
+ qname_shutdown ();
}
int
@@ -76,8 +119,6 @@ main (int argc, char **argv)
g_type_init ();
g_test_init (&argc, &argv, NULL);
- qname_init ("test://local_uri#", "local", "./file-class.cache.test");
-
g_test_add_func ("/html_generator/qname/qname_to_shortname",
test_qname_to_shortname);
@@ -87,9 +128,10 @@ main (int argc, char **argv)
g_test_add_func ("/html_generator/qname/qname_to_link",
test_qname_to_link);
- result = g_test_run ();
+ g_test_add_func ("/html_generator/qname/dc_alike_namespaces",
+ test_process_dc);
- qname_shutdown ();
+ result = g_test_run ();
return result;
}
diff --git a/utils/services/qname.c b/utils/services/qname.c
index def776a..709b633 100644
--- a/utils/services/qname.c
+++ b/utils/services/qname.c
@@ -79,7 +79,10 @@ qname_init (const gchar *luri, const gchar *lprefix, const gchar *class_location
pieces = g_strsplit (lines[i], " ", -1);
g_assert (g_strv_length (pieces) == 2);
- g_hash_table_insert (class_deffile, pieces[1], pieces[0]);
+ g_hash_table_insert (class_deffile,
+ g_strdup(pieces[1]),
+ g_strdup(pieces[0]));
+ g_strfreev (pieces);
}
g_strfreev (lines);
@@ -92,10 +95,46 @@ void
qname_shutdown (void)
{
g_free (NAMESPACES[0].namespace);
+ NAMESPACES[0].namespace = NULL;
+
g_free (NAMESPACES[0].uri);
+ NAMESPACES[0].uri = NULL;
+
if (class_deffile) {
g_hash_table_destroy (class_deffile);
+ class_deffile = NULL;
+ }
+}
+
+static gchar **
+split_qname (const gchar *qname, GError **error)
+{
+ gchar **pieces;
+ gint i;
+
+ /* Try by '#' */
+ pieces = g_strsplit (qname, "#", 2);
+ if (g_strv_length (pieces) == 2) {
+ return pieces;
+ }
+ g_strfreev (pieces);
+ pieces = NULL;
+
+ /* Try by last '/' */
+ for (i = strlen (qname); i >= 0; i--) {
+ if (qname[i] == '/') {
+ pieces = g_new0 (gchar*, 3);
+ pieces[0] = g_strndup (qname, i);
+ pieces[1] = g_strdup (&qname[i+1]);
+ pieces[2] = NULL;
+ break;
+ }
+ }
+
+ if (pieces == NULL) {
+ g_warning ("Unable to split '%s' in prefix and class", qname);
}
+ return pieces;
}
gchar *
@@ -105,10 +144,10 @@ qname_to_link (const gchar *qname)
gchar *name;
if (NAMESPACES[0].uri) {
+
/* There is a local URI! */
if (g_str_has_prefix (qname, NAMESPACES[0].uri)) {
- pieces = g_strsplit (qname, "#", 2);
- g_assert (g_strv_length (pieces) == 2);
+ pieces = split_qname (qname, NULL);
name = g_strdup_printf ("#%s", pieces[1]);
g_strfreev (pieces);
return name;
@@ -129,6 +168,7 @@ qname_to_link (const gchar *qname)
return g_strdup (qname);
}
+
gchar *
qname_to_shortname (const gchar *qname)
{
@@ -139,27 +179,7 @@ qname_to_shortname (const gchar *qname)
for (i = 0; NAMESPACES[i].namespace != NULL; i++) {
if (g_str_has_prefix (qname, NAMESPACES[i].uri)) {
-
- pieces = g_strsplit (qname, "#", 2);
- if (g_strv_length (pieces) != 2) {
-
- /* Special case for DC. It doesnt use # in the namespace */
- if ( g_strcmp0 (NAMESPACES[i].namespace, "dc") == 0) {
- gchar *classname;
-
- g_strfreev (pieces);
- pieces = g_new0 (gchar*, 3);
-
- classname = g_strrstr (qname, "/");
- pieces[0] = g_strdup ("");
- pieces[1] = g_strdup (&classname[1]);
- pieces[2] = NULL;
- } else {
- g_warning ("Unable to get the shortname for %s", qname);
- break;
- }
- }
-
+ pieces = split_qname (qname, NULL);
name = g_strdup_printf ("%s:%s",
NAMESPACES[i].namespace,
pieces[1]);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]