[lasem] Restore correct file loading.



commit 41ff3b485897e884e2194162e545367b0e0c48a1
Author: Emmanuel Pacaud <emmanuel pacaud lapp in2p3 fr>
Date:   Fri Aug 7 21:23:35 2009 +0200

    Restore correct file loading.
    
    Content mime type detection is not reliable if done on
    a memory buffer (returns text/plain for mathml contents).

 src/lasemrender.c       |   17 ++++++-
 src/lasemtest.c         |   18 ++++++--
 src/lsmattributes.c     |    5 ++-
 src/lsmdomattributes.c  |    3 +
 src/lsmdomnode.c        |   13 ++++-
 src/lsmdomparser.c      |   55 ++++++------------------
 src/lsmdomparser.h      |    2 +-
 src/lsmmathmldocument.c |  108 +++++++++++++++++++++++++++++++++++++++++-----
 src/lsmmathmldocument.h |    6 +-
 9 files changed, 157 insertions(+), 70 deletions(-)
---
diff --git a/src/lasemrender.c b/src/lasemrender.c
index b93cc01..0ba287f 100644
--- a/src/lasemrender.c
+++ b/src/lasemrender.c
@@ -159,9 +159,20 @@ int main(int argc, char **argv)
 		g_free (directory);
 	}
 
-	document = lsm_dom_document_new_from_path (input_filename, NULL);
-	if (document == NULL)
-		document = lsm_dom_document_new_from_url (input_filename, NULL);
+	document = lsm_dom_document_new_from_path (input_filename,
+						   NULL);
+	if (document == NULL) {
+		document = lsm_dom_document_new_from_url (input_filename,
+							  NULL);
+		if (document == NULL) {
+			document = LSM_DOM_DOCUMENT (lsm_mathml_document_new_from_itex_path (input_filename,
+											     NULL));
+			if (document == NULL) {
+				document = LSM_DOM_DOCUMENT (lsm_mathml_document_new_from_itex_url (input_filename,
+												    NULL));
+			}
+		}
+	}
 
 	if (document != NULL) {
 		lsm_dom_document_set_resolution (document, option_ppi);
diff --git a/src/lasemtest.c b/src/lasemtest.c
index bf385f6..f73d7e9 100644
--- a/src/lasemtest.c
+++ b/src/lasemtest.c
@@ -29,12 +29,13 @@
 #include <unistd.h>
 #endif
 
-#include <glib/gregex.h>
-#include <glib/gprintf.h>
-#include <gio/gio.h>
 #include <lsmdebug.h>
+#include <lsmmathmldocument.h>
 #include <lsmdomparser.h>
 #include <lsmdomdocument.h>
+#include <glib/gregex.h>
+#include <glib/gprintf.h>
+#include <gio/gio.h>
 
 #include <libxml/parser.h>
 
@@ -45,6 +46,7 @@
 static gboolean option_debug = FALSE;
 static char **option_input_filenames = NULL;
 double option_ppi = 72.0;
+static gboolean fatal_warning = FALSE;
 
 static const GOptionEntry entries[] =
 {
@@ -54,6 +56,8 @@ static const GOptionEntry entries[] =
 		&option_ppi, 			"Pixel per inch", NULL },
 	{ "debug", 		'd', 0, G_OPTION_ARG_NONE,
 		&option_debug, 			"Debug mode", NULL },
+	{ "fatal-warning", 	'f', 0, G_OPTION_ARG_NONE,
+		&fatal_warning,			"Make warning fatal", NULL },
 	{ NULL }
 };
 
@@ -111,16 +115,17 @@ lasem_test_render (char const *filename)
 	success = g_file_get_contents (filename, &buffer, &size, NULL);
 	if (success) {
 		LsmBox viewport;
-		char *xml = NULL;
+		char *xml;
 
 		if (is_xml)
 			xml = buffer;
 		else {
 			xml = itex2MML_parse (buffer, size);
-			size = 0;
+			size = -1;
 		}
 
 		document = lsm_dom_document_new_from_memory (xml, size, NULL);
+
 		lsm_dom_document_set_path (document, filename);
 
 		view = lsm_dom_document_create_view (document);
@@ -316,6 +321,9 @@ main (int argc, char **argv)
 	if (option_debug)
 		lsm_debug_enable ();
 
+	if (fatal_warning)
+		g_log_set_fatal_mask ("Lasem", G_LOG_FATAL_MASK | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);
+
 	timer = g_timer_new ();
 
 	regex_mml = g_regex_new ("\\.(mml|tex|svg)$", 0, 0, &error);
diff --git a/src/lsmattributes.c b/src/lsmattributes.c
index 611a7f8..1dadbd6 100644
--- a/src/lsmattributes.c
+++ b/src/lsmattributes.c
@@ -189,8 +189,11 @@ lsm_attribute_manager_clean_attributes (LsmAttributeManager *manager,
 		attribute_infos = value;
 		trait_class = attribute_infos->trait_class;
 
+		attribute = (void *)(instance + attribute_infos->attribute_offset);
+		g_free (attribute->value);
+		attribute->value = NULL;
+
 		if (trait_class->finalize) {
-			attribute = (void *)(instance + attribute_infos->attribute_offset);
 			trait_class->finalize (ATTRIBUTE_TRAIT (attribute));
 		}
 	}
diff --git a/src/lsmdomattributes.c b/src/lsmdomattributes.c
index 3898164..660696e 100644
--- a/src/lsmdomattributes.c
+++ b/src/lsmdomattributes.c
@@ -294,6 +294,9 @@ lsm_dom_attribute_finalize_cb (gpointer key,
 		g_free (attribute->value);
 		g_free (attribute->css_value);
 
+		attribute->value = NULL;
+		attribute->css_value = NULL;
+
 		if (attribute_infos->attribute_class != NULL &&
 		    attribute_infos->attribute_class->finalize != NULL)
 			attribute_infos->attribute_class->finalize (attribute);
diff --git a/src/lsmdomnode.c b/src/lsmdomnode.c
index e7725d9..2b5d1c5 100644
--- a/src/lsmdomnode.c
+++ b/src/lsmdomnode.c
@@ -20,9 +20,10 @@
  * 	Emmanuel Pacaud <emmanuel gnome org>
  */
 
-#include <glib/gprintf.h>
 #include <lsmdomnode.h>
 #include <lsmdomdocument.h>
+#include <lsmdebug.h>
+#include <glib/gprintf.h>
 #include <stdio.h>
 
 static GObjectClass *parent_class = NULL;
@@ -201,15 +202,21 @@ lsm_dom_node_remove_child (LsmDomNode* self, LsmDomNode* old_child)
 	return old_child;
 }
 
-LsmDomNode*
+LsmDomNode *
 lsm_dom_node_append_child (LsmDomNode* self, LsmDomNode* new_child)
 {
 	LsmDomNodeClass *node_class;
 
 	g_return_val_if_fail (LSM_IS_DOM_NODE (self), NULL);
-	g_return_val_if_fail (LSM_DOM_NODE_GET_CLASS (self)->can_append_child (self, new_child), NULL);
 	g_return_val_if_fail (new_child->parent_node == NULL, NULL);
 
+	if (!LSM_DOM_NODE_GET_CLASS (self)->can_append_child (self, new_child)) {
+		lsm_debug ("[LsmDomNode::append_child] Can't append '%s' to '%s'",
+			   lsm_dom_node_get_node_name (new_child),
+			   lsm_dom_node_get_node_name (self));
+		return NULL;
+	}
+
 	if (self->first_child == NULL)
 		self->first_child = new_child;
 	if (self->last_child != NULL)
diff --git a/src/lsmdomparser.c b/src/lsmdomparser.c
index 66024f4..c7e0b28 100644
--- a/src/lsmdomparser.c
+++ b/src/lsmdomparser.c
@@ -103,10 +103,7 @@ lsm_dom_parser_start_element(void *user_data,
 
 	node = LSM_DOM_NODE (lsm_dom_document_create_element (LSM_DOM_DOCUMENT (state->document), (char *) name));
 
-	if (LSM_IS_DOM_NODE (node)) {
-
-		lsm_dom_node_append_child (state->current_node, node);
-
+	if (LSM_IS_DOM_NODE (node) && lsm_dom_node_append_child (state->current_node, node) != NULL) {
 		if (attrs != NULL)
 			for (i = 0; attrs[i] != NULL && attrs[i+1] != NULL; i += 2)
 				lsm_dom_element_set_attribute (LSM_DOM_ELEMENT (node),
@@ -117,6 +114,9 @@ lsm_dom_parser_start_element(void *user_data,
 		state->is_error = FALSE;
 		state->error_depth = 0;
 	} else {
+		if (node != NULL)
+			g_object_unref (node);
+
 		state->is_error = TRUE;
 		state->error_depth = 1;
 	}
@@ -265,7 +265,7 @@ lsm_dom_document_error_quark (void)
 	static GQuark q = 0;
 
         if (q == 0) {
-                q = g_quark_from_static_string ("mrp-error-quark");
+                q = g_quark_from_static_string ("lsm-dom-error-quark");
         }
 
         return q;
@@ -277,34 +277,19 @@ typedef enum {
 	LSM_DOM_DOCUMENT_ERROR_INVALID_XML
 } LsmDomDocumentError;
 
-static void
-_dummy_error (const char *msg)
-{
-}
-
-static LsmDomDocument *
-lsm_dom_document_new_from_contents (const char *contents, gsize size, GError **error)
+LsmDomDocument *
+lsm_dom_document_new_from_memory (const char *buffer, int size, GError **error)
 {
 	static LsmDomSaxParserState state;
-	char *mime;
-	char *xml;
 
-	state.document = NULL;
+	g_return_val_if_fail (buffer != NULL, NULL);
 
-	mime = g_content_type_guess (NULL, (guchar *) contents, size, NULL);
-	lsm_debug ("[LsmDomDocument::new_from_contents] mime = '%s'", mime);
-	if (g_strcmp0 (mime, "image/svg+xml") == 0 ||
-	    g_strcmp0 (mime, "text/mathml") == 0 ||
-	    g_strcmp0 (mime, "application/xml") == 0) {
-		xml = (char *) contents;
-	} else {
-		itex2MML_error = _dummy_error;
+	state.document = NULL;
 
-		xml = itex2MML_parse (contents, size);
-		size = strlen (contents);
-	}
+	if (size < 0)
+		size = strlen (buffer);
 
-	if (xmlSAXUserParseMemory (&sax_handler, &state, xml, size) < 0) {
+	if (xmlSAXUserParseMemory (&sax_handler, &state, buffer, size) < 0) {
 		if (state.document !=  NULL)
 			g_object_unref (state.document);
 		state.document = NULL;
@@ -317,23 +302,9 @@ lsm_dom_document_new_from_contents (const char *contents, gsize size, GError **e
 			     "Invalid document.");
 	}
 
-	if (xml != contents)
-		itex2MML_free_string (xml);
-
 	return state.document;
 }
 
-LsmDomDocument *
-lsm_dom_document_new_from_memory (const char *buffer, gsize size, GError **error)
-{
-	g_return_val_if_fail (buffer != NULL, NULL);
-
-	if (size < 1)
-		size = strlen (buffer);
-
-	return lsm_dom_document_new_from_contents (buffer, size, error);
-}
-
 static LsmDomDocument *
 lsm_dom_document_new_from_file (GFile *file, GError **error)
 {
@@ -344,7 +315,7 @@ lsm_dom_document_new_from_file (GFile *file, GError **error)
 	if (!g_file_load_contents (file, NULL, &contents, &size, NULL, error))
 		return NULL;
 
-	document = lsm_dom_document_new_from_contents (contents, size, error);
+	document = lsm_dom_document_new_from_memory (contents, size, error);
 
 	g_free (contents);
 
diff --git a/src/lsmdomparser.h b/src/lsmdomparser.h
index 51e6a78..80a2b35 100644
--- a/src/lsmdomparser.h
+++ b/src/lsmdomparser.h
@@ -30,7 +30,7 @@ typedef enum {
 	LSM_DOM_DOCUMENT_ERROR_INVALID_XML
 } LsmDomDocumentError;
 
-LsmDomDocument * 	lsm_dom_document_new_from_memory 	(const char *buffer, gsize size, GError *error);
+LsmDomDocument * 	lsm_dom_document_new_from_memory 	(const char *buffer, int size, GError **error);
 LsmDomDocument * 	lsm_dom_document_new_from_path 		(const char *path, GError *error);
 LsmDomDocument * 	lsm_dom_document_new_from_url 		(const char *url, GError *error);
 
diff --git a/src/lsmmathmldocument.c b/src/lsmmathmldocument.c
index 2532456..51f5f47 100644
--- a/src/lsmmathmldocument.c
+++ b/src/lsmmathmldocument.c
@@ -42,6 +42,8 @@
 #include <lsmmathmlalignmarkelement.h>
 #include <lsmmathmlaligngroupelement.h>
 #include <lsmmathmlview.h>
+#include <lsmdebug.h>
+#include <gio/gio.h>
 #include <string.h>
 
 #include <lsmdomparser.h>
@@ -168,31 +170,113 @@ lsm_mathml_document_class_init (LsmMathmlDocumentClass *m_document_class)
 
 G_DEFINE_TYPE (LsmMathmlDocument, lsm_mathml_document, LSM_TYPE_DOM_DOCUMENT)
 
-#if 0
-static void
-_dummy_error (const char *msg)
+static GQuark
+lsm_mathml_document_error_quark (void)
 {
+	static GQuark q = 0;
+
+        if (q == 0) {
+                q = g_quark_from_static_string ("lsm-mathml-error-quark");
+        }
+
+        return q;
 }
 
+#define LSM_MATHML_DOCUMENT_ERROR lsm_mathml_document_error_quark ()
+
+typedef enum {
+	LSM_MATHML_DOCUMENT_ERROR_INVALID_ITEX
+} LsmMathmlDocumentError;
+
 LsmMathmlDocument *
-lsm_mathml_document_new_from_itex (const char *itex)
+lsm_mathml_document_new_from_itex (const char *itex, int size, GError **error)
 {
-	LsmMathmlDocument *document;
-	char *mathml;
+	LsmDomDocument *document;
+	char *xml;
 
 	g_return_val_if_fail (itex != NULL, NULL);
 
-	itex2MML_error = _dummy_error;
+	xml = itex2MML_parse (itex, size);
+
+	if (xml == NULL) {
+		lsm_debug ("[LsmMathmlDocument::new_from_itex] invalid document");
+
+		g_set_error (error,
+			     LSM_MATHML_DOCUMENT_ERROR,
+			     LSM_MATHML_DOCUMENT_ERROR_INVALID_ITEX,
+			     "Invalid itex document.");
+
+		return NULL;
+	}
+
+	document = lsm_dom_document_new_from_memory (xml, -1, error);
 
-	mathml = itex2MML_parse (itex, strlen (itex));
-	document = LSM_MATHML_DOCUMENT (lsm_dom_document_new_from_memory (mathml));
-	itex2MML_free_string (mathml);
+	itex2MML_free_string (xml);
 
-	if (document != NULL && !LSM_IS_MATHML_DOCUMENT (document)) {
+	if (document == NULL)
+		return NULL;
+
+	if (!LSM_IS_MATHML_DOCUMENT (document)) {
 		g_object_unref (document);
 		return NULL;
 	}
 
+	return LSM_MATHML_DOCUMENT (document);
+}
+
+static LsmMathmlDocument *
+lsm_mathml_document_new_from_itex_file (GFile *file, GError **error)
+{
+	LsmMathmlDocument *document;
+	gsize size = 0;
+	char *contents = NULL;
+
+	if (!g_file_load_contents (file, NULL, &contents, &size, NULL, error))
+		return NULL;
+
+	document = lsm_mathml_document_new_from_itex (contents, size, error);
+
+	g_free (contents);
+
+	return document;
+}
+
+LsmMathmlDocument *
+lsm_mathml_document_new_from_itex_path (const char *path, GError **error)
+{
+	LsmMathmlDocument *document;
+	GFile *file;
+
+	g_return_val_if_fail (path != NULL, NULL);
+
+	file = g_file_new_for_path (path);
+
+	document = lsm_mathml_document_new_from_itex_file (file, error);
+
+	g_object_unref (file);
+
+	if (document != NULL)
+		lsm_dom_document_set_path (LSM_DOM_DOCUMENT (document), path);
+
+	return document;
+}
+
+LsmMathmlDocument *
+lsm_mathml_document_new_from_itex_url (const char *url, GError **error)
+{
+	LsmMathmlDocument *document;
+	GFile *file;
+
+	g_return_val_if_fail (url != NULL, NULL);
+
+	file = g_file_new_for_uri (url);
+
+	document = lsm_mathml_document_new_from_itex_file (file, error);
+
+	g_object_unref (file);
+
+	if (document != NULL)
+		lsm_dom_document_set_url (LSM_DOM_DOCUMENT (document), url);
+
 	return document;
 }
-#endif
diff --git a/src/lsmmathmldocument.h b/src/lsmmathmldocument.h
index e8c47e9..0cfec89 100644
--- a/src/lsmmathmldocument.h
+++ b/src/lsmmathmldocument.h
@@ -50,9 +50,9 @@ GType lsm_mathml_document_get_type (void);
 LsmMathmlDocument *	lsm_mathml_document_new 		(void);
 LsmMathmlMathElement * 	lsm_mathml_document_get_root_element 	(const LsmMathmlDocument *document);
 
-#if 0
-LsmMathmlDocument * 	lsm_mathml_document_new_from_itex	(const char *itex);
-#endif
+LsmMathmlDocument * 	lsm_mathml_document_new_from_itex	(const char *itex, int size, GError **error);
+LsmMathmlDocument * 	lsm_mathml_document_new_from_itex_path 	(const char *url, GError **error);
+LsmMathmlDocument * 	lsm_mathml_document_new_from_itex_url 	(const char *url, GError **error);
 
 G_END_DECLS
 



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