summaryrefslogtreecommitdiff
path: root/docs/tools/ttlresource2xml.c
blob: 84fef3cca0f3c1b2975940919b0ffaeba745c76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2015, Carlos Garnacho
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * Authors: Carlos Garnacho <carlosg@gnome.org>
 */

#include <glib-object.h>
#include <glib/gprintf.h>
#include <gio/gio.h>
#include <stdlib.h>

#include "tracker-ontology-model.h"
#include "tracker-utils.h"
#include "ttl_xml.h"
#include "ttlresource2xml.h"

static void
print_predefined_instances (FILE                 *f,
                            TrackerOntologyClass *klass,
                            TrackerOntologyModel *model)
{
	const gchar *id;
	GList *l;

	if (!klass->instances)
		return;

	id = klass->shortname;

	g_fprintf (f, "<refsect3 id='%s.predefined-instances'>", id);
	g_fprintf (f, "<title>Predefined instances</title><para>");
	g_fprintf (f, "%s has the following predefined instances: ", klass->shortname);
        g_fprintf (f, "<itemizedlist>\n");

	for (l = klass->instances; l; l = l->next) {
		g_fprintf (f, "<listitem><para>");
		g_fprintf (f, "<literal>%s</literal>", (gchar*) l->data);
		g_fprintf (f, "</para></listitem>\n");
	}

	g_fprintf (f, "</itemizedlist></para></refsect3>\n");
}

static void
print_class_hierarchy (FILE                 *f,
                       TrackerOntologyClass *klass,
                       TrackerOntologyModel *model)
{
	GPtrArray *strings;
	const gchar *id;
	gsize i;

	strings = class_get_parent_hierarchy_strings (klass, model);

	if (!strings)
		return;

	id = klass->shortname;

	g_fprintf (f, "<refsect3 id='%s.hierarchy'>", id);
	g_fprintf (f, "<title>Class hierarchy</title>");
	g_fprintf (f, "<screen>");

	for (i = 0; i < strings->len; i++) {
		HierarchyString *str = g_ptr_array_index (strings, i);
		g_fprintf (f, "    %s\n", str->str->str);
	}

	g_fprintf (f, "</screen></refsect3>\n");
	g_ptr_array_unref (strings);
}

static void
print_flag (FILE        *f,
            const gchar *flag_property_link,
            const gchar *icon_name,
            const gchar *flag_description)
{
	/* This must not contain any linebreaks, or gtkdoc-fixxrefs will not
	 * resolve the link. See https://gitlab.gnome.org/GNOME/gtk-doc/-/issues/122
	 */
	g_fprintf (f, "<link linkend=\"%s\">", flag_property_link);
	g_fprintf (f, "<inlinemediaobject>");
	g_fprintf (f, "<imageobject><imagedata fileref=\"%s\" /></imageobject>", icon_name);
	g_fprintf (f, "<alt>%s</alt>", flag_description);
	g_fprintf (f, "</inlinemediaobject>");
	g_fprintf (f, "</link>");
}

static void
print_property_table (FILE                 *f,
                      TrackerOntologyModel *model,
                      const char           *id,
                      GList                *properties)
{
	GList *l, *m;
	g_autoptr(GList) properties_sorted = NULL;

	if (!properties)
		return;

	properties_sorted = g_list_sort (g_list_copy (properties), (GCompareFunc) strcmp);

	/* We (ab)use the "struct_members" role to ensure devhelp2 <keyword> entries are
	 * generated by gtkdoc-mkhtml2. This is needed for xrefs to work between the
	 * libtracker-sparql and nepomuk ontology docs.
	 */
	g_fprintf (f, "<refsect3 role=\"struct_members\" id=\"%s.properties\">", id);
	g_fprintf (f, "<title>Properties</title>");

	g_fprintf (f, "<informaltable frame=\"none\"><tgroup cols=\"4\">");
	g_fprintf (f, "<thead><row><entry>Name</entry><entry>Type</entry><entry>Notes</entry><entry>Description</entry></row></thead>");

	g_fprintf (f, "<tbody>");
	for (l = properties_sorted; l; l = l->next) {
		TrackerOntologyProperty *prop;
		TrackerOntologyClass *range;
		const gchar *shortname = NULL, *basename = NULL, *type_name = NULL, *type_class_id = NULL, *prop_id = NULL;

		prop = tracker_ontology_model_get_property (model, l->data);
		range = tracker_ontology_model_get_class (model, prop->range->data);

		prop_id = shortname = prop->shortname;
		basename = prop->basename;
		type_name = range->basename;
		type_class_id = range->shortname;

		g_fprintf (f, "<row role=\"member\">");

		/* Property name column */
		g_fprintf (f, "<entry role=\"struct_member_name\">");
		/* This id is globally unique and can be used for internal links.
		 * We abuse <structfield> so that gtkdoc-mkhtml2 creates a usable link. */
		g_fprintf (f, "<para><structfield id=\"%s\">%s</structfield></para>", prop_id, basename);
		/* This anchor is unique within the refentry and can be used for external links */
		g_fprintf (f, "<anchor id='%s' />", basename);
		g_fprintf (f, "<indexterm zone='%s'><primary sortas='%s'>%s</primary></indexterm>",
		           prop_id, shortname, shortname);
		g_fprintf (f, "</entry>");

		/* Type column */
		g_fprintf (f, "<entry>");
		g_fprintf (f, "<link linkend=\"%s\">%s</link>", type_class_id, type_name);
		g_fprintf (f, "</entry>");

		/* Flags column */
		g_fprintf (f, "<entry>");

		if (prop->deprecated) {
			print_flag (f, "nrl-deprecated", "icon-deprecated.svg",
			            "This property is deprecated.");
		}

		if (prop->superproperties) {
			for (m = prop->superproperties; m; m = m->next) {
				const gchar *shortname = NULL, *superprop_id = NULL;
				g_autofree gchar *message = NULL;
				TrackerOntologyProperty *superprop;

				superprop = tracker_ontology_model_get_property (model, m->data);
				shortname = superprop_id = superprop->shortname;

				message = g_strdup_printf ("This property extends %s", shortname);

				print_flag (f, superprop_id, "icon-superproperty.svg", message);
			}
		}

		if (prop->max_cardinality != NULL && atoi (prop->max_cardinality) == 1) {
			/* Single valued properties are most common, so we don't display this. */
		} else {
			g_autofree gchar *message = NULL;

			if (prop->max_cardinality != NULL && atoi (prop->max_cardinality) > 0) {
				message = g_strdup_printf ("This property can have a maximum of %i values", *prop->max_cardinality);
			} else {
				message = g_strdup_printf ("This property can have multiple values.");
			}
			print_flag (f, "nrl-maxCardinality", "icon-multivalue.svg", message);
		}

		if (prop->fulltextIndexed) {
			print_flag (f, "nrl-fulltextIndexed", "icon-fulltextindexed.svg",
			            "This property is full-text-indexed, and can be looked up through <literal>fts:match</literal>");
		}

		g_fprintf (f, "</entry>");

		/* Description column */
		g_fprintf (f, "<entry>");
		if (prop->description) {
			g_fprintf (f, "<para>%s</para>", prop->description);
		}
		g_fprintf (f, "</entry>");
		g_fprintf (f, "</row>");
	}

	g_fprintf (f, "</tbody></tgroup>");
	g_fprintf (f, "</informaltable>");

	g_fprintf (f, "</refsect3>");
}

void
print_ontology_class (TrackerOntologyModel *model,
                      TrackerOntologyClass *klass,
                      FILE                 *f)
{
	const gchar *name = NULL, *id = NULL;

	g_return_if_fail (f != NULL);

	name = klass->basename;
	id = klass->shortname;

	/* Anchor for external links. */
	g_fprintf (f, "<anchor id='%s' />\n", name);

	g_fprintf (f, "<refsect2 role='rdf-class' id='%s'>\n", id);
	g_fprintf (f, "<title>%s</title>\n", name);

	if (klass->description || klass->deprecated || klass->notify) {
		g_fprintf (f, "<refsect3 id='%s.description'>\n", id);
		g_fprintf (f, "  <title>Description</title>\n");

		if (klass->description) {
			g_fprintf (f, "  %s", klass->description);
		}

		if (klass->deprecated) {
			g_fprintf (f, "<para>");
			print_flag (f, "nrl-deprecated", "icon-deprecated.svg", "Deprecated icon");
			g_fprintf (f, "This class is deprecated.");
			g_fprintf (f, "</para>");
		}

		if (klass->notify) {
			g_fprintf (f, "<para>");
			print_flag (f, "nrl-notify", "icon-notify.svg", "Notify icon");
			g_fprintf (f, "This class emits notifications about changes, and can "
			             "be monitored using <link linkend=\"TrackerNotifier\">TrackerNotifier</link>.");
			g_fprintf (f, "</para>");
		}
		g_fprintf (f, "</refsect3>\n");
	}

	if (klass->specification) {
		g_fprintf (f, "<refsect3 id='%s.specification'>\n", id);
		g_fprintf (f, "  <title>Specification</title>\n");
		g_fprintf (f, "  <ulink url=\"%s\" />", klass->specification);
		g_fprintf (f, "</refsect3>\n");
	}

	print_class_hierarchy (f, klass, model);
	print_predefined_instances (f, klass, model);

	print_property_table (f, model, id, klass->in_domain_of);

	g_fprintf (f, "</refsect2>\n");
}

void
print_ontology_extra_properties (TrackerOntologyModel *model,
                                 const char           *ontology_prefix,
                                 const char           *classname,
                                 GList                *properties_for_class,
                                 FILE                 *f)
{
	TrackerOntologyClass *klass;
	const gchar *short_classname = NULL, *class_id = NULL;
	gchar *section_id = NULL;

	g_return_if_fail (f != NULL);

	klass = tracker_ontology_model_get_class (model, classname);
	short_classname = class_id = klass->shortname;
	section_id = g_strconcat (ontology_prefix, ".", class_id, NULL);

	g_fprintf (f, "<refsect2 role='rdf-property-list' id='%s'>\n", section_id);
	g_fprintf (f, "<title>Additional properties for %s</title>\n", short_classname);

	g_fprintf (f, "<refsect3>\n");
	g_fprintf (f, "  <title>Description</title>\n");
	g_fprintf (f, "  <para>Properties this ontology defines which can describe %s resources.</para>",
	           short_classname);
	g_fprintf (f, "</refsect3>\n");

	print_property_table (f, model, section_id, properties_for_class);
	g_fprintf (f, "</refsect2>\n");

	g_free (section_id);
}