From 35347922850c0c3435e7ed55597aba02d219d68c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 29 Jun 2010 14:15:08 +0000 Subject: [Bindings] accept simple string in string<->xmlNode converter Some use case ask for passing simple libxml content node (i.e just an UTF-8 string) when a method argument or a field of the xmlNode* type. This commit add a static method in bindings/utils.c named lasso_string_fragment_to_xmlnode which does this transform by trying to parse an XML document then by trying to parse a well balanced XML fragment of only one node (if there is more than one node such as in the string " xxx yyy ", we free the node list and return NULL). --- bindings/utils.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 bindings/utils.c (limited to 'bindings/utils.c') diff --git a/bindings/utils.c b/bindings/utils.c new file mode 100644 index 00000000..d7dd4934 --- /dev/null +++ b/bindings/utils.c @@ -0,0 +1,53 @@ +/* + * In this file we put utility functions shared by all bindings. + * + * They usually are data structure manipulation or conversion functions. + */ +#include +#include "../lasso/utils.h" + +/** + * lasso_string_fragment_to_xmlnode: + * @fragment: a fragment of an XML document + * @size: + * + * Try to get one and only one node from a string, the node can be a simple string or a single node. + * + * Return value: a newly allocated xmlNode* or NULL if parsing fails. + */ +static xmlNode* +lasso_string_fragment_to_xmlnode(const char *fragment, int size) { + xmlDoc *doc = NULL; + xmlNode *node = NULL; + xmlNode *list = NULL, *ref = NULL; + xmlParserErrors errors; + + if (size == 0) { + size = strlen(fragment); + } + + /* single node case, with preceding or following spaces */ + doc = xmlReadMemory(fragment, size, NULL, NULL, XML_PARSE_NONET); + if (doc) { + node = xmlDocGetRootElement(doc); + if (node != NULL) { + node = xmlCopyNode(node, 1); + goto cleanup; + } + lasso_release_doc(doc); + } + /* simple string */ + doc = xmlNewDoc(BAD_CAST "1.0"); + ref = xmlNewNode(NULL, BAD_CAST "root"); + + xmlDocSetRootElement(doc, ref); + errors = xmlParseInNodeContext(ref, fragment, size, + XML_PARSE_NONET, &list); + if (errors == XML_ERR_OK && list != NULL && list->next == NULL) { + node = xmlCopyNode(list, 1); + } +cleanup: + lasso_release_doc(doc); + lasso_release_xml_node_list(list); + return node; +} -- cgit