2 * Copyright (C) 2006, 2007 OpenedHand Ltd.
4 * Author: Jorn Baayen <jorn@openedhand.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "gvalue-util.h"
27 /* libxml DOM interface helpers */
29 xml_util_get_element (xmlNode *node,
34 va_start (var_args, node);
39 arg = va_arg (var_args, const char *);
43 for (node = node->children; node; node = node->next)
44 if (!g_strcmp0 (arg, (char *) node->name))
57 xml_util_get_child_element_content (xmlNode *node,
58 const char *child_name)
62 child_node = xml_util_get_element (node,
68 return xmlNodeGetContent (child_node);
72 xml_util_get_child_element_content_int (xmlNode *node,
73 const char *child_name)
78 content = xml_util_get_child_element_content (node, child_name);
82 i = atoi ((char *) content);
90 xml_util_get_child_element_content_glib (xmlNode *node,
91 const char *child_name)
96 content = xml_util_get_child_element_content (node, child_name);
100 copy = g_strdup ((char *) content);
108 xml_util_get_child_element_content_uri (xmlNode *node,
109 const char *child_name,
115 content = xml_util_get_child_element_content (node, child_name);
120 uri = soup_uri_new_with_base (base, (const char *) content);
122 uri = soup_uri_new ((const char *) content);
130 xml_util_get_child_element_content_url (xmlNode *node,
131 const char *child_name,
137 uri = xml_util_get_child_element_content_uri (node, child_name, base);
141 url = soup_uri_to_string (uri, FALSE);
149 xml_util_get_attribute_contents (xmlNode *node,
150 const char *attribute_name)
154 for (attribute = node->properties;
156 attribute = attribute->next) {
157 if (strcmp (attribute_name, (char *) attribute->name) == 0)
162 return xmlNodeGetContent (attribute->children);
168 * xml_util_real_node:
169 * @node: an %xmlNodePtr
171 * Finds the first "real" node (ie, not a comment or whitespace) at or
172 * after @node at its level in the tree.
174 * Return: a node, or %NULL
176 * (Taken from libsoup)
179 xml_util_real_node (xmlNode *node)
182 (node->type == XML_COMMENT_NODE || xmlIsBlankNode (node)))
187 /* XML string creation helpers */
189 #define INITIAL_XML_STR_SIZE 100 /* Initial xml string size in bytes */
192 xml_util_new_string (void)
194 return g_string_sized_new (INITIAL_XML_STR_SIZE);
198 xml_util_start_element (GString *xml_str,
199 const char *element_name)
201 g_string_append_c (xml_str, '<');
202 g_string_append (xml_str, element_name);
203 g_string_append_c (xml_str, '>');
207 xml_util_end_element (GString *xml_str,
208 const char *element_name)
210 g_string_append (xml_str, "</");
211 g_string_append (xml_str, element_name);
212 g_string_append_c (xml_str, '>');
216 xml_util_add_content (GString *xml_str,
219 /* Modified from GLib gmarkup.c */
226 next = g_utf8_next_char (p);
230 g_string_append (xml_str, "&");
234 g_string_append (xml_str, "<");
238 g_string_append (xml_str, ">");
242 g_string_append (xml_str, """);
246 g_string_append_len (xml_str, p, next - p);