Docs updates
[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 #include "gst_private.h"
24
25 #include "gstxml.h"
26 #include "gstbin.h"
27
28
29 static void     gst_xml_class_init              (GstXMLClass *klass);
30 static void     gst_xml_init                    (GstXML *xml);
31
32 static GstObjectClass *parent_class = NULL;
33
34 GtkType
35 gst_xml_get_type(void)
36 {
37   static GtkType xml_type = 0;
38
39   if (!xml_type) {
40     static const GtkTypeInfo xml_info = {
41       "GstXML",
42       sizeof(GstElement),
43       sizeof(GstElementClass),
44       (GtkClassInitFunc)gst_xml_class_init,
45       (GtkObjectInitFunc)gst_xml_init,
46       (GtkArgSetFunc)NULL,
47       (GtkArgGetFunc)NULL,
48       (GtkClassInitFunc)NULL,
49     };
50     xml_type = gtk_type_unique (GST_TYPE_XML, &xml_info);
51   }
52   return xml_type;
53 }
54
55 static void
56 gst_xml_class_init (GstXMLClass *klass)
57 {
58   parent_class = gtk_type_class (GST_TYPE_OBJECT);
59 }
60
61 static void
62 gst_xml_init(GstXML *xml)
63 {
64 }
65
66 /**
67  * gst_xml_write:
68  * @element: The element to write out
69  *
70  * Converts the given element into an XML presentation.
71  *
72  * Returns: a pointer to an XML document
73  */
74 xmlDocPtr
75 gst_xml_write (GstElement *element)
76 {
77   xmlDocPtr doc;
78
79   doc = xmlNewDoc ("1.0");
80   doc->xmlRootNode = xmlNewDocNode (doc, NULL, "GST-Pipeline", NULL);
81
82   gst_element_save_thyself (element, doc->xmlRootNode);
83
84   return doc;
85 }
86
87 static GstXML*
88 gst_xml_real_parse (xmlDocPtr doc, const guchar *root)
89 {
90   GstXML *xml;
91   xmlNodePtr field;
92
93   if (strcmp(doc->xmlRootNode->name, "GST-Pipeline")) {
94     g_warning("gstxml: XML file is in wrong format\n");
95     return NULL;
96   }
97
98   xml = GST_XML(gtk_type_new(GST_TYPE_XML));
99
100   xml->topelements = NULL;
101
102   field = doc->xmlRootNode->xmlChildrenNode;
103
104   while (field) {
105     if (!strcmp(field->name, "element")) {
106       GstElement *element;
107
108       xml->elements = g_hash_table_new(g_str_hash, g_str_equal);
109
110       element = gst_element_load_thyself(field, xml->elements);
111
112       g_hash_table_destroy (xml->elements);
113
114       xml->topelements = g_list_prepend (xml->topelements, element);
115     }
116     field = field->next;
117   }
118
119   xml->topelements = g_list_reverse (xml->topelements);
120
121   return xml;
122 }
123
124 /**
125  * gst_xml_new:
126  * @fname: The filename with the xml description
127  * @root: The name of the root object to build
128  *
129  * Creates a new GstXML object (and the corresponding elements) from
130  * the XML file fname. Optionally it will only build the element from
131  * the element node root (if it is not NULL). This feature is useful
132  * if you only want to build a specific element from an XML file
133  * but not the pipeline it is embedded in. Note also that the XML parse
134  * tree is cached to speed up creating another GstXML object for
135  * the same file
136  *
137  * Returns: a pointer to a new GstXML object
138  */
139 GstXML*
140 gst_xml_new (const guchar *fname, const guchar *root)
141 {
142   xmlDocPtr doc;
143
144   g_return_val_if_fail(fname != NULL, NULL);
145
146   doc = xmlParseFile(fname);
147
148   if (!doc) {
149     g_warning("gstxml: XML file \"%s\" could not be read\n", fname);
150     return NULL;
151   }
152
153   return gst_xml_real_parse (doc, root);
154 }
155
156 /**
157  * gst_xml_new_from_memory:
158  * @buffer: a pointer to the in memory XML buffer
159  * @size: the size of the buffer
160  * @root: the name of the root objects to build
161  *
162  * Creates a new GstXML object (and the corresponding elements) from
163  * an in memory XML buffer.
164  *
165  * Returns: a pointer to a new GstXML object
166  */
167 GstXML*
168 gst_xml_new_from_memory (guchar *buffer, guint size, const gchar *root)
169 {
170   xmlDocPtr doc;
171
172   g_return_val_if_fail(buffer != NULL, NULL);
173
174   doc = xmlParseMemory (buffer, size);
175
176   return gst_xml_real_parse (doc, root);
177 }
178
179 /**
180  * gst_xml_get_topelements:
181  * @xml: The GstXML to get the elements from
182  *
183  * Retrive a list of toplevel elements.
184  *
185  * Returns: a GList of elements
186  */
187 GList*
188 gst_xml_get_topelements (GstXML *xml)
189 {
190   g_return_val_if_fail (xml != NULL, NULL);
191
192   return xml->topelements;
193 }
194
195 /**
196  * gst_xml_get_element:
197  * @xml: The GstXML to get the element from
198  * @name: The name of element to retreive
199  *
200  * This function is used to get a pointer to the GstElement corresponding
201  * to name in the pipeline description. You would use this if you have
202  * to do anything to the element after loading.
203  *
204  * Returns: a pointer to a new GstElement
205  */
206 GstElement*
207 gst_xml_get_element (GstXML *xml, const guchar *name)
208 {
209   GstElement *element;
210   GList *topelements;
211
212   g_return_val_if_fail(xml != NULL, NULL);
213   g_return_val_if_fail(name != NULL, NULL);
214
215   GST_DEBUG (0,"gstxml: getting element \"%s\"\n", name);
216
217   topelements = gst_xml_get_topelements (xml);
218
219   while (topelements) {
220     GstElement *top = GST_ELEMENT (topelements->data);
221
222     if (!strcmp (gst_element_get_name (top), name)) {
223       return top;
224     }
225     else {
226       if (GST_IS_BIN (top)) {
227         element = gst_bin_get_by_name (GST_BIN (top), name);
228
229         if (element)
230           return element;
231       }
232     }
233     topelements = g_list_next (topelements);
234   }
235   return NULL;
236 }