summaryrefslogtreecommitdiffstats
path: root/bindings/utils.c
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2010-06-29 14:15:08 +0000
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2010-06-29 14:15:08 +0000
commit35347922850c0c3435e7ed55597aba02d219d68c (patch)
treefa20b95112cdc7d5fc4aed97b30241c4e94ab677 /bindings/utils.c
parent2e9e814b0900dae14e291ee7708ee92b4035c019 (diff)
downloadlasso-35347922850c0c3435e7ed55597aba02d219d68c.tar.gz
lasso-35347922850c0c3435e7ed55597aba02d219d68c.tar.xz
lasso-35347922850c0c3435e7ed55597aba02d219d68c.zip
[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 <tag/> yyy ", we free the node list and return NULL).
Diffstat (limited to 'bindings/utils.c')
-rw-r--r--bindings/utils.c53
1 files changed, 53 insertions, 0 deletions
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 <libxml/tree.h>
+#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;
+}