File: fo-xml-doc.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 (410 lines) | stat: -rw-r--r-- 8,846 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
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
/* Fo
 * fo-xml-doc.c: Boxed object type for libxml2 xmlDoc document
 *
 * Copyright (C) 2003 Sun Microsystems
 * Copyright (C) 2007 Menteith Consulting Ltd
 *
 * See COPYING for the status of this software.
 */

#include <libxml/xmlIO.h>
#include "fo-utils.h"
#include "fo-xml-doc-private.h"

/**
 * SECTION:fo-xml-doc
 * @short_description: libxml2 xmlDoc document
 *
 * Boxed object type for libxml2 xmlDoc document.
 */

extern int xmlLoadExtDtdDefaultValue;

const char *fo_xml_doc_error_messages [] = {
  N_("FoXmlDoc error"),
  N_("Unable to parse XML file: %s"),
  N_("Unable to parse XML file from memory or string"),
  N_("Cannot open input document: '%s'")
};

struct _FoXmlDoc
{
  xmlDocPtr xml_doc;

  gchar    *filename;

  guint ref_count;
};

static LibfoVersionInfo version_info =
  {
    LIBFO_MODULE_XML_DOC,
    "libxml2",
    NULL,
    LIBXML_VERSION,
    LIBXML_VERSION_STRING,
    0,
    NULL
  };

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


/**
 * fo_xml_doc_get_type:
 * 
 * Register the #FoXmlDoc object type.
 * 
 * Return value: #GType value of the #FoXmlDoc object type.
 **/
GType fo_xml_doc_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
    our_type = g_boxed_type_register_static ("FoXmlDoc",
					     (GBoxedCopyFunc) fo_xml_doc_ref,
					     (GBoxedFreeFunc) fo_xml_doc_unref);

  return our_type;
}

const LibfoVersionInfo *
fo_xml_doc_version_info (void)
{
  return &version_info;
}

/**
 * fo_xml_doc_new:
 * 
 * Creates a new #FoXmlDoc.
 * 
 * Return value: the newly created #FoXmlDoc. Use #fo_xml_doc_unref to free the
 * result.
 **/
FoXmlDoc *
fo_xml_doc_new (void)
{
  FoXmlDoc *fo_xml_doc = g_new0 (FoXmlDoc, 1);

  fo_xml_doc->ref_count = 1;

  return fo_xml_doc;
}

static gint
context_to_libxml2_options (FoLibfoContext *context)
{
  /* Begin with unchanging defaults. */
  gint options = XML_PARSE_NOENT;

  /* Set 'options' according to 'libfo_context' settings. */
  if (context != NULL)
    {
      if (fo_libfo_context_get_validation (context) == TRUE)
	{
	  options |= XML_PARSE_DTDVALID;
	}
    }

  return options;
}

/**
 * fo_xml_doc_new_from_filename:
 * @filename:      File or URL from which to create an #FoXmlDoc.
 * @libfo_context: #FoLibfoContext with parameters affecting parsing.
 * @error:         Indication of any error that occurred.
 * 
 * Creates a new #FoXmlDoc.
 * 
 * Return value: the newly created #FoXmlDoc. Use fo_xml_doc_unref to free the
 * result.
 **/
FoXmlDoc *
fo_xml_doc_new_from_filename (const gchar    *filename,
			      FoLibfoContext *libfo_context,
			      GError        **error)
{
  FoXmlDoc *fo_xml_doc = fo_xml_doc_new ();

  fo_xml_doc->xml_doc = xmlReadFile (filename,
				     NULL,
				     context_to_libxml2_options (libfo_context));

  if (fo_xml_doc->xml_doc == NULL)
    {
      g_free (fo_xml_doc);
      fo_xml_doc = NULL;

      g_set_error (error,
		   FO_XML_DOC_ERROR,
		   FO_XML_DOC_ERROR_FILENAME_PARSE_FAILED,
		   _(fo_xml_doc_error_messages[FO_XML_DOC_ERROR_FILENAME_PARSE_FAILED]),
		   filename);

    }

  return fo_xml_doc;
}

/**
 * fo_xml_doc_new_from_memory:
 * @buffer:        Pointer to a char array.
 * @size:          Size of the array.
 * @URL:           Base URL to use for the document.
 * @encoding:      The encoding of the document, or NULL.
 * @libfo_context: #FoLibfoContext with parameters affecting parsing.
 * @error:         Indication of any error that occurred.
 * 
 * Creates a new #FoXmlDoc.
 * 
 * Return value: the newly created #FoXmlDoc. Use #fo_xml_doc_unref to free the
 * result.
 **/
FoXmlDoc *
fo_xml_doc_new_from_memory (const gchar    *buffer,
			    gint            size,
			    const gchar    *URL,
			    const gchar    *encoding,
			    FoLibfoContext *libfo_context,
			    GError        **error)
{
  FoXmlDoc *fo_xml_doc = fo_xml_doc_new ();

  fo_xml_doc->xml_doc = xmlReadMemory (buffer, 
				       size, 
				       URL, 
				       encoding, 
				       context_to_libxml2_options (libfo_context));

  if (fo_xml_doc->xml_doc == NULL)
    {
      g_free (fo_xml_doc);
      fo_xml_doc = NULL;

      g_set_error (error,
		   FO_XML_DOC_ERROR,
		   FO_XML_DOC_ERROR_MEMORY_PARSE_FAILED,
		   _(fo_xml_doc_error_messages[FO_XML_DOC_ERROR_MEMORY_PARSE_FAILED]));

    }

  return fo_xml_doc;
}

/**
 * fo_xml_doc_new_from_string:
 * @curr:          Pointer to a zero-terminated string.
 * @URL:           Base URL to use for the document.
 * @encoding:      The encoding of the document, or NULL.
 * @libfo_context: #FoLibfoContext with parameters affecting parsing.
 * @error:         Indication of any error that occurred.
 * 
 * Creates a new #FoXmlDoc.
 * 
 * Return value: the newly created #FoXmlDoc. Use #fo_xml_doc_unref to free the
 * result.
 **/
FoXmlDoc *
fo_xml_doc_new_from_string (const gchar    *curr,
			    const gchar    *URL,
			    const gchar    *encoding,
			    FoLibfoContext *libfo_context,
			    GError        **error)
{
  
  FoXmlDoc *fo_xml_doc = fo_xml_doc_new ();

  fo_xml_doc->xml_doc = xmlReadDoc ((xmlChar *) curr,
				    URL, 
				    encoding, 
				    context_to_libxml2_options (libfo_context));

  if (fo_xml_doc->xml_doc == NULL)
    {
      g_free (fo_xml_doc);
      fo_xml_doc = NULL;

      g_set_error (error,
		   FO_XML_DOC_ERROR,
		   FO_XML_DOC_ERROR_MEMORY_PARSE_FAILED,
		   _(fo_xml_doc_error_messages[FO_XML_DOC_ERROR_MEMORY_PARSE_FAILED]));

    }

  return fo_xml_doc;
}

/**
 * fo_xml_doc_ref:
 * @fo_xml_doc: a #FoXmlDoc
 * 
 * Make a copy of a #FoXmlDoc.
 * 
 * Return value: a newly allocated #FoXmlDoc. This value
 *               must be freed using #fo_xml_doc_unref().
 **/
FoXmlDoc *
fo_xml_doc_ref  (FoXmlDoc  *fo_xml_doc)
{
  g_return_val_if_fail (fo_xml_doc != NULL, NULL);
  g_return_val_if_fail (fo_xml_doc->ref_count > 0, NULL);

  fo_xml_doc->ref_count += 1;

  return fo_xml_doc;
}

/**
 * fo_xml_doc_unref:
 * @fo_xml_doc: #FoXmlDoc.
 * 
 * Unref and possibly free a #FoXmlDoc.
 **/
void
fo_xml_doc_unref  (FoXmlDoc  *fo_xml_doc)
{
  g_return_if_fail (fo_xml_doc != NULL);
  g_return_if_fail (fo_xml_doc->ref_count > 0);

  fo_xml_doc->ref_count -= 1;

  if (fo_xml_doc->ref_count == 0)
    {
      if (fo_xml_doc->filename != NULL)
	g_free (fo_xml_doc->filename);

      if (fo_xml_doc->xml_doc != NULL)
	xmlFreeDoc (fo_xml_doc->xml_doc);

      g_free (fo_xml_doc);
    }
}

/**
 * fo_xml_doc_get_xml_doc:
 * @fo_xml_doc: #FoXmlDoc
 * 
 * Get the xmlDocPtr in @fo_xml_doc.
 * 
 * Return value: #xmlDocPtr.
 **/
xmlDocPtr
fo_xml_doc_get_xml_doc (FoXmlDoc *fo_xml_doc)
{
  g_return_val_if_fail (fo_xml_doc != NULL, NULL);

  return fo_xml_doc->xml_doc;
}

/**
 * fo_xml_doc_set_xml_doc:
 * @fo_xml_doc: #FoXmlDoc.
 * @xml_doc:    #xmlDocPtr.
 * 
 * Set the output #xmlDocPtr in @fo_xml_doc.  Voids the parsed result.
 **/
void
fo_xml_doc_set_xml_doc (FoXmlDoc    *fo_xml_doc,
			xmlDocPtr    xml_doc)
{
  g_return_if_fail (fo_xml_doc != NULL);

  if (fo_xml_doc->filename != NULL)
    {
      g_free (fo_xml_doc->filename);
      fo_xml_doc->filename = NULL;
    }

  if (fo_xml_doc->xml_doc != NULL)
    {
      xmlFreeDoc (fo_xml_doc->xml_doc);
      fo_xml_doc->xml_doc = NULL;
    }

  fo_xml_doc->xml_doc = xml_doc;
}

/**
 * fo_xml_doc_set_filename:
 * @fo_xml_doc: #FoXmlDoc
 * @filename:   Filename of file to be parsed.
 * 
 * Set the filename in @fo_xml_doc.  Voids the parsed result.
 **/
void
fo_xml_doc_set_filename (FoXmlDoc    *fo_xml_doc,
			 const gchar *filename)
{
  g_return_if_fail (fo_xml_doc != NULL);

  if (fo_xml_doc->filename != NULL)
    g_free (fo_xml_doc->filename);

  if (fo_xml_doc->xml_doc != NULL)
    {
      xmlFreeDoc (fo_xml_doc->xml_doc);
      fo_xml_doc->xml_doc = NULL;
    }

  fo_xml_doc->filename = g_strdup (filename);
}

/**
 * fo_xml_doc_set_base:
 * @fo_xml_doc: #FoXmlDoc for which to get base URL.
 * 
 * Gets the base URL of @fo_xml_doc.
 **/
gchar*
fo_xml_doc_get_base (FoXmlDoc *fo_xml_doc)
{
  gchar *base = NULL;
  gchar *return_base = NULL;

  g_return_val_if_fail (fo_xml_doc != NULL, NULL);

  base = (gchar *) xmlNodeGetBase (NULL,
				   (xmlNodePtr) fo_xml_doc->xml_doc);

  return_base = g_strdup (base);

  xmlFree (base);

  return return_base;
}

/**
 * fo_xml_doc_set_base:
 * @fo_xml_doc: #FoXmlDoc for which to set base
 * @URL:        New xml:base URL
 * 
 * Sets the base URL of @fo_xml_doc.
 **/
void
fo_xml_doc_set_base (FoXmlDoc    *fo_xml_doc,
		     const gchar *URL)
{
  g_return_if_fail (fo_xml_doc != NULL);

  xmlNodeSetBase ((xmlNodePtr) fo_xml_doc->xml_doc,
		  (const xmlChar*) URL);
}