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 */
23 #include "gst_private.h"
25 #include "gstxml.h"
26 #include "gstbin.h"
28 enum {
29 OBJECT_LOADED,
30 LAST_SIGNAL
31 };
33 static void gst_xml_class_init (GstXMLClass *klass);
34 static void gst_xml_init (GstXML *xml);
36 static void gst_xml_object_loaded (GstObject *private, GstObject *object, xmlNodePtr self, gpointer data);
38 static GstObjectClass *parent_class = NULL;
39 static guint gst_xml_signals[LAST_SIGNAL] = { 0 };
41 GtkType
42 gst_xml_get_type(void)
43 {
44 static GtkType xml_type = 0;
46 if (!xml_type) {
47 static const GtkTypeInfo xml_info = {
48 "GstXML",
49 sizeof(GstXML),
50 sizeof(GstXMLClass),
51 (GtkClassInitFunc)gst_xml_class_init,
52 (GtkObjectInitFunc)gst_xml_init,
53 (GtkArgSetFunc)NULL,
54 (GtkArgGetFunc)NULL,
55 (GtkClassInitFunc)NULL,
56 };
57 xml_type = gtk_type_unique (GST_TYPE_OBJECT, &xml_info);
58 }
59 return xml_type;
60 }
62 static void
63 gst_xml_class_init (GstXMLClass *klass)
64 {
65 GtkObjectClass *gtkobject_class;
67 gtkobject_class = (GtkObjectClass *)klass;
69 parent_class = gtk_type_class (GST_TYPE_OBJECT);
71 gst_xml_signals[OBJECT_LOADED] =
72 gtk_signal_new ("object_loaded", GTK_RUN_LAST, gtkobject_class->type,
73 GTK_SIGNAL_OFFSET (GstXMLClass, object_loaded),
74 gtk_marshal_NONE__POINTER_POINTER, GTK_TYPE_NONE, 2,
75 GST_TYPE_OBJECT, GTK_TYPE_POINTER);
77 gtk_object_class_add_signals (gtkobject_class, gst_xml_signals, LAST_SIGNAL);
78 }
80 static void
81 gst_xml_init(GstXML *xml)
82 {
83 xml->topelements = NULL;
84 }
86 /**
87 * gst_xml_new:
88 *
89 * Create a new GstXML parser object.
90 *
91 * Returns: a pointer to a new GstXML object.
92 */
93 GstXML*
94 gst_xml_new (void)
95 {
96 return GST_XML (gtk_type_new (GST_TYPE_XML));
97 }
99 /**
100 * gst_xml_write:
101 * @element: The element to write out
102 *
103 * Converts the given element into an XML presentation.
104 *
105 * Returns: a pointer to an XML document
106 */
107 xmlDocPtr
108 gst_xml_write (GstElement *element)
109 {
110 xmlDocPtr doc;
111 xmlNodePtr elementnode;
112 xmlNsPtr ns;
114 doc = xmlNewDoc ("1.0");
115 xmlNewGlobalNs (doc, "http://gstreamer.net/gst-core/1.0/", "gst");
116 ns = xmlNewNs (doc->xmlRootNode, "http://gstreamer.net/gst-core/1.0/", "gst");
118 doc->xmlRootNode = xmlNewDocNode (doc, ns, "gstreamer", NULL);
120 elementnode = xmlNewChild (doc->xmlRootNode, ns, "element", NULL);
122 gst_object_save_thyself (GST_OBJECT (element), elementnode);
124 return doc;
125 }
127 /**
128 * gst_xml_parse_doc:
129 * @xml: a pointer to a GstXML object
130 * @doc: a pointer to an xml document to parse
131 * @root: The name of the root object to build
132 *
133 * Fills the GstXML object with the elements from the
134 * xmlDocPtr.
135 *
136 * Returns: TRUE on success, FALSE otherwise
137 */
138 gboolean
139 gst_xml_parse_doc (GstXML *xml, xmlDocPtr doc, const guchar *root)
140 {
141 xmlNodePtr field, cur;
142 xmlNsPtr ns;
144 cur = xmlDocGetRootElement(doc);
145 if (cur == NULL) {
146 g_warning("gstxml: empty document\n");
147 return FALSE ;
148 }
149 ns = xmlSearchNsByHref(doc, cur, "http://gstreamer.net/gst-core/1.0/");
150 if (ns == NULL) {
151 g_warning("gstxml: document of wrong type, core namespace not found\n");
152 return FALSE;
153 }
154 if (strcmp(cur->name, "gstreamer")) {
155 g_warning("gstxml: XML file is in wrong format\n");
156 return FALSE;
157 }
159 gst_class_signal_connect (GST_OBJECT_CLASS (GTK_OBJECT (xml)->klass),
160 "object_loaded", gst_xml_object_loaded, xml);
162 xml->ns = ns;
164 field = cur->xmlChildrenNode;
166 while (field) {
167 if (!strcmp(field->name, "element") && (field->ns == xml->ns)) {
168 GstElement *element;
170 element = gst_element_restore_thyself(field, NULL);
172 xml->topelements = g_list_prepend (xml->topelements, element);
173 }
174 field = field->next;
175 }
177 xml->topelements = g_list_reverse (xml->topelements);
179 return TRUE;
180 }
182 /**
183 * gst_xml_parse_file:
184 * @xml: a pointer to a GstXML object
185 * @fname: The filename with the xml description
186 * @root: The name of the root object to build
187 *
188 * Fills the GstXML object with the corresponding elements from
189 * the XML file fname. Optionally it will only build the element from
190 * the element node root (if it is not NULL). This feature is useful
191 * if you only want to build a specific element from an XML file
192 * but not the pipeline it is embedded in.
193 *
194 * Returns: TRUE on success, FALSE otherwise
195 */
196 gboolean
197 gst_xml_parse_file (GstXML *xml, const guchar *fname, const guchar *root)
198 {
199 xmlDocPtr doc;
201 g_return_val_if_fail(fname != NULL, FALSE);
203 doc = xmlParseFile(fname);
205 if (!doc) {
206 g_warning("gstxml: XML file \"%s\" could not be read\n", fname);
207 return FALSE;
208 }
210 return gst_xml_parse_doc (xml, doc, root);
211 }
213 /**
214 * gst_xml_parse_memory:
215 * @xml: a pointer to a GstXML object
216 * @buffer: a pointer to the in memory XML buffer
217 * @size: the size of the buffer
218 * @root: the name of the root objects to build
219 *
220 * Fills the GstXML object with the corresponding elements from
221 * an in memory XML buffer.
222 *
223 * Returns: TRUE on success
224 */
225 gboolean
226 gst_xml_parse_memory (GstXML *xml, guchar *buffer, guint size, const gchar *root)
227 {
228 xmlDocPtr doc;
230 g_return_val_if_fail(buffer != NULL, FALSE);
232 doc = xmlParseMemory (buffer, size);
234 return gst_xml_parse_doc (xml, doc, root);
235 }
237 static void
238 gst_xml_object_loaded (GstObject *private, GstObject *object, xmlNodePtr self, gpointer data)
239 {
240 GstXML *xml = GST_XML (data);
242 // FIXME check that this element was created from the same xmlDocPtr...
243 gtk_signal_emit (GTK_OBJECT (xml), gst_xml_signals[OBJECT_LOADED], object, self);
244 }
246 /**
247 * gst_xml_get_topelements:
248 * @xml: The GstXML to get the elements from
249 *
250 * Retrive a list of toplevel elements.
251 *
252 * Returns: a GList of elements
253 */
254 GList*
255 gst_xml_get_topelements (GstXML *xml)
256 {
257 g_return_val_if_fail (xml != NULL, NULL);
259 return xml->topelements;
260 }
262 /**
263 * gst_xml_get_element:
264 * @xml: The GstXML to get the element from
265 * @name: The name of element to retreive
266 *
267 * This function is used to get a pointer to the GstElement corresponding
268 * to name in the pipeline description. You would use this if you have
269 * to do anything to the element after loading.
270 *
271 * Returns: a pointer to a new GstElement
272 */
273 GstElement*
274 gst_xml_get_element (GstXML *xml, const guchar *name)
275 {
276 GstElement *element;
277 GList *topelements;
279 g_return_val_if_fail(xml != NULL, NULL);
280 g_return_val_if_fail(name != NULL, NULL);
282 GST_DEBUG (0,"gstxml: getting element \"%s\"\n", name);
284 topelements = gst_xml_get_topelements (xml);
286 while (topelements) {
287 GstElement *top = GST_ELEMENT (topelements->data);
289 GST_DEBUG (0,"gstxml: getting element \"%s\"\n", name);
290 if (!strcmp (GST_ELEMENT_NAME (top), name)) {
291 return top;
292 }
293 else {
294 if (GST_IS_BIN (top)) {
295 element = gst_bin_get_by_name (GST_BIN (top), name);
297 if (element)
298 return element;
299 }
300 }
301 topelements = g_list_next (topelements);
302 }
303 return NULL;
304 }