File: fo-libfo-basic.c

package info (click to toggle)
xmlroff 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 36,456 kB
  • ctags: 25,964
  • sloc: ansic: 178,200; xml: 109,155; sh: 8,973; makefile: 1,331; perl: 30
file content (269 lines) | stat: -rw-r--r-- 5,649 bytes parent folder | download | duplicates (3)
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
/* Fo
 * fo-libfo-basic.c: 'Basic' (i.e., simple) high-level interface to libfo
 *
 * Copyright (C) 2003 Sun Microsystems
 * Copyright (C) 2007 Menteith Consulting Ltd
 *
 * See COPYING for the status of this software.
 */

#include "fo-libfo-basic.h"
#include "fo-libfo.h"
#include "fo-doc.h"
#include "area-to-pdf.h"
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

const char *fo_libfo_basic_error_messages [] = {
  N_("FoLibfo error"),
  N_("Unknown file format"),
  N_("FoLibfo warning")
};

/**
 * fo_libfo_basic_error_quark:
 * 
 * Get the error quark for #FoLibfo.
 *
 * If the quark does not yet exist, create it.
 * 
 * Return value: Quark associated with #FoLibfo errors.
 **/
GQuark
fo_libfo_basic_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("FoLibfoBasic error");
  return quark;
}

/**
 * fo_libfo_init:
 * 
 * Initialise the libfo formatter.
 *
 * Return value: %TRUE if successful.
 **/
gboolean
fo_libfo_init (void)
{
  g_type_init ();

  return TRUE;
}

/**
 * fo_libfo_init2:
 * @fo_malloc:  Function that allocates memory.
 * @fo_realloc: Function that reallocates memory.
 * @fo_free:    Function that frees memory.
 * 
 * Initialise the libfo formatter and provide functions for
 * allocating, reallocating, and freeing memory.
 *
 * Return value: %TRUE if successful.
 **/
gboolean
fo_libfo_init2 (FoMalloc  fo_malloc,
		FoRealloc fo_realloc,
		FoFree    fo_free)
{
  GMemVTable mem_vtable = {fo_malloc,
			   fo_realloc,
			   fo_free,
			   NULL,
			   NULL,
			   NULL};

  g_mem_set_vtable (&mem_vtable);

  return fo_libfo_init ();
}

/**
 * fo_libfo_shutdown:
 * 
 * Shutdown the libfo formatter.
 *
 * Return value: %TRUE if successful.
 **/
gboolean
fo_libfo_shutdown (void)
{
  xmlCleanupParser();
  xmlMemoryDump();

  return TRUE;
}

static void
fo_basic_bubble_up_error (GError     **dest,
			  GQuark       new_domain,
			  gint         new_code,
			  const gchar *new_message,
			  GError      *src)
{
  gchar *error_string = NULL;

  g_return_if_fail (dest == NULL || *dest == NULL);
  g_return_if_fail (new_domain != 0);
  g_return_if_fail (src != NULL);

  if (dest == NULL)
    {
      if (src != NULL)
        g_error_free (src);
      return;
    }

  error_string =
    g_strconcat (new_message,
		 "\n",
		 g_quark_to_string (src->domain),
		 ": ",
		 src->message,
		 NULL);

  g_error_free (src);

  if (*dest != NULL)
    {
      g_warning ("Can't override existing error message.\n"
		 "Existing error:: %s: %s\n"
		 "New error:: %s: %s",
		 g_quark_to_string ((*dest)->domain),
		 (*dest)->message,
		 g_quark_to_string (src->domain),
		 src->message);
      return;
    }

  g_set_error (dest,
	       new_domain,
	       new_code,
	       error_string);

  g_free (error_string);
}

gboolean
fo_libfo_format (const gchar    *xml,
		 const gchar    *xslt,
		 const gchar    *out,
		 GError        **error)
{
  FoXmlDoc *result;
  FoXmlDoc *stylesheet_doc;
  FoXmlDoc *xml_doc;
  FoXslFormatter *fo_xsl_formatter;
  FoDoc *fo_doc;
  FoAreaToPDFData fo_area_to_pdf_data = {NULL, 0};
  GError *tmp_error = NULL;

  g_return_val_if_fail (xml != NULL, FALSE);
  g_return_val_if_fail (out != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  FoLibfoContext *libfo_context = fo_libfo_context_new ();

  xml_doc = fo_xml_doc_new_from_filename (xml,
					  libfo_context,
					  &tmp_error);

  if (tmp_error != NULL)
    {
      fo_basic_bubble_up_error (error,
				FO_LIBFO_BASIC_ERROR,
				FO_LIBFO_BASIC_ERROR_FAILED,
				_(fo_libfo_basic_error_messages[FO_LIBFO_BASIC_ERROR_FAILED]),
				tmp_error);
      return FALSE;
    }

  if (xslt == NULL)
    {
      result = xml_doc;
    }
  else
    {
      stylesheet_doc = fo_xml_doc_new_from_filename (xslt,
						     libfo_context,
						     &tmp_error);

      if (tmp_error != NULL)
	{
	  fo_basic_bubble_up_error (error,
				    FO_LIBFO_BASIC_ERROR,
				    FO_LIBFO_BASIC_ERROR_FAILED,
				    _(fo_libfo_basic_error_messages[FO_LIBFO_BASIC_ERROR_FAILED]),
				    tmp_error);
	  return FALSE;
	}

      result =
	fo_xml_doc_ref (fo_xslt_transformer_do_transform (xml_doc,
							  stylesheet_doc,
							  &tmp_error));

      if (tmp_error != NULL)
	{
	  fo_basic_bubble_up_error (error,
				    FO_LIBFO_BASIC_ERROR,
				    FO_LIBFO_BASIC_ERROR_FAILED,
				    _(fo_libfo_basic_error_messages[FO_LIBFO_BASIC_ERROR_FAILED]),
				    tmp_error);
	  return FALSE;
	}
    }

  fo_xsl_formatter = fo_xsl_formatter_new ();
  fo_xsl_formatter_set_result_tree (fo_xsl_formatter,
				    result);

  fo_doc = fo_doc_new_from_type (NULL);

  fo_doc_open_file (fo_doc,
		    out,
		    libfo_context,
		    &tmp_error);

  if (tmp_error != NULL)
    {
      fo_basic_bubble_up_error (error,
				FO_LIBFO_BASIC_ERROR,
				FO_LIBFO_BASIC_ERROR_FAILED,
				_(fo_libfo_basic_error_messages[FO_LIBFO_BASIC_ERROR_FAILED]),
				tmp_error);
      return FALSE;
    }

  fo_xsl_formatter_set_fo_doc (fo_xsl_formatter,
			       fo_doc);


  fo_xsl_formatter_format (fo_xsl_formatter,
			   libfo_context,
			   &tmp_error);
  if (tmp_error != NULL)
    {
      fo_basic_bubble_up_error (error,
				FO_LIBFO_BASIC_ERROR,
				FO_LIBFO_BASIC_ERROR_FAILED,
				_(fo_libfo_basic_error_messages[FO_LIBFO_BASIC_ERROR_FAILED]),
				tmp_error);
      return FALSE;
    }

  fo_area_to_pdf_data.fo_doc = fo_doc;

  fo_area_tree_to_pdf (FO_AREA (fo_xsl_formatter_get_area_tree (fo_xsl_formatter)),
		       &fo_area_to_pdf_data);

  g_object_unref (fo_xsl_formatter);
  g_object_unref (fo_doc);
  g_object_unref (libfo_context);
  fo_libfo_shutdown ();

  return TRUE;
}