don't break docs build
[platform/upstream/gstreamer.git] / gst / gstxml.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstxml.c: XML save/restore of pipelines
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:gstxml
24  * @short_description: XML save/restore operations of pipelines
25  *
26  */
27
28 #include "gst_private.h"
29
30 #include "gstxml.h"
31 #include "gstmarshal.h"
32 #include "gstinfo.h"
33 #include "gstbin.h"
34
35 enum
36 {
37   OBJECT_LOADED,
38   LAST_SIGNAL
39 };
40
41 static void gst_xml_class_init (GstXMLClass * klass);
42 static void gst_xml_init (GstXML * xml);
43
44 static void gst_xml_object_loaded (GstObject * private, GstObject * object,
45     xmlNodePtr self, gpointer data);
46
47 static GstObjectClass *parent_class = NULL;
48 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
49
50 GType
51 gst_xml_get_type (void)
52 {
53   static GType xml_type = 0;
54
55   if (!xml_type) {
56     static const GTypeInfo xml_info = {
57       sizeof (GstXMLClass),
58       NULL,
59       NULL,
60       (GClassInitFunc) gst_xml_class_init,
61       NULL,
62       NULL,
63       sizeof (GstXML),
64       0,
65       (GInstanceInitFunc) gst_xml_init,
66       NULL
67     };
68
69     xml_type = g_type_register_static (GST_TYPE_OBJECT, "GstXML", &xml_info, 0);
70   }
71   return xml_type;
72 }
73
74 static void
75 gst_xml_class_init (GstXMLClass * klass)
76 {
77   GObjectClass *gobject_class;
78
79   gobject_class = (GObjectClass *) klass;
80
81   parent_class = g_type_class_ref (GST_TYPE_OBJECT);
82
83   /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
84    * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
85    */
86   /**
87    * GstXML::object-loaded:
88    * @xml: the xml persistence instance
89    * @object: the object that has been loaded
90    * @xml_node: the related xml_node pointer to the document tree
91    *
92    * Signals that a new object has been deserialized.
93    */
94   gst_xml_signals[OBJECT_LOADED] =
95       g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
96       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstXMLClass, object_loaded), NULL,
97       NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
98       G_TYPE_POINTER);
99
100 }
101
102 static void
103 gst_xml_init (GstXML * xml)
104 {
105   xml->topelements = NULL;
106 }
107
108 /**
109  * gst_xml_new:
110  *
111  * Create a new GstXML parser object.
112  *
113  * Returns: a pointer to a new GstXML object.
114  */
115 GstXML *
116 gst_xml_new (void)
117 {
118   return GST_XML (g_object_new (GST_TYPE_XML, NULL));
119 }
120
121 /**
122  * gst_xml_write:
123  * @element: The element to write out
124  *
125  * Converts the given element into an XML presentation.
126  *
127  * Returns: a pointer to an XML document
128  */
129 xmlDocPtr
130 gst_xml_write (GstElement * element)
131 {
132   xmlDocPtr doc;
133   xmlNodePtr elementnode;
134   xmlNsPtr gst_ns;
135
136   doc = xmlNewDoc ((xmlChar *) "1.0");
137
138   doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);
139
140   gst_ns =
141       xmlNewNs (doc->xmlRootNode,
142       (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");
143
144   elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
145       NULL);
146
147   gst_object_save_thyself (GST_OBJECT (element), elementnode);
148
149   return doc;
150 }
151
152 /**
153  * gst_xml_write_file:
154  * @element: The element to write out
155  * @out: an open file, like stdout
156  *
157  * Converts the given element into XML and writes the formatted XML to an open
158  * file.
159  *
160  * Returns: number of bytes written on success, -1 otherwise.
161  */
162 gint
163 gst_xml_write_file (GstElement * element, FILE * out)
164 {
165   xmlDocPtr cur;
166
167 #ifdef HAVE_LIBXML2
168   xmlOutputBufferPtr buf;
169 #endif
170   const char *encoding;
171   xmlCharEncodingHandlerPtr handler = NULL;
172   int indent;
173   gboolean ret;
174
175   cur = gst_xml_write (element);
176   if (!cur)
177     return -1;
178
179 #ifdef HAVE_LIBXML2
180   encoding = (const char *) cur->encoding;
181
182   if (encoding != NULL) {
183     xmlCharEncoding enc;
184
185     enc = xmlParseCharEncoding (encoding);
186
187     if (cur->charset != XML_CHAR_ENCODING_UTF8) {
188       xmlGenericError (xmlGenericErrorContext,
189           "xmlDocDump: document not in UTF8\n");
190       return -1;
191     }
192     if (enc != XML_CHAR_ENCODING_UTF8) {
193       handler = xmlFindCharEncodingHandler (encoding);
194       if (handler == NULL) {
195         xmlFree ((char *) cur->encoding);
196         cur->encoding = NULL;
197       }
198     }
199   }
200
201   buf = xmlOutputBufferCreateFile (out, handler);
202
203   indent = xmlIndentTreeOutput;
204   xmlIndentTreeOutput = 1;
205   ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
206   xmlIndentTreeOutput = indent;
207 #else
208   /* apparently this doesn't return anything in libxml1 */
209   xmlDocDump (out, cur);
210   ret = 1;
211 #endif
212
213   return ret;
214 }
215
216 /**
217  * gst_xml_parse_doc:
218  * @xml: a pointer to a GstXML object
219  * @doc: a pointer to an xml document to parse
220  * @root: The name of the root object to build
221  *
222  * Fills the GstXML object with the elements from the
223  * xmlDocPtr.
224  *
225  * Returns: TRUE on success, FALSE otherwise
226  */
227 gboolean
228 gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
229 {
230   xmlNodePtr field, cur;
231   xmlNsPtr ns;
232
233   cur = xmlDocGetRootElement (doc);
234   if (cur == NULL) {
235     g_warning ("gstxml: empty document\n");
236     return FALSE;
237   }
238   ns = xmlSearchNsByHref (doc, cur,
239       (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
240   if (ns == NULL) {
241     g_warning ("gstxml: document of wrong type, core namespace not found\n");
242     return FALSE;
243   }
244   if (strcmp ((char *) cur->name, "gstreamer")) {
245     g_warning ("gstxml: XML file is in wrong format\n");
246     return FALSE;
247   }
248
249   gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
250       "object_loaded", gst_xml_object_loaded, xml);
251
252   xml->ns = ns;
253
254   field = cur->xmlChildrenNode;
255
256   while (field) {
257     if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
258       GstElement *element;
259
260       element = gst_xml_make_element (field, NULL);
261
262       xml->topelements = g_list_prepend (xml->topelements, element);
263     }
264     field = field->next;
265   }
266
267   xml->topelements = g_list_reverse (xml->topelements);
268
269   return TRUE;
270 }
271
272 /* FIXME 0.9: Why guchar*? */
273 /**
274  * gst_xml_parse_file:
275  * @xml: a pointer to a GstXML object
276  * @fname: The filename with the xml description
277  * @root: The name of the root object to build
278  *
279  * Fills the GstXML object with the corresponding elements from
280  * the XML file fname. Optionally it will only build the element from
281  * the element node root (if it is not NULL). This feature is useful
282  * if you only want to build a specific element from an XML file
283  * but not the pipeline it is embedded in.
284  *
285  * Pass "-" as fname to read from stdin. You can also pass a URI
286  * of any format that libxml supports, including http.
287  *
288  * Returns: TRUE on success, FALSE otherwise
289  */
290 gboolean
291 gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
292 {
293   xmlDocPtr doc;
294
295   g_return_val_if_fail (fname != NULL, FALSE);
296
297   doc = xmlParseFile ((char *) fname);
298
299   if (!doc) {
300     g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
301     return FALSE;
302   }
303
304   return gst_xml_parse_doc (xml, doc, root);
305 }
306
307 /* FIXME 0.9: guchar* */
308 /**
309  * gst_xml_parse_memory:
310  * @xml: a pointer to a GstXML object
311  * @buffer: a pointer to the in memory XML buffer
312  * @size: the size of the buffer
313  * @root: the name of the root objects to build
314  *
315  * Fills the GstXML object with the corresponding elements from
316  * an in memory XML buffer.
317  *
318  * Returns: TRUE on success
319  */
320 gboolean
321 gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
322     const gchar * root)
323 {
324   xmlDocPtr doc;
325
326   g_return_val_if_fail (buffer != NULL, FALSE);
327
328   doc = xmlParseMemory ((char *) buffer, size);
329
330   return gst_xml_parse_doc (xml, doc, (const xmlChar *) root);
331 }
332
333 static void
334 gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
335     gpointer data)
336 {
337   GstXML *xml = GST_XML (data);
338
339   /* FIXME check that this element was created from the same xmlDocPtr... */
340   g_signal_emit (G_OBJECT (xml), gst_xml_signals[OBJECT_LOADED], 0, object,
341       self);
342 }
343
344 /**
345  * gst_xml_get_topelements:
346  * @xml: The GstXML to get the elements from
347  *
348  * Retrive a list of toplevel elements.
349  *
350  * Returns: a GList of elements
351  */
352 GList *
353 gst_xml_get_topelements (GstXML * xml)
354 {
355   g_return_val_if_fail (xml != NULL, NULL);
356
357   return xml->topelements;
358 }
359
360 /* FIXME 0.9: why is the arg guchar* instead of gchar*? */
361 /**
362  * gst_xml_get_element:
363  * @xml: The GstXML to get the element from
364  * @name: The name of element to retreive
365  *
366  * This function is used to get a pointer to the GstElement corresponding
367  * to name in the pipeline description. You would use this if you have
368  * to do anything to the element after loading.
369  *
370  * Returns: a pointer to a new GstElement
371  */
372 GstElement *
373 gst_xml_get_element (GstXML * xml, const guchar * name)
374 {
375   GstElement *element;
376   GList *topelements;
377
378   g_return_val_if_fail (xml != NULL, NULL);
379   g_return_val_if_fail (name != NULL, NULL);
380
381   GST_DEBUG ("gstxml: getting element \"%s\"", name);
382
383   topelements = gst_xml_get_topelements (xml);
384
385   while (topelements) {
386     GstElement *top = GST_ELEMENT (topelements->data);
387
388     GST_DEBUG ("gstxml: getting element \"%s\"", name);
389     if (!strcmp (GST_ELEMENT_NAME (top), (char *) name)) {
390       return top;
391     } else {
392       if (GST_IS_BIN (top)) {
393         element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);
394
395         if (element)
396           return element;
397       }
398     }
399     topelements = g_list_next (topelements);
400   }
401   return NULL;
402 }
403
404 /**
405  * gst_xml_make_element:
406  * @cur: the xml node
407  * @parent: the parent of this object when it's loaded
408  *
409  * Load the element from the XML description
410  *
411  * Returns: the new element
412  */
413 GstElement *
414 gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
415 {
416   xmlNodePtr children = cur->xmlChildrenNode;
417   GstElement *element;
418   gchar *name = NULL;
419   gchar *type = NULL;
420
421   /* first get the needed tags to construct the element */
422   while (children) {
423     if (!strcmp ((char *) children->name, "name")) {
424       name = (gchar *) xmlNodeGetContent (children);
425     } else if (!strcmp ((char *) children->name, "type")) {
426       type = (gchar *) xmlNodeGetContent (children);
427     }
428     children = children->next;
429   }
430   g_return_val_if_fail (name != NULL, NULL);
431   g_return_val_if_fail (type != NULL, NULL);
432
433   GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);
434
435   element = gst_element_factory_make (type, name);
436
437   g_return_val_if_fail (element != NULL, NULL);
438
439   g_free (type);
440   g_free (name);
441
442   /* ne need to set the parent on this object bacause the pads */
443   /* will go through the hierarchy to link to their peers */
444   if (parent)
445     gst_object_set_parent (GST_OBJECT (element), parent);
446
447   gst_object_restore_thyself (GST_OBJECT (element), cur);
448
449   return element;
450 }