summaryrefslogtreecommitdiff
path: root/src/yelp-xslt-pager.c
blob: 0254d0acf797b0688351cdb47b62ec74e8c30668 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * Copyright (C) 2003 Shaun McCance  <shaunm@gnome.org>
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Shaun McCance  <shaunm@gnome.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <glib/gi18n.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xinclude.h>
#include <libxslt/xslt.h>
#include <libxslt/templates.h>
#include <libxslt/transform.h>
#include <libxslt/extensions.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/xsltutils.h>

#include "yelp-error.h"
#include "yelp-settings.h"
#include "yelp-xslt-pager.h"

#ifdef YELP_DEBUG
#define d(x) x
#else
#define d(x)
#endif

#define YELP_NAMESPACE "http://www.gnome.org/yelp/ns"

#define EVENTS_PENDING while (yelp_pager_get_state (pager) <= YELP_PAGER_STATE_RUNNING && gtk_events_pending ()) gtk_main_iteration ();
#define CANCEL_CHECK if (!main_running || yelp_pager_get_state (pager) >= YELP_PAGER_STATE_ERROR) goto done;

extern gboolean main_running;

struct _YelpXsltPagerPriv {
    xmlDocPtr               inputDoc;
    xmlDocPtr               outputDoc;
    xsltStylesheetPtr       stylesheet;
    xsltTransformContextPtr transformContext;
};

static void      xslt_pager_class_init   (YelpXsltPagerClass *klass);
static void      xslt_pager_init         (YelpXsltPager      *pager);
static void      xslt_pager_dispose      (GObject            *gobject);

static void      xslt_pager_error        (YelpPager          *pager);
static void      xslt_pager_cancel       (YelpPager          *pager);
static void      xslt_pager_finish       (YelpPager          *pager);

gboolean         xslt_pager_process      (YelpPager          *pager);

static void      xslt_yelp_document    (xsltTransformContextPtr ctxt,
					xmlNodePtr              node,
					xmlNodePtr              inst,
					xsltStylePreCompPtr     comp);
static void      xslt_yelp_cache       (xsltTransformContextPtr ctxt,
					xmlNodePtr              node,
					xmlNodePtr              inst,
					xsltStylePreCompPtr     comp);

static YelpPagerClass *parent_class;

GType
yelp_xslt_pager_get_type (void)
{
    static GType type = 0;

    if (!type) {
	static const GTypeInfo info = {
	    sizeof (YelpXsltPagerClass),
	    NULL,
	    NULL,
	    (GClassInitFunc) xslt_pager_class_init,
	    NULL,
	    NULL,
	    sizeof (YelpXsltPager),
	    0,
	    (GInstanceInitFunc) xslt_pager_init,
	};
	type = g_type_register_static (YELP_TYPE_PAGER,
				       "YelpXsltPager", 
				       &info, 0);
    }
    return type;
}

static void
xslt_pager_class_init (YelpXsltPagerClass *klass)
{
    GObjectClass   *object_class = G_OBJECT_CLASS (klass);
    YelpPagerClass *pager_class  = YELP_PAGER_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->dispose     = xslt_pager_dispose;

    pager_class->error        = xslt_pager_error;
    pager_class->cancel       = xslt_pager_cancel;
    pager_class->finish       = xslt_pager_finish;

    pager_class->process      = xslt_pager_process;
}

static void
xslt_pager_init (YelpXsltPager *pager)
{
    YelpXsltPagerPriv *priv;

    priv = g_new0 (YelpXsltPagerPriv, 1);
    pager->priv = priv;
}

static void
xslt_pager_dispose (GObject *object)
{
    YelpXsltPager *pager = YELP_XSLT_PAGER (object);
    YelpXsltPagerPriv *priv = pager->priv;

    if (priv->inputDoc)
	xmlFreeDoc (priv->inputDoc);
    if (priv->outputDoc)
	xmlFreeDoc (priv->outputDoc);
    if (priv->stylesheet)
	xsltFreeStylesheet (priv->stylesheet);
    if (priv->transformContext)
	xsltFreeTransformContext (priv->transformContext);

    g_free (pager->priv);

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

/******************************************************************************/

gboolean
xslt_pager_process (YelpPager *pager)
{
    YelpXsltPagerPriv  *priv;
    YelpXsltPagerClass *klass;

    YelpDocInfo     *doc_info;
    gchar           *filename;

    gchar **params;
    gint    params_i = 0;

    GError *error = NULL;

    d (g_print ("xslt_pager_process\n"));

    g_return_val_if_fail (pager != NULL, FALSE);
    g_return_val_if_fail (YELP_IS_XSLT_PAGER (pager), FALSE);

    priv = YELP_XSLT_PAGER (pager)->priv;
    klass = YELP_XSLT_PAGER_GET_CLASS (pager);

    doc_info = yelp_pager_get_doc_info (pager);

    if (yelp_pager_get_state (pager) >= YELP_PAGER_STATE_ERROR)
	goto done;

    filename = yelp_doc_info_get_filename (doc_info);

    g_object_ref (pager);

    yelp_pager_set_state (pager, YELP_PAGER_STATE_PARSING);
    g_signal_emit_by_name (pager, "parse");

    priv->inputDoc = klass->parse (pager);

    if (priv->inputDoc == NULL) {
	g_set_error (&error, YELP_ERROR, YELP_ERROR_NO_DOC,
		     _("The file ‘%s’ could not be parsed. Either the file "
		       "does not exist, or it is improperly formatted."),
		     filename);
	yelp_pager_error (pager, error);
	goto done;
    }

    EVENTS_PENDING;
    CANCEL_CHECK;

    yelp_pager_set_state (pager, YELP_PAGER_STATE_RUNNING);
    g_signal_emit_by_name (pager, "start");

    EVENTS_PENDING;
    CANCEL_CHECK;

    params = klass->params (pager);

    priv->stylesheet = xsltParseStylesheetFile (klass->stylesheet);
    if (!priv->stylesheet) {
	g_set_error (&error, YELP_ERROR, YELP_ERROR_PROC,
		     _("The document ‘%s’ could not be processed. The file "
		       "‘%s’ is either missing, or it is not a valid XSLT "
		       "stylesheet."),
		     filename,
		     klass->stylesheet);
	yelp_pager_error (pager, error);
	goto done;
    }

    priv->transformContext = xsltNewTransformContext (priv->stylesheet,
						      priv->inputDoc);
    if (!priv->transformContext) {
	g_set_error (&error, YELP_ERROR, YELP_ERROR_PROC,
		     _("The document ‘%s’ could not be processed. The file "
		       "‘%s’ is either missing, or it is not a valid XSLT "
		       "stylesheet."),
		     filename,
		     klass->stylesheet);
	yelp_pager_error (pager, error);
	goto done;
    }

    priv->transformContext->_private = pager;
    xsltRegisterExtElement (priv->transformContext,
			    "document",
			    YELP_NAMESPACE,
			    (xsltTransformFunction) xslt_yelp_document);
    xsltRegisterExtElement (priv->transformContext,
			    "cache",
			    YELP_NAMESPACE,
			    (xsltTransformFunction) xslt_yelp_cache);

    EVENTS_PENDING;
    CANCEL_CHECK;

    priv->outputDoc = xsltApplyStylesheetUser (priv->stylesheet,
					       priv->inputDoc,
					       (const char **) params,
					       NULL, NULL,
					       priv->transformContext);
    CANCEL_CHECK;
    g_signal_emit_by_name (pager, "finish");

 done:
    for (params_i = 0; params[params_i] != NULL; params_i++)
	if (params_i % 2 == 1)
	    g_free (params[params_i]);
    g_free (params);
    g_free (filename);

    if (priv->inputDoc) {
	xmlFreeDoc (priv->inputDoc);
	priv->inputDoc = NULL;
    }
    if (priv->outputDoc) {
	xmlFreeDoc (priv->outputDoc);
	priv->outputDoc = NULL;
    }
    if (priv->stylesheet) {
	xsltFreeStylesheet (priv->stylesheet);
	priv->stylesheet = NULL;
    }
    if (priv->transformContext) {
	xsltFreeTransformContext (priv->transformContext);
	priv->transformContext = NULL;
    }

    g_object_unref (pager);

    return FALSE;
}

static void
xslt_pager_error (YelpPager *pager)
{
    d (g_print ("xslt_pager_error\n"));
    yelp_pager_set_state (pager, YELP_PAGER_STATE_ERROR);
}

static void
xslt_pager_cancel (YelpPager *pager)
{
    YelpXsltPagerPriv *priv = YELP_XSLT_PAGER (pager)->priv;

    d (g_print ("xslt_pager_cancel\n"));

    yelp_pager_set_state (pager, YELP_PAGER_STATE_INVALID);

    if (priv->inputDoc) {
	xmlFreeDoc (priv->inputDoc);
	priv->inputDoc = NULL;
    }
    if (priv->outputDoc) {
	xmlFreeDoc (priv->outputDoc);
	priv->outputDoc = NULL;
    }
    if (priv->stylesheet) {
	xsltFreeStylesheet (priv->stylesheet);
	priv->stylesheet = NULL;
    }
    if (priv->transformContext) {
	xsltFreeTransformContext (priv->transformContext);
	priv->transformContext = NULL;
    }
}

static void
xslt_pager_finish (YelpPager *pager)
{
    d (g_print ("xslt_pager_finish\n"));
    yelp_pager_set_state (pager, YELP_PAGER_STATE_FINISHED);
}

/** XSLT Extension Elements ***************************************************/

void
xslt_yelp_document (xsltTransformContextPtr ctxt,
		    xmlNodePtr              node,
		    xmlNodePtr              inst,
		    xsltStylePreCompPtr     comp)
{
    GError  *error;
    YelpPage *page;
    xmlChar *page_id = NULL;
    xmlChar *page_title = NULL;
    xmlChar *page_buf;
    gint     buf_size;
    YelpPager *pager;
    xsltStylesheetPtr style = NULL;
    const char *old_outfile;
    xmlDocPtr   new_doc = NULL;
    xmlDocPtr   old_doc;
    xmlNodePtr  old_insert;
    xmlNodePtr  cur;

    if (!ctxt || !node || !inst || !comp)
	return;

    pager = (YelpPager *) ctxt->_private;

    EVENTS_PENDING;
    CANCEL_CHECK;

    d (g_print ("xslt_yelp_document\n"));

    page_id = xsltEvalAttrValueTemplate (ctxt, inst,
					 (const xmlChar *) "href",
					 NULL);
    if (page_id == NULL) {
	xsltTransformError (ctxt, NULL, inst,
			    _("No href attribute found on yelp:document"));
	/* FIXME: put a real error here */
	error = NULL;
	yelp_pager_error (pager, error);
	goto done;
    }
    d (g_print ("  page_id = \"%s\"\n", page_id));

    old_outfile = ctxt->outputFile;
    old_doc     = ctxt->output;
    old_insert  = ctxt->insert;
    ctxt->outputFile = (const char *) page_id;

    style = xsltNewStylesheet ();
    if (style == NULL) {
	xsltTransformError (ctxt, NULL, inst,
			    _("Out of memory"));
	error = NULL;
	yelp_pager_error (pager, error);
	goto done;
    }

    style->omitXmlDeclaration = TRUE;

    new_doc = xmlNewDoc ("1.0");
    new_doc->charset = XML_CHAR_ENCODING_UTF8;
    new_doc->dict = ctxt->dict;
    xmlDictReference (new_doc->dict);

    ctxt->output = new_doc;
    ctxt->insert = (xmlNodePtr) new_doc;

    xsltApplyOneTemplate (ctxt, node, inst->children, NULL, NULL);

    CANCEL_CHECK;

    xsltSaveResultToString (&page_buf, &buf_size, new_doc, style);

    ctxt->outputFile = old_outfile;
    ctxt->output     = old_doc;
    ctxt->insert     = old_insert;

    CANCEL_CHECK;

    if (!page_title)
	page_title = g_strdup ("FIXME");

    page = g_new0 (YelpPage, 1);

    page->page_id  = g_strdup (page_id);
    xmlFree (page_id);
    page_id = NULL;

    page->title    = page_title;
    page->contents = page_buf;

    cur = xmlDocGetRootElement (new_doc);
    for (cur = cur->children; cur; cur = cur->next) {
	if (!xmlStrcmp (cur->name, (xmlChar *) "head")) {
	    for (cur = cur->children; cur; cur = cur->next) {
		if (!xmlStrcmp (cur->name, (xmlChar *) "link")) {
		    xmlChar *rel = xmlGetProp (cur, "rel");

		    if (!xmlStrcmp (rel, (xmlChar *) "previous"))
			page->prev_id = xmlGetProp (cur, "href");
		    else if (!xmlStrcmp (rel, (xmlChar *) "next"))
			page->next_id = xmlGetProp (cur, "href");
		    else if (!xmlStrcmp (rel, (xmlChar *) "top"))
			page->toc_id = xmlGetProp (cur, "href");

		    xmlFree (rel);
		}
	    }
	    break;
	}
    }

    CANCEL_CHECK;

    yelp_pager_add_page (pager, page);
    g_signal_emit_by_name (pager, "page", page->page_id);

    EVENTS_PENDING;
    CANCEL_CHECK;

 done:
    if (new_doc)
	xmlFreeDoc (new_doc);
    if (style)
	xsltFreeStylesheet (style);
}

void
xslt_yelp_cache (xsltTransformContextPtr ctxt,
		 xmlNodePtr              node,
		 xmlNodePtr              inst,
		 xsltStylePreCompPtr     comp)
{
    xsltApplyOneTemplate (ctxt, node, inst->children, NULL, NULL);

    while (gtk_events_pending ())
	gtk_main_iteration ();
    // FIXME : check for cancel
}