1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003, Novell, Inc.
8 #include <libxml/tree.h>
10 #include "soup-soap-response.h"
11 #include "soup-types.h"
13 #define PARENT_TYPE G_TYPE_OBJECT
15 struct _SoupSoapResponsePrivate {
16 /* the XML document */
20 xmlNodePtr xml_method;
21 xmlNodePtr soap_fault;
25 static GObjectClass *parent_class = NULL;
28 finalize (GObject *object)
30 SoupSoapResponse *response = SOUP_SOAP_RESPONSE (object);
33 if (response->priv->xmldoc) {
34 xmlFreeDoc (response->priv->xmldoc);
35 response->priv->xmldoc = NULL;
38 response->priv->xml_root = NULL;
39 response->priv->xml_body = NULL;
40 response->priv->xml_method = NULL;
42 if (response->priv->parameters != NULL) {
43 g_list_free (response->priv->parameters);
44 response->priv->parameters = NULL;
47 g_free (response->priv);
48 response->priv = NULL;
50 parent_class->finalize (object);
54 class_init (SoupSoapResponseClass *klass)
56 GObjectClass *object_class;
58 parent_class = g_type_class_peek_parent (klass);
60 object_class = G_OBJECT_CLASS (klass);
61 object_class->finalize = finalize;
65 init (SoupSoapResponse *response, SoupSoapResponseClass *klass)
67 response->priv = g_new0 (SoupSoapResponsePrivate, 1);
69 response->priv->xmldoc = xmlNewDoc ("1.0");
72 SOUP_MAKE_TYPE (soup_soap_response, SoupSoapResponse, class_init, init, PARENT_TYPE)
75 * soup_soap_response_new:
77 * Create a new empty %SoupSoapResponse object, which can be modified with the
78 * accessor functions provided with this class.
80 * Return value: the new %SoupSoapResponse (or %NULL if there was an error).
83 soup_soap_response_new (void)
85 SoupSoapResponse *response;
87 response = g_object_new (SOUP_TYPE_SOAP_RESPONSE, NULL);
92 * soup_soap_response_new_from_string:
93 * @xmlstr: the XML string to parse.
95 * Create a new %SoupSoapResponse object from the XML string contained in
98 * Return value: the new %SoupSoapResponse (or %NULL if there was an error).
101 soup_soap_response_new_from_string (const char *xmlstr)
103 SoupSoapResponse *response;
105 g_return_val_if_fail (xmlstr != NULL, NULL);
107 response = g_object_new (SOUP_TYPE_SOAP_RESPONSE, NULL);
108 if (!soup_soap_response_from_string (response, xmlstr)) {
109 g_object_unref (response);
117 parse_parameters (SoupSoapResponse *response, xmlNodePtr xml_method)
121 for (tmp = xml_method->xmlChildrenNode; tmp != NULL; tmp = tmp->next) {
122 if (!strcmp (tmp->name, "Fault")) {
123 response->priv->soap_fault = tmp;
126 /* regular parameters */
127 response->priv->parameters = g_list_append (response->priv->parameters, tmp);
133 * soup_soap_response_from_string:
134 * @response: the %SoupSoapResponse object.
135 * @xmlstr: XML string to parse.
137 * Parses the string contained in @xmlstr and sets all properties from it in the
140 * Return value: %TRUE if successful, %FALSE otherwise.
143 soup_soap_response_from_string (SoupSoapResponse *response, const char *xmlstr)
145 xmlDocPtr old_doc = NULL;
146 xmlNodePtr xml_root, xml_body = NULL, xml_method = NULL;
148 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), FALSE);
149 g_return_val_if_fail (xmlstr != NULL, FALSE);
151 /* clear the previous contents */
152 if (response->priv->xmldoc)
153 old_doc = response->priv->xmldoc;
155 /* parse the string */
156 response->priv->xmldoc = xmlParseMemory (xmlstr, strlen (xmlstr));
157 if (!response->priv->xmldoc) {
158 response->priv->xmldoc = old_doc;
162 xml_root = xmlDocGetRootElement (response->priv->xmldoc);
164 xmlFreeDoc (response->priv->xmldoc);
165 response->priv->xmldoc = old_doc;
169 if (strcmp (xml_root->name, "Envelope") != 0) {
170 xmlFreeDoc (response->priv->xmldoc);
171 response->priv->xmldoc = old_doc;
175 if (xml_root->xmlChildrenNode != NULL) {
176 xml_body = xml_root->xmlChildrenNode;
177 if (strcmp (xml_body->name, "Header") == 0)
178 xml_body = xml_root->xmlChildrenNode->next;
179 if (strcmp (xml_body->name, "Body") != 0) {
180 xmlFreeDoc (response->priv->xmldoc);
181 response->priv->xmldoc = old_doc;
185 xml_method = xml_body->xmlChildrenNode;
187 /* read all parameters */
189 parse_parameters (response, xml_method);
192 xmlFreeDoc (old_doc);
194 response->priv->xml_root = xml_root;
195 response->priv->xml_body = xml_body;
196 response->priv->xml_method = xml_method;
202 * soup_soap_response_get_method_name:
203 * @response: the %SoupSoapResponse object.
205 * Gets the method name from the SOAP response.
207 * Return value: the method name.
210 soup_soap_response_get_method_name (SoupSoapResponse *response)
212 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
213 g_return_val_if_fail (response->priv->xml_method != NULL, NULL);
215 return (const char *) response->priv->xml_method->name;
219 * soup_soap_response_set_method_name:
220 * @response: the %SoupSoapResponse object.
221 * @method_name: the method name to set.
223 * Sets the method name on the given %SoupSoapResponse.
226 soup_soap_response_set_method_name (SoupSoapResponse *response, const char *method_name)
228 g_return_if_fail (SOUP_IS_SOAP_RESPONSE (response));
229 g_return_if_fail (response->priv->xml_method != NULL);
230 g_return_if_fail (method_name != NULL);
232 xmlNodeSetName (response->priv->xml_method, method_name);
236 * soup_soap_parameter_get_name:
239 soup_soap_parameter_get_name (SoupSoapParameter *param)
241 g_return_val_if_fail (param != NULL, NULL);
243 return (const char *) param->name;
247 * soup_soap_parameter_get_int_value:
250 soup_soap_parameter_get_int_value (SoupSoapParameter *param)
254 g_return_val_if_fail (param != NULL, -1);
256 s = xmlNodeGetContent (param);
268 * soup_soap_parameter_get_string_value:
271 soup_soap_parameter_get_string_value (SoupSoapParameter *param)
274 g_return_val_if_fail (param != NULL, NULL);
276 xml_s = xmlNodeGetContent (param);
277 s = g_strdup (xml_s);
284 * soup_soap_parameter_get_first_child:
285 * @param: A %SoupSoapParameter.
287 * Gets the first child of the given %SoupSoapParameter. This is used
288 * for compound data types, which can contain several parameters
291 * Return value: the first child or NULL if there are no children.
294 soup_soap_parameter_get_first_child (SoupSoapParameter *param)
296 g_return_val_if_fail (param != NULL, NULL);
298 return param->xmlChildrenNode ? param->xmlChildrenNode : NULL;
302 * soup_soap_parameter_get_first_child_by_name:
303 * @param: A %SoupSoapParameter.
304 * @name: The name of the child parameter to look for.
306 * Gets the first child of the given %SoupSoapParameter whose name
309 * Return value: the first child with the given name or NULL if there
313 soup_soap_parameter_get_first_child_by_name (SoupSoapParameter *param, const char *name)
315 SoupSoapParameter *tmp;
317 g_return_val_if_fail (param != NULL, NULL);
318 g_return_val_if_fail (name != NULL, NULL);
320 for (tmp = soup_soap_parameter_get_first_child (param);
322 tmp = soup_soap_parameter_get_next_child (tmp)) {
323 if (!strcmp (name, tmp->name))
331 * soup_soap_parameter_get_next_child:
332 * @param: A %SoupSoapParameter.
335 soup_soap_parameter_get_next_child (SoupSoapParameter *param)
337 g_return_val_if_fail (param != NULL, NULL);
343 * soup_soap_parameter_get_next_child_by_name:
346 soup_soap_parameter_get_next_child_by_name (SoupSoapParameter *param,
349 SoupSoapParameter *tmp;
351 g_return_val_if_fail (param != NULL, NULL);
352 g_return_val_if_fail (name != NULL, NULL);
354 for (tmp = soup_soap_parameter_get_next_child (param);
356 tmp = soup_soap_parameter_get_next_child (tmp)) {
357 if (!strcmp (name, tmp->name))
365 * soup_soap_parameter_get_property:
367 * @prop_name: Name of the property to retrieve.
370 soup_soap_parameter_get_property (SoupSoapParameter *param, const char *prop_name)
374 g_return_val_if_fail (param != NULL, NULL);
375 g_return_val_if_fail (prop_name != NULL, NULL);
377 xml_s = xmlGetProp (param, prop_name);
378 s = g_strdup (xml_s);
385 * soup_soap_response_get_parameters:
386 * @response: the %SoupSoapResponse object.
388 * Returns the list of parameters received in the SOAP response.
390 * Return value: the list of parameters, represented in
391 * SoupSoapParameter's, which is an opaque type used to
392 * represent a parameter in the SOAP response.
395 soup_soap_response_get_parameters (SoupSoapResponse *response)
397 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
399 return (const GList *) response->priv->parameters;
403 * soup_soap_response_get_first_parameter:
404 * @response: the %SoupSoapResponse object.
406 * Retrieves the first parameter contained in the SOAP response.
408 * Return value: a %SoupSoapParameter representing the
409 * first parameter. This is an opaque type used to
410 * represent a parameter in the SOAP response.
413 soup_soap_response_get_first_parameter (SoupSoapResponse *response)
415 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
417 return response->priv->parameters ? response->priv->parameters->data : NULL;
421 * soup_soap_response_get_first_parameter_by_name:
422 * @response: the %SoupSoapResponse object.
423 * @name: the name of the parameter to look for.
425 * Retrieves the first parameter contained in the SOAP response whose
428 * Return value: a %SoupSoapParameter representing the
429 * first parameter. This is an opaque type used to
430 * represent a parameter in the SOAP response.
433 soup_soap_response_get_first_parameter_by_name (SoupSoapResponse *response,
438 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
439 g_return_val_if_fail (name != NULL, NULL);
441 for (l = response->priv->parameters; l != NULL; l = l->next) {
442 SoupSoapParameter *param = (SoupSoapParameter *) l->data;
444 if (!strcmp (name, param->name))
452 * soup_soap_response_get_next_parameter:
453 * @response: the %SoupSoapResponse object.
454 * @from: the parameter to start from.
456 * Retrieves the parameter following @from in the %SoupSoapResponse object.
458 * Return value: a %SoupSoapParameter representing the parameter.
461 soup_soap_response_get_next_parameter (SoupSoapResponse *response,
462 SoupSoapParameter *from)
466 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
467 g_return_val_if_fail (from != NULL, NULL);
469 l = g_list_find (response->priv->parameters, (gconstpointer) from);
473 return l->next ? (SoupSoapParameter *) l->next->data : NULL;
477 * soup_soap_response_get_next_parameter_by_name:
478 * @response: the %SoupSoapResponse object.
479 * @from: the parameter to start from.
480 * @name: the name of the parameter to look for.
482 * Retrieves the parameter following @from in the %SoupSoapResponse object
483 * whose name matches @name.
485 * Return value: a %SoupSoapParameter representing the parameter.
488 soup_soap_response_get_next_parameter_by_name (SoupSoapResponse *response,
489 SoupSoapParameter *from,
492 SoupSoapParameter *param;
494 g_return_val_if_fail (SOUP_IS_SOAP_RESPONSE (response), NULL);
495 g_return_val_if_fail (from != NULL, NULL);
496 g_return_val_if_fail (name != NULL, NULL);
498 param = soup_soap_response_get_next_parameter (response, from);
500 const char *param_name = soup_soap_parameter_get_name (param);
503 if (!strcmp (name, param_name))
507 param = soup_soap_response_get_next_parameter (response, param);