ttl_sgml.c 5.52 KB
Newer Older
1
/*
2
 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */

20 21 22 23 24 25 26 27 28 29 30 31
#include <glib/gprintf.h>

#include "ttl_sgml.h"

typedef struct {
	Ontology *ontology;
	OntologyDescription *description;
	FILE *output;
} CallbackInfo;


static void
32
print_itemized_list (FILE *f, GList *list)
33
{
34
	GList *it;
35

36 37 38 39 40
	g_fprintf (f, "<itemizedlist>\n");
	for (it = list; it != NULL; it = it->next) {
		g_fprintf (f, "<listitem>%s</listitem>\n", (gchar *)it->data);
	}
	g_fprintf (f, "</itemizedlist>\n");
41 42 43
}

static void
44 45 46 47 48 49 50
print_people_list (FILE *f,
                   const gchar *role,
                   GList *list) 
{
        if (!list) {
                return;
        }
51

52 53 54 55 56 57
        g_fprintf (f, "<varlistentry>\n");
        g_fprintf (f, "  <term>%s</term>\n", role);
        g_fprintf (f, "  <listitem>\n");
        print_itemized_list (f, list);
        g_fprintf (f, "  </listitem>\n");
        g_fprintf (f, "</varlistentry>\n");
58 59 60
}

static void
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
print_link_as_varlistentry (FILE *f,
                            const gchar *term,
                            const gchar *link_text,
                            const gchar *link)
{
        g_fprintf (f, "  <varlistentry>\n");
        g_fprintf (f,"    <term>%s</term>\n", term);
	if (link) {
                g_fprintf (f, 
                           " <listitem><para><ulink url=\"%s\">%s</ulink></para></listitem>\n",
		           link, link_text);
	} else {
		g_fprintf (f, " <listitem><para>Not available</para></listitem>\n");
	}
        g_fprintf (f, "  </varlistentry>\n");
76 77
}

78
#if 0
79 80 81
static void
print_deprecated_message (FILE *f)
{
82 83 84 85
        g_fprintf (f, "<note>\n");
        g_fprintf (f, "<title>Note:</title>\n");
        g_fprintf (f, "<para>This item is deprecated</para>\n");
        g_fprintf (f, "</note>\n");
86
}
87
#endif
88 89 90 91 92 93 94

static void
print_sgml_header (FILE *f, OntologyDescription *desc)
{
        gchar *upper_name;

        g_fprintf (f, "<?xml version='1.0' encoding='UTF-8'?>\n");
95 96 97 98
	g_fprintf (f, "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\"\n"
		   "        \"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\" [\n");
	g_fprintf (f, "<!ENTITY %% local.common.attrib \"xmlns:xi  CDATA  #FIXED 'http://www.w3.org/2003/XInclude'\">\n");
	g_fprintf (f, "]>");
99 100 101 102

        g_fprintf (f, "<chapter id='%s-ontology'>\n", desc->localPrefix);

        upper_name = g_ascii_strup (desc->localPrefix, -1);
103
        g_fprintf (f, "<title>%s: %s</title>\n", desc->title, desc->description ? desc->description : "");
104 105
        g_free (upper_name);

106 107 108 109 110 111 112
        print_people_list (f, "Authors:", desc->authors);
        print_people_list (f, "Editors:", desc->editors);
        print_people_list (f, "Contributors:", desc->contributors);

        print_link_as_varlistentry (f, "Upstream:", "Upstream version", desc->upstream);
        print_link_as_varlistentry (f, "ChangeLog:", "Tracker changes", desc->gitlog);

113 114 115 116 117 118 119 120
        if (desc->copyright) {
	        g_fprintf (f, "<varlistentry>\n");
	        g_fprintf (f, "  <term>Copyright:</term>\n");
	        g_fprintf (f, "  <listitem>\n");
	        g_fprintf (f, "<para>%s</para>\n", desc->copyright);
	        g_fprintf (f, "  </listitem>\n");
	        g_fprintf (f, "</varlistentry>\n");
        }
121 122 123 124 125 126 127 128
}

static void
print_sgml_footer (FILE *f)
{
	g_fprintf (f,"</chapter>\n");
}

129
static void
130 131 132
print_ontology_class (Ontology      *ontology,
		      OntologyClass *def,
		      FILE          *f)
133 134 135
{
	gchar *name, *id;

136
	g_return_if_fail (f != NULL);
137

138 139
	name = ttl_model_name_to_shortname (ontology, def->classname, NULL);
	id = ttl_model_name_to_shortname (ontology, def->classname, "-");
140 141
	g_fprintf (f, "<xi:include href='%s.xml'/>\n", id);
	g_free (id);
142

143
        g_free (name);
144 145
}

146 147
void
ttl_sgml_print (OntologyDescription *description,
148
                Ontology            *ontology,
149 150
                GFile               *file,
                const gchar         *description_dir)
151
{
152
	GHashTableIter iter;
153
	gchar *upper_name, *path, *introduction, *basename;
154
	OntologyClass *def;
155 156 157 158 159
	FILE *f;

	path = g_file_get_path (file);
	f = fopen (path, "w");
	g_assert (f != NULL);
160 161 162 163

        upper_name = g_ascii_strup (description->localPrefix, -1);
	print_sgml_header (f, description);

164 165 166 167 168 169 170 171
	basename = g_strdup_printf ("%s-introduction.xml", description->localPrefix);
	introduction = g_build_filename (description_dir, basename, NULL);
	g_free (basename);

	if (g_file_test (introduction, G_FILE_TEST_EXISTS)) {
		g_fprintf (f, "<xi:include href='%s'><xi:fallback/></xi:include>",
		           introduction);
	}
172 173 174

        g_fprintf (f, "<section id='%s-classes'>\n", description->localPrefix);
	g_fprintf (f, "<title>%s Ontology Classes</title>\n", upper_name);
175
	g_hash_table_iter_init (&iter, ontology->classes);
176

177 178 179
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &def)) {
		print_ontology_class (ontology, def, f);
	}
180

181
        g_fprintf (f, "</section>\n");
182 183
	print_sgml_footer (f);

184
	g_free (upper_name);
185
	g_free (introduction);
186
	fclose (f);
187
}