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 <glib/gprintf.h>
21 #include <string.h>
22
23 #include "ttl_sgml.h"
24 #include "qname.h"
25
26 #define DEFAULT_COPYRIGHT "© 2009, 2010 <ulink url=\"http://www.nokia.com/\">Nokia</ulink>"
27
28 #define SIGNALS_DOC "http://live.gnome.org/Tracker/Documentation/SignalsOnChanges"
29
30 typedef struct {
31 Ontology *ontology;
32 OntologyDescription *description;
33 FILE *output;
34 } CallbackInfo;
35
36
37 static void
38 print_itemized_list (FILE *f, GList *list)
39 {
40 GList *it;
41
42 g_fprintf (f, "<itemizedlist>\n");
43 for (it = list; it != NULL; it = it->next) {
44 g_fprintf (f, "<listitem>%s</listitem>\n", (gchar *)it->data);
45 }
46 g_fprintf (f, "</itemizedlist>\n");
47 }
48
49 static void
50 print_people_list (FILE *f,
51 const gchar *role,
52 GList *list)
53 {
54 if (!list) {
55 return;
56 }
57
58 g_fprintf (f, "<varlistentry>\n");
59 g_fprintf (f, " <term>%s</term>\n", role);
60 g_fprintf (f, " <listitem>\n");
61 print_itemized_list (f, list);
62 g_fprintf (f, " </listitem>\n");
63 g_fprintf (f, "</varlistentry>\n");
64 }
65
66 static void
67 print_link_as_varlistentry (FILE *f,
68 const gchar *term,
69 const gchar *link_text,
70 const gchar *link)
71 {
72 g_fprintf (f, " <varlistentry>\n");
73 g_fprintf (f," <term>%s</term>\n", term);
74 if (link) {
75 g_fprintf (f,
76 " <listitem><para><ulink url=\"%s\">%s</ulink></para></listitem>\n",
77 link, link_text);
78 } else {
79 g_fprintf (f, " <listitem><para>Not available</para></listitem>\n");
80 }
81 g_fprintf (f, " </varlistentry>\n");
82 }
83
84 static gchar *
85 shortname_to_id (const gchar *name)
86 {
87 gchar *id, *p;
88
89 id = g_strdup (name);
90 p = strchr (id, ':');
91
92 if (p) {
93 *p = '-';
94 }
95
96 return id;
97 }
98
99 static void
100 print_reference (gpointer item, gpointer user_data)
101 {
102 gchar *shortname, *id;
103 FILE *f = (FILE *)user_data;
104
105 shortname = qname_to_shortname ((gchar *) item);
106 id = shortname_to_id (shortname);
107
108 g_fprintf (f,"<link linkend='%s'>%s</link>, ", id , shortname);
109
110 g_free (shortname);
111 g_free (id);
112 }
113
114 static void
115 print_variablelist_entry (FILE *f,
116 const gchar *param,
117 const gchar *value)
118 {
119 g_fprintf (f, "<varlistentry>\n");
120 g_fprintf (f, "<term><parameter>%s</parameter> :</term>\n", param);
121 g_fprintf (f, "<listitem><simpara>%s</simpara></listitem>\n", (value) ? value : "--");
122 g_fprintf (f, "</varlistentry>\n");
123 }
124
125 static void
126 print_variablelist_entry_list (FILE *f,
127 const gchar *param,
128 GList *list)
129 {
130 g_fprintf (f, "<varlistentry>\n");
131 g_fprintf (f, "<term><parameter>%s</parameter> :</term>\n", param);
132 g_fprintf (f, "<listitem><simpara>");
133
134 if (list) {
135 g_list_foreach (list, print_reference, f);
136 } else {
137 g_fprintf (f, "--");
138 }
139
140 g_fprintf (f, "</simpara></listitem>\n");
141 g_fprintf (f, "</varlistentry>\n");
142 }
143
144 #if 0
145 static void
146 print_deprecated_message (FILE *f)
147 {
148 g_fprintf (f, "<note>\n");
149 g_fprintf (f, "<title>Note:</title>\n");
150 g_fprintf (f, "<para>This item is deprecated</para>\n");
151 g_fprintf (f, "</note>\n");
152 }
153 #endif
154
155 static void
156 print_sgml_header (FILE *f, OntologyDescription *desc)
157 {
158 gchar *upper_name;
159
160 g_fprintf (f, "<?xml version='1.0' encoding='UTF-8'?>\n");
161
162 g_fprintf (f, "<chapter id='%s-ontology'>\n", desc->localPrefix);
163
164 upper_name = g_ascii_strup (desc->localPrefix, -1);
165 g_fprintf (f, "<title>%s: %s</title>\n", desc->title, desc->description ? desc->description : "");
166 g_free (upper_name);
167
168 print_people_list (f, "Authors:", desc->authors);
169 print_people_list (f, "Editors:", desc->editors);
170 print_people_list (f, "Contributors:", desc->contributors);
171
172 print_link_as_varlistentry (f, "Upstream:", "Upstream version", desc->upstream);
173 print_link_as_varlistentry (f, "ChangeLog:", "Tracker changes", desc->gitlog);
174
175 g_fprintf (f, "<varlistentry>\n");
176 g_fprintf (f, " <term>Copyright:</term>\n");
177 g_fprintf (f, " <listitem>\n");
178 g_fprintf (f, "<para>%s</para>\n", (desc->copyright ? desc->copyright : DEFAULT_COPYRIGHT));
179 g_fprintf (f, " </listitem>\n");
180 g_fprintf (f, "</varlistentry>\n");
181 }
182
183 static void
184 print_sgml_explanation (FILE *f, const gchar *explanation_file)
185 {
186 gchar *raw_content;
187 gsize length;
188
189 if (explanation_file && g_file_test (explanation_file, G_FILE_TEST_EXISTS)) {
190 if (!g_file_get_contents (explanation_file, &raw_content, &length, NULL)) {
191 g_error ("Unable to load '%s'", explanation_file );
192 }
193 g_fprintf (f, "%s", raw_content);
194 }
195 }
196
197 static void
198 print_sgml_footer (FILE *f)
199 {
200 g_fprintf (f,"</chapter>\n");
201 }
202
203 static void
204 print_ontology_class (gpointer key, gpointer value, gpointer user_data)
205 {
206 OntologyClass *def = (OntologyClass *)value;
207 gchar *name, *id;
208 FILE *f = (FILE *)user_data;
209
210 g_return_if_fail (f != NULL);
211
212 name = qname_to_shortname (def->classname);
213
214 id = shortname_to_id (name);
215 g_fprintf (f, "<refsect2 id='%s'>\n", id);
216
217 g_free (id);
218
219 g_fprintf (f, "<title>%s</title>\n", name);
220
221 if (def->description) {
222 g_fprintf (f, "<para>%s</para>\n", def->description);
223 }
224
225 g_fprintf (f, "<variablelist>\n");
226
227 print_variablelist_entry_list (f, "Superclasses", def->superclasses);
228 print_variablelist_entry_list (f, "Subclasses", def->subclasses);
229 print_variablelist_entry_list (f, "In domain of", def->in_domain_of);
230 print_variablelist_entry_list (f, "In range of", def->in_range_of);
231
232 if (def->instances) {
233 print_variablelist_entry_list (f, "Predefined instances", def->instances);
234 }
235
236 g_fprintf (f, "</variablelist>\n");
237
238 if (def->notify || def->deprecated) {
239 g_fprintf (f, "<note>\n");
240 g_fprintf (f, "<title>Note:</title>\n");
241 if (def->notify) {
242 g_fprintf (f, "<para>This class notifies about changes</para>\n");
243 }
244 if (def->deprecated) {
245 g_fprintf (f, "<para>This class is deprecated</para>\n");
246 }
247 g_fprintf (f, "</note>\n");
248 }
249
250 g_fprintf (f, "</refsect2>\n\n");
251
252 g_free (name);
253 }
254
255 static void
256 print_ontology_property (gpointer key, gpointer value, gpointer user_data)
257 {
258 OntologyProperty *def = (OntologyProperty *) value;
259 gchar *name, *id;
260 FILE *f = (FILE *) user_data;
261
262 g_return_if_fail (f != NULL);
263
264 name = qname_to_shortname (def->propertyname);
265 id = shortname_to_id (name);
266
267 g_fprintf (f, "<refsect2 id='%s'>\n", id);
268 g_free (id);
269
270 g_fprintf (f, "<title>%s</title>\n", name);
271 g_free (name);
272
273 if (def->description) {
274 g_fprintf (f, "<para>%s</para>\n", def->description);
275 }
276
277
278 g_fprintf (f, "<variablelist>\n");
279
280 print_variablelist_entry_list (f, "Type", def->type);
281 print_variablelist_entry_list (f, "Domain", def->domain);
282 print_variablelist_entry_list (f, "Range", def->range);
283 print_variablelist_entry_list (f, "Superproperties", def->superproperties);
284 print_variablelist_entry_list (f, "Subproperties", def->subproperties);
285
286 if (def->max_cardinality) {
287 print_variablelist_entry (f, "Cardinality", def->max_cardinality);
288 }
289
290 if (def->fulltextIndexed) {
291 print_variablelist_entry (f, "Text indexed",
292 "This property is indexed, so it can provide results on text search");
293 }
294
295 g_fprintf (f, "</variablelist>\n");
296
297 if (def->deprecated) {
298 g_fprintf (f, "<note>\n");
299 g_fprintf (f, "<title>Note:</title>\n");
300 g_fprintf (f, "<para>This property is deprecated</para>\n");
301 g_fprintf (f, "</note>\n");
302 }
303
304 g_fprintf (f, "</refsect2>\n\n");
305 }
306
307 static void
308 print_fts_properties (gpointer key, gpointer value, gpointer user_data)
309 {
310 OntologyProperty *def = (OntologyProperty *) value;
311 gchar *name, *id;
312 FILE *fts = (FILE *) user_data;
313
314 g_return_if_fail (fts != NULL);
315 if (!def->fulltextIndexed) {
316 return;
317 }
318
319 name = qname_to_shortname (def->propertyname);
320 id = shortname_to_id (name);
321
322 g_fprintf (fts, "<tr>\n");
323 g_fprintf (fts, " <td>\n");
324 print_reference (def->propertyname, fts);
325 g_fprintf (fts, " </td>\n");
326 g_fprintf (fts, " <td>%s</td>\n", (def->weight ? def->weight : "0"));
327 g_fprintf (fts, "</tr>\n");
328
329 g_free (id);
330
331 }
332
333 void
334 ttl_sgml_print (OntologyDescription *description,
335 Ontology *ontology,
336 FILE *f,
337 FILE *fts,
338 const gchar *explanation_file)
339 {
340 gchar *upper_name;
341
342 upper_name = g_ascii_strup (description->localPrefix, -1);
343
344 qname_init (description->baseUrl, description->localPrefix, NULL);
345 print_sgml_header (f, description);
346
347 /* FIXME: make desc files sgml */
348 print_sgml_explanation (f, explanation_file);
349
350 g_fprintf (f, "<section id='%s-classes'>\n", description->localPrefix);
351 g_fprintf (f, "<title>%s Ontology Classes</title>\n", upper_name);
352 g_hash_table_foreach (ontology->classes, print_ontology_class, f);
353 g_fprintf (f, "</section>\n");
354
355 g_fprintf (f, "<section id='%s-properties'>\n", description->localPrefix);
356 g_fprintf (f, "<title>%s Ontology Properties</title>\n", upper_name);
357 g_hash_table_foreach (ontology->properties, print_ontology_property, f);
358 g_fprintf (f, "</section>\n");
359
360 print_sgml_footer (f);
361
362 g_free (upper_name);
363
364 if (fts) {
365 g_hash_table_foreach (ontology->properties, print_fts_properties, fts);
366 }
367 }