[seed] XML module is pretty usable now for basic DOM navigation. Mirrors the python API to a large extent
- From: Robert Carr <racarr src gnome org>
- To: svn-commits-list gnome org
- Subject: [seed] XML module is pretty usable now for basic DOM navigation. Mirrors the python API to a large extent
- Date: Mon, 11 May 2009 23:59:05 -0400 (EDT)
commit 306e4f489245fc016c87ce916dadff563bed5ecf
Author: Robert Carr <racarr svn gnome org>
Date: Mon May 11 23:58:30 2009 -0400
XML module is pretty usable now for basic DOM navigation. Mirrors the python API to a large extent
---
modules/libxml/libxml.c | 210 +++++++++++++++++++++++++++++++++++++++++++--
modules/libxml/sample.xml | 12 +++
2 files changed, 214 insertions(+), 8 deletions(-)
diff --git a/modules/libxml/libxml.c b/modules/libxml/libxml.c
index ce838e0..c118f83 100644
--- a/modules/libxml/libxml.c
+++ b/modules/libxml/libxml.c
@@ -8,16 +8,34 @@
SeedObject namespace_ref;
SeedEngine *eng;
SeedClass xml_doc_class;
+SeedClass xml_node_class;
-seed_static_function doc_funcs[] = {
- {0, 0, 0}
-};
+#define XML_DOC_PRIV(obj) ((xmlDocPtr)seed_object_get_private(obj))
+#define XML_NODE_PRIV(obj) ((xmlNodePtr)seed_object_get_private(obj))
-seed_static_value doc_values[] = {
- {0, 0, 0, 0}
-};
+static SeedObject
+seed_make_xml_doc (SeedContext ctx,
+ xmlDocPtr doc)
+{
+ SeedObject ret;
+ if (doc->_private)
+ return (SeedObject) doc->_private;
+ ret = seed_make_object (ctx, xml_doc_class, doc);
+ doc->_private = ret;
+ return ret;
+}
-#define XML_DOC_PRIV(obj) ((xmlDocPtr)seed_object_get_private(obj))
+static SeedObject
+seed_make_xml_node (SeedContext ctx,
+ xmlNodePtr node)
+{
+ SeedObject ret;
+ if (node->_private)
+ return (SeedObject) node->_private;
+ ret = seed_make_object (ctx, xml_node_class, node);
+ node->_private = ret;
+ return ret;
+}
static SeedValue
seed_xml_parse_file (SeedContext ctx,
@@ -46,12 +64,60 @@ seed_xml_parse_file (SeedContext ctx,
g_free (path);
return seed_make_null (ctx);
}
- ret = seed_make_object (ctx, xml_doc_class, doc);
+ ret = seed_make_xml_doc (ctx, doc);
g_free (path);
return ret;
}
+static SeedValue
+seed_xml_node_make_child_array (SeedContext ctx,
+ xmlNodePtr node,
+ SeedException exception)
+{
+ SeedValue ret;
+ xmlNodePtr child;
+ GArray *a;
+
+ child = node->xmlChildrenNode;
+
+ a = g_array_new (FALSE, FALSE, sizeof (SeedValue));
+ while (child != NULL) {
+ SeedObject jsnode = seed_make_xml_node (ctx, child);
+ g_array_append_val (a, jsnode);
+
+ child = child->next;
+ }
+ ret = seed_make_array (ctx, (SeedValue)a->data, a->len, exception);
+ g_array_free (a, TRUE);
+
+ return ret;
+}
+
+static SeedValue
+seed_xml_doc_get_root (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlDocPtr doc = XML_DOC_PRIV (object);
+ return seed_make_xml_node (ctx, xmlDocGetRootElement (doc));
+}
+
+static SeedValue
+seed_xml_doc_get_children (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlDocPtr doc = XML_DOC_PRIV (object);
+
+ return seed_xml_node_make_child_array (ctx,
+ xmlDocGetRootElement (doc),
+ exception);
+}
+
+
static void
seed_xml_doc_finalize (SeedObject object)
{
@@ -59,15 +125,143 @@ seed_xml_doc_finalize (SeedObject object)
xmlFreeDoc (ptr);
}
+static SeedValue
+seed_xml_node_get_name (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+ return seed_value_from_string (ctx, node->name, exception);
+}
+
+static SeedValue
+seed_xml_node_get_children (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_xml_node_make_child_array (ctx,
+ node,
+ exception);
+}
+
+static SeedValue
+seed_xml_node_get_parent (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_make_xml_node (ctx, node->parent);
+}
+
+static SeedValue
+seed_xml_node_get_next (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_make_xml_node (ctx, node->next);
+}
+
+static SeedValue
+seed_xml_node_get_prev (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_make_xml_node (ctx, node->prev);
+}
+
+static SeedValue
+seed_xml_node_get_last (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_make_xml_node (ctx, node->last);
+}
+
+static SeedValue
+seed_xml_node_get_doc (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ return seed_make_xml_doc (ctx, node->doc);
+}
+
+static SeedValue
+seed_xml_node_get_content (SeedContext ctx,
+ SeedObject object,
+ SeedString property_name,
+ SeedException *exception)
+{
+ SeedValue ret;
+ gchar *content;
+ xmlNodePtr node = XML_NODE_PRIV (object);
+
+ content = xmlNodeGetContent (node);
+ ret = seed_value_from_string (ctx, content, exception);
+ g_free (content);
+
+ return ret;
+}
+
+seed_static_function doc_funcs[] = {
+ {0, 0, 0}
+};
+
+seed_static_value doc_values[] = {
+ {"root", seed_xml_doc_get_root, 0, 0},
+ {"children", seed_xml_doc_get_children, 0, 0},
+ {0, 0, 0, 0}
+};
+
+seed_static_function node_funcs[] = {
+ {0, 0, 0}
+};
+
+seed_static_value node_values[] = {
+ {"name", seed_xml_node_get_name, 0, 0},
+ {"children", seed_xml_node_get_children, 0, 0},
+ {"parent", seed_xml_node_get_parent, 0, 0},
+ {"next", seed_xml_node_get_next, 0, 0},
+ {"prev", seed_xml_node_get_prev, 0, 0},
+ {"content", seed_xml_node_get_content, 0, 0},
+ {"last", seed_xml_node_get_last, 0, 0},
+ {"doc", seed_xml_node_get_doc, 0, 0},
+ {0, 0, 0, 0}
+};
+
static void
seed_libxml_define_stuff ()
{
seed_class_definition xml_doc_class_def = seed_empty_class;
+ seed_class_definition xml_node_class_def = seed_empty_class;
xml_doc_class_def.class_name="XMLDocument";
xml_doc_class_def.static_functions = doc_funcs;
xml_doc_class_def.static_values = doc_values;
+ xml_doc_class_def.finalize = seed_xml_doc_finalize;
xml_doc_class = seed_create_class (&xml_doc_class_def);
+
+ xml_node_class_def.class_name="XMLNode";
+ xml_node_class_def.static_functions = node_funcs;
+ xml_node_class_def.static_values = node_values;
+ xml_node_class = seed_create_class (&xml_node_class_def);
seed_create_function (eng->context, "parseFile",
(SeedFunctionCallback) seed_xml_parse_file,
diff --git a/modules/libxml/sample.xml b/modules/libxml/sample.xml
new file mode 100644
index 0000000..6eb1412
--- /dev/null
+++ b/modules/libxml/sample.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<story>
+ <storyinfo>
+ <author>John Fleck</author>
+ <datewritten>June 2, 2002</datewritten>
+ <keyword>example keyword</keyword>
+ </storyinfo>
+ <body>
+ <headline>This is the headline</headline>
+ <para>This is the body text.</para>
+ </body>
+</story>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]