tracker-0.16.2/docs/tools/qname.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 
 20 #include "qname.h"
 21 #include <glib/gstdio.h>
 22 #include <string.h>
 23 
 24 /* static gchar *local_uri = NULL;
 25  * static gchar *local_prefix = NULL; */
 26 
 27 typedef struct {
 28 	const gchar *namespace;
 29 	const gchar *uri;
 30 } Namespace;
 31 
 32 static GHashTable *class_deffile = NULL;
 33 
 34 Namespace NAMESPACES [] = {
 35 	{NULL, NULL}, /* Save this for the local_uri and prefix */
 36 	{"dc", "http://purl.org/dc/elements/1.1/"},
 37 	{"xsd", "http://www.w3.org/2001/XMLSchema#"},
 38 	{"fts", "http://www.tracker-project.org/ontologies/fts#"},
 39 	{"mto", "http://www.tracker-project.org/temp/mto#"},
 40 	{"mlo", "http://www.tracker-project.org/temp/mlo#"},
 41 	{"nao", "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#"},
 42 	{"ncal", "http://www.semanticdesktop.org/ontologies/2007/04/02/ncal#"},
 43 	{"nco", "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#"},
 44 	{"nfo", "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#"},
 45 	{"nid3", "http://www.semanticdesktop.org/ontologies/2007/05/10/nid3#"},
 46 	{"nie", "http://www.semanticdesktop.org/ontologies/2007/01/19/nie#"},
 47 	{"nmm", "http://www.tracker-project.org/temp/nmm#"},
 48 	{"nmo", "http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#"},
 49 	{"nrl", "http://www.semanticdesktop.org/ontologies/2007/08/15/nrl#"},
 50 	{"rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"},
 51 	{"rdfs", "http://www.w3.org/2000/01/rdf-schema#"},
 52 	{"tracker", "http://www.tracker-project.org/ontologies/tracker#"},
 53 	{NULL, NULL}
 54 };
 55 
 56 
 57 void
 58 qname_init (const gchar *luri, const gchar *lprefix, const gchar *class_location)
 59 {
 60 	g_return_if_fail (luri != NULL);
 61 
 62 	if (NAMESPACES[0].namespace || NAMESPACES[0].uri) {
 63 		g_warning ("Reinitializing qname_module");
 64 		/* The strange casting is checked */
 65 		g_free ((gchar *) NAMESPACES[0].namespace);
 66 		g_free ((gchar *) NAMESPACES[0].uri);
 67 		if (class_deffile) {
 68 			g_hash_table_destroy (class_deffile);
 69 		}
 70 	}
 71 
 72 	NAMESPACES[0].uri = g_strdup (luri);
 73 	NAMESPACES[0].namespace = (lprefix != NULL ? g_strdup (lprefix) : g_strdup (""));
 74 
 75 	if (class_location) {
 76 		/* Process a file that contains: dir class pairs by line
 77 		 */
 78 		gint   i;
 79 		gchar  *raw_content = NULL;
 80 		gchar **lines;
 81 		gsize   length;
 82 
 83 		if (!g_file_get_contents (class_location, &raw_content, &length, NULL)) {
 84 			g_error ("Unable to load '%s'", class_location);
 85 		}
 86 
 87 		class_deffile = g_hash_table_new_full (g_str_hash,
 88 		                                       g_str_equal,
 89 		                                       g_free,
 90 		                                       g_free);
 91 
 92 		lines = g_strsplit (raw_content, "\n", -1);
 93 		for (i = 0; lines[i] != NULL; i++) {
 94 			gchar **pieces = NULL;
 95 
 96 			if (strlen (lines[i]) < 1) {
 97 				continue;
 98 			}
 99 
100 
101 			pieces = g_strsplit (lines[i], " ", -1);
102 			g_assert (g_strv_length (pieces) == 2);
103 			g_hash_table_insert (class_deffile,
104 			                     g_strdup(pieces[1]),
105 			                     g_strdup(pieces[0]));
106 			g_strfreev (pieces);
107 
108 		}
109 		g_strfreev (lines);
110 		g_free (raw_content);
111 	}
112 
113 }
114 
115 void
116 qname_shutdown (void)
117 {
118 	/* The strange casting is checked */
119 	g_free ((gchar*) NAMESPACES[0].namespace);
120 	NAMESPACES[0].namespace = NULL;
121 
122 	g_free ((gchar*) NAMESPACES[0].uri);
123 	NAMESPACES[0].uri = NULL;
124 
125 	if (class_deffile) {
126 		g_hash_table_destroy (class_deffile);
127 		class_deffile = NULL;
128 	}
129 }
130 
131 static gchar **
132 split_qname (const gchar *qname, GError **error)
133 {
134 	gchar **pieces;
135 	gint    i;
136 
137 	/* Try by '#' */
138 	pieces = g_strsplit (qname, "#", 2);
139 	if (g_strv_length (pieces) == 2) {
140 		return pieces;
141 	}
142 	g_strfreev (pieces);
143 	pieces = NULL;
144 
145 	/* Try by last '/' */
146 	for (i = strlen (qname); i >= 0; i--) {
147 		if (qname[i] == '/') {
148 			pieces = g_new0 (gchar*, 3);
149 			pieces[0] = g_strndup (qname, i);
150 			pieces[1] = g_strdup (&qname[i+1]);
151 			pieces[2] = NULL;
152 			break;
153 		}
154 	}
155 
156 	if (pieces == NULL) {
157 		g_warning ("Unable to split '%s' in prefix and class", qname);
158 	}
159 	return pieces;
160 }
161 
162 gchar *
163 qname_to_link (const gchar *qname)
164 {
165 	gchar **pieces;
166 	gchar *name;
167 
168 	if (NAMESPACES[0].uri) {
169 
170 		/* There is a local URI! */
171 		if (g_str_has_prefix (qname, NAMESPACES[0].uri)) {
172 			pieces = split_qname (qname, NULL);
173 			name = g_strdup_printf ("#%s", pieces[1]);
174 			g_strfreev (pieces);
175 			return name;
176 		}
177 	}
178 
179 	if (class_deffile) {
180 		gchar *dir, *shortname;
181 		shortname = qname_to_shortname (qname);
182 		dir = g_hash_table_lookup (class_deffile, shortname);
183 		g_free (shortname);
184 		if (dir) {
185 			return g_strdup_printf ("../%s/index.html#%s",
186 			                        dir, qname_to_classname (qname));
187 		}
188 	}
189 
190 	return g_strdup (qname);
191 }
192 
193 
194 gchar *
195 qname_to_shortname (const gchar *qname)
196 {
197 	gchar **pieces;
198 	gchar  *name = NULL;
199 	gint    i;
200 
201 	for (i = 0; NAMESPACES[i].namespace != NULL; i++) {
202 		if (g_str_has_prefix (qname, NAMESPACES[i].uri)) {
203 
204 			pieces = split_qname (qname, NULL);
205 			name = g_strdup_printf ("%s:%s",
206 			                        NAMESPACES[i].namespace,
207 			                        pieces[1]);
208 			g_strfreev (pieces);
209 			break;
210 		}
211 	}
212 
213 	if (!name) {
214 		return g_strdup (qname);
215 	} else {
216 		return name;
217 	}
218 }
219 
220 gchar *
221 qname_to_classname (const gchar *qname) {
222 
223 	gchar  *shortname;
224 	gchar **pieces;
225 	gchar  *classname = NULL;
226 
227 	shortname = qname_to_shortname (qname);
228 	if (g_strcmp0 (qname, shortname) == 0) {
229 		return shortname;
230 	}
231 	pieces = g_strsplit (shortname, ":", -1);
232 	g_assert (g_strv_length (pieces) == 2);
233 
234 	classname = g_strdup (pieces[1]);
235 	g_strfreev (pieces);
236 	g_free (shortname);
237 
238 	return classname;
239 }
240 
241 gboolean
242 qname_is_basic_type (const gchar *qname)
243 {
244 	gint i;
245 
246 	/* dc: or xsd: are basic types */
247 	for (i = 1; NAMESPACES[i].namespace != NULL && i < 3; i++) {
248 		if (g_str_has_prefix (qname, NAMESPACES[i].uri)) {
249 			return TRUE;
250 		}
251 	}
252 
253 	return FALSE;
254 }