1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * Copyright (C) 2006 Shaun McCance
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Shaun McCance <shaunm@gnome.org>
24 #include <libxml/parser.h>
25 #include <libxml/xinclude.h>
27 #include "yelp-error.h"
28 #include "yelp-transform.h"
30 static gint num_chunks
= 0;
31 static gboolean freed
;
33 static gint timeout
= -1;
34 static gboolean random_timeout
= FALSE
;
35 static gchar
**files
= NULL
;
36 static const GOptionEntry options
[] = {
37 { "random-timeout", 'r',
40 "Time out after a random amount of time", NULL
},
44 "Time out after N milliseconds", "N" },
46 0, 0, G_OPTION_ARG_FILENAME_ARRAY
,
54 transform_release (YelpTransform
*transform
)
56 printf ("\nRELEASE\n");
58 yelp_transform_cancel (transform
);
59 g_object_unref (transform
);
66 transform_chunk (YelpTransform
*transform
,
67 const gchar
*chunk_id
,
72 printf ("\nCHUNK %i: %s\n", num_chunks
, chunk_id
);
74 chunk
= yelp_transform_take_chunk (transform
, chunk_id
);
75 small
= g_strndup (chunk
, 300);
76 printf ("%s\n", small
);
83 transform_finished (YelpTransform
*transform
,
88 yelp_transform_cancel (transform
);
89 g_object_unref (transform
);
95 transform_error (YelpTransform
*transform
,
102 transform_destroyed (gpointer data
,
105 printf ("\nFREED\n");
106 g_main_loop_quit (loop
);
110 main (gint argc
, gchar
**argv
)
112 GOptionContext
*context
;
113 xmlParserCtxtPtr parser
;
115 YelpTransform
*transform
;
117 const gchar
*stylesheet
;
120 context
= g_option_context_new ("[STYLESHEET] FILE");
121 g_option_context_add_main_entries (context
, options
, NULL
);
122 g_option_context_parse (context
, &argc
, &argv
, NULL
);
124 if (files
== NULL
|| files
[0] == NULL
) {
125 g_printerr ("Usage: test-transform [OPTION...] [STYLESHEET] FILE\n");
129 if (files
[1] == NULL
) {
130 stylesheet
= DATADIR
"/yelp/xslt/db2html.xsl";
133 stylesheet
= files
[0];
137 params
= g_new0 (gchar
*, 7);
138 params
[0] = g_strdup ("db.chunk.extension");
139 params
[1] = g_strdup ("\"\"");
140 params
[2] = g_strdup ("db.chunk.info_basename");
141 params
[3] = g_strdup ("\"x-yelp-titlepage\"");
142 params
[4] = g_strdup ("db.chunk.max_depth");
143 params
[5] = g_strdup ("2");
146 transform
= yelp_transform_new (stylesheet
);
147 g_object_weak_ref ((GObject
*) transform
, transform_destroyed
, NULL
);
149 g_signal_connect (transform
, "chunk-ready", (GCallback
) transform_chunk
, NULL
);
150 g_signal_connect (transform
, "finished", (GCallback
) transform_finished
, NULL
);
151 g_signal_connect (transform
, "error", (GCallback
) transform_error
, NULL
);
153 parser
= xmlNewParserCtxt ();
154 doc
= xmlCtxtReadFile (parser
,
157 XML_PARSE_DTDLOAD
| XML_PARSE_NOCDATA
|
158 XML_PARSE_NOENT
| XML_PARSE_NONET
);
159 xmlFreeParserCtxt (parser
);
160 xmlXIncludeProcessFlags (doc
,
161 XML_PARSE_DTDLOAD
| XML_PARSE_NOCDATA
|
162 XML_PARSE_NOENT
| XML_PARSE_NONET
);
163 if (!yelp_transform_start (transform
, doc
, NULL
, (const gchar
**) params
))
166 if (random_timeout
) {
167 GRand
*rand
= g_rand_new ();
168 timeout
= g_rand_int_range (rand
, 80, 280);
173 g_timeout_add (timeout
, (GSourceFunc
) transform_release
, transform
);
175 loop
= g_main_loop_new (NULL
, FALSE
);
176 g_main_loop_run (loop
);