2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstxml.c: XML save/restore of pipelines
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.
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.
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.
24 * @short_description: XML save/restore operations of pipelines
28 #include "gst_private.h"
31 #include "gstmarshal.h"
41 static void gst_xml_class_init (GstXMLClass * klass);
42 static void gst_xml_init (GstXML * xml);
44 static void gst_xml_object_loaded (GstObject * private, GstObject * object,
45 xmlNodePtr self, gpointer data);
47 static GstObjectClass *parent_class = NULL;
48 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
51 gst_xml_get_type (void)
53 static GType xml_type = 0;
56 static const GTypeInfo xml_info = {
60 (GClassInitFunc) gst_xml_class_init,
65 (GInstanceInitFunc) gst_xml_init,
69 xml_type = g_type_register_static (GST_TYPE_OBJECT, "GstXML", &xml_info, 0);
75 gst_xml_class_init (GstXMLClass * klass)
77 GObjectClass *gobject_class;
79 gobject_class = (GObjectClass *) klass;
81 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
83 /* FIXME G_TYPE_POINTER should be GType of xmlNodePtr
84 * (ensonic) can't be fixed, as libxml does not use GObject (unfortunately)
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
92 * Signals that a new object has been deserialized.
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,
103 gst_xml_init (GstXML * xml)
105 xml->topelements = NULL;
111 * Create a new GstXML parser object.
113 * Returns: a pointer to a new GstXML object.
118 return GST_XML (g_object_new (GST_TYPE_XML, NULL));
123 * @element: The element to write out
125 * Converts the given element into an XML presentation.
127 * Returns: a pointer to an XML document
130 gst_xml_write (GstElement * element)
133 xmlNodePtr elementnode;
136 doc = xmlNewDoc ((xmlChar *) "1.0");
138 doc->xmlRootNode = xmlNewDocNode (doc, NULL, (xmlChar *) "gstreamer", NULL);
141 xmlNewNs (doc->xmlRootNode,
142 (xmlChar *) "http://gstreamer.net/gst-core/1.0/", (xmlChar *) "gst");
144 elementnode = xmlNewChild (doc->xmlRootNode, gst_ns, (xmlChar *) "element",
147 gst_object_save_thyself (GST_OBJECT (element), elementnode);
153 * gst_xml_write_file:
154 * @element: The element to write out
155 * @out: an open file, like stdout
157 * Converts the given element into XML and writes the formatted XML to an open
160 * Returns: number of bytes written on success, -1 otherwise.
163 gst_xml_write_file (GstElement * element, FILE * out)
168 xmlOutputBufferPtr buf;
170 const char *encoding;
171 xmlCharEncodingHandlerPtr handler = NULL;
175 cur = gst_xml_write (element);
180 encoding = (const char *) cur->encoding;
182 if (encoding != NULL) {
185 enc = xmlParseCharEncoding (encoding);
187 if (cur->charset != XML_CHAR_ENCODING_UTF8) {
188 xmlGenericError (xmlGenericErrorContext,
189 "xmlDocDump: document not in UTF8\n");
192 if (enc != XML_CHAR_ENCODING_UTF8) {
193 handler = xmlFindCharEncodingHandler (encoding);
194 if (handler == NULL) {
195 xmlFree ((char *) cur->encoding);
196 cur->encoding = NULL;
201 buf = xmlOutputBufferCreateFile (out, handler);
203 indent = xmlIndentTreeOutput;
204 xmlIndentTreeOutput = 1;
205 ret = xmlSaveFormatFileTo (buf, cur, NULL, 1);
206 xmlIndentTreeOutput = indent;
208 /* apparently this doesn't return anything in libxml1 */
209 xmlDocDump (out, cur);
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
222 * Fills the GstXML object with the elements from the
225 * Returns: TRUE on success, FALSE otherwise
228 gst_xml_parse_doc (GstXML * xml, xmlDocPtr doc, const guchar * root)
230 xmlNodePtr field, cur;
233 cur = xmlDocGetRootElement (doc);
235 g_warning ("gstxml: empty document\n");
238 ns = xmlSearchNsByHref (doc, cur,
239 (xmlChar *) "http://gstreamer.net/gst-core/1.0/");
241 g_warning ("gstxml: document of wrong type, core namespace not found\n");
244 if (strcmp ((char *) cur->name, "gstreamer")) {
245 g_warning ("gstxml: XML file is in wrong format\n");
249 gst_class_signal_connect (GST_OBJECT_CLASS (G_OBJECT_GET_CLASS (xml)),
250 "object_loaded", gst_xml_object_loaded, xml);
254 field = cur->xmlChildrenNode;
257 if (!strcmp ((char *) field->name, "element") && (field->ns == xml->ns)) {
260 element = gst_xml_make_element (field, NULL);
262 xml->topelements = g_list_prepend (xml->topelements, element);
267 xml->topelements = g_list_reverse (xml->topelements);
272 /* FIXME 0.9: Why guchar*? */
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
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.
285 * Pass "-" as fname to read from stdin. You can also pass a URI
286 * of any format that libxml supports, including http.
288 * Returns: TRUE on success, FALSE otherwise
291 gst_xml_parse_file (GstXML * xml, const guchar * fname, const guchar * root)
295 g_return_val_if_fail (fname != NULL, FALSE);
297 doc = xmlParseFile ((char *) fname);
300 g_warning ("gstxml: XML file \"%s\" could not be read\n", fname);
304 return gst_xml_parse_doc (xml, doc, root);
307 /* FIXME 0.9: guchar* */
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
315 * Fills the GstXML object with the corresponding elements from
316 * an in memory XML buffer.
318 * Returns: TRUE on success
321 gst_xml_parse_memory (GstXML * xml, guchar * buffer, guint size,
326 g_return_val_if_fail (buffer != NULL, FALSE);
328 doc = xmlParseMemory ((char *) buffer, size);
330 return gst_xml_parse_doc (xml, doc, (const xmlChar *) root);
334 gst_xml_object_loaded (GstObject * private, GstObject * object, xmlNodePtr self,
337 GstXML *xml = GST_XML (data);
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,
345 * gst_xml_get_topelements:
346 * @xml: The GstXML to get the elements from
348 * Retrive a list of toplevel elements.
350 * Returns: a GList of elements
353 gst_xml_get_topelements (GstXML * xml)
355 g_return_val_if_fail (xml != NULL, NULL);
357 return xml->topelements;
360 /* FIXME 0.9: why is the arg guchar* instead of gchar*? */
362 * gst_xml_get_element:
363 * @xml: The GstXML to get the element from
364 * @name: The name of element to retreive
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.
370 * Returns: a pointer to a new GstElement
373 gst_xml_get_element (GstXML * xml, const guchar * name)
378 g_return_val_if_fail (xml != NULL, NULL);
379 g_return_val_if_fail (name != NULL, NULL);
381 GST_DEBUG ("gstxml: getting element \"%s\"", name);
383 topelements = gst_xml_get_topelements (xml);
385 while (topelements) {
386 GstElement *top = GST_ELEMENT (topelements->data);
388 GST_DEBUG ("gstxml: getting element \"%s\"", name);
389 if (!strcmp (GST_ELEMENT_NAME (top), (char *) name)) {
392 if (GST_IS_BIN (top)) {
393 element = gst_bin_get_by_name (GST_BIN (top), (gchar *) name);
399 topelements = g_list_next (topelements);
405 * gst_xml_make_element:
407 * @parent: the parent of this object when it's loaded
409 * Load the element from the XML description
411 * Returns: the new element
414 gst_xml_make_element (xmlNodePtr cur, GstObject * parent)
416 xmlNodePtr children = cur->xmlChildrenNode;
421 /* first get the needed tags to construct the element */
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);
428 children = children->next;
430 g_return_val_if_fail (name != NULL, NULL);
431 g_return_val_if_fail (type != NULL, NULL);
433 GST_CAT_INFO (GST_CAT_XML, "loading \"%s\" of type \"%s\"", name, type);
435 element = gst_element_factory_make (type, name);
437 g_return_val_if_fail (element != NULL, NULL);
442 /* ne need to set the parent on this object bacause the pads */
443 /* will go through the hierarchy to link to their peers */
445 gst_object_set_parent (GST_OBJECT (element), parent);
447 gst_object_restore_thyself (GST_OBJECT (element), cur);