[lasem] dom_implementation: allow the installation of new types of document.



commit 6942e7bd861e38677a8c1c189e3135beee6ed243
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Thu Nov 18 22:13:14 2010 +0100

    dom_implementation: allow the installation of new types of document.

 docs/reference/lasem/lasem-sections.txt |    3 +-
 src/lsmdomimplementation.c              |   45 ++++++++++++++++++++++++++----
 src/lsmdomimplementation.h              |    7 ++++-
 src/lsmdomparser.c                      |    2 +-
 src/lsmmathmldocument.c                 |    6 ++--
 src/lsmmathmldocument.h                 |    4 +-
 src/lsmsvgdocument.c                    |    5 ++-
 src/lsmsvgdocument.h                    |    5 ++-
 tests/dom.c                             |   12 ++++----
 9 files changed, 65 insertions(+), 24 deletions(-)
---
diff --git a/docs/reference/lasem/lasem-sections.txt b/docs/reference/lasem/lasem-sections.txt
index 84c3f25..fb8ba82 100644
--- a/docs/reference/lasem/lasem-sections.txt
+++ b/docs/reference/lasem/lasem-sections.txt
@@ -56,7 +56,6 @@ lsm_dom_node_write_to_stream
 lsm_dom_node_list_get_item
 lsm_dom_node_list_get_length
 lsm_dom_node_list_get_type
-lsm_dom_node_list_new
 <SUBSECTION Standard>
 LsmDomNodeList
 LsmDomNodeListClass
@@ -75,7 +74,9 @@ LSM_DOM_DOCUMENT_DEFAULT_RESOLUTION
 LSM_DOM_DOCUMENT_DEFAULT_VIEWBOX_WIDTH
 LSM_DOM_DOCUMENT_DEFAULT_VIEWBOX_HEIGHT
 LsmDomDocument
+LsmDomDocumentCreateFunction
 lsm_dom_implementation_create_document
+lsm_dom_implementation_add_document_create_function
 lsm_dom_document_new_from_memory
 lsm_dom_document_new_from_path
 lsm_dom_document_new_from_url
diff --git a/src/lsmdomimplementation.c b/src/lsmdomimplementation.c
index af1a223..fbbd8a3 100644
--- a/src/lsmdomimplementation.c
+++ b/src/lsmdomimplementation.c
@@ -24,17 +24,50 @@
 #include <lsmdomimplementation.h>
 #include <lsmmathmldocument.h>
 #include <lsmsvgdocument.h>
+#include <lsmdebug.h>
 #include <string.h>
 
+static GHashTable *document_types = NULL;
+
+void
+lsm_dom_implementation_add_create_function (const char *qualified_name,
+					    LsmDomDocumentCreateFunction create_function)
+{
+	if (document_types == NULL)
+		document_types = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+	g_hash_table_insert (document_types, g_strdup (qualified_name), create_function);
+}
+
+void
+lsm_dom_implementation_cleanup (void)
+{
+	if (document_types == NULL)
+		return;
+
+	g_hash_table_unref (document_types);
+	document_types = NULL;
+}
+
 LsmDomDocument *
-lsm_dom_implementation_create_document (const char *qualified_name)
+lsm_dom_implementation_create_document (const char *namespace_uri,
+					const char *qualified_name)
 {
+	LsmDomDocumentCreateFunction create_function;
+
 	g_return_val_if_fail (qualified_name != NULL, NULL);
 
-	if (strcmp (qualified_name, "svg") == 0)
-		return LSM_DOM_DOCUMENT (lsm_svg_document_new ());
-	else if (strcmp (qualified_name, "math") == 0)
-		return LSM_DOM_DOCUMENT (lsm_mathml_document_new ());
+	if (document_types == NULL) {
+		lsm_dom_implementation_add_create_function ("math", lsm_mathml_document_new);
+		lsm_dom_implementation_add_create_function ("svg", lsm_svg_document_new);
+	}
+
+	create_function = g_hash_table_lookup (document_types, qualified_name);
+	if (create_function == NULL) {
+		lsm_debug ("dom", "[LsmDomImplementation::create_document] Unknow document type (%s)",
+			   qualified_name);
+		return NULL;
+	}
 
-	return NULL;
+	return create_function ();
 }
diff --git a/src/lsmdomimplementation.h b/src/lsmdomimplementation.h
index c609120..0c7ae40 100644
--- a/src/lsmdomimplementation.h
+++ b/src/lsmdomimplementation.h
@@ -29,7 +29,12 @@
 
 G_BEGIN_DECLS
 
-LsmDomDocument *lsm_dom_implementation_create_document (const char *qualified_name);
+typedef LsmDomDocument * (*LsmDomDocumentCreateFunction) (void);
+
+LsmDomDocument *	lsm_dom_implementation_create_document 			(const char *namespace_uri,
+										 const char *qualified_name);
+void			lsm_dom_implementation_add_document_create_function	(const char *qualified_name,
+										 LsmDomDocumentCreateFunction create_function);
 
 G_END_DECLS
 
diff --git a/src/lsmdomparser.c b/src/lsmdomparser.c
index 73d76ad..ed87546 100644
--- a/src/lsmdomparser.c
+++ b/src/lsmdomparser.c
@@ -97,7 +97,7 @@ lsm_dom_parser_start_element(void *user_data,
 	}
 
 	if (state->document == NULL) {
-		state->document = lsm_dom_implementation_create_document ((char *)name);
+		state->document = lsm_dom_implementation_create_document (NULL, (char *) name);
 		state->current_node = LSM_DOM_NODE (state->document);
 
 		g_return_if_fail (LSM_IS_DOM_DOCUMENT (state->document));
diff --git a/src/lsmmathmldocument.c b/src/lsmmathmldocument.c
index 9e36b6b..1f8003c 100644
--- a/src/lsmmathmldocument.c
+++ b/src/lsmmathmldocument.c
@@ -1,6 +1,6 @@
-/* lsmmathmldocument.c
+/* Lasem - SVG and Mathml library
  *
- * Copyright © 2007-2008  Emmanuel Pacaud
+ * Copyright © 2007-2008 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -141,7 +141,7 @@ lsm_mathml_document_create_view (LsmDomDocument *document)
 
 /* LsmMathmlDocument implementation */
 
-LsmMathmlDocument *
+LsmDomDocument *
 lsm_mathml_document_new (void)
 {
 	return g_object_new (LSM_TYPE_MATHML_DOCUMENT, NULL);
diff --git a/src/lsmmathmldocument.h b/src/lsmmathmldocument.h
index 030c4ce..a2ceddf 100644
--- a/src/lsmmathmldocument.h
+++ b/src/lsmmathmldocument.h
@@ -1,6 +1,6 @@
 /* Lasem - SVG and Mathml library
  *
- * Copyright © 2007-2008  Emmanuel Pacaud
+ * Copyright © 2007-2008 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -48,7 +48,7 @@ struct _LsmMathmlDocumentClass {
 
 GType lsm_mathml_document_get_type (void);
 
-LsmMathmlDocument *	lsm_mathml_document_new 		(void);
+LsmDomDocument *	lsm_mathml_document_new 		(void);
 LsmMathmlMathElement * 	lsm_mathml_document_get_root_element 	(const LsmMathmlDocument *document);
 
 LsmMathmlDocument * 	lsm_mathml_document_new_from_itex	(const char *itex, int size, GError **error);
diff --git a/src/lsmsvgdocument.c b/src/lsmsvgdocument.c
index af5170c..b87271a 100644
--- a/src/lsmsvgdocument.c
+++ b/src/lsmsvgdocument.c
@@ -1,4 +1,5 @@
-/*
+/* Lasem - SVG and Mathml library
+ *
  * Copyright © 2009 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
@@ -150,7 +151,7 @@ lsm_svg_document_create_view (LsmDomDocument *document)
 
 /* LsmSvgDocument implementation */
 
-LsmSvgDocument *
+LsmDomDocument *
 lsm_svg_document_new (void)
 {
 	return g_object_new (LSM_TYPE_SVG_DOCUMENT, NULL);
diff --git a/src/lsmsvgdocument.h b/src/lsmsvgdocument.h
index 04b9097..b6ad86e 100644
--- a/src/lsmsvgdocument.h
+++ b/src/lsmsvgdocument.h
@@ -1,4 +1,5 @@
-/*
+/* Lasem - SVG and Mathml library
+ *
  * Copyright © 2009 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
@@ -47,7 +48,7 @@ struct _LsmSvgDocumentClass {
 
 GType lsm_svg_document_get_type (void);
 
-LsmSvgDocument *	lsm_svg_document_new 			(void);
+LsmDomDocument *	lsm_svg_document_new 			(void);
 LsmSvgSvgElement * 	lsm_svg_document_get_root_element 	(const LsmSvgDocument *document);
 LsmSvgElement * 	lsm_svg_document_get_element_by_url 	(LsmSvgDocument *document, const char *url);
 
diff --git a/tests/dom.c b/tests/dom.c
index 33fe0d9..1a51374 100644
--- a/tests/dom.c
+++ b/tests/dom.c
@@ -7,14 +7,14 @@ create_document_test (void)
 	LsmDomDocument *document;
 	LsmDomElement *element;
 
-	document = lsm_dom_implementation_create_document ("math");
+	document = lsm_dom_implementation_create_document (NULL, "math");
 	g_assert (LSM_IS_DOM_DOCUMENT (document));
 	g_assert_cmpstr (lsm_dom_node_get_node_name (LSM_DOM_NODE (document)), ==, "#document");
 	g_assert (lsm_dom_node_get_node_value (LSM_DOM_NODE (document)) == NULL);
 
 	g_object_unref (document);
 
-	document = lsm_dom_implementation_create_document ("svg");
+	document = lsm_dom_implementation_create_document (NULL, "svg");
 	g_assert (LSM_IS_DOM_DOCUMENT (document));
 	g_assert_cmpstr (lsm_dom_node_get_node_name (LSM_DOM_NODE (document)), ==, "#document");
 	g_assert (lsm_dom_node_get_node_value (LSM_DOM_NODE (document)) == NULL);
@@ -28,7 +28,7 @@ create_element_test (void)
 	LsmDomDocument *document;
 	LsmDomElement *element;
 
-	document = lsm_dom_implementation_create_document ("svg");
+	document = lsm_dom_implementation_create_document (NULL, "svg");
 	g_assert (LSM_IS_DOM_DOCUMENT (document));
 	g_assert_cmpstr (lsm_dom_node_get_node_name (LSM_DOM_NODE (document)), ==, "#document");
 
@@ -48,7 +48,7 @@ add_remove_element_test (void)
 	LsmDomDocument *document;
 	LsmDomElement *element;
 
-	document = lsm_dom_implementation_create_document ("svg");
+	document = lsm_dom_implementation_create_document (NULL, "svg");
 	g_assert (LSM_IS_DOM_DOCUMENT (document));
 
 	element = lsm_dom_document_create_element (document, "unknown");
@@ -85,7 +85,7 @@ node_list_test (void)
 	LsmDomElement *element;
 	LsmDomNodeList *childs;
 
-	document = lsm_dom_implementation_create_document ("svg");
+	document = lsm_dom_implementation_create_document (NULL, "svg");
 	element = lsm_dom_document_create_element (document, "svg");
 	lsm_dom_node_append_child (LSM_DOM_NODE (document), LSM_DOM_NODE (element));
 
@@ -115,7 +115,7 @@ insert_before_test (void)
 	LsmDomElement *mo1;
 	LsmDomElement *mo2;
 
-	document = lsm_dom_implementation_create_document ("math");
+	document = lsm_dom_implementation_create_document (NULL, "math");
 	math = lsm_dom_document_create_element (document, "math");
 	lsm_dom_node_append_child (LSM_DOM_NODE (document), LSM_DOM_NODE (math));
 	mn1 = lsm_dom_document_create_element (document, "mn");



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]