[asterisk-commits] eliel: branch group/data_api_gsoc2009 r203564 - in /team/group/data_api_gsoc2...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jun 25 21:10:50 CDT 2009


Author: eliel
Date: Thu Jun 25 21:10:46 2009
New Revision: 203564

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=203564
Log:
Add a way to generate a xml file instead of an internal structure.
(this is useful for testing the returned tree)

Modified:
    team/group/data_api_gsoc2009/include/asterisk/data.h
    team/group/data_api_gsoc2009/include/asterisk/xml.h
    team/group/data_api_gsoc2009/main/data.c
    team/group/data_api_gsoc2009/main/xml.c

Modified: team/group/data_api_gsoc2009/include/asterisk/data.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/include/asterisk/data.h?view=diff&rev=203564&r1=203563&r2=203564
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/data.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/data.h Thu Jun 25 21:10:46 2009
@@ -198,9 +198,19 @@
  * \retval NULL on error.
  * \retval The dynamically allocated requested sub-tree (it needs to be released using
  *         ast_data_free.
- * \see ast_data_free
+ * \see ast_data_free, ast_data_get_xml
  */
 struct ast_data *ast_data_get(const struct ast_data_query *query);
+
+/*!
+ * \brief Retrieve a subtree from the asterisk data API in XML format..
+ * \param[in] query The query structure specifying what nodes to retrieve.
+ * \retval NULL on error.
+ * \retval The dynamically allocated requested sub-tree (it needs to be released using
+ *         ast_data_free.
+ * \see ast_data_free, ast_data_get
+ */
+struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query);
 
 /*!
  * \brief Release the allocated memory of a tree.

Modified: team/group/data_api_gsoc2009/include/asterisk/xml.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/include/asterisk/xml.h?view=diff&rev=203564&r1=203563&r2=203564
==============================================================================
--- team/group/data_api_gsoc2009/include/asterisk/xml.h (original)
+++ team/group/data_api_gsoc2009/include/asterisk/xml.h Thu Jun 25 21:10:46 2009
@@ -45,11 +45,47 @@
  */
 struct ast_xml_doc *ast_xml_open(char *filename);
 
+/*! \brief
+ *  \retval NULL on error.
+ *  \retval The allocated document structure.
+ */
+struct ast_xml_doc *ast_xml_new(void);
+
+/*! \brief
+ *  \param name
+ *  \retval NULL on error.
+ *  \retval The allocated node structe.
+ */
+struct ast_xml_node *ast_xml_new_node(const char *name);
+
+/*! \brief
+ * \param parent
+ * \param child_name
+ * \retval NULL on error.
+ */
+struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name);
+
+/*!
+ * \brief Add a child node, to a specified parent node.
+ * \param parent Where to add the child node.
+ * \param child The child node to add.
+ * \retval NULL on error.
+ * \retval The add child node on success.
+ */
+struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child);
+
 /*! \brief Close an already open document and free the used
  *        structure.
  *  \retval doc The document reference.
  */
 void ast_xml_close(struct ast_xml_doc *doc);
+
+/*! \brief
+ * \param doc
+ * \param node
+ * \retval NULL on error.
+ */
+void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node);
 
 /*! \brief Get the document root node.
  *  \param doc Document reference
@@ -68,6 +104,13 @@
  */
 void ast_xml_free_attr(const char *attribute);
 
+/*! \brief Get the document based on a node.
+ *  \param node A node that is part of the dom.
+ *  \retval NULL on error.
+ *  \retval The dom pointer where this node resides.
+ */
+struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node);
+
 /*! \brief Free a content element that was returned by ast_xml_get_text()
  *  \param text text to be freed.
  */
@@ -80,6 +123,15 @@
  *  \retval The attribute value on success.
  */
 const char *ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname);
+
+/*! \brief Set an attribute to a node.
+ *  \param node In which node we want to insert the attribute.
+ *  \param name The attribute name.
+ *  \param value The attribute value.
+ *  \retval 0 on success.
+ *  \retval -1 on error.
+ */
+int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value);
 
 /*! \brief Find a node element by name.
  *  \param node This is the node starting point.
@@ -98,6 +150,12 @@
  */
 const char *ast_xml_get_text(struct ast_xml_node *node);
 
+/*! \brief Set an element content string.
+ *  \param node Node from where to set the content string.
+ *  \param content The text to insert in the node.
+ */
+void ast_xml_set_text(struct ast_xml_node *node, const char *content);
+
 /*! \brief Get the name of a node. */
 const char *ast_xml_node_get_name(struct ast_xml_node *node);
 
@@ -113,6 +171,9 @@
 /*! \brief Get the parent of a specified node. */
 struct ast_xml_node *ast_xml_node_get_parent(struct ast_xml_node *node);
 
+/*! \brief Dump the specified document to a file. */
+int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc);
+
 /* Features using ast_xml_ */
 #ifdef HAVE_LIBXML2
 #define AST_XML_DOCS

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=203564&r1=203563&r2=203564
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Thu Jun 25 21:10:46 2009
@@ -31,6 +31,7 @@
 #include "asterisk/lock.h"
 #include "asterisk/data.h"
 #include "asterisk/astobj2.h"
+#include "asterisk/xml.h"
 
 #define NUM_DATA_NODE_BUCKETS	60
 #define NUM_DATA_RESULT_BUCKETS 60
@@ -45,7 +46,7 @@
 		int sint;
 		unsigned int uint;
 		double dbl;
-		unsigned int bool:1;
+		unsigned int boolean:1;
 		char *str;
 		struct in_addr ipaddr;
 		void *ptr;
@@ -517,6 +518,8 @@
 		return NULL;
 	}
 
+	strcpy(res->name, name);
+
 	/* initialize the children container */
 	res->children = ao2_container_alloc(NUM_DATA_RESULT_BUCKETS, data_result_hash,
 		data_result_cmp);
@@ -717,6 +720,86 @@
 	}
 
 	return res;
+}
+
+static void data_get_xml_add_child(struct ast_data *parent_data, struct ast_xml_node *parent_xml)
+{
+	struct ao2_iterator i;
+	struct ast_data *node;
+	struct ast_xml_node *child_xml;
+	char node_content[256];
+
+	i = ao2_iterator_init(parent_data->children, 0);
+	while ((node = ao2_iterator_next(&i))) {
+		child_xml = ast_xml_new_node(node->name);
+		if (!child_xml) {
+			ao2_ref(node, -1);
+			continue;
+		}
+
+		switch (node->type) {
+		case AST_DATA_CONTAINER:
+			data_get_xml_add_child(node, child_xml);
+			break;
+		case AST_DATA_STRING:
+			ast_xml_set_text(child_xml, node->payload.str);
+			break;
+		case AST_DATA_INTEGER:
+			snprintf(node_content, sizeof(node_content), "%d", node->payload.sint);
+			ast_xml_set_text(child_xml, node_content);
+			break;
+		case AST_DATA_UNSIGNED_INTEGER:
+			snprintf(node_content, sizeof(node_content), "%u", node->payload.uint);
+			ast_xml_set_text(child_xml, node_content);
+			break;
+		case AST_DATA_DOUBLE:
+			snprintf(node_content, sizeof(node_content), "%f", node->payload.dbl);
+			ast_xml_set_text(child_xml, node_content);
+			break;
+		case AST_DATA_BOOLEAN:
+			if (node->payload.boolean) {
+				ast_xml_set_text(child_xml, "true");
+			} else {
+				ast_xml_set_text(child_xml, "false");
+			}
+			break;
+		case AST_DATA_POINTER:
+		case AST_DATA_IPADDR:
+			/* add internal data. */
+			break;
+		}
+		ast_xml_add_child(parent_xml, child_xml);
+
+		ao2_ref(node, -1);
+	}
+}
+
+struct ast_xml_doc *ast_data_get_xml(const struct ast_data_query *query)
+{
+	struct ast_xml_doc *doc;
+	struct ast_xml_node *root;
+	struct ast_data *res;
+
+	res = ast_data_get(query);
+	if (!res) {
+		return NULL;
+	}
+
+	doc = ast_xml_new();
+	if (!doc) {
+		return NULL;
+	}
+
+	root = ast_xml_new_node(res->name);
+	if (!root) {
+		ast_xml_close(doc);
+	}
+
+	ast_xml_set_root(doc, root);
+
+	data_get_xml_add_child(res, root);
+
+	return doc;
 }
 
 enum ast_data_type ast_data_get_type(struct ast_data *node, const char *path)
@@ -761,25 +844,29 @@
 	strcpy(node->name, (ast_strlen_zero(name) ? "" : name));
 
 	switch (type) {
-		case AST_DATA_INTEGER:
-		case AST_DATA_UNSIGNED_INTEGER:
-			node->payload.sint = *(int *)ptr;
-			break;
-
-		case AST_DATA_DOUBLE:
-			node->payload.dbl = *(double *)ptr;
-			break;
-
-		case AST_DATA_STRING:
-		case AST_DATA_POINTER:
-			node->payload.ptr = ptr;
-			break;
-
-		case AST_DATA_IPADDR:
-			node->payload.ipaddr = *(struct in_addr *)ptr;
-			break;
-
-		default: break;
+	case AST_DATA_BOOLEAN:
+		node->payload.boolean = *(unsigned int *)ptr;
+		break;
+	case AST_DATA_INTEGER:
+	case AST_DATA_UNSIGNED_INTEGER:
+		node->payload.sint = *(int *)ptr;
+		break;
+
+	case AST_DATA_DOUBLE:
+		node->payload.dbl = *(double *)ptr;
+		break;
+
+	case AST_DATA_STRING:
+	case AST_DATA_POINTER:
+		node->payload.ptr = ptr;
+		break;
+
+	case AST_DATA_IPADDR:
+		node->payload.ipaddr = *(struct in_addr *)ptr;
+		break;
+
+	default:
+		break;
 	}
 
 	data_result_add_child(root, node);
@@ -871,6 +958,8 @@
 	struct ast_data_query query = {
 		.path = "asterisk/node1",
 	};
+	struct ast_xml_doc *doc;
+	FILE *outfile;
 
 	/* some tests */
 	ast_data_register_multiple(test_providers, ARRAY_LEN(test_providers));
@@ -879,6 +968,16 @@
 
 	if (res) {
 		ast_data_free(res);
+	}
+
+	/* and now get it in xml form. */
+	doc = ast_data_get_xml(&query);
+	if (doc) {
+		outfile = fopen("/tmp/ast_data_xml_output", "w+");
+		if (outfile) {
+			ast_xml_doc_dump_file(outfile, doc);
+		}
+		ast_xml_close(doc);
 	}
 
 	ast_data_unregister(NULL);

Modified: team/group/data_api_gsoc2009/main/xml.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/xml.c?view=diff&rev=203564&r1=203563&r2=203564
==============================================================================
--- team/group/data_api_gsoc2009/main/xml.c (original)
+++ team/group/data_api_gsoc2009/main/xml.c Thu Jun 25 21:10:46 2009
@@ -70,6 +70,46 @@
 	return (struct ast_xml_doc *) doc;
 }
 
+struct ast_xml_doc *ast_xml_new(void)
+{
+	xmlDoc *doc;
+
+	doc = xmlNewDoc((const xmlChar *) "1.0");
+	return (struct ast_xml_doc *) doc;
+}
+
+struct ast_xml_node *ast_xml_new_node(const char *name)
+{
+	xmlNode *node;
+	if (!name) {
+		return NULL;
+	}
+
+	node = xmlNewNode(NULL, (const xmlChar *) name);
+
+	return (struct ast_xml_node *) node;
+}
+
+struct ast_xml_node *ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
+{
+	xmlNode *child;
+
+	if (!parent || !child_name) {
+		return NULL;
+	}
+
+	child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
+	return (struct ast_xml_node *) child;
+}
+
+struct ast_xml_node *ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
+{
+	if (!parent || !child) {
+		return NULL;
+	}
+	return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
+}
+
 void ast_xml_close(struct ast_xml_doc *doc)
 {
 	if (!doc) {
@@ -80,6 +120,14 @@
 	doc = NULL;
 }
 
+void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
+{
+	if (!doc || !node) {
+		return;
+	}
+ 
+	xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
+}
 
 struct ast_xml_node *ast_xml_get_root(struct ast_xml_doc *doc)
 {
@@ -133,6 +181,19 @@
 	attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
 
 	return (const char *) attrvalue;
+}
+
+int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
+{
+	if (!name || !value) {
+		return -1;
+	}
+
+	if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
+		return -1;
+	}
+
+	return 0;
 }
 
 struct ast_xml_node *ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
@@ -167,6 +228,15 @@
 	return NULL;
 }
 
+struct ast_xml_doc *ast_xml_get_doc(struct ast_xml_node *node)
+{
+	if (!node) {
+		return NULL;
+	}
+
+	return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
+}
+
 const char *ast_xml_get_text(struct ast_xml_node *node)
 {
 	if (!node) {
@@ -176,6 +246,20 @@
 	return (const char *) xmlNodeGetContent((xmlNode *) node);
 }
 
+void ast_xml_set_text(struct ast_xml_node *node, const char *content)
+{
+	if (!node || !content) {
+		return;
+	}
+
+	xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
+}
+
+int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
+{
+	return xmlDocDump(output, (xmlDocPtr)doc);
+}
+
 const char *ast_xml_node_get_name(struct ast_xml_node *node)
 {
 	return (const char *) ((xmlNode *) node)->name;




More information about the asterisk-commits mailing list